Documentation
¶
Overview ¶
Package security provides shared security primitives for gokit modules.
It includes TLS configuration, certificate handling, and secure-by-default HTTP response header policies that can be reused across transports.
TLS Configuration ¶
cfg := security.TLSConfig{
CAFile: "/path/to/ca.pem",
CertFile: "/path/to/cert.pem",
KeyFile: "/path/to/key.pem",
}
tlsConfig, err := cfg.Build()
Index ¶
Constants ¶
const ( // BasicAuthScheme is the HTTP "Basic" authentication scheme name. BasicAuthScheme = "Basic" // BearerAuthScheme is the HTTP "Bearer" authentication scheme name. BearerAuthScheme = "Bearer" )
Shared HTTP authentication scheme names. These are the canonical spellings used in the Authorization / WWW-Authenticate headers, exposed here so transport middleware references one vocabulary instead of scattering string literals (e.g. server/middleware auth defaults to BearerAuthScheme).
Variables ¶
This section is empty.
Functions ¶
func EnforcePayloadLimit ¶
func ValidateLocalBind ¶
Types ¶
type HeadersConfig ¶
type HeadersConfig struct {
// Disabled turns off header injection entirely.
Disabled bool `yaml:"disabled" mapstructure:"disabled"`
// HSTSMaxAge controls the Strict-Transport-Security max-age value.
HSTSMaxAge time.Duration `yaml:"hsts_max_age" mapstructure:"hsts_max_age"`
// DisableHSTSIncludeSubdomains suppresses the includeSubDomains directive.
DisableHSTSIncludeSubdomains bool `yaml:"disable_hsts_include_subdomains" mapstructure:"disable_hsts_include_subdomains"`
// DisableHSTSPreload suppresses the preload directive.
DisableHSTSPreload bool `yaml:"disable_hsts_preload" mapstructure:"disable_hsts_preload"`
// ContentSecurityPolicy is written to the Content-Security-Policy header.
ContentSecurityPolicy string `yaml:"content_security_policy" mapstructure:"content_security_policy"`
// ReferrerPolicy is written to the Referrer-Policy header.
ReferrerPolicy string `yaml:"referrer_policy" mapstructure:"referrer_policy"`
// PermissionsPolicy is written to the Permissions-Policy header.
PermissionsPolicy string `yaml:"permissions_policy" mapstructure:"permissions_policy"`
// XFrameOptions is written to the X-Frame-Options header.
XFrameOptions string `yaml:"x_frame_options" mapstructure:"x_frame_options"`
}
HeadersConfig configures secure-by-default HTTP response headers.
func (*HeadersConfig) Apply ¶
func (c *HeadersConfig) Apply(header http.Header) error
Apply writes the configured security headers onto the provided response header map.
func (*HeadersConfig) ApplyDefaults ¶
func (c *HeadersConfig) ApplyDefaults()
ApplyDefaults populates secure defaults.
func (*HeadersConfig) HeaderMap ¶
func (c *HeadersConfig) HeaderMap() (map[string]string, error)
HeaderMap returns the configured security headers.
func (*HeadersConfig) Validate ¶
func (c *HeadersConfig) Validate() error
Validate checks configuration consistency.
type TLSConfig ¶
type TLSConfig struct {
// SkipVerify disables server certificate verification. Not recommended for production.
SkipVerify bool `yaml:"skip_verify" mapstructure:"skip_verify"`
// CAFile is the path to the CA certificate file for verifying the server.
CAFile string `yaml:"ca_file" mapstructure:"ca_file"`
// CertFile is the path to the client TLS certificate file (for mTLS).
CertFile string `yaml:"cert_file" mapstructure:"cert_file"`
// KeyFile is the path to the client TLS key file (for mTLS).
KeyFile string `yaml:"key_file" mapstructure:"key_file"`
// ServerName overrides the server name used for certificate verification.
ServerName string `yaml:"server_name" mapstructure:"server_name"`
// MinVersion is the minimum TLS version (e.g., tls.VersionTLS12).
// Defaults to a TLS 1.2 floor while allowing the runtime to negotiate TLS 1.3.
MinVersion uint16 `yaml:"min_version" mapstructure:"min_version"`
}
TLSConfig holds TLS settings shared across gokit modules. Used by httpclient, grpc, kafka, discovery, and other transport layers.
func (*TLSConfig) Build ¶
Build creates a *tls.Config from the configuration. Returns (nil, nil) if no TLS settings are configured (all fields are zero values) — callers treat that as "no TLS" rather than a typed error.
this builder; making it a sentinel error would force every transport setup site to errors.Is for a non-error condition.
type WarnOnlyVerifier ¶
type WarnOnlyVerifier struct{}