Files
shadowdaoandClaude Sonnet 5.5 962d708fdd Rename Go module paths to repo.anhonesthost.net/CyberCoveLLC/threshold
Rename root, api and sdk module paths (imports, go.mod, go_package,
ldflags, build scripts, buf configs) and regenerate protobuf/gateway
code with the project's pinned toolchain. Wire identifiers unchanged.
api gets a local replace for sdk; root/api/sdk require v0.1.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 10:33:41 -07:00

111 lines
4.2 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: BUSL-1.1
package credentiallibraries_test
import (
"context"
"testing"
"repo.anhonesthost.net/CyberCoveLLC/threshold/globals"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/auth"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/auth/oidc"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/authtoken"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/credential/vault"
controllerauth "repo.anhonesthost.net/CyberCoveLLC/threshold/internal/daemon/controller/auth"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/daemon/controller/handlers/credentiallibraries"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db"
pbs "repo.anhonesthost.net/CyberCoveLLC/threshold/internal/gen/controller/api/services"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/iam"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/kms"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/scheduler"
"github.com/stretchr/testify/require"
)
func TestGrants_ReadActions(t *testing.T) {
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrap := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrap)
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
kmsCache := kms.TestKms(t, conn, wrap)
sche := scheduler.TestScheduler(t, conn, wrap)
atRepo, err := authtoken.NewRepository(ctx, rw, rw, kmsCache)
require.NoError(t, err)
vaultRepoFn := func() (*vault.Repository, error) {
return vault.NewRepository(ctx, rw, rw, kmsCache, sche)
}
s, err := credentiallibraries.NewService(ctx, iamRepoFn, vaultRepoFn, 1000)
require.NoError(t, err)
org, proj := iam.TestScopes(t, iamRepo)
proj1CredStore := vault.TestCredentialStores(t, conn, wrap, proj.PublicId, 1)
proj1Libs := vault.TestCredentialLibraries(t, conn, wrap, proj1CredStore[0].GetPublicId(), globals.UnspecifiedCredentialType, 3)
t.Run("List", func(t *testing.T) {
testcases := []struct {
name string
input *pbs.ListCredentialLibrariesRequest
userFunc func() (*iam.User, auth.Account)
wantErr error
wantIDs []string
}{
{
name: "global role grant descendant returns all credentials library",
input: &pbs.ListCredentialLibrariesRequest{
CredentialStoreId: proj1CredStore[0].GetPublicId(),
},
userFunc: iam.TestUserManagedGroupGrantsFunc(t, conn, kmsCache, globals.GlobalPrefix, oidc.TestAuthMethodWithAccountInManagedGroup, []iam.TestRoleGrantsRequest{
{
RoleScopeId: globals.GlobalPrefix,
Grants: []string{"ids=*;type=credential-library;actions=list,read"},
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeDescendants},
},
}),
wantErr: nil,
wantIDs: []string{proj1Libs[0].GetPublicId(), proj1Libs[1].GetPublicId(), proj1Libs[2].GetPublicId()},
},
{
name: "org role grant this and children returns all credentials library",
input: &pbs.ListCredentialLibrariesRequest{
CredentialStoreId: proj1CredStore[0].GetPublicId(),
},
userFunc: iam.TestUserManagedGroupGrantsFunc(t, conn, kmsCache, globals.GlobalPrefix, oidc.TestAuthMethodWithAccountInManagedGroup, []iam.TestRoleGrantsRequest{
{
RoleScopeId: org.GetPublicId(),
Grants: []string{"ids=*;type=credential-library;actions=list,read"},
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeChildren},
},
}),
wantErr: nil,
wantIDs: []string{proj1Libs[0].GetPublicId(), proj1Libs[1].GetPublicId(), proj1Libs[2].GetPublicId()},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
user, account := tc.userFunc()
tok, err := atRepo.CreateAuthToken(ctx, user, account.GetPublicId())
require.NoError(t, err)
fullGrantAuthCtx := controllerauth.TestAuthContextFromToken(t, conn, wrap, tok, iamRepo)
got, finalErr := s.ListCredentialLibraries(fullGrantAuthCtx, tc.input)
if tc.wantErr != nil {
require.ErrorIs(t, finalErr, tc.wantErr)
return
}
require.NoError(t, finalErr)
var gotIDs []string
for _, g := range got.Items {
gotIDs = append(gotIDs, g.GetId())
}
require.ElementsMatch(t, tc.wantIDs, gotIDs)
})
}
})
}