Documentation
¶
Overview ¶
Package scope provides context-based scope extraction for audit events and queries.
When running inside Forge, it extracts AppID, TenantID, UserID, and IP from forge.Scope and authsome.User in the context. When running standalone, it gracefully handles missing scope values.
Index ¶
- func ApplyToAggregateQuery(ctx context.Context, q *audit.AggregateQuery)
- func ApplyToCountQuery(ctx context.Context, q *audit.CountQuery)
- func ApplyToEvent(ctx context.Context, event *audit.Event)
- func ApplyToQuery(ctx context.Context, q *audit.Query)
- func ParseTrustedProxies(entries []string) ([]netip.Prefix, error)
- func WithAppID(ctx context.Context, appID string) context.Context
- func WithIP(ctx context.Context, ip string) context.Context
- func WithInfo(ctx context.Context, info Info) context.Context
- func WithRequestID(ctx context.Context, rid string) context.Context
- func WithTenantID(ctx context.Context, tenantID string) context.Context
- func WithUserAgent(ctx context.Context, ua string) context.Context
- func WithUserID(ctx context.Context, userID string) context.Context
- type Info
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyToAggregateQuery ¶
func ApplyToAggregateQuery(ctx context.Context, q *audit.AggregateQuery)
ApplyToAggregateQuery enforces scope on an aggregate query.
func ApplyToCountQuery ¶
func ApplyToCountQuery(ctx context.Context, q *audit.CountQuery)
ApplyToCountQuery enforces scope on a count query.
func ApplyToEvent ¶
ApplyToEvent sets scope fields on an event from context. Fields already set on the event are not overwritten.
func ApplyToQuery ¶
ApplyToQuery enforces scope on a query. For non-platform callers (those with a TenantID in context), the query's TenantID is forcibly set to the caller's tenant — preventing cross-tenant data access.
func ParseTrustedProxies ¶
ParseTrustedProxies converts CIDR blocks or bare addresses into prefixes for FromRequestWithProxies.
trusted, err := scope.ParseTrustedProxies([]string{"10.0.0.0/8", "192.168.1.1"})
func WithRequestID ¶
WithRequestID returns a context with the given request correlation id.
func WithTenantID ¶
WithTenantID returns a context with the given tenant ID.
func WithUserAgent ¶
WithUserAgent returns a context with the given User-Agent string.
Types ¶
type Info ¶
type Info struct {
AppID string
TenantID string
UserID string
IP string
UserAgent string
RequestID string
}
Info holds extracted scope information from the context.
func FromContext ¶
FromContext extracts scope information from the context. It checks for scope values set via With* functions. Returns zero Info if no scope is present (standalone mode).
func FromRequest ¶
FromRequest extracts the client IP from an HTTP request and merges it with any existing scope in the request's context.
Proxy headers are ignored: the IP comes from the connection's peer address. X-Forwarded-For and X-Real-IP are set by whoever is talking to you, so trusting them lets any caller choose the address written into the audit trail. If your service really sits behind a proxy, use FromRequestWithProxies with that proxy's address range.
func FromRequestWithProxies ¶
FromRequestWithProxies is FromRequest but honours X-Forwarded-For and X-Real-IP when the request's peer is one of the trusted proxies.
With a trusted peer, the client-most entry of X-Forwarded-For is used. Build trusted from your load balancer's ranges with ParseTrustedProxies; passing nil disables header trust entirely.