Files
threshold/internal/iam/role_grant.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.4 KiB
Go
Raw Normal View History

2020-07-10 15:46:26 -04:00
// Copyright (c) HashiCorp, Inc.
2020-08-07 12:55:02 -04:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-07-10 15:46:26 -04:00
package iam
import (
"context"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/db"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/iam/store"
"github.com/hashicorp/boundary/internal/perms"
2020-07-10 15:46:26 -04:00
"google.golang.org/protobuf/proto"
)
const defaultRoleGrantTable = "iam_role_grant"
// RoleGrant defines the grants that are assigned to a role
type RoleGrant struct {
*store.RoleGrant
tableName string `gorm:"-"`
}
2020-08-07 12:55:02 -04:00
// ensure that RoleGrant implements the interfaces of: Cloneable and db.VetForWriter
var (
_ Cloneable = (*RoleGrant)(nil)
_ db.VetForWriter = (*RoleGrant)(nil)
)
2020-07-10 15:46:26 -04:00
// NewRoleGrant creates a new in memory role grant
2023-06-06 16:13:44 -04:00
func NewRoleGrant(ctx context.Context, roleId string, grant string, _ ...Option) (*RoleGrant, error) {
2020-12-23 11:00:00 -08:00
const op = "iam.NewRoleGrant"
2020-07-10 15:46:26 -04:00
if roleId == "" {
2023-06-06 16:13:44 -04:00
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing role id")
2020-07-10 15:46:26 -04:00
}
if grant == "" {
2023-06-06 16:13:44 -04:00
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing grant")
2020-07-10 15:46:26 -04:00
}
// Validate that the grant parses successfully. Note that we fake the scope
// here to avoid a lookup as the scope is only relevant at actual ACL
// checking time and we just care that it parses correctly.
2024-09-17 10:07:43 -04:00
perm, err := perms.Parse(ctx, perms.GrantTuple{RoleScopeId: "o_abcd1234", GrantScopeId: "o_abcd1234", Grant: grant})
2020-07-10 15:46:26 -04:00
if err != nil {
2023-06-06 16:13:44 -04:00
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("parsing grant string"))
2020-07-10 15:46:26 -04:00
}
rg := &RoleGrant{
RoleGrant: &store.RoleGrant{
RoleId: roleId,
RawGrant: grant,
CanonicalGrant: perm.CanonicalString(),
},
}
return rg, nil
}
func allocRoleGrant() RoleGrant {
return RoleGrant{
RoleGrant: &store.RoleGrant{},
}
}
// Clone creates a clone of the RoleGrant
2022-10-25 18:29:38 -05:00
func (g *RoleGrant) Clone() any {
2020-07-10 15:46:26 -04:00
cp := proto.Clone(g.RoleGrant)
return &RoleGrant{
RoleGrant: cp.(*store.RoleGrant),
}
}
// VetForWrite implements db.VetForWrite() interface
func (g *RoleGrant) VetForWrite(ctx context.Context, _ db.Reader, _ db.OpType, _ ...db.Option) error {
2020-12-23 11:00:00 -08:00
const op = "iam.(RoleGrant).VetForWrite"
2020-07-10 15:46:26 -04:00
if g.RawGrant == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing grant")
2020-07-10 15:46:26 -04:00
}
// Validate that the grant parses successfully. Note that we fake the scope
// here to avoid a lookup as the scope is only relevant at actual ACL
// checking time and we just care that it parses correctly. We may have
// already done this in NewRoleGrant, but we re-check and set it here
// anyways because it should still be part of the vetting process.
2024-09-17 10:07:43 -04:00
perm, err := perms.Parse(ctx, perms.GrantTuple{RoleScopeId: "o_abcd1234", GrantScopeId: "o_abcd1234", Grant: g.RawGrant})
2020-07-10 15:46:26 -04:00
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("parsing grant string"))
2020-07-10 15:46:26 -04:00
}
canonical := perm.CanonicalString()
if g.CanonicalGrant != "" && g.CanonicalGrant != canonical {
return errors.Wrap(ctx, err, op, errors.WithMsg("existing canonical grant and derived one do not match"))
2020-07-10 15:46:26 -04:00
}
g.CanonicalGrant = canonical
return nil
}
// TableName returns the tablename to override the default gorm table name
func (g *RoleGrant) TableName() string {
if g.tableName != "" {
return g.tableName
}
return defaultRoleGrantTable
}
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
2020-07-10 15:46:26 -04:00
func (g *RoleGrant) SetTableName(n string) {
2020-07-23 17:58:41 -04:00
g.tableName = n
2020-07-10 15:46:26 -04:00
}