Documentation
¶
Overview ¶
Package identity is the shared identity vocabulary of the platform.
It formats and parses SPIFFE session identities (spiffe://<trust-domain>/ns/<namespace>/session/<session>), finds them in client certificates, builds and validates session-bound certificate signing requests (csr.go), and defines the credential-header and OAuth protected-resource contracts plus bearer-token extraction.
SPIFFE IDs are built and validated with go-spiffe's dependency-free spiffeid package. Beyond that it imports only the standard library and other leaf packages, so the gateway, platform API, runtime API and CLI adapter all agree on what an identity is.
Index ¶
- Constants
- func AudienceMatches(audClaim any, expected string) bool
- func BuildSessionCSR(trustDomain, namespace, sessionName string) (keyPEM, csrPEM []byte, spiffeID string, err error)
- func ExtractBearer(auth string) string
- func ExtractToken(headerName, value string) string
- func FirstClientSPIFFEURI(cert *x509.Certificate, trustDomain string) string
- func FormatTokenHeaderValue(headerName, token string) string
- func GatewayFieldName(index int) string
- func IsSecretHeader(header string, secretHeaders []string) bool
- func NormalizeCredentialPresence(value string) (string, error)
- func ParseSessionSPIFFE(raw, trustDomain string) (namespace, sessionID string, ok bool)
- func ProtectedResourceMetadataPath(resourcePath string) string
- func ProtectedResourceMetadataURL(resource string) string
- func SessionSPIFFEID(trustDomain, namespace, session string) (string, error)
- func ValidateCSRPEM(raw, expectedSPIFFEID string) ([]byte, error)
- func ValidateCredentialHeaderName(name string) error
- func ValidateCredentialHeaderNames(names []string) error
- func ValidateCredentialValue(value string) error
- func ValidateGateway(validationPath string, secretHeaders, headers []string) error
- func ValidateGatewayHeader(name string) error
- func ValidateInjectedValue(value string) error
- func ValidateIssuedCertificatePEM(certPEM string, csrDER []byte, expectedSPIFFEID string, maxTTL time.Duration, ...) error
- func WritePrivateFile(dir, name string, data []byte, mode os.FileMode) error
- type CredentialFailure
- type CredentialPolicy
Constants ¶
const ( AuthTokenHeader = "Authorization" AuthModeHeader = "header" CredentialPresenceAny = "any" CredentialPresenceAll = "all" )
const MaxCredentialHeaders = 16
const MaxValidationPath = 128
MaxValidationPath bounds the credential gateway validation path.
Variables ¶
This section is empty.
Functions ¶
func AudienceMatches ¶
AudienceMatches validates if the JWT audience claim matches the expected value. It handles both string and string slice audience claims as per JWT specifications.
func BuildSessionCSR ¶
func BuildSessionCSR(trustDomain, namespace, sessionName string) (keyPEM, csrPEM []byte, spiffeID string, err error)
BuildSessionCSR generates a fresh P-256 key and a CSR whose only SAN is the session SPIFFE URI. It returns PKCS#8 key PEM, CSR PEM, and the SPIFFE ID string.
func ExtractBearer ¶
ExtractBearer extracts the JWT token from an Authorization header. It expects the format "Bearer <token>" and returns the token part. Returns empty string if the format is invalid.
func ExtractToken ¶
ExtractToken extracts a token from a header value, handling Bearer prefix. If the headerName is "authorization", it only extracts Bearer tokens. Otherwise, it returns the raw value or the Bearer-extracted token if present.
func FirstClientSPIFFEURI ¶
func FirstClientSPIFFEURI(cert *x509.Certificate, trustDomain string) string
FirstClientSPIFFEURI returns the first valid SPIFFE ID URI SAN on cert whose trust domain matches trustDomain when trustDomain is non-empty. URI SANs that are not valid SPIFFE IDs are skipped. Returns "" when no matching URI is present.
func FormatTokenHeaderValue ¶
FormatTokenHeaderValue formats a token for a specific header. If the header is "authorization", it returns "Bearer <token>". Otherwise, it returns the token as-is.
func GatewayFieldName ¶
GatewayFieldName is the sign-in input name for the credential header at position index. Inputs are named by position, so any valid header name works, including names that are not valid identifiers; the header name is the label.
func IsSecretHeader ¶
IsSecretHeader reports whether the header's sign-in input is masked: listed in secretHeaders, or, when none are listed, named like a credential.
func NormalizeCredentialPresence ¶
NormalizeCredentialPresence accepts any, all, or empty (any).
func ParseSessionSPIFFE ¶
ParseSessionSPIFFE parses a session-bound SPIFFE ID of the form spiffe://<trustDomain>/ns/<namespace>/session/<sessionID>. It fails closed on any deviation: go-spiffe rejects malformed IDs (wrong scheme, query, fragment, port, userinfo, empty or dot segments, escapes), and the trust domain and path shape must match exactly.
func ProtectedResourceMetadataPath ¶
ProtectedResourceMetadataPath returns the metadata document path for a resource path, trimming a trailing slash the same way the URL form does so ingress routes and advertised URLs always agree.
func ProtectedResourceMetadataURL ¶
ProtectedResourceMetadataURL returns the RFC 9728 metadata document URL for a resource URL by inserting the well-known path between its origin and path.
func SessionSPIFFEID ¶
SessionSPIFFEID returns the canonical session-bound SPIFFE ID: spiffe://<trustDomain>/ns/<namespace>/session/<session>.
The ID is built and validated by go-spiffe, so every component must be a valid SPIFFE value: the trust domain is lowercase and the namespace and session use only letters, digits, '.', '-' and '_'. Kubernetes namespace names and platform session names already satisfy this. Anything else is an error rather than being escaped.
func ValidateCSRPEM ¶
ValidateCSRPEM parses and validates a PEM CSR. The CSR must be signed, carry exactly one URI SAN equal to expectedSPIFFEID, and must not include DNS, email, or IP subject alternative names. It returns the CSR DER on success.
func ValidateCredentialHeaderName ¶
ValidateCredentialHeaderName accepts HTTP token names, including Authorization, and rejects hop-by-hop, framing, routing, and trusted identity headers.
func ValidateCredentialHeaderNames ¶
ValidateCredentialHeaderNames checks a configured list of credential header names.
func ValidateCredentialValue ¶
ValidateCredentialValue accepts any configured credential value. The value is opaque. It never includes the credential in its error.
func ValidateGateway ¶
ValidateGateway checks the settings that opt a header-mode server into the optional bundled credential gateway. validationPath is appended to the server's public MCP URL; it carries no query or traversal.
func ValidateGatewayHeader ¶
ValidateGatewayHeader checks that a header name may be supplied by the credential gateway: a valid credential header name that is not one the gateway itself owns (Via, Accept-Encoding, Last-Event-Id, Sec-*).
func ValidateInjectedValue ¶
ValidateInjectedValue is the strict check applied to a value the gateway is about to send upstream: non-empty, at most 8192 bytes, printable ASCII (tabs allowed), and no leading or trailing whitespace. It rejects CR, LF, NUL and every other control character, so a submitted value cannot add headers.
func ValidateIssuedCertificatePEM ¶
func ValidateIssuedCertificatePEM(certPEM string, csrDER []byte, expectedSPIFFEID string, maxTTL time.Duration, now time.Time) error
ValidateIssuedCertificatePEM verifies a certificate returned by the workload issuer against the CSR the platform submitted. It is a defense-in-depth check that does not depend on the issuer's approval policy: the leaf must carry exactly the expected SPIFFE URI and no other SAN, be a non-CA client-auth-only certificate bound to the CSR's public key, and not outlive maxTTL (plus a small skew allowance for issuers that backdate NotBefore).
Types ¶
type CredentialFailure ¶
type CredentialFailure string
const ( ErrInvalidCredentialConfig CredentialFailure = "invalid_credential_config" // #nosec G101 -- Public authentication failure code, not credential material. ErrEmptyCredential CredentialFailure = "empty_credential" // #nosec G101 -- Public authentication failure code, not credential material. ErrDuplicateCredential CredentialFailure = "duplicate_credential" ErrMissingCredential CredentialFailure = "missing_credential" )
func (CredentialFailure) Error ¶
func (e CredentialFailure) Error() string
type CredentialPolicy ¶
type CredentialPolicy struct {
// contains filtered or unexported fields
}
CredentialPolicy owns a validated header contract. Configuration is copied on construction and cannot be changed through the caller's original slice.
func NewCredentialPolicy ¶
func NewCredentialPolicy(names []string, presence string) (CredentialPolicy, error)