The backend registry¶
The root signing package holds one process-global registry mapping a backend
name to a Backend, and a second map from a backend type to a factory for
named instances of it. One sync.RWMutex guards both, so concurrent Get,
Names and NewInstance calls are safe.
The Backend contract¶
type Backend interface {
Name() string
NewSigner(ctx context.Context, keyID string) (crypto.Signer, error)
}
Name returns a stable identifier, unique across the process. A registered
default is lowercase kebab-case (local, aws-kms); an instance minted by
NewInstance carries <type>:<instance> (aws-kms:prod). NewSigner returns
a crypto.Signer wrapping the held key; the backend decides what keyID
means.
The interface is deliberately CLI-agnostic (it mentions no flag library), so
the core module can stay dependency-light. A backend with configuration
describes it as settings, which a host binds to flags and a
selector's ?key=value pairs, so no backend declares a flag itself.
Backends do not resolve credentials. The caller configures the SDK, agent or environment before the signer is used.
Register(b Backend)¶
Adds b to the registry. Called from a backend package's init(), so that a
blank import activates it: the same activate-by-side-effect pattern as
net/http/pprof and the image/* decoders.
It panics, rather than returning an error, in four cases:
| Condition | Panic message |
|---|---|
b is nil |
signing: Register called with nil Backend |
b.Name() returns "" |
signing: Backend.Name() returned empty string |
b is Configurable and its settings struct is malformed |
one of the messages under What Register checks |
| the name is already registered | signing: duplicate Backend registration: <name> |
All four are programming errors at process init. Failing fast beats a silently overridden backend, which would mean signing with an unexpected key.
There is no Unregister. Once a backend package is imported, its backend is
present for the life of the process.
Get(name string) (Backend, error)¶
Returns the registered backend, or ErrUnknownBackend wrapped with a listing
so the message says what this binary actually supports:
- with backends registered:
"gcp-kms" (available: aws-kms, local) - with an empty registry:
"local" (no backends are registered — this binary was built without any signing backends compiled in)
The second message almost always means a missing blank import.
Lookup is exact: no case folding, no aliasing, no fuzzy matching. Get sees
only what Register added, so a <type>:<instance> name is not found unless
someone registered that instance explicitly.
Names() []string¶
Returns every registered name, sorted alphabetically. It reports the backends
this binary compiled in, which is exactly the set of backend packages the
binary chose to import. That makes it useful for rendering the accepted values
of a --backend flag. Factories are not listed; a type that offers instances
appears through the default it registers under the same name.
Named instances¶
A binary sometimes needs two configurations of one backend type at once, such as two AWS accounts. A backend package offers that by registering a factory next to its default.
RegisterFactory(typeName string, construct func(canonicalName string) (Backend, error))¶
Records how to construct a named instance of typeName. Call it from init()
beside Register, and register a same-named default too, so that a bare
selection of the type still resolves and the type appears in Names().
Before registering, it calls construct once with the probe name
<type>:registration-check and checks the settings struct that instance
carries, as Register does for a default. So construct must have no side
effects: it runs at init in every binary that imports the backend. If the
probe call returns an error, the check is skipped, and NewInstance checks
each instance it mints instead.
It panics in four cases:
| Condition | Panic message |
|---|---|
typeName is not lowercase kebab-case with no colon (^[a-z0-9][a-z0-9-]*$) |
signing: RegisterFactory called with invalid type name: <type> |
construct is nil |
signing: RegisterFactory called with nil construct for type: <type> |
a factory for typeName already exists |
signing: duplicate factory registration: <type> |
| the probe instance's settings struct is malformed | one of the messages under What Register checks, naming the probe instance |
The colon ban is what keeps a <type>:<instance> selector unambiguous to split.
NewInstance(typeName, instance string) (Backend, error)¶
Calls the factory for typeName with the canonical name
<typeName>:<instance> and returns what it builds. The result is not
registered: the caller owns it, and Get will not find it.
The factory runs outside the registry lock, so it may itself call Register
or Get without deadlocking. NewInstance returns an error when:
| Condition | Error |
|---|---|
instance is empty |
signing: empty instance name for backend type "<type>" |
no factory is registered for typeName |
ErrUnknownBackendType, wrapped with the type name |
| the factory returns an error | that error, wrapped as constructing signing instance "<type>:<instance>" |
| the factory returns nil, or a typed nil | signing: factory for type "<type>" returned a nil Backend |
the result's Name() is not the canonical name |
signing: factory for type "<type>" returned Backend named "<got>", want "<type>:<instance>" |
| the result's settings struct is malformed | signing: factory for type "<type>" returned a Backend with a malformed settings struct |
Those are the only checks. The last error names the type only, because the
instance is caller input and may be a pasted credential. NewInstance applies
no settings: the instance comes back with its defaults.
Resolve is what parses a selector, checks its instance name and
applies its settings.
Both backends in the table below register a factory. The local factory's
instances differ from the default only in their name, because local has no
per-instance settings.
Settings¶
A backend with configuration, such as an AWS region, implements
Configurable:
Settings returns a pointer to the backend's own settings struct. The values
the struct holds when Settings returns are the defaults. An instance minted by
a factory must return its own struct, never one shared with the default, or
configuring one instance would configure the other.
Each field that is a setting carries two tags:
| Tag | Holds | Rule |
|---|---|---|
setting:"<key>" |
The setting's name, used both as a flag name and as a selector key | ^[a-z][a-z0-9-]{0,23}$, unique within the struct |
usage:"<help>" |
One line of help text | Required, non-empty |
A setting's field must be exported, and its type must be time.Duration or
of kind string, bool or int, so a named type such as type Region string
is accepted. Fields without a setting tag are ignored, so the
struct can carry private state beside its settings. go/signing-aws-kms's is the
worked example:
type Settings struct {
Region string `setting:"kms-region" usage:"AWS region the KMS key lives in"`
callerHasRegion bool
}
The struct may also implement Validate() error, to check the settings
together once every value is applied. Its error reaches the user as it is, so
it names a setting and never quotes the value.
SettingsOf(b Backend) ([]*Setting, error)¶
Describes b's settings, sorted by key. A backend that is not Configurable
has none, and gets nil, nil. Each *Setting writes straight into the
backend's struct and has the methods of pflag's Value (String, Set,
Type), plus IsBoolFlag, Key and Usage. So a host binds a setting as a
flag directly, with no dependency in this module. Type reports string,
bool, int or duration.
What Register checks¶
Register, and RegisterFactory for its probe instance, call SettingsOf and
panic with its error, which is one of the messages below. NewInstance runs
the same check on every instance it mints and returns
signing: factory for type "<type>" returned a Backend with a malformed settings struct
instead, because its instance name is caller input.
| Condition | Message |
|---|---|
Settings returns nil, or a non-pointer, or a pointer to a non-struct |
signing: backend "<name>": Settings must return a non-nil pointer to a struct |
a setting key breaks the grammar |
signing: backend "<name>": field <Field>: "<key>" is not a valid setting key (want ^[a-z][a-z0-9-]{0,23}$) |
| the field is unexported | signing: backend "<name>": setting "<key>" is on unexported field <Field> |
| the field's type is not one of the four | signing: backend "<name>": setting "<key>" has unsupported type <type> (want string, bool, int or time.Duration) |
the usage tag is missing or empty |
signing: backend "<name>": setting "<key>" has no usage tag |
| two fields share a key | signing: backend "<name>": duplicate setting "<key>" |
Only the grammar and unexported-field messages name the Go field. The first names only the backend, and the rest name the key.
ValidateSettings(b Backend) error¶
Runs the settings struct's Validate method, if it has one, and returns its
error unwrapped. A host calls it after applying its own flags to a registered
default. Resolve calls it for a named instance.
Resolve(selector string) (Backend, string, error)¶
Turns a --backend selector into a backend and its canonical identity, the
form a host such as sigillum accepts:
<type>returns the registered default throughGet. Its settings are the host's to apply, from its own flags, before it callsValidateSettings. Adding?…to a bare type is an error.<type>:<instance>[?key=value&…]mints a fresh instance throughNewInstance, applies the selector's settings to it, and validates it. The instance name must match^[a-z0-9][a-z0-9-]*$, at most 24 characters.
Settings apply in two phases. Every key and value is checked before any is
written, so a bad selector never leaves an instance half configured. Keys and
values are URL-decoded, a key may appear only once, and a value must not be
empty. A backend with no settings refuses any setting, though a bare trailing
? (local:x?) is accepted as none.
The identity Resolve returns is safe to log or put in an error. The selector
is not, because it can carry setting values. Every error Resolve returns
names a setting's key and never its value. A key, an instance name or a type
that is not shaped like one is not quoted either, since it may be a credential
pasted into the wrong place.
ResetForTesting()¶
Clears the registry and every registered factory. The ForTesting suffix is the Go-standard signal that
this is a test helper; production code must not call it. It is exported rather
than hidden behind a test-only build so that tests in other packages can
drive registry-mutating scenarios against fake backends.
What keyID means per backend¶
keyID is a single string whose format each backend defines.
| Backend name | Module | keyID is |
Key types |
|---|---|---|---|
local |
gitlab.com/phpboyscout/go/signing/local (ships in this module) |
A filesystem path to an unencrypted PEM file | RSA (PKCS#1 or PKCS#8) and Ed25519 (PKCS#8) |
aws-kms |
gitlab.com/phpboyscout/go/signing-aws-kms |
A KMS key ARN or alias | RSA_4096 SIGN_VERIFY for the OpenPGP path, ECC_NIST_EDWARDS25519 for the minisign path |
The local backend is stateless and has no settings; the PEM path arrives
per call as keyID. It reads whatever path it is given, following symlinks,
with no canonicalisation. That is appropriate for an operator-supplied path,
and worth knowing if the path could come from somewhere less trusted.
Registering a configured backend¶
A backend that needs configuration can be constructed directly and used without the registry. That is how the AWS backend takes a region or a logger:
Do not pass that backend to Register. Importing a backend package at all,
blank or not, runs its init(), which has already registered the default
name, so a second registration under aws-kms panics. To put a configured
backend in the registry, give it a name nothing else uses
(awskms.NewDefault("aws-kms-us", ...)).
Related¶
- Implement a custom backend: the task version of this page.
- Backends and the per-provider module pattern: why each provider is its own module.
- Errors:
ErrUnknownBackend,ErrUnknownBackendTypeand the rest. - sigillum spec 0007 D2 and D3, the design behind settings and
Resolve: https://gitlab.com/phpboyscout/sigillum/-/wikis/specs/0007-sigillum-is-the-signing-cli