Documentation
¶
Index ¶
- Constants
- func DirectoryGroupCheck(id *Identity, groups []string) error
- func DirectoryRequired(id *Identity) error
- func Protect(conn net.Conn, fn func() error)
- func RegisterProtocolDetector(fn ProtocolDetector)
- func RegisterProtocolName(p Protocol, name string)
- func SendErrorFrame(conn net.Conn, code ErrorCode, msg string)
- func Serve(conn net.Conn, svc *Service, id *Identity) (err error)
- type Authenticator
- type CIDRCheck
- type ConsoleLogger
- type Counter
- type Directory
- type DirectoryCache
- type DirectoryManager
- type DirectoryRecord
- type DirectoryRequiredCheck
- type Engine
- type ErrorCode
- type GroupMembershipCheck
- type HTTPProxyHandler
- type Handler
- type Identity
- type IdentityMapper
- type LDAPDirectory
- type Logger
- type Monitor
- func (r *Monitor) AddConnection(conn net.Conn, svc *Service) uint64
- func (r *Monitor) RemoveConnection(id uint64) bool
- func (r *Monitor) Services() (svcs map[string]Service)
- func (r *Monitor) SetProtocol(id uint64, p Protocol)
- func (r *Monitor) Snapshot() MonitorSnapshot
- func (r *Monitor) UpdateConnection(id uint64, fn func(net.Conn))
- type MonitorSnapshot
- type MonitorStats
- type NegativeCache
- type NullLogger
- type OIDCAuthConfig
- type OIDCAuthenticator
- type OIDCConfig
- type OIDCDirectory
- type PolicyEngine
- type PreAuthCheck
- type PreAuthPipeline
- type Protocol
- type ProtocolDetector
- type QUICProxyHandler
- type Service
- type TCPProxyHandler
- type TLSAuthConfig
- type TLSAuthenticator
- func (r *TLSAuthenticator) AuthConfig() (cfg TLSAuthConfig)
- func (r *TLSAuthenticator) Authenticate(ctx context.Context, conn net.Conn, state *tls.ConnectionState) (*Identity, error)
- func (r *TLSAuthenticator) CRL() *x509.RevocationList
- func (r *TLSAuthenticator) CRLChanged(a, b *x509.RevocationList) bool
- func (r *TLSAuthenticator) IsRevoked(cert *x509.Certificate) (revoked bool)
- func (r *TLSAuthenticator) LoadCRL(path string) (*x509.RevocationList, error)
- func (r *TLSAuthenticator) PollCRL(path string, interval time.Duration) (err error)
- func (r *TLSAuthenticator) TLSConfigForListener() *tls.Config
- type TLSIdentityMappingRule
- type TLSUserMapping
- type TTLCache
- type UDPProxyHandler
- type YAMLDirectory
Constants ¶
const TLSMinimumVersion uint16 = tls.VersionTLS13
TLSMinimumVersion defines the minimum acceptable TLS version honored by this package. Currently, this is TLS 1.3.
Variables ¶
This section is empty.
Functions ¶
func DirectoryGroupCheck ¶
func DirectoryRequired ¶
func Protect ¶
Protect will protect the application from crashing due to panics. Instead, panics are recovered safely so as to not disconnect other users currently connected.
func RegisterProtocolDetector ¶
func RegisterProtocolDetector(fn ProtocolDetector)
RegisterProtocolDetector allows users to add their own protocol detectors. These run BEFORE built-in detectors.
func RegisterProtocolName ¶
RegisterProtocolName lets users define names for custom protocols. Example:
const ProtocolRedis ztp.Protocol = 1000 ztp.RegisterProtocolName(ProtocolRedis, "REDIS")
Types ¶
type Authenticator ¶
type Authenticator interface {
Authenticate(context.Context, net.Conn, *tls.ConnectionState) (*Identity, error)
}
Authenticator implements an authentication provider, such as the TLSAuthenticator.
type CIDRCheck ¶
type CIDRCheck struct {
// contains filtered or unexported fields
}
func NewCIDRCheck ¶
type ConsoleLogger ¶
type ConsoleLogger struct{}
ConsoleLogger writes log messages via log.Println, log.Fatalln, log.Printf or log.Fatalf.
func (ConsoleLogger) Fatalf ¶
func (_ ConsoleLogger) Fatalf(s string, args ...any)
func (ConsoleLogger) Fatalln ¶
func (_ ConsoleLogger) Fatalln(args ...any)
func (ConsoleLogger) Printf ¶
func (_ ConsoleLogger) Printf(s string, args ...any)
func (ConsoleLogger) Println ¶
func (_ ConsoleLogger) Println(args ...any)
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter is a general use counter. Instances of this type are thread-safe.
func (*Counter) Count ¶
Count returns the current uint64 count from the underlying receiver instance.
type Directory ¶
type Directory interface {
// Lookup returns a *DirectoryEntry alongside an error
// following an attempt to source an identity.
Lookup(identity string) (*DirectoryRecord, error)
// Type returns the string name held by the directory
// (e.g.: "ldap", "yaml", et al).
Type() string
}
type DirectoryCache ¶
type DirectoryCache interface {
Get(string) (*DirectoryRecord, bool)
Set(string, *DirectoryRecord)
Clear()
}
DirectoryCache implements a basic directory backend caching instance.
type DirectoryManager ¶
type DirectoryManager struct {
Cache DirectoryCache
// contains filtered or unexported fields
}
DirectoryManager stores all directory backends for centralized access across various systems of record.
func NewDirectoryManager ¶
func NewDirectoryManager(cache DirectoryCache, sources ...Directory) *DirectoryManager
NewDirectoryManager returns an instance of *DirectoryManager following an attempt to initialize and source one or more directory backends.
func (*DirectoryManager) Lookup ¶
func (r *DirectoryManager) Lookup(identity string) (*DirectoryRecord, error)
Lookup returns an instance of *DirectoryRecord alongside an error following an attempt to lookup the specified identity within a backend.
func (DirectoryManager) Type ¶
func (r DirectoryManager) Type() string
Type returns the string literal `manager`.
type DirectoryRecord ¶
type DirectoryRecord struct {
UserID string `json:"user_id"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Groups []string `json:"groups"`
Attributes map[string][]string `json:"attributes"`
}
Directory describes any single directory entry (e.g.: an identity).
type DirectoryRequiredCheck ¶
type DirectoryRequiredCheck struct {
Directory
}
func NewDirectoryRequiredCheck ¶
func NewDirectoryRequiredCheck(dir Directory) *DirectoryRequiredCheck
func (*DirectoryRequiredCheck) Run ¶
func (r *DirectoryRequiredCheck) Run(id *Identity) error
type GroupMembershipCheck ¶
func NewGroupMembershipCheck ¶
func NewGroupMembershipCheck(dir Directory, groups []string) *GroupMembershipCheck
func (*GroupMembershipCheck) Run ¶
func (r *GroupMembershipCheck) Run(id *Identity) error
type HTTPProxyHandler ¶
type HTTPProxyHandler struct{}
func NewHTTPProxyHandler ¶
func NewHTTPProxyHandler() *HTTPProxyHandler
func (*HTTPProxyHandler) Protocol ¶
func (_ *HTTPProxyHandler) Protocol() Protocol
type Handler ¶
Handler implements a handler interface, usable by users in creating their own handlers for special protocols, or corner-cases in which standard protocols like TCP or QUIC need to be handled differently than the base handlers included in this package operate.
type Identity ¶
type Identity struct {
ID string `json:"id"`
Type string `json:"type"`
Groups []string `json:"groups,omitempty"`
Metadata map[string]string `json:"metadata"`
Source string `json:"source"`
Entry *DirectoryRecord `json:"entry,omitempty"`
Cert *x509.Certificate `json:"client_cert,omitempty"`
}
Identity describes a particular user identity.
type IdentityMapper ¶
IdentityMapper defines a first class function signature for users to leverage in authoring their own username mapping scheme.
The most likely use case for this would be mapping an X.509 client certificate SubjectDN to a derived identity.
If no such function is devised, the raw identity is used as-is. This would mean, in the case of an X.509 Subject DN, it would likely manifest as something resembling:
<X.509 subject DN>,cn=external,cn=auth
... which may or may not be what you want.
type LDAPDirectory ¶
LDAPDirectory describes a given X.500 (LDAP) backend.
func (*LDAPDirectory) Lookup ¶
func (r *LDAPDirectory) Lookup(identity string) (*DirectoryRecord, error)
Lookup returns an instance of *DirectoryRecord alongside an error following an attempt to lookup the specified identity within a backend.
func (LDAPDirectory) Type ¶
func (_ LDAPDirectory) Type() string
Type returns the string literal `ldap`.
type Logger ¶
type Logger interface {
Printf(string, ...any)
Fatalf(string, ...any)
Println(...any)
Fatalln(...any)
}
Logger is qualified through facilities bearing the followint methods. A given qualifier of this interface can be made active via the ActiveLogger global variable.
var ActiveLogger Logger = ConsoleLogger{}
ActiveLogger contains an interface qualifier instance of the logging facility currently engaged within the package. Any Logger qualified facility may be made active here.
type Monitor ¶
type Monitor struct {
// contains filtered or unexported fields
}
Monitor implements the storage type for all metrics and monitoring information currently known to the server.
func NewMonitor ¶
func NewMonitor() *Monitor
NewMonitor returns a freshly initialized instance of *Monitor.
func (*Monitor) AddConnection ¶
AddConnection returns an instance of uint64 following the addition of a new connection record instance to the underlying connection table. When complete, this method increments both the current and total connection counters by one (1).
The return uint64 value reflects the connection number assigned to the new connection record.
func (*Monitor) RemoveConnection ¶
RemoveConnection returns a Boolean value indicative of whether an attempt to delete the underlying net.Conn instance associated with the input uint64 id from the connection table was successful.
If successful, this method decrements the current connection counter by one (1).
func (*Monitor) SetProtocol ¶
SetProtocol updates the given connection -- as identified by the input uint64 value -- with the specified Protocol. This is necessary when a protocol has not been determined yet, for example, before a service lookup has been conducted.
func (*Monitor) Snapshot ¶
func (r *Monitor) Snapshot() MonitorSnapshot
Snapshot returns a read-only copy of MonitorSnapshot, safe for access without risk of panic.
func (*Monitor) UpdateConnection ¶
UpdateConnection executes the input closure function (fn) against the net.Conn instance associated with the input uint64 id. Among other things, this is used to update the "last active" timestamp in the underlying connection table for a particular session.
type MonitorSnapshot ¶
type MonitorSnapshot struct {
Total uint64 `json:"total"`
Current uint64 `json:"current"`
Sessions []*monitoredConnection `json:"sessions"`
Stats MonitorStats `json:"stats"`
Services map[string]Service `json:"services"`
}
MonitorSnapshot contains a read-only copy of the current metrics and connection table. Instances of this type are produced via the Monitor.Snapshot method.
type MonitorStats ¶
type MonitorStats struct {
StartTime time.Time `json:"start_time"`
UptimeSeconds uint64 `json:"uptime_seconds"`
MaxConcurrent uint64 `json:"max_concurrent"`
ProtocolTotals map[string]uint64 `json:"protocol_totals"`
ProtocolCurrent map[string]uint64 `json:"protocol_current"`
}
MonitorStats contains high-level metrics pertaining to the ZTP service, such as total running time, protocol usage counts, etc. Instances of this type may be found within the *Monitor instance.
type NegativeCache ¶
type NullLogger ¶
type NullLogger struct{}
NullLogger ignores all log messages.
func (NullLogger) Fatalf ¶
func (_ NullLogger) Fatalf(_ string, _ ...any)
func (NullLogger) Fatalln ¶
func (_ NullLogger) Fatalln(_ ...any)
func (NullLogger) Printf ¶
func (_ NullLogger) Printf(_ string, _ ...any)
func (NullLogger) Println ¶
func (_ NullLogger) Println(_ ...any)
type OIDCAuthConfig ¶
type OIDCAuthConfig struct {
Directory *DirectoryManager
SourceName string // e.g. "oidc"
}
type OIDCAuthenticator ¶
type OIDCAuthenticator struct {
// contains filtered or unexported fields
}
func NewOIDCAuthenticator ¶
func NewOIDCAuthenticator(cfg OIDCAuthConfig) *OIDCAuthenticator
func (*OIDCAuthenticator) Authenticate ¶
type OIDCConfig ¶
type OIDCConfig struct {
IssuerURL string // e.g. "http://127.0.0.1:5556/dex"
ClientID string // e.g. "ztp"
JWKSURL string // e.g. "http://127.0.0.1:5556/dex/keys"
Timeout time.Duration // HTTP timeout for discovery/JWKS
}
OIDCConfig defines the configuration parameters for an OIDC directory backend.
type OIDCDirectory ¶
type OIDCDirectory struct {
// contains filtered or unexported fields
}
OIDCDirectory implements Directory for an OIDC / JWT-based backend.
func NewOIDCDirectory ¶
func NewOIDCDirectory(cfg OIDCConfig) *OIDCDirectory
func (*OIDCDirectory) Lookup ¶
func (r *OIDCDirectory) Lookup(identity string) (*DirectoryRecord, error)
Lookup validates the provided ID token and returns a DirectoryRecord derived from its claims. ASSUMPTION: identity == raw ID token string.
func (*OIDCDirectory) Type ¶
func (_ *OIDCDirectory) Type() string
Type returns the string literal `oidc`.
type PolicyEngine ¶
type PolicyEngine struct {
// contains filtered or unexported fields
}
PolicyEngine represents the standard package-included Engine qualifier. Users may use this instead of creating a custom Engine qualifier, if unneeded.
func NewPolicyEngine ¶
func NewPolicyEngine(svcs []*Service, mon ...*Monitor) *PolicyEngine
NewPolicyEngine returns a freshly initialized instance of *PolicyEngine.
func (*PolicyEngine) Authorize ¶
func (r *PolicyEngine) Authorize(id *Identity, svc *Service) error
Authorize returns an error following an attempt to authorize the provided *Identity for access to the specified *Service.
func (*PolicyEngine) List ¶
func (r *PolicyEngine) List() (list []string)
List returns slices of names, each corresponding to a registered *Service instance within the receiver instance.
As the underlying index is map-based, the ordering of string slices is not fixed.
type PreAuthCheck ¶
type PreAuthPipeline ¶
type PreAuthPipeline struct {
// contains filtered or unexported fields
}
func NewPreAuthPipeline ¶
func NewPreAuthPipeline(checks ...PreAuthCheck) *PreAuthPipeline
type Protocol ¶
type Protocol int
const ( ProtocolUnknown Protocol = 0 ProtocolTCP Protocol = 1 ProtocolUDP Protocol = 2 ProtocolQUIC Protocol = 3 ProtocolHTTP Protocol = 4 ProtocolSSH Protocol = 5 )
Built‑in protocol constants. Users may define their own in their own packages.
func DetectProtocol ¶
DetectProtocol reads a small prefix from the stream, runs user-registered detectors first, then built-in detectors, and returns the detected protocol along with a reader that preserves the consumed prefix.
UDP, and those protocols upon which UDP is based, are not eligible for this form of detection.
func ParseProtocol ¶
ParseProtocol converts a config string (e.g. "http") into a Protocol enum. Users can extend this via RegisterProtocolName.
func (Protocol) MarshalJSON ¶
MarshalJSON exists merely for special handling support by the json encoding package.
type ProtocolDetector ¶
ProtocolDetector is a user-defined function that inspects the prefix and returns (protocol, true) if it recognizes the protocol.
type QUICProxyHandler ¶
type QUICProxyHandler struct{}
func NewQUICProxyHandler ¶
func NewQUICProxyHandler() *QUICProxyHandler
func (*QUICProxyHandler) Protocol ¶
func (_ *QUICProxyHandler) Protocol() Protocol
type Service ¶
type Service struct {
Name string `json:"listener_name"`
Address string `json:"backend_name"`
AllowedUser string `json:"allowed_user,omitempty"`
Groups []string `json:"groups,omitempty"`
Handler `json:"-"`
Protocol `json:"protocol"`
// contains filtered or unexported fields
}
Service represents a single service to be registered within a qualifying instance of Engine.
func (*Service) MarshalJSON ¶
type TCPProxyHandler ¶
type TCPProxyHandler struct{}
func NewTCPProxyHandler ¶
func NewTCPProxyHandler() *TCPProxyHandler
func (*TCPProxyHandler) Protocol ¶
func (_ *TCPProxyHandler) Protocol() Protocol
Protocol returns the ProtocolTCP Protocol instance.
type TLSAuthConfig ¶
type TLSAuthConfig struct {
TrustedCAPEM []byte // PEM-encoded CA bundle
IdentityRule TLSIdentityMappingRule
Users []TLSUserMapping // optional explicit mappings
SourceName string // e.g. "tls"
Directory *DirectoryManager
}
TLSAuthConfig defines the parameters for authenticating via certificates. An instance of this type circumscribes an issuing certificate (PEM), a mapping rule and other elements.
type TLSAuthenticator ¶
type TLSAuthenticator struct {
// contains filtered or unexported fields
}
TLSAuthenticator implements the basis for TLS authentication. An instance of this type circumscribes an *x509.CertPool, a TLSAuthConfig and a user index.
func NewTLSAuthenticator ¶
func NewTLSAuthenticator(cfg TLSAuthConfig) (*TLSAuthenticator, error)
NewTLSAuthentivator returns a freshly initialized instance of *TLSAuthenticator alongside an error. The input cfg defines the parameters of TLS auth.
func (*TLSAuthenticator) AuthConfig ¶
func (r *TLSAuthenticator) AuthConfig() (cfg TLSAuthConfig)
AuthConfig returns the underlying instance of TLSAuthConfig.
func (*TLSAuthenticator) Authenticate ¶
func (r *TLSAuthenticator) Authenticate( ctx context.Context, conn net.Conn, state *tls.ConnectionState, ) (*Identity, error)
Authenticate returns an instance of *Identity alongside an error following a call by the listener when a user attempts to utilize the proxy service via TLS authentication. This method accepts a context.Context, a net.Conn, and a *tls.ConnectionState.
func (*TLSAuthenticator) CRL ¶
func (r *TLSAuthenticator) CRL() *x509.RevocationList
CRL returns the underlying instance of *x509.RevocationList, or nil if unset.
This method is thread-safe.
func (*TLSAuthenticator) CRLChanged ¶
func (r *TLSAuthenticator) CRLChanged(a, b *x509.RevocationList) bool
CRLChanged returns a Boolean value indicative of *x509.RevocationList "a" differing from "b".
func (*TLSAuthenticator) IsRevoked ¶
func (r *TLSAuthenticator) IsRevoked(cert *x509.Certificate) (revoked bool)
IsRevoked returns true if cert.SerialNumber is listed in the CRL.
func (*TLSAuthenticator) LoadCRL ¶
func (r *TLSAuthenticator) LoadCRL(path string) (*x509.RevocationList, error)
LoadCRL returns an instance of *x509.RevocationList alongside an error following a one-time load of a CRL from disk (PEM or DER).
This method is NOT thread-safe on its own.
See *TLSAuthenticator.PollCRL for the thread-safe and continuous caller of this method.
func (*TLSAuthenticator) PollCRL ¶
func (r *TLSAuthenticator) PollCRL(path string, interval time.Duration) (err error)
PollCRL performs a continuous call of *TLSAuthenticator.LoadCRL with a frequency of time.Duration. This method is thread-safe.
func (*TLSAuthenticator) TLSConfigForListener ¶
func (r *TLSAuthenticator) TLSConfigForListener() *tls.Config
TLSConfigForListener returns an instance of *tls.Config, which is intended for submission to the ZTP Listener.
type TLSIdentityMappingRule ¶
type TLSIdentityMappingRule struct {
// One of these can be used to derive the Identity.ID
UseSANEmail bool // prefer SAN email if present
UseSANDNS bool // or SAN DNSName
UseSubjectCN bool
}
TLSIdentityMappingRule defines the requirements for mapping a particular certificate to a particular user.
type TLSUserMapping ¶
type TLSUserMapping struct {
// Optional explicit mapping from cert attributes → canonical ID
SubjectDN string // full subject string match (e.g. "CN=Alice,...")
Email string // SAN email
Principal string // resulting principal ID, e.g. "alice"
Groups []string // optional groups
}
TLSUserMapping implements the storage type for TLS certificate mapping logic, whereby a user's email address in a certificate can be mapped to an identity.
type TTLCache ¶
type TTLCache struct {
// contains filtered or unexported fields
}
TTLCache implements a caching type for directory entries.
func NewTTLCache ¶
NewTTLCache returns a freshly initialized instance of *TTLCache.
func (*TTLCache) Clear ¶
func (r *TTLCache) Clear()
Clear purges all cached items from the receiver instance.
func (*TTLCache) Get ¶
func (r *TTLCache) Get(id string) (*DirectoryRecord, bool)
Get returns a *DirectoryRecord instance alongside a Boolean value indicative of a successful call of id from within the receiver instance.
func (*TTLCache) Set ¶
func (r *TTLCache) Set(id string, rec *DirectoryRecord)
Set associates a id with an instance of *DirectoryRecord within the receiver instance.
type UDPProxyHandler ¶
type UDPProxyHandler struct{}
func NewUDPProxyHandler ¶
func NewUDPProxyHandler() *UDPProxyHandler
func (*UDPProxyHandler) Protocol ¶
func (_ *UDPProxyHandler) Protocol() Protocol
Protocol returns the ProtocolUDP Protocol instance.
type YAMLDirectory ¶
type YAMLDirectory struct {
// contains filtered or unexported fields
}
YAMLDirectory describes a YAML-based (flat file) backend.
func NewYAMLDirectory ¶
func NewYAMLDirectory(x any) (y *YAMLDirectory, err error)
NewYAMLDirectory returns a freshly initialized instance of *YAMLDirectory alongside an error following an attempt to read x as YAML data. If x is a string, it is assumed the value represents a path/filename, while if []byte is used, it is assumed to be a pre-read YAML byte sequence. Any other type input is an error.
func (*YAMLDirectory) HasChanged ¶
func (r *YAMLDirectory) HasChanged() (changed bool)
HasChanged returns a Boolean value indicative of whether the specified directory backend has been updated.
func (*YAMLDirectory) Lookup ¶
func (r *YAMLDirectory) Lookup(identity string) (*DirectoryRecord, error)
func (*YAMLDirectory) Type ¶
func (_ *YAMLDirectory) Type() string
Type returns the string literal `yaml`.