ServiceSpec.Context documented which addresses a service must end up with, but nothing read it, so a typo'd context was accepted and meant nothing. Review caught it as scaffolding that looked load-bearing. RegisterService now validates it: an unknown context, or a hosted (server/both) service registered with no default listen address, panics. Registrations come from this repository's own init(), never from operator input, so an invalid one is a programming error with no caller positioned to handle an error return, and failing at process start beats a service binding where nobody dials. Services.RequireTarget replaces the same check and message hand-written in composeStateStoreClient and composeChatContract. An empty registered default target is what declares a target operator-supplied, so the requirement belongs to the registry rather than to each consumer. It stays a lazy check at composition time rather than becoming load-time validation: a process that never dials a service must still load a configuration that omits it. The review's stated failure mode does not occur. A hosted service with an empty listen does not reach net.Listen as an ephemeral port; resolveServiceListen returns "no listen address" and archied refuses to start. The defect was the inert field, not a silent bind. Refs: archie-core-u23g
80 lines
3.4 KiB
Go
80 lines
3.4 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Services maps a registered service name to its connection settings, keyed by
|
|
// the [services.<name>] section it was decoded from.
|
|
//
|
|
// It was a struct with one field per service. That stated each service's name
|
|
// three times over -- here, in the loader's defaulting branches, and at every
|
|
// consumer's field access -- so a new service meant editing all three and
|
|
// nothing prevented them disagreeing. Which names are legitimate now comes
|
|
// from RegisterService; see ServiceSpec.
|
|
type Services map[string]ServiceConnection
|
|
|
|
// Get returns the connection configured for name, or the zero value when the
|
|
// configuration carries no section for it. A registered service always has an
|
|
// entry once defaults have been applied.
|
|
func (s Services) Get(name string) ServiceConnection {
|
|
return s[name]
|
|
}
|
|
|
|
// ResolvedToken is the bearer token a client presents when dialling name: the
|
|
// explicit target_token if set, otherwise the environment variable the service
|
|
// registered. getenv is passed in rather than read directly so this stays free
|
|
// of internal/secret, which imports this package.
|
|
//
|
|
// This replaces two resolvers that were identical apart from the variable name
|
|
// each hard-coded.
|
|
func (s Services) ResolvedToken(name string, getenv func(string) string) string {
|
|
if token := s[name].TargetToken; token != "" {
|
|
return token
|
|
}
|
|
spec, ok := LookupService(name)
|
|
if !ok || spec.TokenEnv == "" {
|
|
return ""
|
|
}
|
|
return getenv(spec.TokenEnv)
|
|
}
|
|
|
|
// RequireTarget returns the dial address for name, or an error naming the
|
|
// config key when the operator has not supplied one.
|
|
//
|
|
// A service registered with an empty default target is one the operator must
|
|
// configure, so this is the registry's own contract rather than a consumer's
|
|
// private rule. It stays a lazy check at composition time, not a load-time
|
|
// validation: a process that never dials a given service must still be able to
|
|
// load a configuration that omits it.
|
|
func (s Services) RequireTarget(name string) (string, error) {
|
|
target := strings.TrimSpace(s[name].Target)
|
|
if target == "" {
|
|
return "", fmt.Errorf("services.%s.target is required", name)
|
|
}
|
|
return target, nil
|
|
}
|
|
|
|
// ServiceConnection is a service's gRPC addresses plus the bearer token a
|
|
// client presents when the target is non-loopback. A client reads only Target
|
|
// and TargetToken; Listen belongs to the process that owns the service. The
|
|
// struct does not itself register services or start listeners. Changes require
|
|
// a daemon restart.
|
|
type ServiceConnection struct {
|
|
Target string `toml:"target" yaml:"target"`
|
|
// Listen is the address the process that OWNS this service binds
|
|
// (archie-state-store, archie-gateway). It is read only by that process;
|
|
// every client ignores it. Defaulted from the service's registration, so
|
|
// a loaded config is never empty here -- an empty address reaches
|
|
// net.Listen as "any free port", which would silently move the service
|
|
// off the address its clients dial.
|
|
Listen string `toml:"listen" yaml:"listen"`
|
|
// TargetToken is the bearer token a client (daemon or agent) presents
|
|
// when dialing a non-loopback service target, and the standalone
|
|
// archie-state-store server validates. Empty means no token, which is
|
|
// the loopback-only, daemon-only listener topology permitted without
|
|
// auth (docs/prds/state-store-contract.md §9).
|
|
TargetToken string `toml:"target_token" yaml:"target_token"`
|
|
}
|