identity

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

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

View Source
const (
	AuthTokenHeader       = "Authorization"
	AuthModeHeader        = "header"
	CredentialPresenceAny = "any"
	CredentialPresenceAll = "all"
)
View Source
const MaxCredentialHeaders = 16
View Source
const MaxValidationPath = 128

MaxValidationPath bounds the credential gateway validation path.

Variables

This section is empty.

Functions

func AudienceMatches

func AudienceMatches(audClaim any, expected string) bool

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

func ExtractBearer(auth string) string

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

func ExtractToken(headerName, value string) string

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

func FormatTokenHeaderValue(headerName, token string) string

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

func GatewayFieldName(index int) string

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

func IsSecretHeader(header string, secretHeaders []string) bool

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

func NormalizeCredentialPresence(value string) (string, error)

NormalizeCredentialPresence accepts any, all, or empty (any).

func ParseSessionSPIFFE

func ParseSessionSPIFFE(raw, trustDomain string) (namespace, sessionID string, ok bool)

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

func ProtectedResourceMetadataPath(resourcePath string) string

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

func ProtectedResourceMetadataURL(resource string) string

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

func SessionSPIFFEID(trustDomain, namespace, session string) (string, error)

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

func ValidateCSRPEM(raw, expectedSPIFFEID string) ([]byte, error)

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

func ValidateCredentialHeaderName(name string) error

ValidateCredentialHeaderName accepts HTTP token names, including Authorization, and rejects hop-by-hop, framing, routing, and trusted identity headers.

func ValidateCredentialHeaderNames

func ValidateCredentialHeaderNames(names []string) error

ValidateCredentialHeaderNames checks a configured list of credential header names.

func ValidateCredentialValue

func ValidateCredentialValue(value string) error

ValidateCredentialValue accepts any configured credential value. The value is opaque. It never includes the credential in its error.

func ValidateGateway

func ValidateGateway(validationPath string, secretHeaders, headers []string) error

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

func ValidateGatewayHeader(name string) error

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

func ValidateInjectedValue(value string) error

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).

func WritePrivateFile

func WritePrivateFile(dir, name string, data []byte, mode os.FileMode) error

WritePrivateFile writes data to dir/name with mode, rejecting path traversal.

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)

func (CredentialPolicy) Check

func (p CredentialPolicy) Check(h http.Header) error

Check never returns credential values. The zero policy fails closed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL