Documentation
¶
Overview ¶
Package iam implements the management-plane RBAC of project.md §2.11: flat permission strings bundled into roles, history-preserving role assignments (grant/revoke rows are never deleted — 안전성 확보조치 기준 제5조), scoped API keys, and the default Authorizer.
The Authorizer only decides. Audit logging and masked-by-default projections are enforced by callers (internal/api) regardless of which Authorizer is installed.
Index ¶
- Constants
- Variables
- func HashToken(token string) []byte
- func NewAuthorizer(pool *pgxpool.Pool) ports.Authorizer
- func NewAuthorizerFor(pools PoolFunc) ports.Authorizer
- func NewToken() string
- func ValidTokenFormat(token string) bool
- func ValidateCustomPermission(name string) error
- type APIKey
- type Assignment
- type AssignmentFilter
- type Error
- type Holder
- type Permission
- type PoolFunc
- type Role
- type Service
- func (s *Service) CreateKey(ctx context.Context, projectID, name string, scopes []string, ...) (string, APIKey, error)
- func (s *Service) CreateKeyBy(ctx context.Context, projectID, name string, scopes []string, ...) (string, APIKey, error)
- func (s *Service) CreatePermission(ctx context.Context, projectID, name string) (Permission, error)
- func (s *Service) CreateRole(ctx context.Context, projectID, name string, permissions []string) (Role, error)
- func (s *Service) GetRole(ctx context.Context, projectID, roleID string) (Role, error)
- func (s *Service) GrantRole(ctx context.Context, projectID, roleID, actorID, grantedBy string) (Assignment, error)
- func (s *Service) ListAssignments(ctx context.Context, projectID string, f AssignmentFilter, ...) (httpapi.Page[Assignment], error)
- func (s *Service) ListKeys(ctx context.Context, projectID string, p httpapi.ListParams) (httpapi.Page[APIKey], error)
- func (s *Service) ListPermissions(ctx context.Context, projectID string, p httpapi.ListParams) (httpapi.Page[Permission], error)
- func (s *Service) ListRoles(ctx context.Context, projectID string, p httpapi.ListParams) (httpapi.Page[Role], error)
- func (s *Service) Recertification(ctx context.Context, projectID, permission string) ([]Holder, error)
- func (s *Service) RevokeAssignment(ctx context.Context, projectID string, id int64, revokedBy string) (Assignment, error)
- func (s *Service) RevokeKey(ctx context.Context, projectID, keyID, revokedBy string) (APIKey, error)
- func (s *Service) VerifyKey(ctx context.Context, token string) (*ports.Actor, []string, error)
Constants ¶
const ( ActorTypeAdmin = "admin" ActorTypeAPIKey = "api_key" // ActorTypeUser is a regular end-user access token (`role=authenticated`) // acting on the management plane. Its authority comes exclusively from // role_assignments keyed by the user UUID (§2.11, deny-by-default). ActorTypeUser = "user" ActorTypeServiceRole = "service_role" )
Actor types (ports.Actor.Type).
const ( PermUsersRead = "users.read" PermPIIRead = "pii.read" PermPIIReveal = "pii.reveal" PermPIIExport = "pii.export" // PermPIIWrite guards mutation of a subject's stored personal data // (§5.2 "개인정보 수정"). Seeded by migration 0302. PermPIIWrite = "pii.write" // PermConsentsWrite guards recording a consent change (ledger append). It // is deliberately separate from privacy.requests.manage so a signup-flow // API key can record consent without holding DSR authority. Seeded by // migration 0304. PermConsentsWrite = "consents.write" PermPrivacyRequestsManage = "privacy.requests.manage" PermHoldsManage = "holds.manage" PermPoliciesManage = "policies.manage" PermDestinationsManage = "destinations.manage" PermKeysManage = "keys.manage" PermAuditRead = "audit.read" // PermUsersAdmin authorises the Supabase-compatible admin surface // (/auth/v1/admin/*) for a regular user access token. Seeded by migration // 0303 and bundled into the builtin `owner` role only. PermUsersAdmin = "users.admin" )
Builtin permissions (§2.11). Un-namespaced names are reserved: custom permissions must be namespaced and may not collide with these.
const ( RoleViewer = "role_00000000000000000000000000000001" RoleSupport = "role_00000000000000000000000000000002" RolePrivacyOfficer = "role_00000000000000000000000000000003" RoleSecurityAdmin = "role_00000000000000000000000000000004" RoleOwner = "role_00000000000000000000000000000005" )
Builtin role ids seeded by migration 0300.
const DefaultProjectID = "default"
DefaultProjectID — wave 1 is single-project (PLAN.md §3.5).
const RoleAuthenticated = "authenticated"
RoleAuthenticated is the Supabase JWT `role` claim of a regular signed-in user. It mirrors auth.RoleAuthenticated (the packages must not import each other).
const TokenPrefix = "dk_"
TokenPrefix is the scoped API key prefix (docs/api-conventions.md).
Variables ¶
var BuiltinPermissions = []string{ PermUsersRead, PermPIIRead, PermPIIReveal, PermPIIWrite, PermPIIExport, PermConsentsWrite, PermPrivacyRequestsManage, PermHoldsManage, PermPoliciesManage, PermDestinationsManage, PermKeysManage, PermAuditRead, PermUsersAdmin, }
BuiltinPermissions is the authoritative list seeded by migrations 0300 (all but pii.write), 0302 (pii.write), 0303 (users.admin) and 0304 (consents.write).
Functions ¶
func NewAuthorizer ¶
func NewAuthorizer(pool *pgxpool.Pool) ports.Authorizer
NewAuthorizer returns the builtin RBAC Authorizer (§2.11): deny-by-default, project-scoped, decided from active role assignments plus (for API key actors) the key's own scopes. It decides only — callers still audit the decision and apply masked-by-default projections.
The pool is fixed: every decision is made against it. Multi-instance deployments use NewAuthorizerFor instead, which resolves the pool of the instance selected on the request context.
func NewAuthorizerFor ¶
func NewAuthorizerFor(pools PoolFunc) ports.Authorizer
NewAuthorizerFor returns the builtin RBAC Authorizer over a per-request pool provider (internal/instances.Registry.Pool).
func NewToken ¶
func NewToken() string
NewToken mints a `dk_<48 url-safe random>` token. The plaintext token is returned to the caller exactly once; only SHA-256(token) is persisted.
func ValidTokenFormat ¶
ValidTokenFormat reports whether a string looks like a Dilion API key.
func ValidateCustomPermission ¶
ValidateCustomPermission enforces §2.11: custom permissions must be namespaced and must not collide with a builtin (un-namespaced) permission.
Types ¶
type APIKey ¶
type Assignment ¶
type AssignmentFilter ¶
type AssignmentFilter struct {
RoleID string
ActorID string
IncludeRevoked bool // history view (권한 이력 = 컴플라이언스 데이터)
}
AssignmentFilter narrows ListAssignments. Zero value = active assignments of the whole project.
type Holder ¶
type Holder struct {
ActorID string
RoleID string
RoleName string
GrantedBy *string
GrantedAt time.Time
}
Holder is one row of the CC6.3 recertification report: an actor currently holding a permission, together with the grant metadata that justifies it.
type Permission ¶
type PoolFunc ¶
PoolFunc resolves the database pool for a request. It is structurally identical to instances.PoolFunc, which is what production wires in; the type is declared locally so this package stays free of an instances import.
func StaticPool ¶
StaticPool is the single-instance PoolFunc: it always answers with pool. A nil pool yields an error at use time (spec generation constructs services with a nil pool and never calls them).
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the RBAC + API key store. It is safe for concurrent use.
func NewFor ¶
NewFor builds the service over a per-request pool provider, so each call is served by the database of the instance selected on the context.
func (*Service) CreateKey ¶
func (s *Service) CreateKey(ctx context.Context, projectID, name string, scopes []string, expires *time.Time) (string, APIKey, error)
CreateKey issues a scoped API key (§2.11 "Machine key"). The returned token is the only time the plaintext is available.
func (*Service) CreateKeyBy ¶
func (s *Service) CreateKeyBy(ctx context.Context, projectID, name string, scopes []string, expires *time.Time, createdBy string) (string, APIKey, error)
CreateKeyBy is CreateKey with the granting actor recorded.
func (*Service) CreatePermission ¶
CreatePermission registers a custom (namespaced) permission.
func (*Service) CreateRole ¶
func (s *Service) CreateRole(ctx context.Context, projectID, name string, permissions []string) (Role, error)
CreateRole registers a custom role. Every listed permission must already be registered (builtin seed or a custom permission) in the project.
func (*Service) GrantRole ¶
func (s *Service) GrantRole(ctx context.Context, projectID, roleID, actorID, grantedBy string) (Assignment, error)
GrantRole records a new active assignment. Re-granting an already active (actor, role) pair is a conflict — the history table must stay unambiguous.
func (*Service) ListAssignments ¶
func (s *Service) ListAssignments(ctx context.Context, projectID string, f AssignmentFilter, p httpapi.ListParams) (httpapi.Page[Assignment], error)
func (*Service) ListPermissions ¶
func (s *Service) ListPermissions(ctx context.Context, projectID string, p httpapi.ListParams) (httpapi.Page[Permission], error)
func (*Service) Recertification ¶
func (s *Service) Recertification(ctx context.Context, projectID, permission string) ([]Holder, error)
Recertification answers "who currently holds permission X, and on what grant basis" (CC6.3 recertification report, §2.11).
func (*Service) RevokeAssignment ¶
func (s *Service) RevokeAssignment(ctx context.Context, projectID string, id int64, revokedBy string) (Assignment, error)
RevokeAssignment marks an assignment revoked. The row is preserved.
func (*Service) RevokeKey ¶
func (s *Service) RevokeKey(ctx context.Context, projectID, keyID, revokedBy string) (APIKey, error)
RevokeKey revokes a key. Idempotency is intentionally not offered: revoking an already revoked key is a conflict so operators notice stale automation.