Documentation
¶
Overview ¶
Package libtokenkey holds the credential-hashing key: the server-side secret under which an instance token, a pairing key and a proxy key are HMAC'd before any of them is written down.
Why an HMAC and not a bare digest ¶
The stored value used to be base64(SHA-256(secret)). A bare digest is computable by anyone, so a leaked database alone lets an attacker confirm a candidate token offline — which matters the moment a token reaches a log, a backup, a screenshot or a support ticket. Under an HMAC they must also hold this key, which lives in the deployment's secret store and never in the database. libcipher.NewHash is the only construction used here.
Why the key must be its own secret ¶
libcipher's rule: keys must be secret and DISTINCT per purpose. This key is therefore not the deployment's Ed25519 identity — that one's public half is published to every enrolled machine, which is the opposite of what an HMAC key needs. FromEnv refuses if the two are configured to the same value.
Where it comes from ¶
Configuration or a mounted secret, never the repository and never a default. FromEnv reads RELAY_TOKEN_KEY_FILE (a path, which is how a Kubernetes Secret arrives) or RELAY_TOKEN_KEY, and returns ErrNoSigningKey rather than inventing one: a host that generated this key at startup would silently invalidate every instance token in the fleet on every rollout, and the symptom is indistinguishable from a wrong token. Generate exists for tests and for minting the secret once by hand.
Index ¶
Constants ¶
const ( EnvSigningKeyFile = "CONTENOX_TOKEN_KEY_FILE" EnvSigningKey = "CONTENOX_TOKEN_KEY" )
Environment variables carrying the credential-hashing key. The file form is preferred where both are set, because a mounted secret does not appear in the process listing or in a crash dump of the environment.
const ( EnvIdentityKeyFile = "RELAY_PRIVATE_KEY_FILE" EnvIdentityKey = "RELAY_PRIVATE_KEY" )
Environment variables carrying the Ed25519 identity secret, read only to refuse configuring it as the hashing key.
const MinKeyLen = 32
MinKeyLen is the shortest key accepted. libcipher.GenerateKey(32) produces 64 hex characters, so a correctly minted secret is never near this bound and anything that is, is a placeholder somebody meant to replace.
Variables ¶
var ( ErrNoSigningKey = errors.New("tokenkey: no credential hashing key configured") ErrKeyTooShort = errors.New("tokenkey: credential hashing key is too short") // ErrKeyReused reports the key configured here is also the Ed25519 // identity secret. Refused rather than warned about: libcipher's rule is // that keys are distinct per purpose, and the identity key's public half // is handed to every enrolled machine. ErrKeyReused = errors.New("tokenkey: credential hashing key must not be the identity key") )
Configuration failures. They are all fatal at startup; see the package comment on why a generated fallback would be worse than not starting.
Functions ¶
Types ¶
type Hasher ¶
type Hasher struct {
// contains filtered or unexported fields
}
Hasher computes and compares stored credential digests under one key.
It is immutable after construction and safe for concurrent use: every dial in the fleet hashes through the same value.
func FromEnv ¶
FromEnv loads the key from the environment, preferring EnvSigningKeyFile over EnvSigningKey and falling back to the names those carried before the gateway was its own product. It returns ErrNoSigningKey when none is set, so a caller can distinguish "not configured" from "configured and wrong" — the first is a deployment that has not been finished, the second is a deployment that will reject every token it ever issued.
It also refuses a key that is the Ed25519 identity secret; see ErrKeyReused. An UNREADABLE identity key is not reuse — its own loader reports that failure, and refusing here would take a host down over a file it may not even use.
func FromSecret ¶
FromSecret builds a Hasher from the configured key text.
The bytes are used as configured rather than decoded: an HMAC key is opaque bytes, and a decode step would only add a way for two deployments to disagree about what the secret was. Surrounding whitespace and a trailing newline are trimmed.
func Generate ¶
Generate mints a fresh key. For tests and for the one-off creation of the deployment secret — a host never calls this at startup.
func (*Hasher) Hash ¶
Hash returns the stored form of a credential: the scheme tag and the base64 (std, padded) libcipher digest of secret under this key and purpose.
The result is deterministic for a given (key, purpose, secret), which is what lets it be the indexed column a lookup matches on; see Purpose for why that costs nothing here. The plaintext is not recoverable from it.
type Purpose ¶
type Purpose string
Purpose is the domain separator mixed into a digest, so that a value computed for one kind of credential can never equal one computed for another under the same key. It is passed to libcipher as the salt.
Fixed per kind rather than random per row, deliberately. The digest IS the lookup key, so a per-row random salt would make the row unfindable without scanning and comparing every one of them. The salt's usual job — stopping one precomputation from covering many rows — is already done by the input being 256 bits of crypto/rand.
const ( // PurposeInstanceToken is the long-lived bearer credential a connector // presents on every dial. PurposeInstanceToken Purpose = "contenox-relay/instance-token/v1" // PurposePairingKey is the short-lived activation key an account holder // mints in the app and types into a machine to enrol it. PurposePairingKey Purpose = "contenox-relay/pairing-key/v1" // PurposeSession is the opaque secret carried in the browser's session // cookie, of which only the digest is written down. // // Separated from the instance token because without the separator a value // that resolved to a session would also resolve to an instance, and one // leaked digest column would be usable against the other table. PurposeSession Purpose = "contenox-relay/session/v1" // PurposeProxyKey is the bearer a license carries for the model gateway // (the api_key claim minted at checkin). Only its digest is ever at rest: // the ledger row is what lets the gateway verify a presented key is one // this host issued, and to whom, without the plaintext token existing // anywhere on the server. PurposeProxyKey Purpose = "contenox-relay/proxy-key/v1" // PurposeRequestSource pseudonymises a caller's address for a log line. // // NOT a credential, unlike every purpose above it, and the only one whose // input is not 256 bits of crypto/rand — an IPv4 address is 32 bits and the // whole space is enumerable, so this digest is unlinkable only to somebody // who does not hold the key. That is the property being bought: an operator // can still tell two failures apart and correlate a burst, and a leaked log // alone names nobody. Rotating the key invalidates every digest at once. PurposeRequestSource Purpose = "contenox-relay/request-source/v1" )
The credential kinds. The version suffix is what lets the construction change later without a value minted under the old one silently verifying.