2021-05-04 19:57:35 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-07-22 00:14:28 -04:00
|
|
|
package auth
|
|
|
|
|
|
2021-03-25 03:15:43 -04:00
|
|
|
import (
|
|
|
|
|
"context"
|
2025-01-24 10:23:33 -08:00
|
|
|
"testing"
|
2021-03-25 03:15:43 -04:00
|
|
|
|
2022-10-28 15:52:06 -04:00
|
|
|
"github.com/hashicorp/boundary/globals"
|
2025-01-24 10:23:33 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/authtoken"
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/controller/common"
|
2025-01-24 10:23:33 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2021-10-19 17:21:41 -04:00
|
|
|
authpb "github.com/hashicorp/boundary/internal/gen/controller/auth"
|
2025-01-24 10:23:33 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/kms"
|
2021-05-04 19:57:35 -05:00
|
|
|
"github.com/hashicorp/boundary/internal/requests"
|
2025-01-24 10:23:33 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/server"
|
|
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-25 03:15:43 -04:00
|
|
|
)
|
2020-07-22 00:14:28 -04:00
|
|
|
|
2021-05-04 19:57:35 -05:00
|
|
|
// DisabledAuthTestContext is meant for testing, and uses a context that has
|
|
|
|
|
// auth checking entirely disabled. Supported options: WithScopeId an WithUserId
|
|
|
|
|
// are used directly; WithKms is passed through into the verifier context.
|
2021-03-25 03:15:43 -04:00
|
|
|
func DisabledAuthTestContext(iamRepoFn common.IamRepoFactory, scopeId string, opt ...Option) context.Context {
|
2021-10-19 17:21:41 -04:00
|
|
|
reqInfo := authpb.RequestInfo{DisableAuthEntirely: true}
|
2020-07-22 00:14:28 -04:00
|
|
|
opts := getOpts(opt...)
|
2021-10-19 17:21:41 -04:00
|
|
|
reqInfo.ScopeIdOverride = opts.withScopeId
|
|
|
|
|
if reqInfo.ScopeIdOverride == "" {
|
|
|
|
|
reqInfo.ScopeIdOverride = scopeId
|
2021-03-25 03:15:43 -04:00
|
|
|
}
|
2021-10-19 17:21:41 -04:00
|
|
|
reqInfo.UserIdOverride = opts.withUserId
|
|
|
|
|
if reqInfo.UserIdOverride == "" {
|
2022-10-28 15:52:06 -04:00
|
|
|
reqInfo.UserIdOverride = globals.AnyAuthenticatedUserId
|
2021-05-04 19:57:35 -05:00
|
|
|
}
|
2024-08-29 17:14:42 -04:00
|
|
|
reqInfo.Actions = opts.withActions
|
2021-05-04 19:57:35 -05:00
|
|
|
requestContext := context.WithValue(context.Background(), requests.ContextRequestInformationKey, &requests.RequestContext{})
|
2021-10-19 17:21:41 -04:00
|
|
|
return NewVerifierContext(requestContext, iamRepoFn, nil, nil, opts.withKms, &reqInfo)
|
2020-07-22 00:14:28 -04:00
|
|
|
}
|
2025-01-24 10:23:33 -08:00
|
|
|
|
|
|
|
|
// TestAuthContextFromToken creates an auth context with provided token
|
|
|
|
|
// This is used in conjunction with TestAuthTokenWithRoles which creates a test token
|
|
|
|
|
func TestAuthContextFromToken(t *testing.T, conn *db.DB, wrap wrapping.Wrapper, token *authtoken.AuthToken, iamRepo *iam.Repository) context.Context {
|
|
|
|
|
t.Helper()
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
rw := db.New(conn)
|
|
|
|
|
kmsCache := kms.TestKms(t, conn, wrap)
|
|
|
|
|
atRepo, err := authtoken.NewRepository(ctx, rw, rw, kmsCache)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
serversRepoFn := func() (*server.Repository, error) {
|
|
|
|
|
return server.NewRepository(ctx, rw, rw, kmsCache)
|
|
|
|
|
}
|
|
|
|
|
iamRepoFn := func() (*iam.Repository, error) {
|
|
|
|
|
return iamRepo, nil
|
|
|
|
|
}
|
|
|
|
|
atRepoFn := func() (*authtoken.Repository, error) {
|
|
|
|
|
return atRepo, nil
|
|
|
|
|
}
|
|
|
|
|
fullGrantAuthCtx := NewVerifierContext(requests.NewRequestContext(ctx, requests.WithUserId(token.GetIamUserId())),
|
|
|
|
|
iamRepoFn, atRepoFn, serversRepoFn, kmsCache, &authpb.RequestInfo{
|
|
|
|
|
PublicId: token.PublicId,
|
|
|
|
|
Token: token.GetToken(),
|
|
|
|
|
TokenFormat: uint32(AuthTokenTypeBearer),
|
|
|
|
|
})
|
|
|
|
|
return fullGrantAuthCtx
|
|
|
|
|
}
|