Documentation
¶
Overview ¶
Package register provides scheme registration and authorization generation for the varwof project.
Index ¶
- Variables
- func Deduplicate(caps []string) []string
- func FilterByScheme(caps []string, schemeID string) []string
- func FormatCapability(schemeID, capID string) string
- func FormatSchemeID(vendor, product string) string
- func GenAuthzToFile(cfg GenAuthzConfig, outputPath string) error
- func GenDocs(def *SchemeDefinition) (string, error)
- func GenDocsToFile(def *SchemeDefinition, outputPath string) error
- func GetSignerCert(p7sPath string) (*x509.Certificate, error)
- func HasSignature(capPath string) bool
- func ListCapabilities(def *SchemeDefinition) []string
- func LoadAllSchemes(root string) (map[string]*SchemeDefinition, error)
- func LoadCertFile(path string) ([]*x509.Certificate, error)
- func LoadEmbedded() (map[string]*SchemeDefinition, error)
- func LoadFromBoth(diskDir string) (map[string]*SchemeDefinition, error)
- func LoadFromDir(root string) (map[string]*SchemeDefinition, error)
- func LoadFromFS(fsys fs.FS) (map[string]*SchemeDefinition, error)
- func LoadTrustRoots(path string) ([]*x509.Certificate, error)
- func MatchCapability(id, pattern string) bool
- func ParseCapability(s string) (schemeID, capID string, ok bool)
- func ParseSchemeID(schemeID string) (vendor, product string, ok bool)
- func SignCapability(certPath, keyPath, capPath, outputPath string) error
- func ValidateSchemeID(schemeID string) error
- func VerifyCapabilityPKCS7(capPath string, trustRoots []*x509.Certificate) error
- func WriteScheme(def *SchemeDefinition, path string) error
- type AuthzDocument
- type AuthzRoleDef
- type CapabilityClaim
- type CapabilityEntry
- type ClaimResult
- type GatewayNSDef
- type GenAuthzConfig
- type MinSetReport
- type ParameterDef
- type Registry
- func (r *Registry) CheckIntersection(setA, setB []string) (common []string)
- func (r *Registry) CheckMinimalCapabilitySet(claims []CapabilityClaim, grantedPatterns []string) *MinSetReport
- func (r *Registry) CheckSubset(declared, allowed []string) (denied []string)
- func (r *Registry) Get(schemeID string) (*SchemeDefinition, bool)
- func (r *Registry) Has(schemeID string) bool
- func (r *Registry) HasCapability(schemeID, capID string) bool
- func (r *Registry) Register(def *SchemeDefinition)
- func (r *Registry) RoleGrantCovered(schemeID, grant string) bool
- func (r *Registry) SchemeIDs() []string
- func (r *Registry) Summary() string
- func (r *Registry) ValidateCapabilities(caps []string) *ValidationResult
- func (r *Registry) ValidateCapability(formatted string) (*SchemeDefinition, *CapabilityEntry, error)
- func (r *Registry) ValidateClaims(claims []CapabilityClaim) []ClaimResult
- func (r *Registry) ValidateRoles(schemeID string) ([]string, error)
- type RoleDef
- type SchemeDefinition
- type ValidationError
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
var Version = "0.1.0"
Version is the package version, set via -ldflags -X github.com/varwof/register.Version=x.y.z.
Functions ¶
func Deduplicate ¶
Deduplicate removes duplicate capabilities from a list.
func FilterByScheme ¶
FilterByScheme returns only capabilities belonging to a specific scheme.
func FormatCapability ¶
FormatCapability formats a capability as "vendor/product:capability_id".
func FormatSchemeID ¶
FormatSchemeID formats vendor and product into "vendor/product".
func GenAuthzToFile ¶
func GenAuthzToFile(cfg GenAuthzConfig, outputPath string) error
GenAuthzToFile generates and writes the authz.json file.
func GenDocs ¶
func GenDocs(def *SchemeDefinition) (string, error)
GenDocs generates a markdown permission documentation from a capability.json scheme. The documentation targets both human readers and AI models: fully describing each capability's semantics, parameter constraints, wildcard rules, role and grants mappings, serving as the authoritative reference for AI to generate minimal privilege capability sets.
Output markdown structure:
- Product overview + capability catalog table
- Detailed capability semantics (summary/usage/when_not/examples/parameters/related)
- Wildcard and matching rules
- Role and grants mapping
- Least privilege principle guidelines
func GenDocsToFile ¶
func GenDocsToFile(def *SchemeDefinition, outputPath string) error
GenDocsToFile generates markdown permission documentation and writes it to a file.
func GetSignerCert ¶
func GetSignerCert(p7sPath string) (*x509.Certificate, error)
GetSignerCert extracts the signer certificate from a .p7s file.
func HasSignature ¶
HasSignature checks if a .p7s file exists for the given capability file.
func ListCapabilities ¶
func ListCapabilities(def *SchemeDefinition) []string
ListCapabilities returns all capability IDs for a scheme, sorted.
func LoadAllSchemes ¶
func LoadAllSchemes(root string) (map[string]*SchemeDefinition, error)
LoadAllSchemes loads all capability JSON files under a directory tree. Expected structure: root/vendor/product/v*.json
func LoadCertFile ¶
func LoadCertFile(path string) ([]*x509.Certificate, error)
LoadCertFile reads all certificates in a certificate chain from a PEM file.
func LoadEmbedded ¶
func LoadEmbedded() (map[string]*SchemeDefinition, error)
LoadEmbedded is removed: capability data now lives in the separate capability module and is loaded from a directory on disk. Use LoadFromDir or LoadFromBoth with a path into the capability data tree.
func LoadFromBoth ¶
func LoadFromBoth(diskDir string) (map[string]*SchemeDefinition, error)
LoadFromBoth requires a non-empty disk directory. Embedded schemes are gone; disk is the only source. An empty dir returns an error.
func LoadFromDir ¶
func LoadFromDir(root string) (map[string]*SchemeDefinition, error)
LoadFromDir loads all capability JSON files from a directory tree on disk. Expected structure: root/vendor/product/v*.json
func LoadFromFS ¶
func LoadFromFS(fsys fs.FS) (map[string]*SchemeDefinition, error)
LoadFromFS loads all capability JSON files from an embedded filesystem.
func LoadTrustRoots ¶
func LoadTrustRoots(path string) ([]*x509.Certificate, error)
LoadTrustRoots loads PEM certificates from a file or directory.
func MatchCapability ¶
MatchCapability checks if a capability id matches a pattern (glob semantics). Supports: exact match, *, ?, a:b:* prefix wildcards. Same semantics as pki-types MatchCapability.
func ParseCapability ¶
ParseCapability parses "vendor/product:capability_id" into scheme and capID. capability_id may itself contain colons (e.g., "query:users").
func ParseSchemeID ¶
ParseSchemeID parses "vendor/product" into vendor and product.
func SignCapability ¶
SignCapability signs a capability JSON file using PKCS#7 detached signature. certPath: PEM certificate chain (signer cert + intermediates) keyPath: PEM private key capPath: path to capability.json outputPath: path to write .p7s file (defaults to capPath + ".p7s")
func ValidateSchemeID ¶
ValidateSchemeID checks if scheme_id follows the naming convention. Public: vendor/product (e.g., oracle/mysql, varwof/core) Private: x-vendor/product (e.g., x-acme/order)
func VerifyCapabilityPKCS7 ¶
func VerifyCapabilityPKCS7(capPath string, trustRoots []*x509.Certificate) error
VerifyCapabilityPKCS7 verifies a capability JSON against its .p7s signature. trustRoots: PEM root/intermediate certificates for chain verification.
func WriteScheme ¶
func WriteScheme(def *SchemeDefinition, path string) error
WriteScheme serializes a scheme definition as JSON and writes it to a file (for gen-authz tests/rewrites).
Types ¶
type AuthzDocument ¶
type AuthzDocument struct {
Version string `json:"version"`
Roles map[string]AuthzRoleDef `json:"roles"`
OUMapping map[string]string `json:"ou_mapping"`
GatewayNamespaces map[string]GatewayNSDef `json:"gateway_namespaces,omitempty"`
CapabilityParameters map[string]map[string]any `json:"capability_parameters,omitempty"`
}
AuthzDocument is the complete authz.json document generated by gen-authz. The top-level structure is compatible with core/auth.Policy; capability_parameters is an extension field (core's encoding/json deserialization ignores unknown fields).
func GenAuthz ¶
func GenAuthz(cfg GenAuthzConfig) (*AuthzDocument, error)
GenAuthz generates an authz.json document from capability.json schemes.
Mapping rules:
- Roles from the primary scheme (first) become authz.json roles (grants preserved as-is)
- Each role's OUs are expanded into ou_mapping (OU → role name)
- Role names with namespace prefixes (e.g. gateway:admin) are aggregated into gateway_namespaces
- Capability parameter defaults from all schemes are aggregated into capability_parameters
type AuthzRoleDef ¶
type AuthzRoleDef struct {
DisplayName string `json:"display_name"`
Profiles []string `json:"profiles"`
Grants []string `json:"grants"`
Scope []string `json:"scope,omitempty"`
}
AuthzRoleDef is a role entry for generating authz.json (compatible with core Policy.RoleDef).
type CapabilityClaim ¶
type CapabilityClaim struct {
SchemeID string `json:"scheme_id"` // vendor/product
Capability string `json:"capability"` // capability_id (may contain wildcards)
Parameters map[string]any `json:"parameters,omitempty"`
Rationale string `json:"rationale,omitempty"` // Authorization rationale from AI
}
CapabilityClaim is a single AI-generated capability claim (pending validation/minimal privilege detection).
func ParseCapabilityClaims ¶
func ParseCapabilityClaims(data []byte) ([]CapabilityClaim, error)
ParseCapabilityClaims parses a list of capability claims from JSON data. Expected structure: [{"scheme_id":"varwof/core","capability":"cert:issue",...}]
type CapabilityEntry ¶
type CapabilityEntry struct {
ID string `json:"id"`
Description string `json:"description"`
Parameters map[string]ParameterDef `json:"parameters,omitempty"`
// ParamsSchema carries a JSON Schema document for structured,
// nested capability parameters (e.g. database tables/columns/
// row_filter). Additive: schemes using only the flat ParameterDef
// model leave it unset.
ParamsSchema json.RawMessage `json:"params_schema,omitempty"`
// AI-friendly semantic description fields (used by gen-docs to generate markdown permission docs).
// These fields help LLMs understand the exact purpose of each capability,
// enabling them to generate minimal privilege capability sets per task.
Summary string `json:"summary,omitempty"` // One-line summary (defaults to Description)
Usage string `json:"usage,omitempty"` // When this capability is needed (typical scenarios)
WhenNot string `json:"when_not,omitempty"` // When this capability should NOT be granted (avoid over-provisioning)
Examples []string `json:"examples,omitempty"` // Typical usage examples
Related []string `json:"related,omitempty"` // Related capability IDs (collaboration/alternative relationships)
}
CapabilityEntry defines a single capability within a scheme.
type ClaimResult ¶
type ClaimResult struct {
Claim CapabilityClaim
Valid bool
Error string // Reason when Valid=false
}
ClaimResult is the validation result for a single claim.
type GatewayNSDef ¶
type GatewayNSDef struct {
DisplayName string `json:"display_name"`
Prefix string `json:"prefix"`
Grants []string `json:"grants"`
}
GatewayNSDef is a gateway namespace entry for generating authz.json.
type GenAuthzConfig ¶
type GenAuthzConfig struct {
// SchemePaths is the list of capability.json file paths to merge.
// The primary scheme (providing roles) must be the first; other schemes only contribute capability catalogs.
SchemePaths []string
// When VerifySignature is true, enforce signature verification if .p7s exists; fail on error.
// Files without .p7s only error when VerifyRequired is true.
VerifySignature bool
// When VerifyRequired is true, capability files missing .p7s signature fail immediately.
VerifyRequired bool
// TrustRootsPEM is the trust root certificates for signature verification (PEM file paths).
TrustRootsPEM []string
// Version is the generated authz.json version field (default "v2").
Version string
// NamespacePrefix is appended to role names to generate gateway namespace prefix role grants,
// e.g. "gateway" → gateway_namespaces["gateway:"].
// By default, extracts the "gateway:xxx" prefix from all Roles in the primary scheme.
NamespacePrefix string
}
GenAuthzConfig is the input configuration for GenAuthz.
type MinSetReport ¶
type MinSetReport struct {
// ValidClaims are valid and non-redundant claims.
ValidClaims []CapabilityClaim
// InvalidClaims are invalid claims (illegal capability/illegal parameters/unknown scheme).
InvalidClaims []ClaimResult
// RedundantClaims are claims covered by a wildcard or duplicated (recommended to remove).
RedundantClaims []ClaimResult
// MissingGranted are capabilities that are claimed but not covered by any role grant
// (the AI-generated set references a capability not authorized for this identity).
MissingGranted []string
// AllowedPatterns are the grants actually held by the identity (wildcards expanded).
AllowedPatterns []string
// IsMinimal is true when the set is already minimal privilege.
IsMinimal bool
}
MinSetReport is the complete report for minimal privilege validation.
type ParameterDef ¶
type ParameterDef struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Default interface{} `json:"default,omitempty"`
Min interface{} `json:"min,omitempty"`
Max interface{} `json:"max,omitempty"`
Enum []string `json:"enum,omitempty"`
Required bool `json:"required,omitempty"`
}
ParameterDef defines a parameter for a capability.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds all loaded scheme definitions.
func NewRegistryFromBoth ¶
NewRegistryFromBoth requires a non-empty disk directory and creates a registry pre-loaded from it.
func NewRegistryFromDisk ¶
NewRegistryFromDisk creates a registry pre-loaded with schemes from a directory into the capability data tree.
func NewRegistryWithEmbedded ¶
NewRegistryWithEmbedded is removed. Use NewRegistryFromDisk instead.
func (*Registry) CheckIntersection ¶
CheckIntersection returns capabilities present in both sets.
func (*Registry) CheckMinimalCapabilitySet ¶
func (r *Registry) CheckMinimalCapabilitySet(claims []CapabilityClaim, grantedPatterns []string) *MinSetReport
CheckMinimalCapabilitySet performs minimal privilege validation:
- Validate each claim's legality (scheme/capability/parameters)
- Detect redundancy: covered by another wildcard claim, or completely duplicated
- Detect over-privilege: claimed capabilities not within the identity's granted authorization scope
- Determine whether minimal privilege has been achieved
grantedPatterns are the grants actually held by the identity (e.g. role grants, may contain wildcards). Pass nil to skip over-privilege checking (only check legality and redundancy).
func (*Registry) CheckSubset ¶
CheckSubset checks if declared capabilities are a subset of allowed capabilities. Returns denied capabilities that are not in the allowed set.
func (*Registry) Get ¶
func (r *Registry) Get(schemeID string) (*SchemeDefinition, bool)
Get returns a scheme definition by scheme_id.
func (*Registry) HasCapability ¶
HasCapability checks if a specific capability is registered.
func (*Registry) Register ¶
func (r *Registry) Register(def *SchemeDefinition)
Register adds a scheme definition. Overwrites if scheme_id already exists.
func (*Registry) RoleGrantCovered ¶
RoleGrantCovered checks if a single grant is covered by the scheme's capabilities (wildcard expanded).
func (*Registry) ValidateCapabilities ¶
func (r *Registry) ValidateCapabilities(caps []string) *ValidationResult
ValidateCapabilities validates a list of "scheme:cap_id" strings against the registry.
func (*Registry) ValidateCapability ¶
func (r *Registry) ValidateCapability(formatted string) (*SchemeDefinition, *CapabilityEntry, error)
ValidateCapability checks if "scheme:cap_id" is valid and returns the entry.
func (*Registry) ValidateClaims ¶
func (r *Registry) ValidateClaims(claims []CapabilityClaim) []ClaimResult
ValidateClaims validates capability claims: scheme exists, capability is legal (supports wildcards). Returns the result for each claim. Does not include minimal privilege detection.
func (*Registry) ValidateRoles ¶
ValidateRoles validates that all role grants in a scheme are covered by capabilities. Returns uncovered grants (wildcards expanded against capabilities for validation). Use case: ensure role grants are all legal capabilities before gen-authz generates authz.json.
type RoleDef ¶
type RoleDef struct {
DisplayName string `json:"display_name,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Grants []string `json:"grants"`
// OUs is the list of certificate OrganizationalUnits this role can be bound to.
// When generating authz.json, written into ou_mapping; if left empty, no OU mapping entry is generated.
OUs []string `json:"ous,omitempty"`
}
RoleDef defines a role within a product (used to generate authz.json). grants is a list of capability_id values (e.g. "ca:list", "cert:*"), supports wildcards (* / a:b:*); during expansion validation, all must fall within Capabilities.
type SchemeDefinition ¶
type SchemeDefinition struct {
SchemeID string `json:"scheme_id"`
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Vendor string `json:"vendor"`
Product string `json:"product"`
Author string `json:"author,omitempty"`
License string `json:"license,omitempty"`
Homepage string `json:"homepage,omitempty"`
Capabilities []CapabilityEntry `json:"capabilities"`
// Roles defines roles within this product (grants reference capability_id from this scheme).
// Used by gen-authz tool when generating authz.json; can be empty (pure capability catalog products).
Roles map[string]RoleDef `json:"roles,omitempty"`
}
SchemeDefinition defines all capabilities for a product.
func LoadScheme ¶
func LoadScheme(path string) (*SchemeDefinition, error)
LoadScheme reads a capability JSON file and returns the definition.
func (*SchemeDefinition) ValidateSchemeRoles ¶
func (def *SchemeDefinition) ValidateSchemeRoles() ([]error, []string)
ValidateSchemeRoles validates the consistency of role definitions within a scheme:
- Role names are non-empty
- Role grants are non-empty
- Non-wildcard grants must be covered by capabilities (strict error)
- Wildcard grants not covered locally are treated as cross-scheme namespace authorization (e.g. core role referencing gateway:*), returning a warning
Returns (errors, warnings).
type ValidationError ¶
ValidationError describes a single validation failure.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
type ValidationResult ¶
type ValidationResult struct {
Valid bool
Errors []ValidationError
Warnings []string
Checked int
}
ValidationResult holds the outcome of validating a set of capabilities.