统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
1 parent
28cd9d3406
commit
f60d3b0880
603 files changed
+2351
-660
No files matched your search
@@ -0,0 +1,155 @@
|
||||
package access
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDefaultDeny(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
req Request
|
||||
code DenialCode
|
||||
}{
|
||||
{name: "invalid role", req: Request{ActorRole: Role("root"), Capability: CapabilityConfigRead}, code: CodeInvalidRole},
|
||||
{name: "unknown capability", req: Request{ActorRole: RoleSuperAdmin, Capability: Capability("config.delete")}, code: CodeUnknownCapability},
|
||||
{name: "missing grant", req: Request{ActorRole: RoleUser, Capability: CapabilityConfigRollback}, code: CodeAccessDenied},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if err := Authorize(test.req); !IsDenied(err, test.code) {
|
||||
t.Fatalf("expected %s, got %v", test.code, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleBoundaries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
req Request
|
||||
code DenialCode
|
||||
}{
|
||||
{name: "super admin resets admin", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPasswordReset, TargetRole: RoleAdmin, RecentAuth: true, RecoveryCodeConfigured: true}},
|
||||
{name: "super admin cannot be web reset target", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPasswordReset, TargetRole: RoleSuperAdmin, RecentAuth: true, RecoveryCodeConfigured: true}, code: CodeTargetRoleDenied},
|
||||
{name: "admin resets user", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPasswordReset, TargetRole: RoleUser, RecentAuth: true, RecoveryCodeConfigured: true}},
|
||||
{name: "admin cannot reset admin", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPasswordReset, TargetRole: RoleAdmin, RecentAuth: true, RecoveryCodeConfigured: true}, code: CodeTargetRoleDenied},
|
||||
{name: "user cannot reset another user", req: Request{ActorRole: RoleUser, Capability: CapabilityPasswordReset, TargetRole: RoleUser, RecentAuth: true, RecoveryCodeConfigured: true}, code: CodeAccessDenied},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := Authorize(test.req)
|
||||
if test.code == "" && err != nil {
|
||||
t.Fatalf("expected allow, got %v", err)
|
||||
}
|
||||
if test.code != "" && !IsDenied(err, test.code) {
|
||||
t.Fatalf("expected %s, got %v", test.code, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryAndRecentAuthentication(t *testing.T) {
|
||||
admin := Request{ActorRole: RoleAdmin, Capability: CapabilityPluginRestart, RecentAuth: true, PluginInstanceAuthorized: true}
|
||||
if err := Authorize(admin); !IsDenied(err, CodeRecoveryCodeRequired) {
|
||||
t.Fatalf("expected recovery code requirement, got %v", err)
|
||||
}
|
||||
admin.RecoveryCodeConfigured = true
|
||||
admin.RecentAuth = false
|
||||
if err := Authorize(admin); !IsDenied(err, CodeRecentAuthRequired) {
|
||||
t.Fatalf("expected recent authentication requirement, got %v", err)
|
||||
}
|
||||
superAdmin := Request{ActorRole: RoleSuperAdmin, Capability: CapabilityConfigRollback, RecentAuth: true}
|
||||
if err := Authorize(superAdmin); err != nil {
|
||||
t.Fatalf("super admin should be exempt from recovery code precondition: %v", err)
|
||||
}
|
||||
adminRollback := Request{ActorRole: RoleAdmin, Capability: CapabilityConfigRollback, RecentAuth: true}
|
||||
if err := Authorize(adminRollback); !IsDenied(err, CodeAccessDenied) {
|
||||
t.Fatalf("admin must not receive a recovery-code bypass for a super-admin capability: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleChangeRequiresTargetRecovery(t *testing.T) {
|
||||
req := Request{ActorRole: RoleSuperAdmin, Capability: CapabilityRoleManage, TargetRole: RoleUser, NewRole: RoleAdmin, RecentAuth: true, RecoveryCodeConfigured: true}
|
||||
if err := Authorize(req); !IsDenied(err, CodeRecoveryCodeRequired) {
|
||||
t.Fatalf("expected target recovery requirement, got %v", err)
|
||||
}
|
||||
req.TargetRecoveryConfigured = true
|
||||
if err := Authorize(req); err != nil {
|
||||
t.Fatalf("expected role change to be allowed, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPluginRestartScope(t *testing.T) {
|
||||
req := Request{ActorRole: RoleAdmin, Capability: CapabilityPluginRestart, RecentAuth: true, RecoveryCodeConfigured: true}
|
||||
if err := Authorize(req); !IsDenied(err, CodePluginScopeDenied) {
|
||||
t.Fatalf("expected plugin scope denial, got %v", err)
|
||||
}
|
||||
req.PluginInstanceAuthorized = true
|
||||
if err := Authorize(req); err != nil {
|
||||
t.Fatalf("expected plugin restart to be allowed, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagementEndpointCatalog(t *testing.T) {
|
||||
endpoints := ManagementEndpoints()
|
||||
if len(endpoints) != 32 {
|
||||
t.Fatalf("unexpected endpoint count: %d", len(endpoints))
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
for _, endpoint := range endpoints {
|
||||
key := endpoint.Method + " " + endpoint.Path
|
||||
if _, ok := seen[key]; ok {
|
||||
t.Fatalf("duplicate endpoint: %s", key)
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
if _, ok := GetPolicy(endpoint.Capability); !ok {
|
||||
t.Fatalf("endpoint %s references unknown capability", key)
|
||||
}
|
||||
}
|
||||
endpoint, ok := LookupEndpoint("POST", "/api/plugin/settings/example")
|
||||
if !ok || endpoint.Capability != CapabilityPluginWriteOwn {
|
||||
t.Fatal("plugin settings endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("POST", "/api/v1/config/smtp/validate")
|
||||
if !ok || endpoint.Capability != CapabilityConfigWriteGlobal {
|
||||
t.Fatal("configuration validation endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("POST", "/api/v1/system/restart")
|
||||
if !ok || endpoint.Capability != CapabilityServiceRestart {
|
||||
t.Fatal("system restart endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("GET", "/api/v1/config/email")
|
||||
if !ok || endpoint.Capability != CapabilityConfigRead {
|
||||
t.Fatal("configuration section endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("PUT", "/api/v1/config/email")
|
||||
if !ok || endpoint.Capability != CapabilityConfigWriteGlobal {
|
||||
t.Fatal("configuration update endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("GET", "/api/v1/config/backups")
|
||||
if !ok || endpoint.Capability != CapabilityConfigRollback {
|
||||
t.Fatal("configuration backup endpoint did not require rollback capability")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEveryManagementEndpointHasAllowAndDeny(t *testing.T) {
|
||||
for _, endpoint := range ManagementEndpoints() {
|
||||
actorRole := RoleSuperAdmin
|
||||
allowed := Request{
|
||||
ActorRole: actorRole,
|
||||
Capability: endpoint.Capability,
|
||||
TargetRole: RoleUser,
|
||||
OwnResource: true,
|
||||
RecentAuth: true,
|
||||
RecoveryCodeConfigured: true,
|
||||
TargetRecoveryConfigured: true,
|
||||
PluginInstanceAuthorized: true,
|
||||
}
|
||||
if err := Authorize(allowed); err != nil {
|
||||
t.Fatalf("expected allow for %s %s: %v", endpoint.Method, endpoint.Path, err)
|
||||
}
|
||||
denied := allowed
|
||||
denied.ActorRole = Role("unknown")
|
||||
if err := Authorize(denied); !IsDenied(err, CodeInvalidRole) {
|
||||
t.Fatalf("expected deny for %s %s, got %v", endpoint.Method, endpoint.Path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package access
|
||||
|
||||
import "errors"
|
||||
|
||||
type DenialCode string
|
||||
|
||||
const (
|
||||
CodeAccessDenied DenialCode = "ACCESS_DENIED"
|
||||
CodeInvalidRole DenialCode = "INVALID_ROLE"
|
||||
CodeUnknownCapability DenialCode = "UNKNOWN_CAPABILITY"
|
||||
CodeRecentAuthRequired DenialCode = "RECENT_AUTH_REQUIRED"
|
||||
CodeRecoveryCodeRequired DenialCode = "RECOVERY_CODE_REQUIRED"
|
||||
CodeTargetRequired DenialCode = "TARGET_REQUIRED"
|
||||
CodeTargetRoleDenied DenialCode = "TARGET_ROLE_DENIED"
|
||||
CodePluginScopeDenied DenialCode = "PLUGIN_SCOPE_DENIED"
|
||||
)
|
||||
|
||||
type Denial struct{ Code DenialCode }
|
||||
|
||||
func (e *Denial) Error() string { return string(e.Code) }
|
||||
|
||||
func IsDenied(err error, code DenialCode) bool {
|
||||
var denial *Denial
|
||||
return errors.As(err, &denial) && denial.Code == code
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
ActorRole Role
|
||||
Capability Capability
|
||||
TargetRole Role
|
||||
NewRole Role
|
||||
OwnResource bool
|
||||
RecentAuth bool
|
||||
RecoveryCodeConfigured bool
|
||||
TargetRecoveryConfigured bool
|
||||
PluginInstanceAuthorized bool
|
||||
AuditScopeAuthorized bool
|
||||
DeploymentChannel bool
|
||||
}
|
||||
|
||||
func Authorize(request Request) error {
|
||||
if !request.ActorRole.Valid() {
|
||||
return deny(CodeInvalidRole)
|
||||
}
|
||||
policy, ok := GetPolicy(request.Capability)
|
||||
if !ok {
|
||||
return deny(CodeUnknownCapability)
|
||||
}
|
||||
if _, ok := grants[request.ActorRole][request.Capability]; !ok {
|
||||
return deny(CodeAccessDenied)
|
||||
}
|
||||
if policy.RequiresTarget && !request.TargetRole.Valid() {
|
||||
return deny(CodeTargetRequired)
|
||||
}
|
||||
if policy.RequiresRecentAuth && !request.RecentAuth {
|
||||
return deny(CodeRecentAuthRequired)
|
||||
}
|
||||
if policy.RequiresRecoveryCode && request.ActorRole != RoleSuperAdmin && !request.RecoveryCodeConfigured && !request.DeploymentChannel {
|
||||
return deny(CodeRecoveryCodeRequired)
|
||||
}
|
||||
return authorizeResource(request)
|
||||
}
|
||||
|
||||
func authorizeResource(request Request) error {
|
||||
switch request.Capability {
|
||||
case CapabilityPasswordChangeOwn, CapabilityPluginWriteOwn:
|
||||
if !request.OwnResource {
|
||||
return deny(CodeAccessDenied)
|
||||
}
|
||||
case CapabilityUserRead:
|
||||
if request.ActorRole == RoleUser && !request.OwnResource {
|
||||
return deny(CodeAccessDenied)
|
||||
}
|
||||
case CapabilityAuditRead:
|
||||
if request.ActorRole == RoleUser && !request.OwnResource {
|
||||
return deny(CodeAccessDenied)
|
||||
}
|
||||
if request.ActorRole == RoleAdmin && !request.OwnResource && !request.AuditScopeAuthorized {
|
||||
return deny(CodeAccessDenied)
|
||||
}
|
||||
case CapabilityUserManage, CapabilityPasswordReset:
|
||||
if !canManageAccountTarget(request.ActorRole, request.TargetRole) {
|
||||
return deny(CodeTargetRoleDenied)
|
||||
}
|
||||
case CapabilityPluginWriteAnyUser:
|
||||
if !canManagePluginTarget(request.ActorRole, request.TargetRole) {
|
||||
return deny(CodeTargetRoleDenied)
|
||||
}
|
||||
case CapabilityRoleManage:
|
||||
if !canManageAccountTarget(request.ActorRole, request.TargetRole) || !request.NewRole.Valid() || request.NewRole == RoleSuperAdmin {
|
||||
return deny(CodeTargetRoleDenied)
|
||||
}
|
||||
if !request.TargetRecoveryConfigured {
|
||||
return deny(CodeRecoveryCodeRequired)
|
||||
}
|
||||
case CapabilityPluginRestart:
|
||||
if request.ActorRole == RoleAdmin && !request.PluginInstanceAuthorized {
|
||||
return deny(CodePluginScopeDenied)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func canManageAccountTarget(actor Role, target Role) bool {
|
||||
switch actor {
|
||||
case RoleSuperAdmin:
|
||||
return target == RoleAdmin || target == RoleUser
|
||||
case RoleAdmin:
|
||||
return target == RoleUser
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func canManagePluginTarget(actor Role, target Role) bool {
|
||||
if actor == RoleSuperAdmin {
|
||||
return target.Valid()
|
||||
}
|
||||
if actor == RoleAdmin {
|
||||
return target == RoleUser
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func deny(code DenialCode) error { return &Denial{Code: code} }
|
||||
@@ -0,0 +1,99 @@
|
||||
package access
|
||||
|
||||
type Capability string
|
||||
|
||||
const (
|
||||
CapabilityConfigRead Capability = "config.read"
|
||||
CapabilityConfigWriteGlobal Capability = "config.write_global"
|
||||
CapabilityConfigRollback Capability = "config.rollback"
|
||||
CapabilityDatabaseMigrate Capability = "database.migrate"
|
||||
CapabilityPluginRead Capability = "plugin.read"
|
||||
CapabilityPluginControl Capability = "plugin.control"
|
||||
CapabilityPluginRestart Capability = "plugin.restart"
|
||||
CapabilityServiceRestart Capability = "service.restart"
|
||||
CapabilityPluginWriteGlobal Capability = "plugin.write_global"
|
||||
CapabilityPluginWriteAnyUser Capability = "plugin.write_any_user"
|
||||
CapabilityPluginWriteOwn Capability = "plugin.write_own"
|
||||
CapabilityUserRead Capability = "user.read"
|
||||
CapabilityUserManage Capability = "user.manage"
|
||||
CapabilityRoleManage Capability = "role.manage"
|
||||
CapabilityPasswordChangeOwn Capability = "password.change_own"
|
||||
CapabilityPasswordReset Capability = "password.reset"
|
||||
CapabilityAuditRead Capability = "audit.read"
|
||||
CapabilityAuditExport Capability = "audit.export"
|
||||
)
|
||||
|
||||
type Policy struct {
|
||||
Capability Capability
|
||||
HighRisk bool
|
||||
RequiresRecentAuth bool
|
||||
RequiresRecoveryCode bool
|
||||
RequiresTarget bool
|
||||
}
|
||||
|
||||
var policies = map[Capability]Policy{
|
||||
CapabilityConfigRead: {Capability: CapabilityConfigRead},
|
||||
CapabilityConfigWriteGlobal: {Capability: CapabilityConfigWriteGlobal, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityConfigRollback: {Capability: CapabilityConfigRollback, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityDatabaseMigrate: {Capability: CapabilityDatabaseMigrate, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityPluginRead: {Capability: CapabilityPluginRead},
|
||||
CapabilityPluginControl: {Capability: CapabilityPluginControl, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityPluginRestart: {Capability: CapabilityPluginRestart, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityServiceRestart: {Capability: CapabilityServiceRestart, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityPluginWriteGlobal: {Capability: CapabilityPluginWriteGlobal, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true},
|
||||
CapabilityPluginWriteAnyUser: {Capability: CapabilityPluginWriteAnyUser, RequiresTarget: true},
|
||||
CapabilityPluginWriteOwn: {Capability: CapabilityPluginWriteOwn},
|
||||
CapabilityUserRead: {Capability: CapabilityUserRead},
|
||||
CapabilityUserManage: {Capability: CapabilityUserManage, RequiresTarget: true},
|
||||
CapabilityRoleManage: {Capability: CapabilityRoleManage, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true, RequiresTarget: true},
|
||||
CapabilityPasswordChangeOwn: {Capability: CapabilityPasswordChangeOwn},
|
||||
CapabilityPasswordReset: {Capability: CapabilityPasswordReset, HighRisk: true, RequiresRecentAuth: true, RequiresRecoveryCode: true, RequiresTarget: true},
|
||||
CapabilityAuditRead: {Capability: CapabilityAuditRead},
|
||||
CapabilityAuditExport: {Capability: CapabilityAuditExport, HighRisk: true, RequiresRecentAuth: true},
|
||||
}
|
||||
|
||||
var grants = map[Role]map[Capability]struct{}{
|
||||
RoleSuperAdmin: grant(
|
||||
CapabilityConfigRead, CapabilityConfigWriteGlobal, CapabilityConfigRollback,
|
||||
CapabilityDatabaseMigrate, CapabilityPluginRead, CapabilityPluginControl,
|
||||
CapabilityPluginRestart, CapabilityServiceRestart, CapabilityPluginWriteGlobal, CapabilityPluginWriteAnyUser,
|
||||
CapabilityPluginWriteOwn, CapabilityUserRead, CapabilityUserManage,
|
||||
CapabilityRoleManage, CapabilityPasswordChangeOwn, CapabilityPasswordReset,
|
||||
CapabilityAuditRead, CapabilityAuditExport,
|
||||
),
|
||||
RoleAdmin: grant(
|
||||
CapabilityConfigRead, CapabilityPluginRead, CapabilityPluginRestart,
|
||||
CapabilityPluginWriteAnyUser, CapabilityPluginWriteOwn, CapabilityUserRead,
|
||||
CapabilityUserManage, CapabilityPasswordChangeOwn, CapabilityPasswordReset,
|
||||
CapabilityAuditRead,
|
||||
),
|
||||
RoleUser: grant(
|
||||
CapabilityPluginRead, CapabilityPluginWriteOwn, CapabilityUserRead,
|
||||
CapabilityPasswordChangeOwn, CapabilityAuditRead,
|
||||
),
|
||||
}
|
||||
|
||||
func grant(capabilities ...Capability) map[Capability]struct{} {
|
||||
result := make(map[Capability]struct{}, len(capabilities))
|
||||
for _, capability := range capabilities {
|
||||
result[capability] = struct{}{}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func GetPolicy(capability Capability) (Policy, bool) {
|
||||
policy, ok := policies[capability]
|
||||
return policy, ok
|
||||
}
|
||||
|
||||
func Capabilities(role Role) []Capability {
|
||||
roleGrants, ok := grants[role]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
result := make([]Capability, 0, len(roleGrants))
|
||||
for capability := range roleGrants {
|
||||
result = append(result, capability)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package access
|
||||
|
||||
import "strings"
|
||||
|
||||
type Endpoint struct {
|
||||
Method string
|
||||
Path string
|
||||
Capability Capability
|
||||
}
|
||||
|
||||
var managementEndpoints = []Endpoint{
|
||||
{Method: "POST", Path: "/api/settings/modify_password", Capability: CapabilityPasswordChangeOwn},
|
||||
{Method: "POST", Path: "/api/user/create", Capability: CapabilityUserManage},
|
||||
{Method: "POST", Path: "/api/user/edit", Capability: CapabilityUserManage},
|
||||
{Method: "POST", Path: "/api/user/info", Capability: CapabilityUserRead},
|
||||
{Method: "POST", Path: "/api/user/list", Capability: CapabilityUserRead},
|
||||
{Method: "GET", Path: "/api/plugin/list", Capability: CapabilityPluginRead},
|
||||
{Method: "*", Path: "/api/plugin/settings/", Capability: CapabilityPluginWriteOwn},
|
||||
{Method: "GET", Path: "/api/v1/config/status", Capability: CapabilityConfigRead},
|
||||
{Method: "GET", Path: "/api/v1/config/backups", Capability: CapabilityConfigRollback},
|
||||
{Method: "POST", Path: "/api/v1/config/rollback/preview", Capability: CapabilityConfigRollback},
|
||||
{Method: "POST", Path: "/api/v1/config/rollback/ticket", Capability: CapabilityConfigRollback},
|
||||
{Method: "POST", Path: "/api/v1/config/rollback", Capability: CapabilityConfigRollback},
|
||||
{Method: "POST", Path: "/api/v1/config/:section/validate", Capability: CapabilityConfigWriteGlobal},
|
||||
{Method: "POST", Path: "/api/v1/config/:section/apply", Capability: CapabilityConfigWriteGlobal},
|
||||
{Method: "GET", Path: "/api/v1/config/:section", Capability: CapabilityConfigRead},
|
||||
{Method: "PUT", Path: "/api/v1/config/:section", Capability: CapabilityConfigWriteGlobal},
|
||||
{Method: "GET", Path: "/api/v1/audits/config", Capability: CapabilityAuditRead},
|
||||
{Method: "GET", Path: "/api/v1/audits/config/:id", Capability: CapabilityAuditRead},
|
||||
{Method: "POST", Path: "/api/v1/audits/config", Capability: CapabilityAuditExport},
|
||||
{Method: "POST", Path: "/api/v1/audits/config/export", Capability: CapabilityAuditExport},
|
||||
{Method: "POST", Path: "/api/v1/settings/modify_password", Capability: CapabilityPasswordChangeOwn},
|
||||
{Method: "POST", Path: "/api/v1/system/restart", Capability: CapabilityServiceRestart},
|
||||
{Method: "POST", Path: "/api/v1/system/restart/preview", Capability: CapabilityServiceRestart},
|
||||
{Method: "POST", Path: "/api/v1/database/migrations/preview", Capability: CapabilityDatabaseMigrate},
|
||||
{Method: "POST", Path: "/api/v1/database/migrations", Capability: CapabilityDatabaseMigrate},
|
||||
{Method: "GET", Path: "/api/v1/database/migrations/:id", Capability: CapabilityDatabaseMigrate},
|
||||
{Method: "POST", Path: "/api/v1/user/create", Capability: CapabilityUserManage},
|
||||
{Method: "POST", Path: "/api/v1/user/edit", Capability: CapabilityUserManage},
|
||||
{Method: "POST", Path: "/api/v1/user/info", Capability: CapabilityUserRead},
|
||||
{Method: "POST", Path: "/api/v1/user/list", Capability: CapabilityUserRead},
|
||||
{Method: "GET", Path: "/api/v1/plugin/list", Capability: CapabilityPluginRead},
|
||||
{Method: "*", Path: "/api/v1/plugin/settings/", Capability: CapabilityPluginWriteOwn},
|
||||
}
|
||||
|
||||
func ManagementEndpoints() []Endpoint {
|
||||
result := make([]Endpoint, len(managementEndpoints))
|
||||
copy(result, managementEndpoints)
|
||||
return result
|
||||
}
|
||||
|
||||
func LookupEndpoint(method string, path string) (Endpoint, bool) {
|
||||
method = strings.ToUpper(strings.TrimSpace(method))
|
||||
for _, endpoint := range managementEndpoints {
|
||||
methodMatches := endpoint.Method == "*" || endpoint.Method == method
|
||||
pathMatches := matchEndpointPath(endpoint.Path, path)
|
||||
if methodMatches && pathMatches {
|
||||
return endpoint, true
|
||||
}
|
||||
}
|
||||
return Endpoint{}, false
|
||||
}
|
||||
|
||||
func matchEndpointPath(pattern, path string) bool {
|
||||
if pattern == path {
|
||||
return true
|
||||
}
|
||||
if strings.HasSuffix(pattern, "/") {
|
||||
return strings.HasPrefix(path, pattern)
|
||||
}
|
||||
patternParts := strings.Split(strings.Trim(pattern, "/"), "/")
|
||||
pathParts := strings.Split(strings.Trim(path, "/"), "/")
|
||||
if len(patternParts) != len(pathParts) {
|
||||
return false
|
||||
}
|
||||
for index := range patternParts {
|
||||
if strings.HasPrefix(patternParts[index], ":") && patternParts[index] != ":" {
|
||||
if pathParts[index] == "" {
|
||||
return false
|
||||
}
|
||||
continue
|
||||
}
|
||||
if patternParts[index] != pathParts[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package access
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCapabilityMatrix(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
req Request
|
||||
allowed bool
|
||||
}{
|
||||
{name: "super admin reads config", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityConfigRead}, allowed: true},
|
||||
{name: "admin reads config", req: Request{ActorRole: RoleAdmin, Capability: CapabilityConfigRead}, allowed: true},
|
||||
{name: "user cannot read config", req: Request{ActorRole: RoleUser, Capability: CapabilityConfigRead}},
|
||||
{name: "super admin controls plugin", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPluginControl, RecentAuth: true, RecoveryCodeConfigured: true}, allowed: true},
|
||||
{name: "super admin controls plugin without recovery code", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPluginControl, RecentAuth: true}, allowed: true},
|
||||
{name: "admin cannot control plugin", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPluginControl, RecentAuth: true, RecoveryCodeConfigured: true}},
|
||||
{name: "super admin writes global plugin settings", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPluginWriteGlobal, RecentAuth: true, RecoveryCodeConfigured: true}, allowed: true},
|
||||
{name: "admin cannot write global plugin settings", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPluginWriteGlobal, RecentAuth: true, RecoveryCodeConfigured: true}},
|
||||
{name: "admin writes user plugin settings", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPluginWriteAnyUser, TargetRole: RoleUser}, allowed: true},
|
||||
{name: "admin cannot write admin plugin settings", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPluginWriteAnyUser, TargetRole: RoleAdmin}},
|
||||
{name: "super admin writes all user plugin settings", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPluginWriteAnyUser, TargetRole: RoleSuperAdmin}, allowed: true},
|
||||
{name: "user writes own plugin settings", req: Request{ActorRole: RoleUser, Capability: CapabilityPluginWriteOwn, OwnResource: true}, allowed: true},
|
||||
{name: "user cannot write another plugin settings", req: Request{ActorRole: RoleUser, Capability: CapabilityPluginWriteOwn}},
|
||||
{name: "authenticated super admin changes own password", req: Request{ActorRole: RoleSuperAdmin, Capability: CapabilityPasswordChangeOwn, OwnResource: true}, allowed: true},
|
||||
{name: "admin changes own password", req: Request{ActorRole: RoleAdmin, Capability: CapabilityPasswordChangeOwn, OwnResource: true}, allowed: true},
|
||||
{name: "user reads own audit", req: Request{ActorRole: RoleUser, Capability: CapabilityAuditRead, OwnResource: true}, allowed: true},
|
||||
{name: "user cannot read another audit", req: Request{ActorRole: RoleUser, Capability: CapabilityAuditRead}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := Authorize(test.req)
|
||||
if test.allowed && err != nil {
|
||||
t.Fatalf("expected allow, got %v", err)
|
||||
}
|
||||
if !test.allowed && err == nil {
|
||||
t.Fatal("expected denial")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package access
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleSuperAdmin Role = "super_admin"
|
||||
RoleAdmin Role = "admin"
|
||||
RoleUser Role = "user"
|
||||
)
|
||||
|
||||
func (r Role) Valid() bool {
|
||||
switch r {
|
||||
case RoleSuperAdmin, RoleAdmin, RoleUser:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user