mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package authorization
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/tasks/common/api"
|
|
)
|
|
|
|
type (
|
|
defaultAuthorizer struct {
|
|
}
|
|
)
|
|
|
|
var _ Authorizer = (*defaultAuthorizer)(nil)
|
|
|
|
// NewDefaultAuthorizer creates a default authorizer
|
|
func NewDefaultAuthorizer() Authorizer {
|
|
return &defaultAuthorizer{}
|
|
}
|
|
|
|
var resultAllow = Result{Decision: DecisionAllow}
|
|
var resultDeny = Result{Decision: DecisionDeny}
|
|
|
|
// Authorize determines if an API call by given claims should be allowed or denied.
|
|
// Rules:
|
|
//
|
|
// Health check APIs are allowed to everyone.
|
|
// System Admin is allowed to access all APIs on all namespaces and cluster-level.
|
|
// System Writer is allowed to access non admin APIs on all namespaces and cluster-level.
|
|
// System Reader is allowed to access readonly APIs on all namespaces and cluster-level.
|
|
// Namespace Admin is allowed to access all APIs on their namespaces.
|
|
// Namespace Writer is allowed to access non admin APIs on their namespaces.
|
|
// Namespace Reader is allowed to access non admin readonly APIs on their namespaces.
|
|
func (a *defaultAuthorizer) Authorize(_ context.Context, claims *Claims, target *CallTarget) (Result, error) {
|
|
// APIs that are essentially read-only health checks with no sensitive information are
|
|
// always allowed
|
|
if IsHealthCheckAPI(target.APIName) {
|
|
return resultAllow, nil
|
|
}
|
|
if claims == nil {
|
|
return resultDeny, nil
|
|
}
|
|
|
|
metadata := api.GetMethodMetadata(target.APIName)
|
|
|
|
var hasRole Role
|
|
switch metadata.Scope {
|
|
case api.ScopeCluster:
|
|
hasRole = claims.System
|
|
case api.ScopeNamespace:
|
|
// Note: system-level claims apply across all namespaces.
|
|
// Note: if claims.Namespace is nil or target.Namespace is not found, the lookup will return zero.
|
|
hasRole = claims.System | claims.Namespaces[target.Namespace]
|
|
default:
|
|
return resultDeny, nil
|
|
}
|
|
|
|
if hasRole >= getRequiredRole(metadata.Access) {
|
|
return resultAllow, nil
|
|
}
|
|
return resultDeny, nil
|
|
}
|
|
|
|
// Convert from api.Access to Role
|
|
func getRequiredRole(access api.Access) Role {
|
|
switch access {
|
|
case api.AccessReadOnly:
|
|
return RoleReader
|
|
case api.AccessWrite:
|
|
return RoleWriter
|
|
default:
|
|
return RoleAdmin
|
|
}
|
|
}
|