* 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>
236 lines
7.8 KiB
Go
236 lines
7.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"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"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/testing/protocmp"
|
|
)
|
|
|
|
func TestRepository_CreateAuthMethod(t *testing.T) {
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
|
ctx := context.Background()
|
|
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
|
|
|
convertAlg := func(alg ...Alg) []string {
|
|
s := make([]string, 0, len(alg))
|
|
for _, a := range alg {
|
|
s = append(s, string(a))
|
|
}
|
|
return s
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
am func(*testing.T) *AuthMethod
|
|
opt []Option
|
|
wantErrMatch *errors.Template
|
|
}{
|
|
{
|
|
name: "valid",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
algs := []Alg{RS256, ES256}
|
|
cbs := TestConvertToUrls(t, "https://www.alice.com/callback")[0]
|
|
auds := []string{"alice-rp", "bob-rp"}
|
|
cert1, pem1 := testGenerateCA(t, "localhost")
|
|
cert2, pem2 := testGenerateCA(t, "localhost")
|
|
certs := []*x509.Certificate{cert1, cert2}
|
|
pems := []string{pem1, pem2}
|
|
am, err := NewAuthMethod(
|
|
ctx,
|
|
org.PublicId,
|
|
"alice-rp",
|
|
"alice-secret", WithAudClaims("alice-rp"),
|
|
WithAudClaims(auds...),
|
|
WithIssuer(TestConvertToUrls(t, "https://www.alice.com")[0]),
|
|
WithApiUrl(cbs),
|
|
WithSigningAlgs(algs...),
|
|
WithCertificates(certs...),
|
|
WithName("alice's restaurant"),
|
|
WithDescription("it's a good place to eat"),
|
|
WithClaimsScopes("email", "profile"),
|
|
WithAccountClaimMap(map[string]AccountToClaim{"display_name": ToNameClaim, "oid": ToSubClaim}),
|
|
)
|
|
require.NoError(t, err)
|
|
require.Equal(t, am.SigningAlgs, convertAlg(algs...))
|
|
require.Equal(t, am.ApiUrl, cbs.String())
|
|
require.Equal(t, "https://www.alice.com", am.Issuer)
|
|
require.Equal(t, am.AudClaims, auds)
|
|
require.Equal(t, am.Certificates, pems)
|
|
require.Equal(t, am.OperationalState, string(InactiveState))
|
|
return am
|
|
},
|
|
},
|
|
{
|
|
name: "valid with custom ID",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
algs := []Alg{RS256, ES256}
|
|
cbs := TestConvertToUrls(t, "https://www.alice.com/callback")[0]
|
|
auds := []string{"alice-rp-custom", "bob-rp-custom"}
|
|
cert1, pem1 := testGenerateCA(t, "localhost")
|
|
cert2, pem2 := testGenerateCA(t, "localhost")
|
|
certs := []*x509.Certificate{cert1, cert2}
|
|
pems := []string{pem1, pem2}
|
|
am, err := NewAuthMethod(
|
|
ctx,
|
|
org.PublicId,
|
|
"alice-rp-custom",
|
|
"alice-secret-custom", WithAudClaims("alice-rp-custom"),
|
|
WithAudClaims(auds...),
|
|
WithIssuer(TestConvertToUrls(t, "https://www.alice.com")[0]),
|
|
WithApiUrl(cbs),
|
|
WithSigningAlgs(algs...),
|
|
WithCertificates(certs...),
|
|
WithName("alice's restaurant with a twist"),
|
|
WithDescription("it's an okay but kinda weird place to eat"),
|
|
WithClaimsScopes("email", "profile"),
|
|
WithAccountClaimMap(map[string]AccountToClaim{"display_name": ToNameClaim, "oid": ToSubClaim}),
|
|
)
|
|
require.NoError(t, err)
|
|
require.Equal(t, am.SigningAlgs, convertAlg(algs...))
|
|
require.Equal(t, am.ApiUrl, cbs.String())
|
|
require.Equal(t, "https://www.alice.com", am.Issuer)
|
|
require.Equal(t, am.AudClaims, auds)
|
|
require.Equal(t, am.Certificates, pems)
|
|
require.Equal(t, am.OperationalState, string(InactiveState))
|
|
return am
|
|
},
|
|
opt: []Option{WithPublicId("amoidc_1234567890")},
|
|
},
|
|
{
|
|
name: "bad custom ID",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
algs := []Alg{RS256, ES256}
|
|
cbs := TestConvertToUrls(t, "https://www.alice.com/callback")[0]
|
|
auds := []string{"alice-rp-bad", "bob-rp-bad"}
|
|
cert1, pem1 := testGenerateCA(t, "localhost")
|
|
cert2, pem2 := testGenerateCA(t, "localhost")
|
|
certs := []*x509.Certificate{cert1, cert2}
|
|
pems := []string{pem1, pem2}
|
|
am, err := NewAuthMethod(
|
|
ctx,
|
|
org.PublicId,
|
|
"alice-rp-bad",
|
|
"alice-secret-bad", WithAudClaims("alice-rp-bad"),
|
|
WithAudClaims(auds...),
|
|
WithIssuer(TestConvertToUrls(t, "https://www.alice.com")[0]),
|
|
WithApiUrl(cbs),
|
|
WithSigningAlgs(algs...),
|
|
WithCertificates(certs...),
|
|
WithName("alice's restaurant is bad"),
|
|
WithDescription("their food is awful"),
|
|
WithClaimsScopes("email", "profile"),
|
|
WithAccountClaimMap(map[string]AccountToClaim{"display_name": ToNameClaim, "oid": ToSubClaim}),
|
|
)
|
|
require.NoError(t, err)
|
|
require.Equal(t, am.SigningAlgs, convertAlg(algs...))
|
|
require.Equal(t, am.ApiUrl, cbs.String())
|
|
require.Equal(t, "https://www.alice.com", am.Issuer)
|
|
require.Equal(t, am.AudClaims, auds)
|
|
require.Equal(t, am.Certificates, pems)
|
|
require.Equal(t, am.OperationalState, string(InactiveState))
|
|
return am
|
|
},
|
|
opt: []Option{WithPublicId("amoic_1234567890")},
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
},
|
|
{
|
|
name: "bad-state",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
am, err := NewAuthMethod(ctx, org.PublicId, "bad-state-rp", "alice-secret",
|
|
WithAudClaims("alice-rp"), WithApiUrl(TestConvertToUrls(t, "https://api.com")[0]))
|
|
require.NoError(t, err)
|
|
am.OperationalState = "not-a-valid-state"
|
|
return am
|
|
},
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
},
|
|
{
|
|
name: "missing-auth-method",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
return nil
|
|
},
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
},
|
|
{
|
|
name: "bad-public-id",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
id, err := newAuthMethodId(ctx)
|
|
require.NoError(t, err)
|
|
am := AllocAuthMethod()
|
|
am.PublicId = id
|
|
return &am
|
|
},
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
},
|
|
{
|
|
name: "bad-version",
|
|
am: func(t *testing.T) *AuthMethod {
|
|
am := AllocAuthMethod()
|
|
am.Version = 22
|
|
return &am
|
|
},
|
|
wantErrMatch: errors.T(errors.InvalidParameter),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
repo, err := NewRepository(ctx, rw, rw, kmsCache)
|
|
assert.NoError(err)
|
|
require.NotNil(repo)
|
|
am := tt.am(t)
|
|
got, err := repo.CreateAuthMethod(ctx, am, tt.opt...)
|
|
if tt.wantErrMatch != nil {
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(tt.wantErrMatch, err), "want err code: %q got: %q", tt.wantErrMatch, err)
|
|
assert.Nil(got)
|
|
|
|
if am != nil {
|
|
err := db.TestVerifyOplog(t, rw, am.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second))
|
|
require.Errorf(err, "should not have found oplog entry for %s", am.PublicId)
|
|
}
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
if tt.opt != nil {
|
|
if opts := getOpts(tt.opt...); opts.withPublicId != "" {
|
|
require.Equal(opts.withPublicId, got.PublicId)
|
|
}
|
|
}
|
|
am.PublicId = got.PublicId
|
|
am.CreateTime = got.CreateTime
|
|
am.UpdateTime = got.UpdateTime
|
|
am.Version = got.Version
|
|
assert.Truef(proto.Equal(am.AuthMethod, got.AuthMethod), "got %+v expected %+v", got, tt.am)
|
|
|
|
err = db.TestVerifyOplog(t, rw, am.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second))
|
|
require.NoErrorf(err, "unexpected error verifying oplog entry: %s", err)
|
|
|
|
found, err := repo.LookupAuthMethod(ctx, am.PublicId)
|
|
require.NoError(err)
|
|
found.CreateTime = got.CreateTime
|
|
found.UpdateTime = got.UpdateTime
|
|
found.Version = got.Version
|
|
TestSortAuthMethods(t, []*AuthMethod{found, am})
|
|
assert.Empty(cmp.Diff(found.AuthMethod, am.AuthMethod, protocmp.Transform()))
|
|
})
|
|
}
|
|
}
|