* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
359 lines
13 KiB
Go
359 lines
13 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/auth/oidc/store"
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/hashicorp/boundary/internal/kms"
|
|
"github.com/hashicorp/boundary/internal/oplog"
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func Test_upsertAccount(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rootWrapper := db.TestWrapper(t)
|
|
kmsCache := kms.TestKms(t, conn, rootWrapper)
|
|
rw := db.New(conn)
|
|
|
|
r, err := NewRepository(ctx, rw, rw, kmsCache)
|
|
require.NoError(t, err)
|
|
|
|
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, rootWrapper))
|
|
databaseWrapper, err := kmsCache.GetWrapper(ctx, org.PublicId, kms.KeyPurposeDatabase)
|
|
require.NoError(t, err)
|
|
amActivePriv := TestAuthMethod(
|
|
t,
|
|
conn, databaseWrapper, org.PublicId, ActivePrivateState,
|
|
"alice_rp", "fido",
|
|
WithApiUrl(TestConvertToUrls(t, "https://alice-active-priv.com/callback")[0]),
|
|
WithSigningAlgs(RS256))
|
|
|
|
amWithMapping := TestAuthMethod(
|
|
t,
|
|
conn, databaseWrapper, org.PublicId, ActivePrivateState,
|
|
"alice_rp", "fido",
|
|
WithAccountClaimMap(map[string]AccountToClaim{"oid": ToSubClaim}),
|
|
WithApiUrl(TestConvertToUrls(t, "https://alice-active-priv.com/callback")[0]),
|
|
WithSigningAlgs(RS256))
|
|
|
|
tests := []struct {
|
|
name string
|
|
am *AuthMethod
|
|
idClaims map[string]any
|
|
atClaims map[string]any
|
|
wantAcct *Account
|
|
wantErrMatch *errors.Template
|
|
wantErrContains string
|
|
}{
|
|
{
|
|
name: "success-defaults",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": "https://alice-active-priv.com", "sub": "success-defaults"},
|
|
atClaims: map[string]any{},
|
|
wantAcct: &Account{Account: &store.Account{
|
|
AuthMethodId: amActivePriv.PublicId,
|
|
Issuer: "https://alice-active-priv.com",
|
|
Subject: "success-defaults",
|
|
TokenClaims: `{"iss":"https://alice-active-priv.com","sub":"success-defaults"}`,
|
|
UserinfoClaims: "{}",
|
|
}},
|
|
},
|
|
{
|
|
name: "success-atTk-full-name-and-email",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": "https://alice-active-priv.com", "sub": "success-atTk-full-name-and-email"},
|
|
atClaims: map[string]any{"name": "alice eve-smith", "email": "alice@alice.com"},
|
|
wantAcct: &Account{Account: &store.Account{
|
|
AuthMethodId: amActivePriv.PublicId,
|
|
Issuer: "https://alice-active-priv.com",
|
|
Subject: "success-atTk-full-name-and-email",
|
|
Email: "alice@alice.com",
|
|
FullName: "alice eve-smith",
|
|
TokenClaims: `{"iss":"https://alice-active-priv.com","sub":"success-atTk-full-name-and-email"}`,
|
|
UserinfoClaims: `{"email":"alice@alice.com","name":"alice eve-smith"}`,
|
|
}},
|
|
},
|
|
{
|
|
name: "success-idTk-full-name-and-email",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": "https://alice-active-priv.com", "sub": "success-idTk-full-name-and-email", "name": "alice eve-smith", "email": "alice@alice.com"},
|
|
atClaims: map[string]any{},
|
|
wantAcct: &Account{Account: &store.Account{
|
|
AuthMethodId: amActivePriv.PublicId,
|
|
Issuer: "https://alice-active-priv.com",
|
|
Subject: "success-idTk-full-name-and-email",
|
|
Email: "alice@alice.com",
|
|
FullName: "alice eve-smith",
|
|
TokenClaims: `{"email":"alice@alice.com","iss":"https://alice-active-priv.com","name":"alice eve-smith","sub":"success-idTk-full-name-and-email"}`,
|
|
UserinfoClaims: `{}`,
|
|
}},
|
|
},
|
|
{
|
|
name: "success-map-idTk",
|
|
am: amWithMapping,
|
|
idClaims: map[string]any{"iss": "https://alice-active-priv.com", "sub": "success-defaults", "oid": "success-map"},
|
|
atClaims: map[string]any{},
|
|
wantAcct: &Account{Account: &store.Account{
|
|
AuthMethodId: amWithMapping.PublicId,
|
|
Issuer: "https://alice-active-priv.com",
|
|
Subject: "success-map",
|
|
TokenClaims: `{"iss":"https://alice-active-priv.com","oid":"success-map","sub":"success-defaults"}`,
|
|
UserinfoClaims: `{}`,
|
|
}},
|
|
},
|
|
{
|
|
name: "non-existent-auth-method-scope-id",
|
|
am: func() *AuthMethod { cp := amActivePriv.Clone(); cp.ScopeId = "non-existent-scope-id"; return cp }(),
|
|
idClaims: map[string]any{"iss": "https://alice.com", "sub": "non-existent-auth-method-scope-id"},
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "unable to get oplog wrapper",
|
|
},
|
|
{
|
|
name: "non-parsable-issuer",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": ":::::alice.com", "sub": "non-parsable-issuer"},
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "unable to parse issuer",
|
|
},
|
|
{
|
|
name: "empty-sub",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": "https://alice.com", "sub": ""},
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "missing subject",
|
|
},
|
|
{
|
|
name: "empty-issuer",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": "", "sub": "bad-issuer"},
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "missing issuer",
|
|
},
|
|
{
|
|
name: "missing-id-token-issuer",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{},
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "issuer is not present in ID Token",
|
|
},
|
|
{
|
|
name: "missing-id-token-sub",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{"iss": "https://alice.com"},
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "to account subject and it is not present in ID Token",
|
|
},
|
|
{
|
|
name: "missing-id-token-claims",
|
|
am: amActivePriv,
|
|
atClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "missing ID Token claims",
|
|
},
|
|
{
|
|
name: "missing-access-token-claims",
|
|
am: amActivePriv,
|
|
idClaims: map[string]any{},
|
|
wantErrMatch: errors.T(errors.Unknown),
|
|
wantErrContains: "missing Access Token claims",
|
|
},
|
|
{
|
|
name: "missing-auth-method",
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "missing auth method",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
gotAcct, err := r.upsertAccount(ctx, tt.am, tt.idClaims, tt.atClaims)
|
|
if tt.wantErrMatch != nil {
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(tt.wantErrMatch, err), "got err %s but wanted: %+v", tt.wantErrMatch.Code, err)
|
|
if tt.wantErrContains != "" {
|
|
assert.Contains(err.Error(), tt.wantErrContains)
|
|
}
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
tt.wantAcct.PublicId = gotAcct.PublicId
|
|
tt.wantAcct.Version = gotAcct.Version
|
|
tt.wantAcct.CreateTime = gotAcct.CreateTime
|
|
tt.wantAcct.UpdateTime = gotAcct.UpdateTime
|
|
require.NoError(err)
|
|
ok := proto.Equal(tt.wantAcct.Account, gotAcct.Account)
|
|
assert.True(ok)
|
|
if !ok {
|
|
assert.Equalf(tt.wantAcct, gotAcct, "using assert.Equal to just show the diff between want and got")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_upsertOplog(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rootWrapper := db.TestWrapper(t)
|
|
kmsCache := kms.TestKms(t, conn, rootWrapper)
|
|
rw := db.New(conn)
|
|
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, rootWrapper))
|
|
databaseWrapper, err := kmsCache.GetWrapper(ctx, org.PublicId, kms.KeyPurposeDatabase)
|
|
require.NoError(t, err)
|
|
oplogWrapper, err := kmsCache.GetWrapper(ctx, org.PublicId, kms.KeyPurposeOplog)
|
|
require.NoError(t, err)
|
|
|
|
testAuthMethod := TestAuthMethod(
|
|
t, conn, databaseWrapper, org.PublicId, ActivePrivateState,
|
|
"alice-rp", "fido",
|
|
WithSigningAlgs(RS256),
|
|
WithIssuer(TestConvertToUrls(t, "https://www.alice.com")[0]),
|
|
WithApiUrl(TestConvertToUrls(t, "https://www.alice.com/callback")[0]),
|
|
)
|
|
|
|
tests := []struct {
|
|
name string
|
|
writer db.Writer
|
|
wrapper wrapping.Wrapper
|
|
op oplog.OpType
|
|
scopeId string
|
|
acct *Account
|
|
fieldMasks []string
|
|
nullFields []string
|
|
wantErrMatch *errors.Template
|
|
wantErrContains string
|
|
}{
|
|
{
|
|
name: "create-success",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_CREATE,
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "create-success"),
|
|
},
|
|
{
|
|
name: "update-success-withFieldMask-only",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_UPDATE,
|
|
fieldMasks: []string{"FullName"},
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "update-success-withFieldMask-only"),
|
|
},
|
|
{
|
|
name: "update-success-withNullFields-only",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_UPDATE,
|
|
nullFields: []string{"FullName"},
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "update-success-withNullFields-only"),
|
|
},
|
|
{
|
|
name: "update-missing-fieldMask-and-nullFields",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_UPDATE,
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "update-missing-fieldMask-and-nullFields"),
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "update operations must specify field masks and/or null masks",
|
|
},
|
|
{
|
|
name: "missing-writer",
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_CREATE,
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "missing-writer"),
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "missing db writer",
|
|
},
|
|
{
|
|
name: "missing-oplog-wrapper",
|
|
writer: rw,
|
|
op: oplog.OpType_OP_TYPE_CREATE,
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "missing-oplog-wrapper"),
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "missing oplog wrapper",
|
|
},
|
|
{
|
|
name: "bad-op-type",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_DELETE,
|
|
scopeId: org.PublicId,
|
|
acct: TestAccount(t, conn, testAuthMethod, "bad-op-type"),
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "not a supported operation",
|
|
},
|
|
{
|
|
name: "missing-scope-id",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_UPDATE,
|
|
acct: TestAccount(t, conn, testAuthMethod, "missing-scope-id"),
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "missing scope id",
|
|
},
|
|
{
|
|
name: "missing-account",
|
|
writer: rw,
|
|
wrapper: oplogWrapper,
|
|
op: oplog.OpType_OP_TYPE_UPDATE,
|
|
scopeId: org.PublicId,
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
wantErrContains: "missing account",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
// start with no oplog entries
|
|
_, err = rw.Exec(ctx, "delete from oplog_entry", nil)
|
|
require.NoError(err)
|
|
|
|
err := upsertOplog(ctx, tt.writer, tt.wrapper, tt.op, tt.scopeId, tt.acct, tt.fieldMasks, tt.nullFields)
|
|
if tt.wantErrMatch != nil {
|
|
require.Error(err)
|
|
if tt.wantErrContains != "" {
|
|
assert.Contains(err.Error(), tt.wantErrContains)
|
|
}
|
|
if tt.acct != nil {
|
|
// check for create
|
|
err := db.TestVerifyOplog(t, rw, tt.acct.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second))
|
|
require.Errorf(err, "should not have found oplog entry for %s", tt.acct.AuthMethodId)
|
|
|
|
// check for update
|
|
err = db.TestVerifyOplog(t, rw, tt.acct.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second))
|
|
require.Errorf(err, "should not have found oplog entry for %s", tt.acct.AuthMethodId)
|
|
}
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
err = db.TestVerifyOplog(t, rw, tt.acct.PublicId, db.WithOperation(tt.op), db.WithCreateNotBefore(10*time.Second))
|
|
require.NoErrorf(err, "unexpected error verifying oplog entry: %s", err)
|
|
})
|
|
}
|
|
}
|