Files
threshold/internal/iam/testing.go
T

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

391 lines
11 KiB
Go
Raw Normal View History

2020-08-19 16:47:18 -04:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-05-27 18:16:43 -04:00
package iam
import (
"context"
2020-08-19 16:47:18 -04:00
"crypto/rand"
2020-05-27 18:16:43 -04:00
"testing"
2024-01-23 14:33:09 -05:00
"github.com/hashicorp/boundary/globals"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/auth/store"
"github.com/hashicorp/boundary/internal/db"
dbassert "github.com/hashicorp/boundary/internal/db/assert"
2020-08-19 16:47:18 -04:00
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/types/scope"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
2020-05-31 19:55:56 -04:00
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
2020-05-27 18:16:43 -04:00
)
2020-08-19 16:47:18 -04:00
// TestRepo creates a repo that can be used for various purposes. Crucially, it
// ensures that the global scope contains a valid root key.
func TestRepo(t testing.TB, conn *db.DB, rootWrapper wrapping.Wrapper, opt ...Option) *Repository {
t.Helper()
ctx := context.Background()
2020-08-19 16:47:18 -04:00
require := require.New(t)
rw := db.New(conn)
kmsCache := kms.TestKms(t, conn, rootWrapper)
wrapper, err := kmsCache.GetWrapper(ctx, scope.Global.String(), kms.KeyPurposeOplog)
2020-08-19 16:47:18 -04:00
if err != nil {
err = kmsCache.CreateKeys(ctx, scope.Global.String(), kms.WithRandomReader(rand.Reader))
2020-08-19 16:47:18 -04:00
require.NoError(err)
wrapper, err = kmsCache.GetWrapper(ctx, scope.Global.String(), kms.KeyPurposeOplog)
2020-08-19 16:47:18 -04:00
if err != nil {
panic(err)
}
}
require.NoError(err)
require.NotNil(wrapper)
repo, err := NewRepository(ctx, rw, rw, kmsCache, opt...)
2020-08-19 16:47:18 -04:00
require.NoError(err)
return repo
}
2021-04-08 20:54:32 -04:00
// TestSetPrimaryAuthMethod will set the PrimaryAuthMethodId for a scope.
func TestSetPrimaryAuthMethod(t testing.TB, repo *Repository, s *Scope, authMethodId string) {
2021-04-08 20:54:32 -04:00
t.Helper()
require := require.New(t)
require.NotEmpty(s)
require.NotEmpty(authMethodId)
s.PrimaryAuthMethodId = authMethodId
_, _, err := repo.UpdateScope(context.Background(), s, s.Version, []string{"PrimaryAuthMethodId"})
require.NoError(err)
updated, err := repo.LookupScope(context.Background(), s.PublicId)
require.NoError(err)
require.Equalf(authMethodId, updated.PrimaryAuthMethodId, "expected %s to be the primary auth method for scope: %s", authMethodId, updated.PublicId)
2021-04-08 20:54:32 -04:00
}
2020-07-11 12:00:49 -04:00
// TestScopes creates an org and project suitable for testing.
func TestScopes(t testing.TB, repo *Repository, opt ...Option) (org *Scope, prj *Scope) {
2020-05-27 18:16:43 -04:00
t.Helper()
ctx := context.Background()
require := require.New(t)
2020-05-27 18:16:43 -04:00
opts := getOpts(opt...)
org, err := NewOrg(ctx, opt...)
require.NoError(err)
org, err = repo.CreateScope(ctx, org, opts.withUserId, opt...)
require.NoError(err)
require.NotNil(org)
require.NotEmpty(org.GetPublicId())
2020-05-27 18:16:43 -04:00
prj, err = NewProject(ctx, org.GetPublicId(), opt...)
require.NoError(err)
prj, err = repo.CreateScope(ctx, prj, opts.withUserId, opt...)
require.NoError(err)
require.NotNil(prj)
require.NotEmpty(prj.GetPublicId())
2020-05-27 18:16:43 -04:00
return
}
2020-05-31 19:55:56 -04:00
func TestOrg(t testing.TB, repo *Repository, opt ...Option) *Scope {
2020-08-17 15:38:41 -04:00
t.Helper()
ctx := context.Background()
2020-08-17 15:38:41 -04:00
require := require.New(t)
opts := getOpts(opt...)
org, err := NewOrg(ctx, opt...)
2020-08-17 15:38:41 -04:00
require.NoError(err)
org, err = repo.CreateScope(ctx, org, opts.withUserId, opt...)
2020-08-17 15:38:41 -04:00
require.NoError(err)
require.NotNil(org)
require.NotEmpty(org.GetPublicId())
return org
}
func TestProject(t testing.TB, repo *Repository, orgId string, opt ...Option) *Scope {
t.Helper()
ctx := context.Background()
require := require.New(t)
opts := getOpts(opt...)
proj, err := NewProject(ctx, orgId, opt...)
require.NoError(err)
proj, err = repo.CreateScope(ctx, proj, opts.withUserId, opt...)
require.NoError(err)
require.NotNil(proj)
require.NotEmpty(proj.GetPublicId())
return proj
2020-08-17 15:38:41 -04:00
}
func testOrg(t testing.TB, repo *Repository, name, description string) (org *Scope) {
2020-05-31 19:55:56 -04:00
t.Helper()
ctx := context.Background()
require := require.New(t)
2020-05-31 19:55:56 -04:00
o, err := NewOrg(ctx, WithDescription(description), WithName(name))
require.NoError(err)
o, err = repo.CreateScope(ctx, o, "")
require.NoError(err)
require.NotNil(o)
require.NotEmpty(o.GetPublicId())
2020-08-19 16:47:18 -04:00
2020-05-31 19:55:56 -04:00
return o
}
func testProject(t testing.TB, repo *Repository, orgId string, opt ...Option) *Scope {
2020-06-18 14:53:11 -04:00
t.Helper()
ctx := context.Background()
2020-06-18 14:53:11 -04:00
require := require.New(t)
p, err := NewProject(ctx, orgId, opt...)
2020-06-18 14:53:11 -04:00
require.NoError(err)
p, err = repo.CreateScope(ctx, p, "")
2020-06-18 14:53:11 -04:00
require.NoError(err)
require.NotNil(p)
require.NotEmpty(p.GetPublicId())
2020-08-19 16:47:18 -04:00
2020-06-18 14:53:11 -04:00
return p
}
func testId(t testing.TB) string {
2020-05-31 19:55:56 -04:00
t.Helper()
id, err := uuid.GenerateUUID()
require.NoError(t, err)
2020-05-31 19:55:56 -04:00
return id
}
func testPublicId(t testing.TB, prefix string) string {
2020-05-31 19:55:56 -04:00
t.Helper()
publicId, err := db.NewPublicId(context.Background(), prefix)
require.NoError(t, err)
2020-05-31 19:55:56 -04:00
return publicId
}
2020-06-04 20:05:08 -04:00
2021-04-08 20:54:32 -04:00
// TestUser creates a user suitable for testing. Supports the options:
// WithName, WithDescription and WithAccountIds.
func TestUser(t testing.TB, repo *Repository, scopeId string, opt ...Option) *User {
2020-06-04 20:05:08 -04:00
t.Helper()
ctx := context.Background()
2020-08-19 16:47:18 -04:00
require := require.New(t)
2020-06-04 20:05:08 -04:00
user, err := NewUser(ctx, scopeId, opt...)
require.NoError(err)
user, err = repo.CreateUser(ctx, user)
require.NoError(err)
require.NotEmpty(user.PublicId)
2021-04-08 20:54:32 -04:00
opts := getOpts(opt...)
if len(opts.withAccountIds) > 0 {
_, err := repo.AddUserAccounts(ctx, user.PublicId, user.Version, opts.withAccountIds)
2021-04-08 20:54:32 -04:00
require.NoError(err)
2023-12-20 16:51:57 -08:00
// now that we have updated user accounts, we need to re-fetch the user
// to get the updated version and update time
user, err = repo.lookupUser(ctx, user.GetPublicId())
require.NoError(err)
2021-04-08 20:54:32 -04:00
}
2020-06-04 20:05:08 -04:00
return user
}
2020-06-11 16:36:47 -04:00
2024-01-23 14:33:09 -05:00
// TestRole creates a role suitable for testing. It will use a default grant
// scope ID unless WithGrantScopeId is used. To prevent a grant scope from being
// created, pass in a grant scope ID option with the value "testing-none".
func TestRole(t testing.TB, conn *db.DB, scopeId string, opt ...Option) *Role {
2020-06-15 12:42:08 -04:00
t.Helper()
2024-01-23 14:33:09 -05:00
opts := getOpts(opt...)
ctx := context.Background()
2020-06-15 12:42:08 -04:00
require := require.New(t)
rw := db.New(conn)
role, err := NewRole(ctx, scopeId, opt...)
2020-06-15 12:42:08 -04:00
require.NoError(err)
id, err := newRoleId(ctx)
2020-06-15 12:42:08 -04:00
require.NoError(err)
role.PublicId = id
2024-01-23 14:33:09 -05:00
require.NoError(rw.Create(ctx, role))
2020-06-15 12:42:08 -04:00
require.NotEmpty(role.PublicId)
2024-01-23 14:33:09 -05:00
grantScopeIds := opts.withGrantScopeIds
if len(grantScopeIds) == 0 {
grantScopeIds = []string{globals.GrantScopeThis}
2024-01-23 14:33:09 -05:00
}
for _, gsi := range grantScopeIds {
if gsi == "testing-none" {
continue
}
gs, err := NewRoleGrantScope(ctx, id, gsi)
require.NoError(err)
require.NoError(rw.Create(ctx, gs))
2024-06-25 13:05:52 -04:00
role.GrantScopes = append(role.GrantScopes, gs)
2024-01-23 14:33:09 -05:00
}
2020-06-15 12:42:08 -04:00
require.Equal(opts.withDescription, role.Description)
require.Equal(opts.withName, role.Name)
return role
}
func TestRoleGrant(t testing.TB, conn *db.DB, roleId, grant string, opt ...Option) *RoleGrant {
2020-07-10 15:46:26 -04:00
t.Helper()
require := require.New(t)
rw := db.New(conn)
2023-06-06 16:13:44 -04:00
g, err := NewRoleGrant(context.Background(), roleId, grant, opt...)
2020-07-10 15:46:26 -04:00
require.NoError(err)
err = rw.Create(context.Background(), g)
require.NoError(err)
return g
}
2024-01-23 14:33:09 -05:00
func TestRoleGrantScope(t testing.TB, conn *db.DB, roleId, grantScopeId string, opt ...Option) *RoleGrantScope {
t.Helper()
require := require.New(t)
rw := db.New(conn)
gs, err := NewRoleGrantScope(context.Background(), roleId, grantScopeId, opt...)
require.NoError(err)
require.NoError(rw.Create(context.Background(), gs))
return gs
}
2020-06-11 16:36:47 -04:00
// TestGroup creates a group suitable for testing.
func TestGroup(t testing.TB, conn *db.DB, scopeId string, opt ...Option) *Group {
2020-06-11 16:36:47 -04:00
t.Helper()
ctx := context.Background()
2020-06-11 16:36:47 -04:00
require := require.New(t)
rw := db.New(conn)
grp, err := NewGroup(ctx, scopeId, opt...)
2020-06-11 16:36:47 -04:00
require.NoError(err)
id, err := newGroupId(ctx)
2020-06-11 16:36:47 -04:00
require.NoError(err)
grp.PublicId = id
err = rw.Create(ctx, grp)
2020-06-11 16:36:47 -04:00
require.NoError(err)
require.NotEmpty(grp.PublicId)
return grp
}
2020-06-29 08:18:57 -04:00
func TestGroupMember(t testing.TB, conn *db.DB, groupId, userId string, opt ...Option) *GroupMemberUser {
2020-07-10 19:01:23 -04:00
t.Helper()
ctx := context.Background()
2020-07-10 19:01:23 -04:00
require := require.New(t)
rw := db.New(conn)
gm, err := NewGroupMemberUser(ctx, groupId, userId)
2020-07-10 19:01:23 -04:00
require.NoError(err)
require.NotNil(gm)
err = rw.Create(ctx, gm)
2020-07-10 19:01:23 -04:00
require.NoError(err)
require.NotEmpty(gm.CreateTime)
return gm
}
func TestUserRole(t testing.TB, conn *db.DB, roleId, userId string, opt ...Option) *UserRole {
t.Helper()
ctx := context.Background()
require := require.New(t)
rw := db.New(conn)
r, err := NewUserRole(ctx, roleId, userId, opt...)
require.NoError(err)
err = rw.Create(ctx, r)
require.NoError(err)
2020-07-10 15:46:26 -04:00
return r
}
func TestGroupRole(t testing.TB, conn *db.DB, roleId, grpId string, opt ...Option) *GroupRole {
t.Helper()
require := require.New(t)
rw := db.New(conn)
r, err := NewGroupRole(context.Background(), roleId, grpId, opt...)
require.NoError(err)
err = rw.Create(context.Background(), r)
require.NoError(err)
2020-07-10 15:46:26 -04:00
return r
}
func TestManagedGroupRole(t testing.TB, conn *db.DB, roleId, managedGrpId string, opt ...Option) *ManagedGroupRole {
t.Helper()
require := require.New(t)
rw := db.New(conn)
r, err := NewManagedGroupRole(context.Background(), roleId, managedGrpId, opt...)
require.NoError(err)
err = rw.Create(context.Background(), r)
require.NoError(err)
return r
}
// testAccount is a temporary test function. TODO - replace with an auth
// subsystem testAccount function. If userId is zero value, then an auth
2020-06-29 08:18:57 -04:00
// account will be created with a null IamUserId
func testAccount(t testing.TB, conn *db.DB, scopeId, authMethodId, userId string) *authAccount {
2020-06-29 08:18:57 -04:00
const (
accountPrefix = "aa_"
2020-06-29 08:18:57 -04:00
)
t.Helper()
ctx := context.Background()
2020-06-29 08:18:57 -04:00
rw := db.New(conn)
require := require.New(t)
require.NotNil(conn)
require.NotEmpty(scopeId)
require.NotEmpty(authMethodId)
if userId != "" {
foundUser := AllocUser()
2020-06-29 08:18:57 -04:00
foundUser.PublicId = userId
err := rw.LookupByPublicId(context.Background(), &foundUser)
require.NoError(err)
require.Equal(scopeId, foundUser.ScopeId)
}
var count int
underlyingDB, err := conn.SqlDB(ctx)
require.NoError(err)
err = underlyingDB.QueryRow(whereValidAuthMethod, authMethodId, scopeId).Scan(&count)
2020-06-29 08:18:57 -04:00
require.NoError(err)
require.Equal(1, count)
id, err := db.NewPublicId(ctx, accountPrefix)
2020-06-29 08:18:57 -04:00
require.NoError(err)
acct := &authAccount{
Account: &store.Account{
2020-06-29 08:18:57 -04:00
PublicId: id,
ScopeId: scopeId,
AuthMethodId: authMethodId,
IamUserId: userId,
},
}
err = rw.Create(context.Background(), acct)
require.NoError(err)
require.NotEmpty(acct.PublicId)
if userId == "" {
underlyingDB, err := conn.SqlDB(ctx)
require.NoError(err)
dbassert := dbassert.New(t, underlyingDB)
2020-06-29 08:18:57 -04:00
dbassert.IsNull(acct, "IamUserId")
}
return acct
}
// testAuthMethod is a temporary test function. TODO - replace with an auth
// subsystem testAuthMethod function.
func testAuthMethod(t testing.TB, conn *db.DB, scopeId string) string {
2020-06-29 08:18:57 -04:00
const (
authMethodPrefix = "am_"
)
t.Helper()
ctx := context.Background()
2020-06-29 08:18:57 -04:00
require := require.New(t)
require.NotNil(conn)
require.NotEmpty(scopeId)
id, err := db.NewPublicId(ctx, authMethodPrefix)
2020-06-29 08:18:57 -04:00
require.NoError(err)
rw := db.New(conn)
_, err = rw.Exec(ctx, insertAuthMethod, []any{id, scopeId})
2020-06-29 08:18:57 -04:00
require.NoError(err)
return id
}