Documentation
¶
Overview ¶
Package repository reads and writes users, roles, groups and group memberships.
Index ¶
- Constants
- type CILogonUserProvisioner
- type DBRoleLookup
- type GroupMemberRepository
- func (r *GroupMemberRepository) Delete(ctx context.Context, m *model.GroupMember) error
- func (r *GroupMemberRepository) FindByGroupID(ctx context.Context, groupID string) ([]model.GroupMember, error)
- func (r *GroupMemberRepository) FindByGroupIDAndUserID(ctx context.Context, groupID, userID string) (*model.GroupMember, error)
- func (r *GroupMemberRepository) FindByUserID(ctx context.Context, userID string) ([]model.GroupMember, error)
- func (r *GroupMemberRepository) Save(ctx context.Context, m *model.GroupMember) error
- func (r *GroupMemberRepository) WithTx(tx *gorm.DB) *GroupMemberRepository
- type GroupRepository
- func (r *GroupRepository) Delete(ctx context.Context, g *model.Group) error
- func (r *GroupRepository) FindAll(ctx context.Context) ([]model.Group, error)
- func (r *GroupRepository) FindByID(ctx context.Context, id string) (*model.Group, error)
- func (r *GroupRepository) FindVisibleTo(ctx context.Context, userID string) ([]model.Group, error)
- func (r *GroupRepository) Save(ctx context.Context, g *model.Group) error
- func (r *GroupRepository) WithTx(tx *gorm.DB) *GroupRepository
- type PermissionResolver
- type ServiceAccountRepository
- func (r *ServiceAccountRepository) FindAll(ctx context.Context) ([]model.User, error)
- func (r *ServiceAccountRepository) FindByID(ctx context.Context, id string) (*model.User, error)
- func (r *ServiceAccountRepository) ReplacePermissions(ctx context.Context, id string, want []permission.Permission) error
- func (r *ServiceAccountRepository) Save(ctx context.Context, sa *model.ServiceAccount) error
- func (r *ServiceAccountRepository) WithTx(tx *gorm.DB) *ServiceAccountRepository
- type ServiceAccountTokenLookup
- type UserRepository
- func (r *UserRepository) AddRole(ctx context.Context, userID string, role model.Role) error
- func (r *UserRepository) FindAll(ctx context.Context) ([]model.User, error)
- func (r *UserRepository) FindByID(ctx context.Context, id string) (*model.User, error)
- func (r *UserRepository) FindByIDWithRoles(ctx context.Context, id string) (*model.User, error)
- func (r *UserRepository) RemoveRole(ctx context.Context, userID string, role model.Role) error
- func (r *UserRepository) Save(ctx context.Context, u *model.User) error
- func (r *UserRepository) WithTx(tx *gorm.DB) *UserRepository
Constants ¶
const DefaultPermissionTTL = time.Minute
DefaultPermissionTTL is how long a role's permissions are remembered. It bounds how stale a grant can be on a server that did not itself make the change.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CILogonUserProvisioner ¶
CILogonUserProvisioner resolves a CILogon principal to its users row, creating the row the first time the principal authenticates. A new row is SUSPENDED, with the profile filled from the CILogon claims, until an administrator activates it. An existing row is never changed here.
It runs on every CILogon request, so a status change takes effect on the user's next request.
func (*CILogonUserProvisioner) ProvisionUser ¶
func (p *CILogonUserProvisioner) ProvisionUser(ctx context.Context, principal *auth.Principal) (string, error)
ProvisionUser implements auth.UserProvisioner.
type DBRoleLookup ¶
DBRoleLookup resolves a caller's authorities from the user_roles table.
It is the intended replacement for auth.MockRoleLookup: the schema already carries the rows, they just are not administered through any endpoint yet.
func (DBRoleLookup) Roles ¶
func (l DBRoleLookup) Roles(ctx context.Context, username string) []string
Roles implements auth.RoleLookup.
A caller with no rows — including one who was never registered — gets USER. Failing closed to the least privilege matters more here than reporting the error, since the alternative on a transient database fault would be to deny every request; USER is the same answer the mock gives an unknown caller.
type GroupMemberRepository ¶
type GroupMemberRepository struct {
// contains filtered or unexported fields
}
GroupMemberRepository reads and writes group memberships.
func NewGroupMemberRepository ¶
func NewGroupMemberRepository(db *gorm.DB) *GroupMemberRepository
NewGroupMemberRepository returns a repository backed by db.
func (*GroupMemberRepository) Delete ¶
func (r *GroupMemberRepository) Delete(ctx context.Context, m *model.GroupMember) error
Delete removes a membership.
The where clause is spelled out because a composite primary key carried on the struct is the only thing that scopes the delete, and an empty field would otherwise widen it to every row that matches the rest.
func (*GroupMemberRepository) FindByGroupID ¶
func (r *GroupMemberRepository) FindByGroupID(ctx context.Context, groupID string) ([]model.GroupMember, error)
FindByGroupID returns every membership of one group.
func (*GroupMemberRepository) FindByGroupIDAndUserID ¶
func (r *GroupMemberRepository) FindByGroupIDAndUserID(ctx context.Context, groupID, userID string) (*model.GroupMember, error)
FindByGroupIDAndUserID returns one membership, or gorm.ErrRecordNotFound.
func (*GroupMemberRepository) FindByUserID ¶
func (r *GroupMemberRepository) FindByUserID(ctx context.Context, userID string) ([]model.GroupMember, error)
FindByUserID returns every membership held by one user.
func (*GroupMemberRepository) Save ¶
func (r *GroupMemberRepository) Save(ctx context.Context, m *model.GroupMember) error
Save inserts or updates a membership.
This is an explicit upsert rather than a plain Save, which decides between an insert and an update by running the update first and inserting when it reports no rows affected. That indirection is worth avoiding for a composite key: the whole key is always known here, so stating the conflict is both cheaper and exact — one statement that cannot be tripped up by a dialect's idea of how many rows an unchanged update affected.
func (*GroupMemberRepository) WithTx ¶
func (r *GroupMemberRepository) WithTx(tx *gorm.DB) *GroupMemberRepository
WithTx returns a repository bound to tx.
type GroupRepository ¶
type GroupRepository struct {
// contains filtered or unexported fields
}
GroupRepository reads and writes groups.
func NewGroupRepository ¶
func NewGroupRepository(db *gorm.DB) *GroupRepository
NewGroupRepository returns a repository backed by db.
func (*GroupRepository) Delete ¶
Delete removes a group; its membership rows go with it via the cascading constraint.
func (*GroupRepository) FindVisibleTo ¶
FindVisibleTo returns the groups userID owns or holds a membership in.
Inactive memberships count here: a suspended member can still see that the group exists, which is what makes "why can I no longer read anything shared with it?" answerable. What an inactive membership does not grant is reading through the group — that check lives in the service.
func (*GroupRepository) WithTx ¶
func (r *GroupRepository) WithTx(tx *gorm.DB) *GroupRepository
WithTx returns a repository bound to tx.
type PermissionResolver ¶
type PermissionResolver struct {
// contains filtered or unexported fields
}
PermissionResolver resolves a caller to the permissions they hold.
A person holds what their roles grant, read from the role_permissions table. The cache for that is keyed by role rather than by user: there are few roles and many users, so it stays small however many people are signed in, and a change to a role is one Invalidate rather than a hunt for every user holding it.
A service account holds what has been granted to that account in service_account_permissions, and nothing from any role. Those are cached per account, which is bounded by the number of service accounts.
A change made through this process calls Invalidate or InvalidateUser and takes effect at once. A change made by another server instance, or directly in the database, is picked up when the cached entry expires.
func NewPermissionResolver ¶
func NewPermissionResolver(db *gorm.DB, ttl time.Duration) *PermissionResolver
NewPermissionResolver returns a resolver over db. A ttl of zero or less means DefaultPermissionTTL.
func (*PermissionResolver) Invalidate ¶
func (r *PermissionResolver) Invalidate()
Invalidate forgets everything cached, so the next request reads the tables again. Call it after changing role_permissions.
func (*PermissionResolver) InvalidateUser ¶
func (r *PermissionResolver) InvalidateUser(id string)
InvalidateUser forgets one service account's permissions. Call it after changing or deleting that account's grants.
func (*PermissionResolver) Permissions ¶
func (r *PermissionResolver) Permissions(ctx context.Context, name string, authorities []string) permission.Set
Permissions implements auth.PermissionResolver.
For a service account (told apart by its reserved id prefix, which no identity provider can produce) it is the account's own grants. For anyone else it is the union of what every authority grants; an authority that is not a recognised role grants nothing.
SUPER_ADMIN holds every permission without a lookup, so the bootstrap account keeps working whatever state the table is in.
type ServiceAccountRepository ¶
type ServiceAccountRepository struct {
// contains filtered or unexported fields
}
ServiceAccountRepository reads and writes service account token rows.
func NewServiceAccountRepository ¶
func NewServiceAccountRepository(db *gorm.DB) *ServiceAccountRepository
NewServiceAccountRepository returns a repository backed by db.
func (*ServiceAccountRepository) FindByID ¶
FindByID returns one service account with its user row, or gorm.ErrRecordNotFound when id names no service account — including when it names a person.
func (*ServiceAccountRepository) ReplacePermissions ¶
func (r *ServiceAccountRepository) ReplacePermissions(ctx context.Context, id string, want []permission.Permission) error
ReplacePermissions makes want the account's complete set of grants: rows not in want are deleted and missing ones inserted, and rows already present are left alone, so an unchanged permission leaves no trace in the audit log.
func (*ServiceAccountRepository) Save ¶
func (r *ServiceAccountRepository) Save(ctx context.Context, sa *model.ServiceAccount) error
Save inserts or updates a service account row. Its permissions are written only through ReplacePermissions, never as a side effect of saving the account.
func (*ServiceAccountRepository) WithTx ¶
func (r *ServiceAccountRepository) WithTx(tx *gorm.DB) *ServiceAccountRepository
WithTx returns a repository bound to tx.
type ServiceAccountTokenLookup ¶
type ServiceAccountTokenLookup struct {
DB *gorm.DB
// Now is the clock, replaceable in tests. Nil means time.Now.
Now func() time.Time
}
ServiceAccountTokenLookup resolves service account tokens against the database. It implements auth.ServiceAccountResolver.
func (ServiceAccountTokenLookup) ResolveServiceAccount ¶
func (l ServiceAccountTokenLookup) ResolveServiceAccount(ctx context.Context, token string) (*auth.Principal, error)
ResolveServiceAccount implements auth.ServiceAccountResolver.
A token is accepted only if its digest matches an account's current token, the token has not expired, and the account's user row is ACTIVE. Every one of those failures is the same ErrInvalidToken, so a caller cannot tell a revoked token from one that never existed.
The principal's authorities are fixed to USER by auth.ServiceAccountPrincipal; user_roles is deliberately not consulted.
type UserRepository ¶
type UserRepository struct {
// contains filtered or unexported fields
}
UserRepository reads and writes user records.
func NewUserRepository ¶
func NewUserRepository(db *gorm.DB) *UserRepository
NewUserRepository returns a repository backed by db.
func (*UserRepository) AddRole ¶
AddRole grants role to the user. Granting a role already held is a no-op.
func (*UserRepository) FindByIDWithRoles ¶
FindByIDWithRoles returns one user with their roles, or gorm.ErrRecordNotFound.
func (*UserRepository) RemoveRole ¶
RemoveRole withdraws role from the user. Withdrawing a role not held is a no-op.
func (*UserRepository) WithTx ¶
func (r *UserRepository) WithTx(tx *gorm.DB) *UserRepository
WithTx returns a repository bound to tx.