Help us shape the future of Go! Take the 2026 Go Developer Survey

rbac

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package rbac implements local control-plane identities, tenant isolation, role inheritance, and fail-closed permission evaluation.

Index

Constants

View Source
const (
	DefaultTenantID = "tenant_default"
	BootstrapUserID = "user_bootstrap_admin"
	AdminRoleID     = "role_default_admin"
	AuditorRoleID   = "role_default_auditor"
	OperatorRoleID  = "role_default_operator"
	ViewerRoleID    = "role_default_viewer"

	PermissionDatasourceManage = "datasource.manage"
	PermissionStrategyManage   = "strategy.manage"
	PermissionQueryExecute     = "query.execute"
	PermissionAuditView        = "audit.view"
	PermissionUserManage       = "user.manage"
	PermissionRoleManage       = "role.manage"
	PermissionTenantManage     = "tenant.manage"
)

Variables

View Source
var (
	ErrInvalidCredentials = errors.New("invalid credentials")
	ErrForbidden          = errors.New("permission denied")
	ErrInvalidTenant      = errors.New("invalid tenant context")
)

Functions

func AllPermissions

func AllPermissions() []string

func HashPassword

func HashPassword(password string) (string, error)

HashPassword produces a bcrypt password hash suitable for persistence.

func NewID

func NewID(prefix string) string

func VerifyPassword

func VerifyPassword(hash, password string) bool

VerifyPassword compares a password with its bcrypt hash and rejects malformed hashes without exposing the underlying parser error.

Types

type Principal

type Principal struct {
	UserID      string
	TenantID    string
	Username    string
	Permissions []string
	RoleIDs     []string
}

Principal is the authenticated, tenant-bound identity placed in a request.

func (Principal) Has

func (principal Principal) Has(permission string) bool

func (Principal) IsBootstrapAdmin

func (principal Principal) IsBootstrapAdmin() bool

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service composes password verification and the persisted RBAC graph.

func NewService

func NewService(repository *store.RBACRepository) *Service

func (*Service) Authenticate

func (service *Service) Authenticate(ctx context.Context, tenantID, username, password string) (Principal, error)

func (*Service) Authorize

func (service *Service) Authorize(ctx context.Context, tenantID, userID, permission string) (Principal, error)

func (*Service) Bootstrap

func (service *Service) Bootstrap(ctx context.Context, username, password string) error

Bootstrap creates the default tenant, preset roles, and initial administrator exactly when missing. The password is accepted from the existing environment credential source and is persisted only as a bcrypt hash.

func (*Service) CanDelegateRole added in v0.6.0

func (service *Service) CanDelegateRole(ctx context.Context, principal Principal, candidate model.Role) error

CanDelegateRole evaluates the resulting role graph before writing a new or updated role. Unknown parents and inheritance cycles fail closed.

func (*Service) CanDelegateRoles added in v0.6.0

func (service *Service) CanDelegateRoles(ctx context.Context, principal Principal, tenantID string, roleIDs []string) error

CanDelegateRoles prevents a role manager from assigning permissions that the manager does not already hold, including permissions inherited by roles.

func (*Service) CanManageUser added in v0.6.0

func (service *Service) CanManageUser(ctx context.Context, principal Principal, tenantID, userID string) error

CanManageUser protects password resets, status changes, deletion, and role replacement of accounts whose effective privileges exceed the caller's.

func (*Service) Principal

func (service *Service) Principal(ctx context.Context, tenantID, userID string) (Principal, error)

Principal re-loads user, tenant, roles, inheritance, and permissions on each request. Revocations therefore take effect immediately even for live tokens.

func (*Service) ProvisionTenant

func (service *Service) ProvisionTenant(ctx context.Context, tenantID string) ([]model.Role, error)

ProvisionTenant installs the same public preset role vocabulary for a newly created tenant. It does not create a user or accept credentials implicitly.

Jump to

Keyboard shortcuts

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