mirror of
https://github.com/kopia/kopia.git
synced 2026-01-27 07:48:06 -05:00
* nit: replaced harcoded string constants with named constants * acl: added management of ACL entries * auth: implemented DefaultAuthorizer which uses ACLs if any entries are found in the system and falls back to LegacyAuthorizer if not * cli: switch to DefaultAuthorizer when starting server * cli: added ACL management * server: refactored authenticator + added refresh Authenticator is now an interface which also supports Refresh. * authz: refactored authorizer to be an interface + added Refresh() * server: refresh authentication and authorizer * e2e tests for ACLs * server: handling of SIGHUP to refresh authn/authz caches * server: reorganized flags to specify auth options: - removed '--allow-repository-users' - it's always on - one of --without-password, --server-password or --random-password can be specified to specify password for the UI user - htpasswd-file - can be specified to provide password for UI or remote users * cli: moved 'kopia user' to 'kopia server user' * server: allow all UI actions if no authenticator is set * acl: removed priority until we have a better understood use case for it * acl: added validation of allowed labels when adding ACL entries * site: added docs for ACLs
95 lines
3.0 KiB
Go
95 lines
3.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/kopia/kopia/internal/acl"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/manifest"
|
|
"github.com/kopia/kopia/snapshot"
|
|
"github.com/kopia/kopia/snapshot/policy"
|
|
)
|
|
|
|
// Authorizer gets authorization info for logged in user.
|
|
type Authorizer interface {
|
|
Authorize(ctx context.Context, rep repo.Repository, username string) AuthorizationInfo
|
|
Refresh(ctx context.Context) error
|
|
}
|
|
|
|
// AccessLevel specifies access level when accessing repository objects.
|
|
type AccessLevel = acl.AccessLevel
|
|
|
|
// Access levels forwarded to 'acl' package to allow it to easily implement AuthorizationInfo interface.
|
|
const (
|
|
AccessLevelNone = acl.AccessLevelNone
|
|
AccessLevelRead = acl.AccessLevelRead // RO access
|
|
AccessLevelAppend = acl.AccessLevelAppend // RO + create new
|
|
AccessLevelFull = acl.AccessLevelFull // read/write/delete
|
|
)
|
|
|
|
// AuthorizationInfo determines logged in user's access level.
|
|
type AuthorizationInfo interface {
|
|
// ContentAccessLevel determines whether the user can read/write contents.
|
|
ContentAccessLevel() AccessLevel
|
|
|
|
// ManifestAccessLevel determines whether the user has access to a manifest with given labels.
|
|
ManifestAccessLevel(labels map[string]string) AccessLevel
|
|
}
|
|
|
|
type noAccessAuthorizationInfo struct{}
|
|
|
|
func (noAccessAuthorizationInfo) ContentAccessLevel() AccessLevel { return AccessLevelNone }
|
|
func (noAccessAuthorizationInfo) ManifestAccessLevel(labels map[string]string) AccessLevel {
|
|
return AccessLevelNone
|
|
}
|
|
|
|
// NoAccess returns AuthorizationInfo which grants no permissions.
|
|
func NoAccess() AuthorizationInfo {
|
|
return noAccessAuthorizationInfo{}
|
|
}
|
|
|
|
type legacyAuthorizationInfo struct {
|
|
usernameAtHostname string
|
|
}
|
|
|
|
func (la legacyAuthorizationInfo) ContentAccessLevel() AccessLevel { return AccessLevelFull }
|
|
func (la legacyAuthorizationInfo) ManifestAccessLevel(labels map[string]string) AccessLevel {
|
|
if labels[manifest.TypeLabelKey] == policy.ManifestType {
|
|
// everybody can read global policy.
|
|
switch labels[policy.PolicyTypeLabel] {
|
|
case policy.PolicyTypeGlobal:
|
|
return AccessLevelRead
|
|
|
|
case policy.PolicyTypeHost:
|
|
if strings.HasSuffix(la.usernameAtHostname, "@"+labels[snapshot.HostnameLabel]) {
|
|
return AccessLevelRead
|
|
}
|
|
}
|
|
}
|
|
|
|
// full access to policies/snapshots for the username@hostname
|
|
if labels[snapshot.UsernameLabel]+"@"+labels[snapshot.HostnameLabel] == la.usernameAtHostname {
|
|
return AccessLevelFull
|
|
}
|
|
|
|
// no access otherwise
|
|
return AccessLevelNone
|
|
}
|
|
|
|
type legacyAuthorizer struct{}
|
|
|
|
func (legacyAuthorizer) Authorize(ctx context.Context, rep repo.Repository, username string) AuthorizationInfo {
|
|
return legacyAuthorizationInfo{usernameAtHostname: username}
|
|
}
|
|
|
|
func (legacyAuthorizer) Refresh(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// LegacyAuthorizer is an Authorizer that returns authorizer with legacy (pre-ACL)
|
|
// authorization rules (authenticated users can see their own snapshots/policies only).
|
|
func LegacyAuthorizer() Authorizer {
|
|
return legacyAuthorizer{}
|
|
}
|