basic keys mgmt repo (#264)
This commit is contained in:
140 files changed
+5667
-1223
No files matched your search
@@ -84,6 +84,7 @@ protobuild:
|
||||
@protoc-go-inject-tag -input=./internal/auth/store/account.pb.go
|
||||
@protoc-go-inject-tag -input=./internal/auth/password/store/password.pb.go
|
||||
@protoc-go-inject-tag -input=./internal/auth/password/store/argon2.pb.go
|
||||
@protoc-go-inject-tag -input=./internal/kms/store/root_key.pb.go
|
||||
@rm -R ${TMP_DIR}
|
||||
|
||||
protolint:
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestAccounts_List(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
accountClient := authmethods.NewAccountsClient(client)
|
||||
@@ -88,7 +88,7 @@ func TestAccount_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
accountClient := authmethods.NewAccountsClient(client)
|
||||
@@ -142,7 +142,7 @@ func TestAccount_CustomMethods(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
accountClient := authmethods.NewAccountsClient(client)
|
||||
@@ -179,7 +179,7 @@ func TestAccount_Errors(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
accountClient := authmethods.NewAccountsClient(client)
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestGroup_List(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
@@ -96,7 +96,7 @@ func TestGroup_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
@@ -188,7 +188,7 @@ func TestGroup_Errors(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestCatalogs_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
@@ -80,7 +80,7 @@ func TestCatalogs_Errors(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
_, proj := iam.TestScopes(t, tc.DbConn())
|
||||
_, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(proj.GetPublicId())
|
||||
pc := hosts.NewHostCatalogsClient(client)
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestCustom(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
@@ -141,7 +141,7 @@ func TestRole_List(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
@@ -222,7 +222,7 @@ func TestRole_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
@@ -295,7 +295,7 @@ func TestRole_Errors(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, proj := iam.TestScopes(t, tc.DbConn())
|
||||
org, proj := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
projClient := client.Clone()
|
||||
projClient.SetScopeId(proj.GetPublicId())
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestProjects_List(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
scps := scopes.NewScopesClient(client)
|
||||
@@ -74,7 +74,7 @@ func TestProjects_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, _ := iam.TestScopes(t, tc.DbConn())
|
||||
org, _ := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
scps := scopes.NewScopesClient(client)
|
||||
@@ -127,7 +127,7 @@ func TestProject_Errors(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org, _ := iam.TestScopes(t, tc.DbConn())
|
||||
org, _ := iam.TestScopes(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
|
||||
scps := scopes.NewScopesClient(client)
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestUsers_List(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
userClient := users.NewUsersClient(client)
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestUser_Crud(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
userClient := users.NewUsersClient(client)
|
||||
|
||||
@@ -139,7 +139,7 @@ func TestUser_Errors(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := iam.TestOrg(t, tc.DbConn())
|
||||
org := iam.TestOrg(t, tc.IamRepo())
|
||||
client.SetScopeId(org.GetPublicId())
|
||||
userClient := users.NewUsersClient(client)
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ require (
|
||||
github.com/hashicorp/go-alpnmux v0.0.0-20200513011953-0293f5d23c31
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1
|
||||
github.com/hashicorp/go-hclog v0.14.1
|
||||
github.com/hashicorp/go-kms-wrapping v0.5.12
|
||||
github.com/hashicorp/go-kms-wrapping v0.5.14
|
||||
github.com/hashicorp/go-multierror v1.1.0
|
||||
github.com/hashicorp/go-plugin v1.2.0 // indirect
|
||||
github.com/hashicorp/go-retryablehttp v0.6.6
|
||||
|
||||
@@ -158,6 +158,7 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
|
||||
github.com/bufbuild/buf v0.20.5 h1:msLCy5vBoCdFXD+PxtMNA43KMkVJml7UCBCSaFWmEkU=
|
||||
github.com/bufbuild/buf v0.20.5/go.mod h1:Kgpp44ayt6oQ0J0kXEzIkYRfPggDxOriiEHNswd2vEo=
|
||||
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
|
||||
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||
@@ -528,6 +529,8 @@ github.com/hashicorp/go-immutable-radix v1.1.0 h1:vN9wG1D6KG6YHRTWr8512cxGOVgTME
|
||||
github.com/hashicorp/go-immutable-radix v1.1.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-kms-wrapping v0.5.12 h1:4zZCaLqOob5moaAmpS6ZtGZYm4yOcqvmt2lO+zNXHls=
|
||||
github.com/hashicorp/go-kms-wrapping v0.5.12/go.mod h1:yVIWtGOTh/cdGc++/NOlXLus0hJ19Lz4iFrpF6WsZh4=
|
||||
github.com/hashicorp/go-kms-wrapping v0.5.14 h1:XtNZYE9n4ypz9CnhQc/5Vd6ovGqbHO1lQ3IVPc2TV+w=
|
||||
github.com/hashicorp/go-kms-wrapping v0.5.14/go.mod h1:hKJ7tS+eMXOLxwFs7mYJtPRQtT/rGtwqE6awY3JATCw=
|
||||
github.com/hashicorp/go-kms-wrapping/entropy v0.1.0/go.mod h1:d1g9WGtAunDNpek8jUIEJnBlbgKS1N2Q61QkHiZyR1g=
|
||||
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
@@ -572,10 +575,13 @@ github.com/hashicorp/shared-secure-libs v0.0.2/go.mod h1:xhtA0FH6AYYFOy0sir7u0O0
|
||||
github.com/hashicorp/vault/api v1.0.5-0.20200514164214-89b1987e38c2/go.mod h1:P8A7gn1a6j/7qQ4zOaWI6FC/Q9jQWEBRTOwZH33tC9o=
|
||||
github.com/hashicorp/vault/api v1.0.5-0.20200519221902-385fac77e20f h1:PYtnlUZzFSZxPcq7mYp5oC9N+BcJ8IKYf6/EG0GHM2Y=
|
||||
github.com/hashicorp/vault/api v1.0.5-0.20200519221902-385fac77e20f/go.mod h1:euTFbi2YJgwcju3imEt919lhJKF68nN1cQPq3aA+kBE=
|
||||
github.com/hashicorp/vault/api v1.0.5-0.20200805123347-1ef507638af6 h1:ChHpobQse3kjVX7GmbpUFVqaMTf4lb1gcLrJltsUmS4=
|
||||
github.com/hashicorp/vault/api v1.0.5-0.20200805123347-1ef507638af6/go.mod h1:R3Umvhlxi2TN7Ex2hzOowyeNb+SfbVWI973N+ctaFMk=
|
||||
github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvhkWnjtSYCaS2M=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200514144402-4bfac290c352/go.mod h1:WX57W2PwkrOPQ6rVQk+dy5/htHIaB4aBM70EwKThu10=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200514164214-89b1987e38c2/go.mod h1:WX57W2PwkrOPQ6rVQk+dy5/htHIaB4aBM70EwKThu10=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221530-14615acda45f/go.mod h1:WX57W2PwkrOPQ6rVQk+dy5/htHIaB4aBM70EwKThu10=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267/go.mod h1:WX57W2PwkrOPQ6rVQk+dy5/htHIaB4aBM70EwKThu10=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200805123347-1ef507638af6 h1:8bzfSEdW5ztWR73O5ByXbOAx8V6lHL1qTH7PYDLRITI=
|
||||
github.com/hashicorp/vault/sdk v0.1.14-0.20200805123347-1ef507638af6/go.mod h1:+S2qzS1Tex9JgbHxb/Jv7CdZyKydxqg09G/qVvyVmUc=
|
||||
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
|
||||
@@ -1027,6 +1033,8 @@ github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhe
|
||||
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
|
||||
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||
github.com/yandex-cloud/go-genproto v0.0.0-20200722140432-762fe965ce77/go.mod h1:HEUYX/p8966tMUHHT+TsS0hF/Ca/NYwqprC5WXSDMfE=
|
||||
github.com/yandex-cloud/go-sdk v0.0.0-20200722140627-2194e5077f13/go.mod h1:LEdAMqa1v/7KYe4b13ALLkonuDxLph57ibUb50ctvJk=
|
||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
@@ -1165,6 +1173,7 @@ golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200320220750-118fecf932d8/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
@@ -1379,6 +1388,7 @@ google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfG
|
||||
google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200323114720-3f67cca34472/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/authtoken"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/types/action"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/kr/pretty"
|
||||
@@ -19,14 +20,12 @@ import (
|
||||
|
||||
func TestHandler_AuthDecoration(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
org, proj := iam.TestScopes(t, conn)
|
||||
org, proj := iam.TestScopes(t, iamRepo)
|
||||
cases := []struct {
|
||||
name string
|
||||
path string
|
||||
@@ -272,10 +271,10 @@ func TestAuthTokenAuthenticator(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
tokenRepo, err := authtoken.NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
tokenRepo, err := authtoken.NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
tokenRepoFn := func() (*authtoken.Repository, error) {
|
||||
return tokenRepo, nil
|
||||
}
|
||||
@@ -283,8 +282,8 @@ func TestAuthTokenAuthenticator(t *testing.T) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
at := authtoken.TestAuthToken(t, conn, wrapper, o.GetPublicId())
|
||||
o, _ := iam.TestScopes(t, iamRepo)
|
||||
at := authtoken.TestAuthToken(t, conn, kms, o.GetPublicId())
|
||||
|
||||
tokValue := at.GetPublicId() + "_" + at.GetToken()
|
||||
jsCookieVal, httpCookieVal := tokValue[:len(tokValue)/2], tokValue[len(tokValue)/2:]
|
||||
|
||||
@@ -41,6 +41,7 @@ select count(*) from test_auth_method where public_id = $1;
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
db := conn.DB()
|
||||
_, err := db.Exec(createTable)
|
||||
require.NoError(err)
|
||||
@@ -48,7 +49,7 @@ select count(*) from test_auth_method where public_id = $1;
|
||||
_, err = db.Exec(addTriggers)
|
||||
require.NoError(err)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
id := "l1Ocw0TpHn800CekIxIXlmQqRDgFDfYl"
|
||||
_, err = db.Query(insert, id, org.GetPublicId())
|
||||
@@ -105,6 +106,8 @@ values
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := iam.TestRepo(t, conn, wrapper)
|
||||
db := conn.DB()
|
||||
_, err := db.Exec(createTable)
|
||||
require.NoError(err)
|
||||
@@ -112,7 +115,7 @@ values
|
||||
_, err = db.Exec(addTriggers)
|
||||
require.NoError(err)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, repo)
|
||||
meth_id := "31Ocw0TpHn800CekIxIXlmQqRDgFDfYl"
|
||||
_, err = db.Query(insertAuthMethod, meth_id, org.GetPublicId())
|
||||
require.NoError(err)
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
|
||||
func TestAccount_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
auts := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
aut := auts[0]
|
||||
|
||||
@@ -59,7 +59,7 @@ func TestAccount_New(t *testing.T) {
|
||||
want: &Account{
|
||||
Account: &store.Account{
|
||||
AuthMethodId: aut.GetPublicId(),
|
||||
LoginName: "kazmierczak1",
|
||||
LoginName: "kazmierczak1",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -173,6 +173,7 @@ func (c *Argon2Credential) encrypt(ctx context.Context, cipher wrapping.Wrapper)
|
||||
if err := structwrapping.WrapStruct(ctx, cipher, c.Argon2Credential, nil); err != nil {
|
||||
return fmt.Errorf("error encrypting argon2 credential: %w", err)
|
||||
}
|
||||
c.KeyId = cipher.KeyID()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ import (
|
||||
func TestArgon2Configuration_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
authMethodId := authMethod.GetPublicId()
|
||||
@@ -93,7 +94,7 @@ func TestArgon2Configuration_New(t *testing.T) {
|
||||
|
||||
func TestArgon2Configuration_Readonly(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
|
||||
wrapper := db.TestWrapper(t)
|
||||
rw := db.New(conn)
|
||||
|
||||
changeIterations := func() func(*Argon2Configuration) (*Argon2Configuration, []string) {
|
||||
@@ -115,7 +116,7 @@ func TestArgon2Configuration_Readonly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
authMethodId := authMethod.GetPublicId()
|
||||
@@ -303,7 +304,7 @@ func TestArgon2Credential_New(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
auts := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
aut := auts[0]
|
||||
accts := TestAccounts(t, conn, aut.PublicId, 5)
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
func TestAuthMethod_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
w := db.New(conn)
|
||||
|
||||
@@ -73,7 +74,7 @@ func TestAuthMethod_New(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
got, err := NewAuthMethod(org.GetPublicId(), tt.args.opts...)
|
||||
if tt.wantErr {
|
||||
assert.Error(err)
|
||||
|
||||
@@ -5,15 +5,15 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
)
|
||||
|
||||
// A Repository stores and retrieves the persistent types in the password
|
||||
// package. It is not safe to use a repository concurrently.
|
||||
type Repository struct {
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
kms *kms.Kms
|
||||
// defaultLimit provides a default for limiting the number of results returned from the repo
|
||||
defaultLimit int
|
||||
}
|
||||
@@ -22,14 +22,14 @@ type Repository struct {
|
||||
// only be used for one transaction and it is not safe for concurrent go
|
||||
// routines to access it. WithLimit option is used as a repo wide default
|
||||
// limit applied to all ListX methods.
|
||||
func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Option) (*Repository, error) {
|
||||
func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, fmt.Errorf("db.Reader: %w", db.ErrNilParameter)
|
||||
case w == nil:
|
||||
return nil, fmt.Errorf("db.Writer: %w", db.ErrNilParameter)
|
||||
case wrapper == nil:
|
||||
return nil, fmt.Errorf("wrapping.Wrapper: %w", db.ErrNilParameter)
|
||||
case kms == nil:
|
||||
return nil, fmt.Errorf("kms: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -41,7 +41,7 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Op
|
||||
return &Repository{
|
||||
reader: r,
|
||||
writer: w,
|
||||
wrapper: wrapper,
|
||||
kms: kms,
|
||||
defaultLimit: opts.withLimit,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbcommon "github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -24,7 +25,7 @@ import (
|
||||
//
|
||||
// Both a.Name and a.Description are optional. If a.Name is set, it must be
|
||||
// unique within a.AuthMethodId.
|
||||
func (r *Repository) CreateAccount(ctx context.Context, a *Account, opt ...Option) (*Account, error) {
|
||||
func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Account, opt ...Option) (*Account, error) {
|
||||
if a == nil {
|
||||
return nil, fmt.Errorf("create: password account: %w", db.ErrNilParameter)
|
||||
}
|
||||
@@ -37,6 +38,9 @@ func (r *Repository) CreateAccount(ctx context.Context, a *Account, opt ...Optio
|
||||
if a.PublicId != "" {
|
||||
return nil, fmt.Errorf("create: password account: public id not empty: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("create: password account: scope id empty: %w", db.ErrNilParameter)
|
||||
}
|
||||
if !validLoginName(a.LoginName) {
|
||||
return nil, fmt.Errorf("create: password account: invalid user name: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
@@ -69,21 +73,30 @@ func (r *Repository) CreateAccount(ctx context.Context, a *Account, opt ...Optio
|
||||
}
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: password account: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: password account: unable to get database wrapper: %w", err)
|
||||
}
|
||||
|
||||
var newCred *Argon2Credential
|
||||
var newAccount *Account
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
newAccount = a.clone()
|
||||
if err := w.Create(ctx, newAccount, db.WithOplog(r.wrapper, a.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
if err := w.Create(ctx, newAccount, db.WithOplog(oplogWrapper, a.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cred != nil {
|
||||
newCred = cred.clone()
|
||||
if err := newCred.encrypt(ctx, r.wrapper); err != nil {
|
||||
if err := newCred.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.Create(ctx, newCred, db.WithOplog(r.wrapper, cred.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
if err := w.Create(ctx, newCred, db.WithOplog(oplogWrapper, cred.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -139,22 +152,30 @@ func (r *Repository) ListAccounts(ctx context.Context, withAuthMethodId string,
|
||||
|
||||
// DeleteAccount deletes the account for the provided id from the repository returning a count of the
|
||||
// number of records deleted. All options are ignored.
|
||||
func (r *Repository) DeleteAccount(ctx context.Context, withPublicId string, opt ...Option) (int, error) {
|
||||
func (r *Repository) DeleteAccount(ctx context.Context, scopeId, withPublicId string, opt ...Option) (int, error) {
|
||||
if withPublicId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password account: missing public id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password account: scope id empty: %w", db.ErrNilParameter)
|
||||
}
|
||||
ac := allocAccount()
|
||||
ac.PublicId = withPublicId
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password account: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) (err error) {
|
||||
metadata := ac.oplog(oplog.OpType_OP_TYPE_DELETE)
|
||||
dAc := ac.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, dAc, db.WithOplog(r.wrapper, metadata))
|
||||
rowsDeleted, err = w.Delete(ctx, dAc, db.WithOplog(oplogWrapper, metadata))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
@@ -191,7 +212,7 @@ func validLoginName(u string) bool {
|
||||
// An attribute of a will be set to NULL in the database if the attribute
|
||||
// in a is the zero value and it is included in fieldMaskPaths. a.LoginName
|
||||
// cannot be set to NULL.
|
||||
func (r *Repository) UpdateAccount(ctx context.Context, a *Account, version uint32, fieldMaskPaths []string, opt ...Option) (*Account, int, error) {
|
||||
func (r *Repository) UpdateAccount(ctx context.Context, scopeId string, a *Account, version uint32, fieldMaskPaths []string, opt ...Option) (*Account, int, error) {
|
||||
if a == nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password account: %w", db.ErrNilParameter)
|
||||
}
|
||||
@@ -204,6 +225,9 @@ func (r *Repository) UpdateAccount(ctx context.Context, a *Account, version uint
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password account: no version supplied: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password account: scope id empty: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
var changeLoginName bool
|
||||
for _, f := range fieldMaskPaths {
|
||||
@@ -242,17 +266,22 @@ func (r *Repository) UpdateAccount(ctx context.Context, a *Account, version uint
|
||||
}
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password account: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
a = a.clone()
|
||||
|
||||
metadata := a.oplog(oplog.OpType_OP_TYPE_UPDATE)
|
||||
|
||||
var rowsUpdated int
|
||||
var returnedAccount *Account
|
||||
_, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
returnedAccount = a.clone()
|
||||
var err error
|
||||
rowsUpdated, err = w.Update(ctx, returnedAccount, dbMask, nullFields, db.WithOplog(r.wrapper, metadata), db.WithVersion(&version))
|
||||
rowsUpdated, err = w.Update(ctx, returnedAccount, dbMask, nullFields, db.WithOplog(oplogWrapper, metadata), db.WithVersion(&version))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbassert "github.com/hashicorp/boundary/internal/db/assert"
|
||||
"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"
|
||||
@@ -40,7 +41,10 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
|
||||
authMethods := TestAuthMethods(t, conn, org.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
|
||||
@@ -213,10 +217,10 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateAccount(context.Background(), tt.in, tt.opts...)
|
||||
got, err := repo.CreateAccount(context.Background(), org.GetPublicId(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
@@ -235,7 +239,7 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
|
||||
opts := getOpts(tt.opts...)
|
||||
if opts.withPassword {
|
||||
authAcct, err := repo.Authenticate(context.Background(), tt.in.AuthMethodId, tt.in.LoginName, opts.password)
|
||||
authAcct, err := repo.Authenticate(context.Background(), org.GetPublicId(), tt.in.AuthMethodId, tt.in.LoginName, opts.password)
|
||||
require.NoError(err)
|
||||
assert.NoError(db.TestVerifyOplog(t, rw, authAcct.CredentialId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
}
|
||||
@@ -244,11 +248,11 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
authMethods := TestAuthMethods(t, conn, org.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
|
||||
@@ -260,7 +264,7 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
got, err := repo.CreateAccount(context.Background(), in)
|
||||
got, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in)
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, AccountPrefix, got.PublicId)
|
||||
@@ -269,18 +273,18 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
assert.Equal(in.Description, got.Description)
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
got2, err := repo.CreateAccount(context.Background(), in)
|
||||
got2, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in)
|
||||
assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err)
|
||||
assert.Nil(got2)
|
||||
})
|
||||
|
||||
t.Run("valid-duplicate-names-diff-parents", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
authMethods := TestAuthMethods(t, conn, org.GetPublicId(), 2)
|
||||
authMethoda, authMethodb := authMethods[0], authMethods[1]
|
||||
in := &Account{
|
||||
@@ -292,7 +296,7 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
in2 := in.clone()
|
||||
|
||||
in.AuthMethodId = authMethoda.GetPublicId()
|
||||
got, err := repo.CreateAccount(context.Background(), in)
|
||||
got, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in)
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, AccountPrefix, got.PublicId)
|
||||
@@ -302,7 +306,7 @@ func TestRepository_CreateAccount(t *testing.T) {
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
in2.AuthMethodId = authMethodb.GetPublicId()
|
||||
got2, err := repo.CreateAccount(context.Background(), in2)
|
||||
got2, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in2)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got2)
|
||||
assertPublicId(t, AccountPrefix, got2.PublicId)
|
||||
@@ -318,7 +322,10 @@ func TestRepository_LookupAccount(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
|
||||
authMethod := TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
account := TestAccounts(t, conn, authMethod.GetPublicId(), 1)[0]
|
||||
|
||||
@@ -349,7 +356,7 @@ func TestRepository_LookupAccount(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.LookupAccount(context.Background(), tt.in)
|
||||
@@ -369,7 +376,10 @@ func TestRepository_DeleteAccount(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
|
||||
authMethod := TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
account := TestAccounts(t, conn, authMethod.GetPublicId(), 1)[0]
|
||||
|
||||
@@ -401,10 +411,10 @@ func TestRepository_DeleteAccount(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteAccount(context.Background(), tt.in)
|
||||
got, err := repo.DeleteAccount(context.Background(), org.GetPublicId(), tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
@@ -421,7 +431,10 @@ func TestRepository_ListAccounts(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
|
||||
authMethods := TestAuthMethods(t, conn, org.GetPublicId(), 3)
|
||||
accounts1 := TestAccounts(t, conn, authMethods[0].GetPublicId(), 3)
|
||||
accounts2 := TestAccounts(t, conn, authMethods[1].GetPublicId(), 4)
|
||||
@@ -454,7 +467,7 @@ func TestRepository_ListAccounts(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListAccounts(context.Background(), tt.in, tt.opts...)
|
||||
@@ -474,7 +487,10 @@ func TestRepository_ListAccounts_Limits(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
|
||||
am := TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
|
||||
accountCount := 10
|
||||
@@ -528,7 +544,7 @@ func TestRepository_ListAccounts_Limits(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, tt.repoOpts...)
|
||||
repo, err := NewRepository(rw, rw, kms, tt.repoOpts...)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListAccounts(context.Background(), am.GetPublicId(), tt.listOpts...)
|
||||
@@ -542,6 +558,8 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
changeLoginName := func(s string) func(*Account) *Account {
|
||||
return func(a *Account) *Account {
|
||||
@@ -853,25 +871,25 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
am := TestAuthMethods(t, conn, org.PublicId, 1)[0]
|
||||
|
||||
tt.orig.AuthMethodId = am.PublicId
|
||||
if tt.orig.LoginName == "" {
|
||||
tt.orig.LoginName = "kazmierczak"
|
||||
}
|
||||
orig, err := repo.CreateAccount(context.Background(), tt.orig)
|
||||
orig, err := repo.CreateAccount(context.Background(), org.GetPublicId(), tt.orig)
|
||||
assert.NoError(err)
|
||||
require.NotNil(orig)
|
||||
|
||||
if tt.chgFn != nil {
|
||||
orig = tt.chgFn(orig)
|
||||
}
|
||||
got, gotCount, err := repo.UpdateAccount(context.Background(), orig, 1, tt.masks)
|
||||
got, gotCount, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), orig, 1, tt.masks)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Equal(tt.wantCount, gotCount, "row count")
|
||||
@@ -909,12 +927,12 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
name := "test-dup-name"
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
am := TestAuthMethods(t, conn, org.PublicId, 1)[0]
|
||||
acts := TestAccounts(t, conn, am.PublicId, 2)
|
||||
|
||||
@@ -922,7 +940,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
ab := acts[1]
|
||||
|
||||
aa.Name = name
|
||||
got1, gotCount1, err := repo.UpdateAccount(context.Background(), aa, 1, []string{"name"})
|
||||
got1, gotCount1, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), aa, 1, []string{"name"})
|
||||
assert.NoError(err)
|
||||
require.NotNil(got1)
|
||||
assert.Equal(name, got1.Name)
|
||||
@@ -930,7 +948,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
assert.NoError(db.TestVerifyOplog(t, rw, aa.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
|
||||
ab.Name = name
|
||||
got2, gotCount2, err := repo.UpdateAccount(context.Background(), ab, 1, []string{"name"})
|
||||
got2, gotCount2, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), ab, 1, []string{"name"})
|
||||
assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err)
|
||||
assert.Nil(got2)
|
||||
assert.Equal(db.NoRowsAffected, gotCount2, "row count")
|
||||
@@ -941,11 +959,11 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
t.Run("valid-duplicate-names-diff-AuthMethods", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
ams := TestAuthMethods(t, conn, org.PublicId, 2)
|
||||
|
||||
ama := ams[0]
|
||||
@@ -960,7 +978,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
in.AuthMethodId = ama.PublicId
|
||||
in.LoginName = "kazmierczak"
|
||||
got, err := repo.CreateAccount(context.Background(), in)
|
||||
got, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, AccountPrefix, got.PublicId)
|
||||
@@ -971,11 +989,11 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
in2.AuthMethodId = amb.PublicId
|
||||
in2.LoginName = "kazmierczak2"
|
||||
in2.Name = "first-name"
|
||||
got2, err := repo.CreateAccount(context.Background(), in2)
|
||||
got2, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in2)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got2)
|
||||
got2.Name = got.Name
|
||||
got3, gotCount3, err := repo.UpdateAccount(context.Background(), got2, 1, []string{"name"})
|
||||
got3, gotCount3, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), got2, 1, []string{"name"})
|
||||
assert.NoError(err)
|
||||
require.NotNil(got3)
|
||||
assert.NotSame(got2, got3)
|
||||
@@ -987,12 +1005,12 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-loginnames", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
loginName := "kazmierczak12"
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
am := TestAuthMethods(t, conn, org.PublicId, 1)[0]
|
||||
acts := TestAccounts(t, conn, am.PublicId, 2)
|
||||
|
||||
@@ -1000,7 +1018,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
ab := acts[1]
|
||||
|
||||
aa.LoginName = loginName
|
||||
got1, gotCount1, err := repo.UpdateAccount(context.Background(), aa, 1, []string{"LoginName"})
|
||||
got1, gotCount1, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), aa, 1, []string{"LoginName"})
|
||||
assert.NoError(err)
|
||||
require.NotNil(got1)
|
||||
assert.Equal(loginName, got1.LoginName)
|
||||
@@ -1008,7 +1026,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
assert.NoError(db.TestVerifyOplog(t, rw, aa.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
|
||||
ab.LoginName = loginName
|
||||
got2, gotCount2, err := repo.UpdateAccount(context.Background(), ab, 1, []string{"LoginName"})
|
||||
got2, gotCount2, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), ab, 1, []string{"LoginName"})
|
||||
assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err)
|
||||
assert.Nil(got2)
|
||||
assert.Equal(db.NoRowsAffected, gotCount2, "row count")
|
||||
@@ -1019,11 +1037,11 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
t.Run("valid-duplicate-loginnames-diff-AuthMethods", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
ams := TestAuthMethods(t, conn, org.PublicId, 2)
|
||||
|
||||
ama := ams[0]
|
||||
@@ -1037,7 +1055,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
in2 := in.clone()
|
||||
|
||||
in.AuthMethodId = ama.PublicId
|
||||
got, err := repo.CreateAccount(context.Background(), in)
|
||||
got, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, AccountPrefix, got.PublicId)
|
||||
@@ -1046,11 +1064,11 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
in2.AuthMethodId = amb.PublicId
|
||||
in2.LoginName = "kazmierczak2"
|
||||
got2, err := repo.CreateAccount(context.Background(), in2)
|
||||
got2, err := repo.CreateAccount(context.Background(), org.GetPublicId(), in2)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got2)
|
||||
got2.LoginName = got.LoginName
|
||||
got3, gotCount3, err := repo.UpdateAccount(context.Background(), got2, 1, []string{"LoginName"})
|
||||
got3, gotCount3, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), got2, 1, []string{"LoginName"})
|
||||
assert.NoError(err)
|
||||
require.NotNil(got3)
|
||||
assert.NotSame(got2, got3)
|
||||
@@ -1061,11 +1079,11 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
|
||||
t.Run("change-authmethod-id", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
ams := TestAuthMethods(t, conn, org.PublicId, 2)
|
||||
|
||||
ama := ams[0]
|
||||
@@ -1080,7 +1098,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
|
||||
aa.AuthMethodId = ab.AuthMethodId
|
||||
assert.Equal(aa.AuthMethodId, ab.AuthMethodId)
|
||||
|
||||
got1, gotCount1, err := repo.UpdateAccount(context.Background(), aa, 1, []string{"name"})
|
||||
got1, gotCount1, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), aa, 1, []string{"name"})
|
||||
|
||||
assert.NoError(err)
|
||||
require.NotNil(got1)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbcommon "github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -66,16 +67,21 @@ func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ..
|
||||
}
|
||||
m.PasswordConfId, c.PasswordMethodId = c.PrivateId, m.PublicId
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, m.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: password auth method: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var newAuthMethod *AuthMethod
|
||||
var newArgon2Conf *Argon2Configuration
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
newArgon2Conf = c.clone()
|
||||
if err := w.Create(ctx, newArgon2Conf, db.WithOplog(r.wrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
if err := w.Create(ctx, newArgon2Conf, db.WithOplog(oplogWrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
return err
|
||||
}
|
||||
newAuthMethod = m.clone()
|
||||
return w.Create(ctx, newAuthMethod, db.WithOplog(r.wrapper, m.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
return w.Create(ctx, newAuthMethod, db.WithOplog(oplogWrapper, m.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
},
|
||||
)
|
||||
|
||||
@@ -127,22 +133,27 @@ func (r *Repository) ListAuthMethods(ctx context.Context, scopeId string, opt ..
|
||||
|
||||
// DeleteAuthMethod deletes the auth method for the provided id from the repository returning a count of the
|
||||
// number of records deleted. All options are ignored.
|
||||
func (r *Repository) DeleteAuthMethod(ctx context.Context, publicId string, opt ...Option) (int, error) {
|
||||
func (r *Repository) DeleteAuthMethod(ctx context.Context, scopeId, publicId string, opt ...Option) (int, error) {
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password auth method: missing public id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
am := allocAuthMethod()
|
||||
am.PublicId = publicId
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password auth method: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) (err error) {
|
||||
metadata := am.oplog(oplog.OpType_OP_TYPE_DELETE)
|
||||
dAc := am.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, dAc, db.WithOplog(r.wrapper, metadata))
|
||||
rowsDeleted, err = w.Delete(ctx, dAc, db.WithOplog(oplogWrapper, metadata))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
@@ -176,6 +187,9 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMetho
|
||||
if authMethod.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password auth method: missing authMethod public id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if authMethod.ScopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password auth method: scope id empty: %w", db.ErrNilParameter)
|
||||
}
|
||||
for _, f := range fieldMaskPaths {
|
||||
switch {
|
||||
case strings.EqualFold("name", f):
|
||||
@@ -200,15 +214,20 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMetho
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password auth method: %w", db.ErrEmptyFieldMask)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, authMethod.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: password auth method: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
upAuthMethod := authMethod.clone()
|
||||
var rowsUpdated int
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
dbOpts := []db.Option{
|
||||
db.WithOplog(r.wrapper, upAuthMethod.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithOplog(oplogWrapper, upAuthMethod.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithVersion(&version),
|
||||
}
|
||||
var err error
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbassert "github.com/hashicorp/boundary/internal/db/assert"
|
||||
"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"
|
||||
@@ -23,8 +24,9 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -132,7 +134,7 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateAuthMethod(context.Background(), tt.in, tt.opts...)
|
||||
@@ -154,11 +156,11 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
in := &AuthMethod{
|
||||
AuthMethod: &store.AuthMethod{
|
||||
ScopeId: org.GetPublicId(),
|
||||
@@ -182,11 +184,11 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
|
||||
t.Run("valid-duplicate-names-diff-scopes", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org1, _ := iam.TestScopes(t, conn)
|
||||
org1, _ := iam.TestScopes(t, iamRepo)
|
||||
in := &AuthMethod{
|
||||
AuthMethod: &store.AuthMethod{
|
||||
Name: "test-name-repo",
|
||||
@@ -204,7 +206,7 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
assert.Equal(in.Description, got.Description)
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
org2, _ := iam.TestScopes(t, conn)
|
||||
org2, _ := iam.TestScopes(t, iamRepo)
|
||||
in2.ScopeId = org2.GetPublicId()
|
||||
got2, err := repo.CreateAuthMethod(context.Background(), in2)
|
||||
require.NoError(err)
|
||||
@@ -218,11 +220,11 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
|
||||
t.Run("valid-with-publicid", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org1, _ := iam.TestScopes(t, conn)
|
||||
org1, _ := iam.TestScopes(t, iamRepo)
|
||||
in := allocAuthMethod()
|
||||
|
||||
amId, err := newAuthMethodId()
|
||||
@@ -238,11 +240,11 @@ func TestRepository_CreateAuthMethod(t *testing.T) {
|
||||
|
||||
t.Run("invalid-with-badpublicid", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
org1, _ := iam.TestScopes(t, conn)
|
||||
org1, _ := iam.TestScopes(t, iamRepo)
|
||||
in := allocAuthMethod()
|
||||
|
||||
in.ScopeId = org1.GetPublicId()
|
||||
@@ -257,8 +259,9 @@ func TestRepository_LookupAuthMethod(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethod := TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
|
||||
|
||||
amId, err := newAuthMethodId()
|
||||
@@ -288,7 +291,7 @@ func TestRepository_LookupAuthMethod(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.LookupAuthMethod(context.Background(), tt.in)
|
||||
@@ -307,8 +310,9 @@ func TestRepository_DeleteAuthMethod(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethod := TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
|
||||
|
||||
newAuthMethodId, err := newAuthMethodId()
|
||||
@@ -339,10 +343,10 @@ func TestRepository_DeleteAuthMethod(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteAuthMethod(context.Background(), tt.in)
|
||||
got, err := repo.DeleteAuthMethod(context.Background(), o.GetPublicId(), tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
@@ -358,9 +362,10 @@ func TestRepository_ListAuthMethods(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
noAuthMethodOrg, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
noAuthMethodOrg, _ := iam.TestScopes(t, iamRepo)
|
||||
o, _ := iam.TestScopes(t, iamRepo)
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 3)
|
||||
|
||||
var tests = []struct {
|
||||
@@ -390,7 +395,7 @@ func TestRepository_ListAuthMethods(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListAuthMethods(context.Background(), tt.in, tt.opts...)
|
||||
@@ -409,8 +414,9 @@ func TestRepository_ListAuthMethods_Limits(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethodCount := 10
|
||||
ams := TestAuthMethods(t, conn, o.GetPublicId(), authMethodCount)
|
||||
|
||||
@@ -462,7 +468,7 @@ func TestRepository_ListAuthMethods_Limits(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, tt.repoOpts...)
|
||||
repo, err := NewRepository(rw, rw, kms, tt.repoOpts...)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListAuthMethods(context.Background(), ams[0].GetScopeId(), tt.listOpts...)
|
||||
@@ -477,8 +483,10 @@ func TestRepository_UpdateAuthMethod(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -654,7 +662,7 @@ func TestRepository_UpdateAuthMethod(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
// create the initial auth method
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iamRepo)
|
||||
am, err := NewAuthMethod(o.GetPublicId(), WithName("default"), WithDescription("default"))
|
||||
require.NoError(err)
|
||||
origAM, err := repo.CreateAuthMethod(ctx, am)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/auth/password/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func (r *Repository) GetConfiguration(ctx context.Context, authMethodId string)
|
||||
// for c.AuthMethodId, SetConfiguration ignores c. If c contains settings
|
||||
// equal to a previous configuration for c.AuthMethodId, SetConfiguration
|
||||
// updates AuthMethod to use the previous configuration.
|
||||
func (r *Repository) SetConfiguration(ctx context.Context, c Configuration) (Configuration, error) {
|
||||
func (r *Repository) SetConfiguration(ctx context.Context, scopeId string, c Configuration) (Configuration, error) {
|
||||
if c == nil {
|
||||
return nil, fmt.Errorf("set password configuration: %w", db.ErrNilParameter)
|
||||
}
|
||||
@@ -52,7 +53,7 @@ func (r *Repository) SetConfiguration(ctx context.Context, c Configuration) (Con
|
||||
|
||||
switch v := c.(type) {
|
||||
case *Argon2Configuration:
|
||||
out, err := r.setArgon2Conf(ctx, v)
|
||||
out, err := r.setArgon2Conf(ctx, scopeId, v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("set password configuration: %w", err)
|
||||
}
|
||||
@@ -62,7 +63,7 @@ func (r *Repository) SetConfiguration(ctx context.Context, c Configuration) (Con
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) setArgon2Conf(ctx context.Context, c *Argon2Configuration) (*Argon2Configuration, error) {
|
||||
func (r *Repository) setArgon2Conf(ctx context.Context, scopeId string, c *Argon2Configuration) (*Argon2Configuration, error) {
|
||||
c = c.clone()
|
||||
|
||||
id, err := newArgon2ConfigurationId()
|
||||
@@ -77,6 +78,11 @@ func (r *Repository) setArgon2Conf(ctx context.Context, c *Argon2Configuration)
|
||||
},
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update: password account: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
newArgon2Conf := &Argon2Configuration{Argon2Configuration: &store.Argon2Configuration{}}
|
||||
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
@@ -87,13 +93,13 @@ func (r *Repository) setArgon2Conf(ctx context.Context, c *Argon2Configuration)
|
||||
return err
|
||||
}
|
||||
newArgon2Conf = c.clone()
|
||||
if err := w.Create(ctx, newArgon2Conf, db.WithOplog(r.wrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
if err := w.Create(ctx, newArgon2Conf, db.WithOplog(oplogWrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
a.PasswordConfId = newArgon2Conf.PrivateId
|
||||
rowsUpdated, err := w.Update(ctx, a, []string{"PasswordConfId"}, nil, db.WithOplog(r.wrapper, a.oplog(oplog.OpType_OP_TYPE_UPDATE)))
|
||||
rowsUpdated, err := w.Update(ctx, a, []string{"PasswordConfId"}, nil, db.WithOplog(oplogWrapper, a.oplog(oplog.OpType_OP_TYPE_UPDATE)))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/password/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"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"
|
||||
@@ -18,11 +19,12 @@ func TestRepository_GetSetConfiguration(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
authMethodId := authMethod.GetPublicId()
|
||||
@@ -68,7 +70,7 @@ func TestRepository_GetSetConfiguration(t *testing.T) {
|
||||
newConf.PasswordMethodId = currentConf.PasswordMethodId
|
||||
newConf.Memory = currentConf.Memory * 2
|
||||
|
||||
updated, err := repo.SetConfiguration(ctx, newConf)
|
||||
updated, err := repo.SetConfiguration(ctx, o.GetPublicId(), newConf)
|
||||
assert.NoError(err)
|
||||
require.NotNil(updated)
|
||||
assert.NotSame(newConf, updated)
|
||||
@@ -97,7 +99,7 @@ func TestRepository_GetSetConfiguration(t *testing.T) {
|
||||
newConf.PasswordMethodId = authMethodId
|
||||
assert.Empty(newConf.PrivateId)
|
||||
|
||||
updated, err := repo.SetConfiguration(ctx, newConf)
|
||||
updated, err := repo.SetConfiguration(ctx, o.GetPublicId(), newConf)
|
||||
assert.NoError(err)
|
||||
require.NotNil(updated)
|
||||
assert.NotSame(newConf, updated)
|
||||
@@ -123,11 +125,12 @@ func TestRepository_GetConfiguration(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
authMethodId := authMethod.GetPublicId()
|
||||
@@ -194,11 +197,12 @@ func TestRepository_SetConfiguration(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
authMethodId := authMethod.GetPublicId()
|
||||
@@ -285,7 +289,7 @@ func TestRepository_SetConfiguration(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, err := repo.SetConfiguration(context.Background(), tt.in)
|
||||
got, err := repo.SetConfiguration(context.Background(), o.GetPublicId(), tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got, "returned configuration")
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
@@ -29,7 +30,7 @@ type authAccount struct {
|
||||
// Authenticate will update the stored values for password to the current
|
||||
// password settings for authMethodId if authentication is successful and
|
||||
// the stored values are not using the current password settings.
|
||||
func (r *Repository) Authenticate(ctx context.Context, authMethodId string, loginName string, password string) (*Account, error) {
|
||||
func (r *Repository) Authenticate(ctx context.Context, scopeId, authMethodId, loginName, password string) (*Account, error) {
|
||||
if authMethodId == "" {
|
||||
return nil, fmt.Errorf("password authenticate: no authMethodId: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
@@ -39,8 +40,16 @@ func (r *Repository) Authenticate(ctx context.Context, authMethodId string, logi
|
||||
if password == "" {
|
||||
return nil, fmt.Errorf("password authenticate: no password: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("password authenticate: no scopeId: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
acct, err := r.authenticate(ctx, authMethodId, loginName, password)
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("password authenticate: unable to get database wrapper: %w", err)
|
||||
}
|
||||
|
||||
acct, err := r.authenticate(ctx, scopeId, authMethodId, loginName, password)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("password authenticate: %w", err)
|
||||
}
|
||||
@@ -58,9 +67,14 @@ func (r *Repository) Authenticate(ctx context.Context, authMethodId string, logi
|
||||
return acct.Account, fmt.Errorf("password authenticate: update credential: %w", err)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return acct.Account, fmt.Errorf("password authenticate: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
// do not change the Credential Id
|
||||
cred.PrivateId = acct.CredentialId
|
||||
if err := cred.encrypt(ctx, r.wrapper); err != nil {
|
||||
if err := cred.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return acct.Account, fmt.Errorf("password authenticate: update credential: encrypt: %w", err)
|
||||
}
|
||||
|
||||
@@ -69,7 +83,7 @@ func (r *Repository) Authenticate(ctx context.Context, authMethodId string, logi
|
||||
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
rowsUpdated, err := w.Update(ctx, cred, fields, nil, db.WithOplog(r.wrapper, metadata))
|
||||
rowsUpdated, err := w.Update(ctx, cred, fields, nil, db.WithOplog(oplogWrapper, metadata))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
@@ -89,7 +103,7 @@ func (r *Repository) Authenticate(ctx context.Context, authMethodId string, logi
|
||||
//
|
||||
// Returns nil if old does not match the stored password for accountId.
|
||||
// Returns ErrPasswordsEqual if old and new are equal.
|
||||
func (r *Repository) ChangePassword(ctx context.Context, accountId string, old, new string, version uint32) (*Account, error) {
|
||||
func (r *Repository) ChangePassword(ctx context.Context, scopeId, accountId, old, new string, version uint32) (*Account, error) {
|
||||
if accountId == "" {
|
||||
return nil, fmt.Errorf("change password: no account id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
@@ -105,6 +119,9 @@ func (r *Repository) ChangePassword(ctx context.Context, accountId string, old,
|
||||
if version == 0 {
|
||||
return nil, fmt.Errorf("change password: no version supplied: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("change password: no scopeId: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
authAccount, err := r.LookupAccount(ctx, accountId)
|
||||
if err != nil {
|
||||
@@ -114,7 +131,16 @@ func (r *Repository) ChangePassword(ctx context.Context, accountId string, old,
|
||||
return nil, fmt.Errorf("change password: lookup account: account not found: %w", db.ErrRecordNotFound)
|
||||
}
|
||||
|
||||
acct, err := r.authenticate(ctx, authAccount.GetAuthMethodId(), authAccount.GetLoginName(), old)
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("change password: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("change password: unable to get database wrapper: %w", err)
|
||||
}
|
||||
|
||||
acct, err := r.authenticate(ctx, scopeId, authAccount.GetAuthMethodId(), authAccount.GetLoginName(), old)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("change password: %w", err)
|
||||
}
|
||||
@@ -134,7 +160,7 @@ func (r *Repository) ChangePassword(ctx context.Context, accountId string, old,
|
||||
return nil, fmt.Errorf("change password: %w", err)
|
||||
}
|
||||
|
||||
if err := newCred.encrypt(ctx, r.wrapper); err != nil {
|
||||
if err := newCred.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, fmt.Errorf("change password: encrypt: %w", err)
|
||||
}
|
||||
|
||||
@@ -146,7 +172,7 @@ func (r *Repository) ChangePassword(ctx context.Context, accountId string, old,
|
||||
updatedAccount = allocAccount()
|
||||
updatedAccount.PublicId = accountId
|
||||
updatedAccount.Version = version + 1
|
||||
rowsUpdated, err := w.Update(ctx, updatedAccount, []string{"Version"}, nil, db.WithOplog(r.wrapper, acct.Account.oplog(oplog.OpType_OP_TYPE_UPDATE)), db.WithVersion(&version))
|
||||
rowsUpdated, err := w.Update(ctx, updatedAccount, []string{"Version"}, nil, db.WithOplog(oplogWrapper, acct.Account.oplog(oplog.OpType_OP_TYPE_UPDATE)), db.WithVersion(&version))
|
||||
if err != nil {
|
||||
return fmt.Errorf("change password: unable to update account version: %w", err)
|
||||
}
|
||||
@@ -154,14 +180,14 @@ func (r *Repository) ChangePassword(ctx context.Context, accountId string, old,
|
||||
return fmt.Errorf("change password: updated account and %d rows updated", rowsUpdated)
|
||||
}
|
||||
|
||||
rowsDeleted, err := w.Delete(ctx, oldCred, db.WithOplog(r.wrapper, oldCred.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
rowsDeleted, err := w.Delete(ctx, oldCred, db.WithOplog(oplogWrapper, oldCred.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Create(ctx, newCred, db.WithOplog(r.wrapper, newCred.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
return w.Create(ctx, newCred, db.WithOplog(oplogWrapper, newCred.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
@@ -173,7 +199,7 @@ func (r *Repository) ChangePassword(ctx context.Context, accountId string, old,
|
||||
return updatedAccount, nil
|
||||
}
|
||||
|
||||
func (r *Repository) authenticate(ctx context.Context, authMethodId string, loginName string, password string) (*authAccount, error) {
|
||||
func (r *Repository) authenticate(ctx context.Context, scopeId, authMethodId, loginName, password string) (*authAccount, error) {
|
||||
var accts []authAccount
|
||||
|
||||
tx, err := r.reader.DB()
|
||||
@@ -204,7 +230,13 @@ func (r *Repository) authenticate(ctx context.Context, authMethodId string, logi
|
||||
acct = accts[0]
|
||||
}
|
||||
|
||||
if err := acct.decrypt(ctx, r.wrapper); err != nil {
|
||||
// We don't pass a wrapper in here because for ecryption we want to indicate the expected key ID
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase, kms.WithKeyId(acct.GetKeyId()))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get database wrapper: %w", err)
|
||||
}
|
||||
|
||||
if err := acct.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, fmt.Errorf("cannot decrypt credential: %w", err)
|
||||
}
|
||||
|
||||
@@ -218,13 +250,25 @@ func (r *Repository) authenticate(ctx context.Context, authMethodId string, logi
|
||||
|
||||
// SetPassword sets the password for accountId to password. If password
|
||||
// contains an empty string, the password for accountId will be deleted.
|
||||
func (r *Repository) SetPassword(ctx context.Context, accountId string, password string, version uint32) (*Account, error) {
|
||||
func (r *Repository) SetPassword(ctx context.Context, scopeId, accountId, password string, version uint32) (*Account, error) {
|
||||
if accountId == "" {
|
||||
return nil, fmt.Errorf("set password: no accountId: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, fmt.Errorf("set password: no version supplied: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("set password: no scopeId: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("set password: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("set password: unable to get database wrapper: %w", err)
|
||||
}
|
||||
|
||||
var newCred *Argon2Credential
|
||||
if password != "" {
|
||||
@@ -242,18 +286,18 @@ func (r *Repository) SetPassword(ctx context.Context, accountId string, password
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("set password: %w", err)
|
||||
}
|
||||
if err := newCred.encrypt(ctx, r.wrapper); err != nil {
|
||||
if err := newCred.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, fmt.Errorf("set password: encrypt: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var acct *Account
|
||||
_, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(rr db.Reader, w db.Writer) error {
|
||||
updatedAccount := allocAccount()
|
||||
updatedAccount.PublicId = accountId
|
||||
updatedAccount.Version = version + 1
|
||||
rowsUpdated, err := w.Update(ctx, updatedAccount, []string{"Version"}, nil, db.WithOplog(r.wrapper, updatedAccount.oplog(oplog.OpType_OP_TYPE_UPDATE)), db.WithVersion(&version))
|
||||
rowsUpdated, err := w.Update(ctx, updatedAccount, []string{"Version"}, nil, db.WithOplog(oplogWrapper, updatedAccount.oplog(oplog.OpType_OP_TYPE_UPDATE)), db.WithVersion(&version))
|
||||
if err != nil {
|
||||
return fmt.Errorf("set password: unable to update account version: %w", err)
|
||||
}
|
||||
@@ -270,7 +314,7 @@ func (r *Repository) SetPassword(ctx context.Context, accountId string, password
|
||||
}
|
||||
if oldCred.PrivateId != "" {
|
||||
dCred := oldCred.clone()
|
||||
rowsDeleted, err := w.Delete(ctx, dCred, db.WithOplog(r.wrapper, oldCred.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
rowsDeleted, err := w.Delete(ctx, dCred, db.WithOplog(oplogWrapper, oldCred.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
@@ -279,7 +323,7 @@ func (r *Repository) SetPassword(ctx context.Context, accountId string, password
|
||||
}
|
||||
}
|
||||
if newCred != nil {
|
||||
return w.Create(ctx, newCred, db.WithOplog(r.wrapper, newCred.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
return w.Create(ctx, newCred, db.WithOplog(oplogWrapper, newCred.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/password/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"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"
|
||||
@@ -18,8 +19,8 @@ func TestRepository_Authenticate(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
|
||||
@@ -31,10 +32,10 @@ func TestRepository_Authenticate(t *testing.T) {
|
||||
}
|
||||
passwd := "12345678"
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
outAcct, err := repo.CreateAccount(context.Background(), inAcct, WithPassword(passwd))
|
||||
outAcct, err := repo.CreateAccount(context.Background(), o.GetPublicId(), inAcct, WithPassword(passwd))
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, outAcct)
|
||||
|
||||
@@ -101,7 +102,7 @@ func TestRepository_Authenticate(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
authAcct, err := repo.Authenticate(context.Background(), tt.args.authMethodId, tt.args.loginName, tt.args.password)
|
||||
authAcct, err := repo.Authenticate(context.Background(), o.GetPublicId(), tt.args.authMethodId, tt.args.loginName, tt.args.password)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(authAcct, "returned account")
|
||||
@@ -128,8 +129,8 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
kmsCache := kms.TestKms(t, conn, wrapper)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 1)
|
||||
authMethod := authMethods[0]
|
||||
authMethodId := authMethod.GetPublicId()
|
||||
@@ -137,7 +138,7 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
passwd := "12345678"
|
||||
ctx := context.Background()
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kmsCache)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
@@ -158,7 +159,7 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
origAcct, err := repo.CreateAccount(ctx, inAcct, WithPassword(passwd))
|
||||
origAcct, err := repo.CreateAccount(ctx, o.GetPublicId(), inAcct, WithPassword(passwd))
|
||||
require.NoError(err)
|
||||
require.NotNil(origAcct)
|
||||
assert.NotEmpty(origAcct.PublicId)
|
||||
@@ -173,7 +174,7 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
origCredId := origCred.PrivateId
|
||||
|
||||
// Authenticate and verify the credential ID
|
||||
authAcct, err := repo.Authenticate(ctx, authMethodId, loginName, passwd)
|
||||
authAcct, err := repo.Authenticate(ctx, o.GetPublicId(), authMethodId, loginName, passwd)
|
||||
require.NoError(err)
|
||||
require.NotNil(authAcct, "auth account")
|
||||
assert.Equal(origAcct.PublicId, authAcct.PublicId)
|
||||
@@ -199,7 +200,7 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
inArgonConf := origArgonConf.clone()
|
||||
inArgonConf.Threads = origArgonConf.Threads + 1
|
||||
|
||||
upConf, err := repo.SetConfiguration(ctx, inArgonConf)
|
||||
upConf, err := repo.SetConfiguration(ctx, o.GetPublicId(), inArgonConf)
|
||||
require.NoError(err)
|
||||
require.NotNil(upConf)
|
||||
assert.NotSame(inArgonConf, upConf)
|
||||
@@ -209,7 +210,7 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
assert.NotEqual(origConfId, upArgonConf.PrivateId)
|
||||
|
||||
// Authenticate and verify the credential ID has not changed
|
||||
auth2Acct, err := repo.Authenticate(ctx, authMethodId, loginName, passwd)
|
||||
auth2Acct, err := repo.Authenticate(ctx, o.GetPublicId(), authMethodId, loginName, passwd)
|
||||
require.NoError(err)
|
||||
require.NotNil(auth2Acct, "auth2 account")
|
||||
assert.Equal(origAcct.PublicId, auth2Acct.PublicId)
|
||||
@@ -230,7 +231,9 @@ func TestRepository_AuthenticateRehash(t *testing.T) {
|
||||
assert.Equal(origCred.CreateTime, auth2Cred.CreateTime, "the create time should not change")
|
||||
|
||||
// Verify fields that should change
|
||||
require.NoError(auth2Cred.decrypt(ctx, wrapper))
|
||||
decryptWrapper, err := kmsCache.GetWrapper(ctx, o.GetPublicId(), kms.KeyPurposeDatabase, kms.WithKeyId(auth2Cred.GetKeyId()))
|
||||
require.NoError(err)
|
||||
require.NoError(auth2Cred.decrypt(ctx, decryptWrapper))
|
||||
assert.NotEqual(origCred.UpdateTime, auth2Cred.UpdateTime, "the update time should be different")
|
||||
assert.NotEqual(origCred.PasswordConfId, auth2Cred.PasswordConfId, "the configuration Id should be different")
|
||||
assert.NotEqual(origCred.Salt, auth2Cred.Salt, "a new salt value should be generated")
|
||||
@@ -243,12 +246,13 @@ func TestRepository_ChangePassword(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethod := TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
|
||||
|
||||
inAcct := &Account{
|
||||
@@ -259,7 +263,7 @@ func TestRepository_ChangePassword(t *testing.T) {
|
||||
}
|
||||
passwd := "12345678"
|
||||
|
||||
acct, err := repo.CreateAccount(context.Background(), inAcct, WithPassword(passwd))
|
||||
acct, err := repo.CreateAccount(context.Background(), o.GetPublicId(), inAcct, WithPassword(passwd))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, acct)
|
||||
|
||||
@@ -356,7 +360,7 @@ func TestRepository_ChangePassword(t *testing.T) {
|
||||
|
||||
// Calls to Authenticate should always succeed in these tests
|
||||
authFn := func(pwd string, name string) *Account {
|
||||
acct, err := repo.Authenticate(context.Background(), inAcct.AuthMethodId, inAcct.LoginName, pwd)
|
||||
acct, err := repo.Authenticate(context.Background(), o.GetPublicId(), inAcct.AuthMethodId, inAcct.LoginName, pwd)
|
||||
require.NotNilf(acct, "%s: Authenticate should return an account", name)
|
||||
require.NoErrorf(err, "%s: Authenticate should succeed", name)
|
||||
return acct
|
||||
@@ -364,7 +368,7 @@ func TestRepository_ChangePassword(t *testing.T) {
|
||||
// authenticate with original password
|
||||
authAcct1 := authFn(passwd, "original account")
|
||||
|
||||
chgAuthAcct, err := repo.ChangePassword(context.Background(), tt.args.acctId, tt.args.old, tt.args.new, authAcct1.Version)
|
||||
chgAuthAcct, err := repo.ChangePassword(context.Background(), o.GetPublicId(), tt.args.acctId, tt.args.old, tt.args.new, authAcct1.Version)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Error(err)
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
@@ -399,12 +403,13 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
authMethod := TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
|
||||
|
||||
origPasswd := "12345678"
|
||||
@@ -421,7 +426,7 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
if pw != "" {
|
||||
opts = append(opts, WithPassword(origPasswd))
|
||||
}
|
||||
acct, err := repo.CreateAccount(context.Background(), inAcct, opts...)
|
||||
acct, err := repo.CreateAccount(context.Background(), o.GetPublicId(), inAcct, opts...)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, acct)
|
||||
return acct
|
||||
@@ -429,7 +434,7 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
}
|
||||
|
||||
wantAuthenticate := func(t *testing.T, ln, pw, msg string) string {
|
||||
acct, err := repo.Authenticate(context.Background(), authMethod.PublicId, ln, pw)
|
||||
acct, err := repo.Authenticate(context.Background(), o.GetPublicId(), authMethod.PublicId, ln, pw)
|
||||
assert.NoErrorf(t, err, "%s: authenticate should not return an error", msg)
|
||||
if assert.NotNilf(t, acct, "%s: authenticate should succeed", msg) {
|
||||
return acct.CredentialId
|
||||
@@ -437,7 +442,7 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
return ""
|
||||
}
|
||||
wantNoAuthenticate := func(t *testing.T, ln, pw, msg string) string {
|
||||
acct, err := repo.Authenticate(context.Background(), authMethod.PublicId, ln, pw)
|
||||
acct, err := repo.Authenticate(context.Background(), o.GetPublicId(), authMethod.PublicId, ln, pw)
|
||||
assert.NoErrorf(t, err, "%s: authenticate should not return an error", msg)
|
||||
assert.Nilf(t, acct, "%s: authenticate should not succeed", msg)
|
||||
return ""
|
||||
@@ -480,7 +485,7 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
}
|
||||
oldCredId := nextAuth(t, acct.LoginName, origPasswd, "after create account")
|
||||
|
||||
acct, err = repo.SetPassword(context.Background(), acct.PublicId, tt.newPw, acct.Version)
|
||||
acct, err = repo.SetPassword(context.Background(), o.GetPublicId(), acct.PublicId, tt.newPw, acct.Version)
|
||||
require.NoError(err)
|
||||
|
||||
if oldCredId != "" {
|
||||
@@ -527,7 +532,7 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
acct, err := repo.SetPassword(context.Background(), tt.accountId, tt.pw, tt.version)
|
||||
acct, err := repo.SetPassword(context.Background(), o.GetPublicId(), tt.accountId, tt.pw, tt.version)
|
||||
assert.Error(err)
|
||||
assert.Truef(errors.Is(err, tt.wantError), "want err: %q got: %q", tt.wantError, err)
|
||||
assert.Nil(acct)
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -15,12 +15,13 @@ func TestRepository_New(t *testing.T) {
|
||||
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kmsCache := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
type args struct {
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
opts []Option
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
kms *kms.Kms
|
||||
opts []Option
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
@@ -32,38 +33,38 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "valid",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: kmsCache,
|
||||
defaultLimit: db.DefaultLimit,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid with limit",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
opts: []Option{WithLimit(5)},
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
opts: []Option{WithLimit(5)},
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: kmsCache,
|
||||
defaultLimit: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nil-reader",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: nil,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -71,9 +72,9 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "nil-writer",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: nil,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: nil,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -81,9 +82,9 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "nil-wrapper",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: nil,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -91,9 +92,9 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "all-nils",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: nil,
|
||||
wrapper: nil,
|
||||
r: nil,
|
||||
w: nil,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -103,7 +104,7 @@ func TestRepository_New(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.wrapper, tt.args.opts...)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.kms, tt.args.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
|
||||
@@ -187,6 +187,11 @@ type Argon2Credential struct {
|
||||
// derivation function.
|
||||
// @inject_tag: `gorm:"not_null"`
|
||||
DerivedKey []byte `protobuf:"bytes,9,opt,name=derived_key,json=derivedKey,proto3" json:"derived_key,omitempty" gorm:"not_null"`
|
||||
// key_id is the key ID that was used for the encryption operation. It can be
|
||||
// used to identify a specific version of the key needed to decrypt the value,
|
||||
// which is useful for caching purposes.
|
||||
// @inject_tag: `gorm:"not_null"`
|
||||
KeyId string `protobuf:"bytes,10,opt,name=key_id,json=keyId,proto3" json:"key_id,omitempty" gorm:"not_null"`
|
||||
}
|
||||
|
||||
func (x *Argon2Credential) Reset() {
|
||||
@@ -284,6 +289,13 @@ func (x *Argon2Credential) GetDerivedKey() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Argon2Credential) GetKeyId() string {
|
||||
if x != nil {
|
||||
return x.KeyId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_controller_storage_auth_password_store_v1_argon2_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_controller_storage_auth_password_store_v1_argon2_proto_rawDesc = []byte{
|
||||
@@ -316,7 +328,7 @@ var file_controller_storage_auth_password_store_v1_argon2_proto_rawDesc = []byte
|
||||
0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x73,
|
||||
0x61, 0x6c, 0x74, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x12, 0x1d, 0x0a, 0x0a, 0x6b, 0x65, 0x79,
|
||||
0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x6b,
|
||||
0x65, 0x79, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x22, 0xa1, 0x03, 0x0a, 0x10, 0x41, 0x72, 0x67,
|
||||
0x65, 0x79, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x22, 0xb8, 0x03, 0x0a, 0x10, 0x41, 0x72, 0x67,
|
||||
0x6f, 0x6e, 0x32, 0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x12, 0x1d, 0x0a,
|
||||
0x0a, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x09, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x49, 0x64, 0x12, 0x4b, 0x0a, 0x0b,
|
||||
@@ -342,12 +354,13 @@ var file_controller_storage_auth_password_store_v1_argon2_proto_rawDesc = []byte
|
||||
0x06, 0x63, 0x74, 0x53, 0x61, 0x6c, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x61, 0x6c, 0x74, 0x18,
|
||||
0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x73, 0x61, 0x6c, 0x74, 0x12, 0x1f, 0x0a, 0x0b, 0x64,
|
||||
0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x5f, 0x6b, 0x65, 0x79, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0c,
|
||||
0x52, 0x0a, 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x4b, 0x65, 0x79, 0x42, 0x42, 0x5a, 0x40,
|
||||
0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69,
|
||||
0x63, 0x6f, 0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e,
|
||||
0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2f, 0x70, 0x61, 0x73, 0x73,
|
||||
0x77, 0x6f, 0x72, 0x64, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65,
|
||||
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x52, 0x0a, 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x4b, 0x65, 0x79, 0x12, 0x15, 0x0a, 0x06,
|
||||
0x6b, 0x65, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6b, 0x65,
|
||||
0x79, 0x49, 0x64, 0x42, 0x42, 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
|
||||
0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e,
|
||||
0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x61, 0x75,
|
||||
0x74, 0x68, 0x2f, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x2f, 0x73, 0x74, 0x6f, 0x72,
|
||||
0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -12,7 +12,8 @@ import (
|
||||
func Test_TestAuthMethods(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
require.NotNil(org)
|
||||
assert.NotEmpty(org.GetPublicId())
|
||||
|
||||
@@ -28,7 +29,8 @@ func Test_TestAccounts(t *testing.T) {
|
||||
t.Helper()
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
require.NotNil(org)
|
||||
assert.NotEmpty(org.GetPublicId())
|
||||
|
||||
@@ -59,6 +59,7 @@ func (s *writableAuthToken) encrypt(ctx context.Context, cipher wrapping.Wrapper
|
||||
if err := structwrapping.WrapStruct(ctx, cipher, s.AuthToken, nil); err != nil {
|
||||
return fmt.Errorf("error encrypting auth token: %w", err)
|
||||
}
|
||||
s.KeyId = cipher.KeyID()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
@@ -23,8 +24,10 @@ import (
|
||||
func TestAuthToken_DbUpdate(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
am := password.TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
acct := password.TestAccounts(t, conn, am.GetPublicId(), 1)[0]
|
||||
|
||||
@@ -94,8 +97,8 @@ func TestAuthToken_DbUpdate(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
w := db.New(conn)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
authTok := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
authTok := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
proto.Merge(authTok.AuthToken, tt.args.authTok)
|
||||
|
||||
wAuthToken := authTok.toWritableAuthToken()
|
||||
@@ -116,11 +119,11 @@ func TestAuthToken_DbUpdate(t *testing.T) {
|
||||
func TestAuthToken_DbCreate(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
am := password.TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
acct := password.TestAccounts(t, conn, am.GetPublicId(), 1)[0]
|
||||
createdAuthToken := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
createdAuthToken := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
|
||||
testAuthTokenId := func() string {
|
||||
id, err := newAuthTokenId()
|
||||
@@ -178,8 +181,9 @@ func TestAuthToken_DbDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
wrapper := db.TestWrapper(t)
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
existingAuthTok := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
existingAuthTok := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
@@ -20,9 +21,10 @@ func TestAuthToken_ImmutableFields(t *testing.T) {
|
||||
w := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
new := TestAuthToken(t, conn, wrapper, org.PublicId)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, repo)
|
||||
new := TestAuthToken(t, conn, kms, org.PublicId)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
|
||||
@@ -10,8 +10,9 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/authtoken/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"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"
|
||||
)
|
||||
|
||||
// TODO (ICU-406): Make these fields configurable.
|
||||
@@ -24,23 +25,23 @@ var (
|
||||
// A Repository stores and retrieves the persistent types in the authtoken
|
||||
// package. It is not safe to use a repository concurrently.
|
||||
type Repository struct {
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
kms *kms.Kms
|
||||
// defaultLimit provides a default for limiting the number of results returned from the repo
|
||||
defaultLimit int
|
||||
}
|
||||
|
||||
// NewRepository creates a new Repository. The returned repository is not safe for concurrent go
|
||||
// routines to access it.
|
||||
func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Option) (*Repository, error) {
|
||||
func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, fmt.Errorf("db.Reader: auth token: %w", db.ErrNilParameter)
|
||||
case w == nil:
|
||||
return nil, fmt.Errorf("db.Writer: auth token: %w", db.ErrNilParameter)
|
||||
case wrapper == nil:
|
||||
return nil, fmt.Errorf("wrapping.Wrapper: auth token: %w", db.ErrNilParameter)
|
||||
case kms == nil:
|
||||
return nil, fmt.Errorf("kms: auth token: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -51,7 +52,7 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Op
|
||||
return &Repository{
|
||||
reader: r,
|
||||
writer: w,
|
||||
wrapper: wrapper,
|
||||
kms: kms,
|
||||
defaultLimit: opts.withLimit,
|
||||
}, nil
|
||||
}
|
||||
@@ -59,8 +60,11 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Op
|
||||
// CreateAuthToken inserts an Auth Token into the repository and returns a new Auth Token. The returned auth token
|
||||
// contains the auth token value. The provided IAM User ID must be associated to the provided auth account id
|
||||
// or an error will be returned. All options are ignored.
|
||||
func (r *Repository) CreateAuthToken(ctx context.Context, withIamUserId, withAuthAccountId string, opt ...Option) (*AuthToken, error) {
|
||||
if withIamUserId == "" {
|
||||
func (r *Repository) CreateAuthToken(ctx context.Context, withIamUser *iam.User, withAuthAccountId string, opt ...Option) (*AuthToken, error) {
|
||||
if withIamUser == nil {
|
||||
return nil, fmt.Errorf("create: auth token: no user: %w", db.ErrNilParameter)
|
||||
}
|
||||
if withIamUser.GetPublicId() == "" {
|
||||
return nil, fmt.Errorf("create: auth token: no user id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if withAuthAccountId == "" {
|
||||
@@ -82,6 +86,15 @@ func (r *Repository) CreateAuthToken(ctx context.Context, withIamUserId, withAut
|
||||
}
|
||||
at.Token = token
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, withIamUser.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, withIamUser.GetScopeId(), kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: unable to get database wrapper: %w", err)
|
||||
}
|
||||
|
||||
// TODO: Allow the caller to specify something different than the default duration.
|
||||
// We truncate the expiration time to the nearest second to make testing in different platforms with
|
||||
// different time resolutions easier.
|
||||
@@ -102,8 +115,8 @@ func (r *Repository) CreateAuthToken(ctx context.Context, withIamUserId, withAut
|
||||
if err := read.LookupByPublicId(ctx, acct); err != nil {
|
||||
return fmt.Errorf("create: auth token: auth account lookup: %w", err)
|
||||
}
|
||||
if acct.GetIamUserId() != withIamUserId {
|
||||
return fmt.Errorf("create: auth token: auth account %q mismatch with iam user %q", withAuthAccountId, withIamUserId)
|
||||
if acct.GetIamUserId() != withIamUser.GetPublicId() {
|
||||
return fmt.Errorf("create: auth token: auth account %q mismatch with iam user %q", withAuthAccountId, withIamUser.GetPublicId())
|
||||
}
|
||||
at.ScopeId = acct.GetScopeId()
|
||||
at.AuthMethodId = acct.GetAuthMethodId()
|
||||
@@ -111,10 +124,10 @@ func (r *Repository) CreateAuthToken(ctx context.Context, withIamUserId, withAut
|
||||
|
||||
metadata := newAuthTokenMetadata(at, oplog.OpType_OP_TYPE_CREATE)
|
||||
newAuthToken = at.toWritableAuthToken()
|
||||
if err := newAuthToken.encrypt(ctx, r.wrapper); err != nil {
|
||||
if err := newAuthToken.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.Create(ctx, newAuthToken, db.WithOplog(r.wrapper, metadata)); err != nil {
|
||||
if err := w.Create(ctx, newAuthToken, db.WithOplog(oplogWrapper, metadata)); err != nil {
|
||||
return err
|
||||
}
|
||||
newAuthToken.CtToken = nil
|
||||
@@ -147,12 +160,17 @@ func (r *Repository) LookupAuthToken(ctx context.Context, id string, opt ...Opti
|
||||
return nil, fmt.Errorf("auth token: lookup: %w", err)
|
||||
}
|
||||
if opts.withTokenValue {
|
||||
if err := at.decrypt(ctx, r.wrapper); err != nil {
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, at.GetScopeId(), kms.KeyPurposeDatabase, kms.WithKeyId(at.GetKeyId()))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lookup: unable to get database wrapper: %w", err)
|
||||
}
|
||||
if err := at.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, fmt.Errorf("lookup: auth token: cannot decrypt auth token value: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
at.CtToken = nil
|
||||
at.KeyId = ""
|
||||
return at, nil
|
||||
}
|
||||
|
||||
@@ -193,6 +211,11 @@ func (r *Repository) ValidateToken(ctx context.Context, id, token string, opt ..
|
||||
return nil, fmt.Errorf("validate token: last accessed time : %w", err)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, retAT.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("validate token: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
sinceLastAccessed := now.Sub(lastAccessed)
|
||||
if now.After(exp) || sinceLastAccessed > maxStaleness {
|
||||
@@ -204,7 +227,7 @@ func (r *Repository) ValidateToken(ctx context.Context, id, token string, opt ..
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
metadata := newAuthTokenMetadata(retAT, oplog.OpType_OP_TYPE_DELETE)
|
||||
delAt := retAT.toWritableAuthToken()
|
||||
if _, err := w.Delete(ctx, delAt, db.WithOplog(r.wrapper, metadata)); err != nil {
|
||||
if _, err := w.Delete(ctx, delAt, db.WithOplog(oplogWrapper, metadata)); err != nil {
|
||||
return fmt.Errorf("validate token: delete auth token: %w", err)
|
||||
}
|
||||
retAT = nil
|
||||
@@ -237,7 +260,7 @@ func (r *Repository) ValidateToken(ctx context.Context, id, token string, opt ..
|
||||
at,
|
||||
nil,
|
||||
[]string{"ApproximateLastAccessTime"},
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
@@ -271,6 +294,7 @@ func (r *Repository) ListAuthTokens(ctx context.Context, withOrgId string, opt .
|
||||
for _, at := range authTokens {
|
||||
at.Token = ""
|
||||
at.CtToken = nil
|
||||
at.KeyId = ""
|
||||
}
|
||||
return authTokens, nil
|
||||
}
|
||||
@@ -293,6 +317,11 @@ func (r *Repository) DeleteAuthToken(ctx context.Context, id string, opt ...Opti
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, at.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -301,7 +330,7 @@ func (r *Repository) DeleteAuthToken(ctx context.Context, id string, opt ...Opti
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
metadata := newAuthTokenMetadata(at, oplog.OpType_OP_TYPE_DELETE)
|
||||
deleteAT := at.toWritableAuthToken()
|
||||
rowsDeleted, err = w.Delete(ctx, deleteAT, db.WithOplog(r.wrapper, metadata))
|
||||
rowsDeleted, err = w.Delete(ctx, deleteAT, db.WithOplog(oplogWrapper, metadata))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
@@ -25,12 +25,13 @@ func TestRepository_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kmsCache := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
type args struct {
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
opts []Option
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
kms *kms.Kms
|
||||
opts []Option
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
@@ -42,38 +43,38 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "valid default limit",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: kmsCache,
|
||||
defaultLimit: db.DefaultLimit,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid new limit",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
opts: []Option{WithLimit(5)},
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
opts: []Option{WithLimit(5)},
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: kmsCache,
|
||||
defaultLimit: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nil-reader",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: nil,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -81,19 +82,19 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "nil-writer",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: nil,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: nil,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-wrapper",
|
||||
name: "nil-kms",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: nil,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -101,9 +102,9 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "all-nils",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: nil,
|
||||
wrapper: nil,
|
||||
r: nil,
|
||||
w: nil,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -113,7 +114,7 @@ func TestRepository_New(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.wrapper, tt.args.opts...)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.kms, tt.args.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
@@ -130,29 +131,31 @@ func TestRepository_CreateAuthToken(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
org1, _ := iam.TestScopes(t, conn)
|
||||
org1, _ := iam.TestScopes(t, repo)
|
||||
am := password.TestAuthMethods(t, conn, org1.GetPublicId(), 1)[0]
|
||||
aAcct := password.TestAccounts(t, conn, am.GetPublicId(), 1)[0]
|
||||
|
||||
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
|
||||
iamRepo, err := iam.NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
u1, err := iamRepo.LookupUserWithLogin(context.Background(), aAcct.GetPublicId(), iam.WithAutoVivify(true))
|
||||
require.NoError(t, err)
|
||||
|
||||
org2, _ := iam.TestScopes(t, conn)
|
||||
u2 := iam.TestUser(t, conn, org2.GetPublicId())
|
||||
org2, _ := iam.TestScopes(t, repo)
|
||||
u2 := iam.TestUser(t, repo, org2.GetPublicId())
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
iamUserId string
|
||||
iamUser *iam.User
|
||||
authAcctId string
|
||||
want *AuthToken
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid",
|
||||
iamUserId: u1.GetPublicId(),
|
||||
iamUser: u1,
|
||||
authAcctId: aAcct.GetPublicId(),
|
||||
want: &AuthToken{
|
||||
AuthToken: &store.AuthToken{
|
||||
@@ -162,14 +165,14 @@ func TestRepository_CreateAuthToken(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "unconnected-authaccount-user",
|
||||
iamUserId: u2.GetPublicId(),
|
||||
iamUser: u2,
|
||||
authAcctId: aAcct.GetPublicId(),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no-authacctid",
|
||||
iamUserId: u1.GetPublicId(),
|
||||
wantErr: true,
|
||||
name: "no-authacctid",
|
||||
iamUser: u1,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no-userid",
|
||||
@@ -178,13 +181,13 @@ func TestRepository_CreateAuthToken(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "invalid-authacctid",
|
||||
iamUserId: u1.GetPublicId(),
|
||||
iamUser: u1,
|
||||
authAcctId: "this_is_invalid",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid-userid",
|
||||
iamUserId: "this_is_invalid",
|
||||
iamUser: func() *iam.User { u := u1.Clone().(*iam.User); u.PublicId = "this_is_invalid"; return u }(),
|
||||
authAcctId: aAcct.GetPublicId(),
|
||||
wantErr: true,
|
||||
},
|
||||
@@ -193,16 +196,16 @@ func TestRepository_CreateAuthToken(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateAuthToken(context.Background(), tt.iamUserId, tt.authAcctId)
|
||||
got, err := repo.CreateAuthToken(context.Background(), tt.iamUser, tt.authAcctId)
|
||||
if tt.wantErr {
|
||||
assert.Error(err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
require.NoError(err, "Got error for CreateAuthToken(ctx, %v, %v)", tt.iamUserId, tt.authAcctId)
|
||||
require.NoError(err, "Got error for CreateAuthToken(ctx, %v, %v)", tt.iamUser, tt.authAcctId)
|
||||
assert.NotNil(got)
|
||||
db.AssertPublicId(t, AuthTokenPrefix, got.PublicId)
|
||||
assert.Equal(tt.authAcctId, got.GetAuthAccountId())
|
||||
@@ -217,11 +220,13 @@ func TestRepository_LookupAuthToken(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
at := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, repo)
|
||||
at := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
at.Token = ""
|
||||
at.CtToken = nil
|
||||
at.KeyId = ""
|
||||
|
||||
badId, err := newAuthTokenId()
|
||||
require.NoError(t, err)
|
||||
@@ -255,7 +260,7 @@ func TestRepository_LookupAuthToken(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
@@ -292,15 +297,18 @@ func TestRepository_ValidateToken(t *testing.T) {
|
||||
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
at := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
at := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
atToken := at.GetToken()
|
||||
at.Token = ""
|
||||
at.CtToken = nil
|
||||
at.KeyId = ""
|
||||
atTime, err := ptypes.Timestamp(at.GetApproximateLastAccessTime().GetTimestamp())
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, atTime)
|
||||
@@ -402,17 +410,21 @@ func TestRepository_ValidateToken_expired(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
baseAT := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
org, _ := iam.TestScopes(t, iamRepo)
|
||||
baseAT := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
baseAT.GetAuthAccountId()
|
||||
aAcct := allocAuthAccount()
|
||||
aAcct.PublicId = baseAT.GetAuthAccountId()
|
||||
require.NoError(t, rw.LookupByPublicId(context.Background(), aAcct))
|
||||
iamUserId := aAcct.GetIamUserId()
|
||||
iamUser, err := iamRepo.LookupUser(context.Background(), aAcct.GetIamUserId())
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, iamUser)
|
||||
|
||||
defaultStaleTime := maxStaleness
|
||||
defaultExpireDuration := maxTokenDuration
|
||||
@@ -451,7 +463,7 @@ func TestRepository_ValidateToken_expired(t *testing.T) {
|
||||
maxTokenDuration = tt.expirationDuration
|
||||
|
||||
ctx := context.Background()
|
||||
at, err := repo.CreateAuthToken(ctx, iamUserId, baseAT.GetAuthAccountId())
|
||||
at, err := repo.CreateAuthToken(ctx, iamUser, baseAT.GetAuthAccountId())
|
||||
require.NoError(err)
|
||||
|
||||
got, err := repo.ValidateToken(ctx, at.GetPublicId(), at.GetToken())
|
||||
@@ -475,9 +487,10 @@ func TestRepository_DeleteAuthToken(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
at := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, repo)
|
||||
at := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
badId, err := newAuthTokenId()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, badId)
|
||||
@@ -510,7 +523,7 @@ func TestRepository_DeleteAuthToken(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
@@ -532,16 +545,20 @@ func TestRepository_ListAuthTokens(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
at1 := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo := iam.TestRepo(t, conn, wrapper)
|
||||
org, _ := iam.TestScopes(t, repo)
|
||||
at1 := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
at1.Token = ""
|
||||
at2 := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
at1.KeyId = ""
|
||||
at2 := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
at2.Token = ""
|
||||
at3 := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
at2.KeyId = ""
|
||||
at3 := TestAuthToken(t, conn, kms, org.GetPublicId())
|
||||
at3.Token = ""
|
||||
at3.KeyId = ""
|
||||
|
||||
emptyOrg, _ := iam.TestScopes(t, conn)
|
||||
emptyOrg, _ := iam.TestScopes(t, repo)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -571,7 +588,7 @@ func TestRepository_ListAuthTokens(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
|
||||
@@ -68,6 +68,11 @@ type AuthToken struct {
|
||||
// iam_user_id is not stored in the backing DB but it derived from the linked to auth account.
|
||||
// @inject_tag: gorm:"-"
|
||||
IamUserId string `protobuf:"bytes,13,opt,name=iam_user_id,json=iamUserId,proto3" json:"iam_user_id,omitempty" gorm:"-"`
|
||||
// key_id is the key ID that was used for the encryption operation. It can be
|
||||
// used to identify a specific version of the key needed to decrypt the value,
|
||||
// which is useful for caching purposes.
|
||||
// @inject_tag: `gorm:"not_null"`
|
||||
KeyId string `protobuf:"bytes,14,opt,name=key_id,json=keyId,proto3" json:"key_id,omitempty" gorm:"not_null"`
|
||||
}
|
||||
|
||||
func (x *AuthToken) Reset() {
|
||||
@@ -179,6 +184,13 @@ func (x *AuthToken) GetIamUserId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *AuthToken) GetKeyId() string {
|
||||
if x != nil {
|
||||
return x.KeyId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_controller_storage_authtoken_store_v1_authtoken_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_controller_storage_authtoken_store_v1_authtoken_proto_rawDesc = []byte{
|
||||
@@ -191,7 +203,7 @@ var file_controller_storage_authtoken_store_v1_authtoken_proto_rawDesc = []byte{
|
||||
0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x61,
|
||||
0x67, 0x65, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2f, 0x76, 0x31, 0x2f,
|
||||
0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22,
|
||||
0xbe, 0x04, 0x0a, 0x09, 0x41, 0x75, 0x74, 0x68, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1b, 0x0a,
|
||||
0xd5, 0x04, 0x0a, 0x09, 0x41, 0x75, 0x74, 0x68, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1b, 0x0a,
|
||||
0x09, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x08, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x49, 0x64, 0x12, 0x4b, 0x0a, 0x0b, 0x63, 0x72,
|
||||
0x65, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32,
|
||||
@@ -227,11 +239,12 @@ var file_controller_storage_authtoken_store_v1_authtoken_proto_rawDesc = []byte{
|
||||
0x28, 0x09, 0x52, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x49, 0x64,
|
||||
0x12, 0x1e, 0x0a, 0x0b, 0x69, 0x61, 0x6d, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18,
|
||||
0x0d, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x69, 0x61, 0x6d, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64,
|
||||
0x42, 0x3e, 0x5a, 0x3c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68,
|
||||
0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
|
||||
0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x74,
|
||||
0x6f, 0x6b, 0x65, 0x6e, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65,
|
||||
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x12, 0x15, 0x0a, 0x06, 0x6b, 0x65, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x05, 0x6b, 0x65, 0x79, 0x49, 0x64, 0x42, 0x3e, 0x5a, 0x3c, 0x67, 0x69, 0x74, 0x68, 0x75,
|
||||
0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f,
|
||||
0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61,
|
||||
0x6c, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x2f, 0x73, 0x74, 0x6f, 0x72,
|
||||
0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAuthToken(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, scopeId string) *AuthToken {
|
||||
func TestAuthToken(t *testing.T, conn *gorm.DB, kms *kms.Kms, scopeId string) *AuthToken {
|
||||
t.Helper()
|
||||
authMethod := password.TestAuthMethods(t, conn, scopeId, 1)[0]
|
||||
// auth account is only used to join auth method to user.
|
||||
@@ -21,16 +21,16 @@ func TestAuthToken(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, scopeI
|
||||
|
||||
ctx := context.Background()
|
||||
rw := db.New(conn)
|
||||
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
|
||||
iamRepo, err := iam.NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err := iamRepo.LookupUserWithLogin(ctx, acct.GetPublicId(), iam.WithAutoVivify(true))
|
||||
require.NoError(t, err)
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
|
||||
at, err := repo.CreateAuthToken(ctx, u.GetPublicId(), acct.GetPublicId())
|
||||
at, err := repo.CreateAuthToken(ctx, u, acct.GetPublicId())
|
||||
require.NoError(t, err)
|
||||
return at
|
||||
}
|
||||
@@ -18,12 +18,14 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/version"
|
||||
"github.com/hashicorp/errwrap"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
"github.com/hashicorp/shared-secure-libs/configutil"
|
||||
"github.com/hashicorp/shared-secure-libs/gatedwriter"
|
||||
"github.com/hashicorp/shared-secure-libs/reloadutil"
|
||||
@@ -49,8 +51,9 @@ type Server struct {
|
||||
CombineLogs bool
|
||||
LogLevel hclog.Level
|
||||
|
||||
ControllerKMS wrapping.Wrapper
|
||||
WorkerAuthKMS wrapping.Wrapper
|
||||
RootKms wrapping.Wrapper
|
||||
WorkerAuthKms wrapping.Wrapper
|
||||
Kms *kms.Kms
|
||||
SecureRandomReader io.Reader
|
||||
|
||||
InmemSink *metrics.InmemSink
|
||||
@@ -347,9 +350,9 @@ func (b *Server) SetupKMSes(ui cli.Ui, config *configutil.SharedConfig, purposes
|
||||
}
|
||||
|
||||
if purpose == "root" {
|
||||
b.ControllerKMS = wrapper
|
||||
b.RootKms = wrapper
|
||||
} else {
|
||||
b.WorkerAuthKMS = wrapper
|
||||
b.WorkerAuthKms = wrapper
|
||||
}
|
||||
|
||||
// Ensure that the seal finalizer is called, even if using verify-only
|
||||
@@ -365,7 +368,7 @@ func (b *Server) SetupKMSes(ui cli.Ui, config *configutil.SharedConfig, purposes
|
||||
|
||||
// prepare a secure random reader
|
||||
var err error
|
||||
b.SecureRandomReader, err = configutil.CreateSecureRandomReaderFunc(config, b.ControllerKMS)
|
||||
b.SecureRandomReader, err = configutil.CreateSecureRandomReaderFunc(config, b.RootKms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -433,7 +436,23 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
b.Database.LogMode(true)
|
||||
|
||||
rw := db.New(b.Database)
|
||||
repo, err := iam.NewRepository(rw, rw, b.ControllerKMS)
|
||||
|
||||
kmsRepo, err := kms.NewRepository(rw, rw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms repository: %w", err)
|
||||
}
|
||||
kmsCache, err := kms.NewKms(kmsRepo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms cache: %w", err)
|
||||
}
|
||||
if err := kmsCache.AddExternalWrappers(
|
||||
kms.WithRootWrapper(b.RootKms),
|
||||
kms.WithWorkerAuthWrapper(b.WorkerAuthKms),
|
||||
); err != nil {
|
||||
return fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
}
|
||||
|
||||
repo, err := iam.NewRepository(rw, rw, kmsCache, iam.WithRandomReader(b.SecureRandomReader))
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create repo for org id: %w", err)
|
||||
}
|
||||
@@ -444,8 +463,17 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
cancel()
|
||||
}()
|
||||
|
||||
rootKey, err := uuid.GenerateRandomBytesWithReader(32, b.SecureRandomReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating random bytes for scope root key: %w", err)
|
||||
}
|
||||
_, _, err = kmsRepo.CreateRootKey(ctx, b.RootKms, scope.Global.String(), rootKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error saving global scope root key: %w", err)
|
||||
}
|
||||
|
||||
// Create the dev auth method
|
||||
pwRepo, err := password.NewRepository(rw, rw, b.ControllerKMS)
|
||||
pwRepo, err := password.NewRepository(rw, rw, kmsCache)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating password repo: %w", err)
|
||||
}
|
||||
@@ -482,7 +510,7 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new in memory auth account: %w", err)
|
||||
}
|
||||
acct, err = pwRepo.CreateAccount(ctx, acct, password.WithPassword(pw))
|
||||
acct, err = pwRepo.CreateAccount(ctx, scope.Global.String(), acct, password.WithPassword(pw))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error saving auth account to the db: %w", err)
|
||||
}
|
||||
|
||||
@@ -202,11 +202,11 @@ func (c *Command) Run(args []string) int {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
if c.ControllerKMS == nil {
|
||||
if c.RootKms == nil {
|
||||
c.UI.Error("Controller KMS not found after parsing KMS blocks")
|
||||
return 1
|
||||
}
|
||||
if c.WorkerAuthKMS == nil {
|
||||
if c.WorkerAuthKms == nil {
|
||||
c.UI.Error("Worker Auth KMS not found after parsing KMS blocks")
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -225,11 +225,11 @@ func (c *Command) Run(args []string) int {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
if c.ControllerKMS == nil {
|
||||
if c.RootKms == nil {
|
||||
c.UI.Error("Controller KMS not found after parsing KMS blocks")
|
||||
return 1
|
||||
}
|
||||
if c.WorkerAuthKMS == nil {
|
||||
if c.WorkerAuthKms == nil {
|
||||
c.UI.Error("Worker Auth KMS not found after parsing KMS blocks")
|
||||
return 1
|
||||
}
|
||||
@@ -237,7 +237,7 @@ func (c *Command) Run(args []string) int {
|
||||
c.Info["[Controller] AEAD Key Bytes"] = devConfig.Controller.DevControllerKey
|
||||
c.InfoKeys = append(c.InfoKeys, "[Worker-Auth] AEAD Key Bytes")
|
||||
c.Info["[Worker-Auth] AEAD Key Bytes"] = devConfig.Controller.DevWorkerAuthKey
|
||||
if c.WorkerAuthKMS == nil {
|
||||
if c.WorkerAuthKms == nil {
|
||||
c.UI.Error("Worker Auth KMS not found after parsing KMS blocks")
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ func (c *Command) Run(args []string) int {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
if c.WorkerAuthKMS == nil {
|
||||
if c.WorkerAuthKms == nil {
|
||||
c.UI.Error("Worker Auth KMS not found after parsing KMS blocks")
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -33,12 +33,14 @@ kms "aead" {
|
||||
purpose = "root"
|
||||
aead_type = "aes-gcm"
|
||||
key = "%s"
|
||||
key_id = "global_root"
|
||||
}
|
||||
|
||||
kms "aead" {
|
||||
purpose = "worker-auth"
|
||||
aead_type = "aes-gcm"
|
||||
key = "%s"
|
||||
key_id = "global_worker-auth"
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
|
||||
@@ -52,6 +52,7 @@ func TestDevController(t *testing.T) {
|
||||
Purpose: []string{"root"},
|
||||
Config: map[string]string{
|
||||
"aead_type": "aes-gcm",
|
||||
"key_id": "global_root",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -59,6 +60,7 @@ func TestDevController(t *testing.T) {
|
||||
Purpose: []string{"worker-auth"},
|
||||
Config: map[string]string{
|
||||
"aead_type": "aes-gcm",
|
||||
"key_id": "global_worker-auth",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -285,6 +285,65 @@ returning public_id;
|
||||
assert.Equal(3, v)
|
||||
}
|
||||
|
||||
func TestDomain_wt_version_with_private_id(t *testing.T) {
|
||||
const (
|
||||
createTable = `
|
||||
create table if not exists test_table_wt_version(
|
||||
private_id bigint generated always as identity primary key,
|
||||
name text,
|
||||
version wt_version
|
||||
);
|
||||
`
|
||||
addTrigger = `
|
||||
create trigger
|
||||
update_version_column
|
||||
after update on test_table_wt_version
|
||||
for each row execute procedure update_version_column('private_id');
|
||||
`
|
||||
insert = `
|
||||
insert into test_table_wt_version (name)
|
||||
values ($1)
|
||||
returning private_id;
|
||||
`
|
||||
update = `update test_table_wt_version set name = $1 where private_id = $2;`
|
||||
|
||||
updateVersion = `update test_table_wt_version set version = $1 where private_id = $2;`
|
||||
|
||||
search = `select version from test_table_wt_version where private_id = $1`
|
||||
)
|
||||
conn, _ := TestSetup(t, "postgres")
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
db := conn.DB()
|
||||
_, err := db.Exec(createTable)
|
||||
require.NoError(err)
|
||||
|
||||
_, err = db.Exec(addTrigger)
|
||||
require.NoError(err)
|
||||
|
||||
var id int
|
||||
err = db.QueryRow(insert, "alice").Scan(&id)
|
||||
require.NoError(err)
|
||||
require.NotEmpty(id)
|
||||
|
||||
var v int
|
||||
err = db.QueryRow(search, id).Scan(&v)
|
||||
require.NoError(err)
|
||||
assert.Equal(1, v)
|
||||
|
||||
_, err = db.Exec(update, "bob", id)
|
||||
require.NoError(err)
|
||||
err = db.QueryRow(search, id).Scan(&v)
|
||||
require.NoError(err)
|
||||
assert.Equal(2, v)
|
||||
|
||||
_, err = db.Exec(updateVersion, 22, id)
|
||||
require.NoError(err)
|
||||
err = db.QueryRow(search, id).Scan(&v)
|
||||
require.NoError(err)
|
||||
assert.Equal(3, v)
|
||||
}
|
||||
|
||||
func TestDomain_DefaultUsersExist(t *testing.T) {
|
||||
conn, _ := TestSetup(t, "postgres")
|
||||
db := conn.DB()
|
||||
|
||||
@@ -121,7 +121,8 @@ comment on domain wt_version is
|
||||
|
||||
-- update_version_column() will increment the version column whenever row data
|
||||
-- is updated and should only be used in an update after trigger. This function
|
||||
-- will overwrite any explicit updates to the version column.
|
||||
-- will overwrite any explicit updates to the version column. The function
|
||||
-- accepts an optional parameter of 'private_id' for the tables primary key.
|
||||
create or replace function
|
||||
update_version_column()
|
||||
returns trigger
|
||||
@@ -129,9 +130,16 @@ as $$
|
||||
begin
|
||||
if pg_trigger_depth() = 1 then
|
||||
if row(new.*) is distinct from row(old.*) then
|
||||
execute format('update %I set version = $1 where public_id = $2', tg_relid::regclass) using old.version+1, new.public_id;
|
||||
new.version = old.version + 1;
|
||||
return new;
|
||||
if tg_nargs = 0 then
|
||||
execute format('update %I set version = $1 where public_id = $2', tg_relid::regclass) using old.version+1, new.public_id;
|
||||
new.version = old.version + 1;
|
||||
return new;
|
||||
end if;
|
||||
if tg_argv[0] = 'private_id' then
|
||||
execute format('update %I set version = $1 where private_id = $2', tg_relid::regclass) using old.version+1, new.private_id;
|
||||
new.version = old.version + 1;
|
||||
return new;
|
||||
end if;
|
||||
end if;
|
||||
end if;
|
||||
return new;
|
||||
@@ -1323,7 +1331,8 @@ begin;
|
||||
create table servers (
|
||||
private_id text,
|
||||
type text,
|
||||
name text not null unique,
|
||||
name text not null unique
|
||||
check(length(trim(name)) > 0),
|
||||
description text,
|
||||
address text,
|
||||
create_time wt_timestamp,
|
||||
@@ -1373,6 +1382,9 @@ begin;
|
||||
create table auth_token (
|
||||
public_id wt_public_id primary key,
|
||||
token bytea not null unique,
|
||||
-- TODO: Make key_id a foreign key once we have DEKs
|
||||
key_id text not null
|
||||
check(length(trim(key_id)) > 0),
|
||||
auth_account_id wt_public_id not null
|
||||
references auth_account(public_id)
|
||||
on delete cascade
|
||||
@@ -1888,6 +1900,9 @@ begin;
|
||||
check(length(salt) > 0),
|
||||
derived_key bytea not null
|
||||
check(length(derived_key) > 0),
|
||||
-- TODO: Make key_id a foreign key once we have DEKs
|
||||
key_id text not null
|
||||
check(length(trim(key_id)) > 0),
|
||||
foreign key (password_method_id, password_conf_id)
|
||||
references auth_password_argon2_conf (password_method_id, private_id)
|
||||
on delete cascade
|
||||
@@ -2427,6 +2442,341 @@ begin;
|
||||
|
||||
commit;
|
||||
|
||||
`),
|
||||
},
|
||||
"migrations/30_keys.down.sql": {
|
||||
name: "30_keys.down.sql",
|
||||
bytes: []byte(`
|
||||
begin;
|
||||
|
||||
drop function kms_version_column cascade;
|
||||
|
||||
commit;
|
||||
|
||||
`),
|
||||
},
|
||||
"migrations/30_keys.up.sql": {
|
||||
name: "30_keys.up.sql",
|
||||
bytes: []byte(`
|
||||
begin;
|
||||
|
||||
-- kms_version_column() will increment the version column whenever row data
|
||||
-- is inserted and should only be used in an before insert trigger. This
|
||||
-- function will overwrite any explicit values to the version column.
|
||||
create or replace function
|
||||
kms_version_column()
|
||||
returns trigger
|
||||
as $$
|
||||
declare
|
||||
_key_id text;
|
||||
_max bigint;
|
||||
begin
|
||||
execute format('SELECT $1.%I', tg_argv[0]) into _key_id using new;
|
||||
execute format('select max(version) + 1 from %I where %I = $1', tg_relid::regclass, tg_argv[0]) using _key_id into _max;
|
||||
if _max is null then
|
||||
_max = 1;
|
||||
end if;
|
||||
new.version = _max;
|
||||
return new;
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
comment on function
|
||||
kms_version_column()
|
||||
is
|
||||
'function used in before insert triggers to properly set version columns for kms_* tables with a version column';
|
||||
|
||||
commit;
|
||||
`),
|
||||
},
|
||||
"migrations/31_keys.down.sql": {
|
||||
name: "31_keys.down.sql",
|
||||
bytes: []byte(`
|
||||
begin;
|
||||
|
||||
drop table kms_root_key cascade;
|
||||
drop table kms_root_key_version cascade;
|
||||
drop table kms_database_key cascade;
|
||||
drop table kms_database_key_version cascade;
|
||||
drop table kms_oplog_key cascade;
|
||||
drop table kms_oplog_key_version cascade;
|
||||
drop table kms_session_key cascade;
|
||||
drop table kms_session_key_version cascade;
|
||||
|
||||
commit;
|
||||
|
||||
`),
|
||||
},
|
||||
"migrations/31_keys.up.sql": {
|
||||
name: "31_keys.up.sql",
|
||||
bytes: []byte(`
|
||||
begin;
|
||||
|
||||
/*
|
||||
┌────────────────────────────────────────────────────────────────────────────────────────────┐
|
||||
├────────────────────────────────────────────────────────────────┐ ○
|
||||
├────────────────────────────────────┐ ○ ┼
|
||||
│ ○ ┼ ┌────────────────────────┐
|
||||
┼ ┼ ┌────────────────────────┐ │ kms_session_key │
|
||||
┌────────────────────────┐ ┌────────────────────────┐ │ kms_oplog_key │ ├────────────────────────┤
|
||||
│ kms_root_key │ │ kms_database_key │ ├────────────────────────┤ │private_id │
|
||||
├────────────────────────┤ ├────────────────────────┤ │private_id │ │root_key_id │
|
||||
│private_id │ │private_id │ │root_key_id │ │ │
|
||||
│scope_id │ │root_key_id │ │ │ │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
└────────────────────────┘ └────────────────────────┘ └────────────────────────┘ └────────────────────────┘
|
||||
┼ ┼ ┼ ┼
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
┼ ┼ ┼ ┼
|
||||
╱│╲ ╱│╲ ╱│╲ ╱│╲
|
||||
┌────────────────────────┐ ┌────────────────────────┐ ┌────────────────────────┐ ┌────────────────────────┐
|
||||
│ kms_root_key_version │ │kms_database_key_version│ │ kms_oplog_key_version │ │kms_session_key_version │
|
||||
├────────────────────────┤ ├────────────────────────┤ ├────────────────────────┤ ├────────────────────────┤
|
||||
│private_id │ │private_id │ │private_id │ │private_id │
|
||||
│root_key_id │ │database_key_id │ │oplog_key_id │ │session_key_id │
|
||||
│key │ │root_key_id │ │root_key_id │ │root_key_id │
|
||||
│version │ │key │ │key │ │key │
|
||||
│ │ │version │ │version │ │version │
|
||||
└────────────────────────┘ └────────────────────────┘ │ │ │ │
|
||||
┼ ┼ └────────────────────────┘ │ │
|
||||
│ ○ ┼ └────────────────────────┘
|
||||
├────────────────────────────────────┘ ○ ┼
|
||||
├────────────────────────────────────────────────────────────────┘ ○
|
||||
└────────────────────────────────────────────────────────────────────────────────────────────┘
|
||||
*/
|
||||
|
||||
create table kms_root_key (
|
||||
private_id wt_private_id primary key,
|
||||
scope_id wt_scope_id not null unique -- there can only be one root key for a scope.
|
||||
references iam_scope(public_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_root_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_root_key
|
||||
for each row execute procedure immutable_columns('private_id', 'scope_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_root_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_root_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(root_key_id, version)
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_root_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_root_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_root_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_root_key_version
|
||||
for each row execute procedure kms_version_column('root_key_id');
|
||||
|
||||
create table kms_database_key (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_database_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_database_key
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_database_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_database_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
database_key_id wt_private_id not null
|
||||
references kms_database_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
root_key_version_id wt_private_id not null
|
||||
references kms_root_key_version(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(database_key_id, version)
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_database_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_database_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'database_key_id', 'root_key_version_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_database_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_database_key_version
|
||||
for each row execute procedure kms_version_column('database_key_id');
|
||||
|
||||
create table kms_oplog_key (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_oplog_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_oplog_key
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_oplog_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_oplog_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
oplog_key_id wt_private_id not null
|
||||
references kms_oplog_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
root_key_version_id wt_private_id not null
|
||||
references kms_root_key_version(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(oplog_key_id, version)
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_oplog_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_oplog_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'oplog_key_id', 'root_key_version_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_oplog_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_oplog_key_version
|
||||
for each row execute procedure kms_version_column('oplog_key_id');
|
||||
|
||||
create table kms_session_key (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_oplog_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_session_key
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_session_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_session_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
session_key_id wt_private_id not null
|
||||
references kms_session_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
root_key_version_id wt_private_id not null
|
||||
references kms_root_key_version(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(session_key_id, version)
|
||||
);
|
||||
|
||||
|
||||
-- define the immutable fields for kms_session_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_session_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'session_key_id', 'root_key_version_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_session_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_session_key_version
|
||||
for each row execute procedure kms_version_column('session_key_id');
|
||||
|
||||
insert into oplog_ticket
|
||||
(name, version)
|
||||
values
|
||||
('kms_root_key', 1),
|
||||
('kms_root_key_version', 1);
|
||||
|
||||
commit;
|
||||
`),
|
||||
},
|
||||
}
|
||||
@@ -90,7 +90,8 @@ comment on domain wt_version is
|
||||
|
||||
-- update_version_column() will increment the version column whenever row data
|
||||
-- is updated and should only be used in an update after trigger. This function
|
||||
-- will overwrite any explicit updates to the version column.
|
||||
-- will overwrite any explicit updates to the version column. The function
|
||||
-- accepts an optional parameter of 'private_id' for the tables primary key.
|
||||
create or replace function
|
||||
update_version_column()
|
||||
returns trigger
|
||||
@@ -98,9 +99,16 @@ as $$
|
||||
begin
|
||||
if pg_trigger_depth() = 1 then
|
||||
if row(new.*) is distinct from row(old.*) then
|
||||
execute format('update %I set version = $1 where public_id = $2', tg_relid::regclass) using old.version+1, new.public_id;
|
||||
new.version = old.version + 1;
|
||||
return new;
|
||||
if tg_nargs = 0 then
|
||||
execute format('update %I set version = $1 where public_id = $2', tg_relid::regclass) using old.version+1, new.public_id;
|
||||
new.version = old.version + 1;
|
||||
return new;
|
||||
end if;
|
||||
if tg_argv[0] = 'private_id' then
|
||||
execute format('update %I set version = $1 where private_id = $2', tg_relid::regclass) using old.version+1, new.private_id;
|
||||
new.version = old.version + 1;
|
||||
return new;
|
||||
end if;
|
||||
end if;
|
||||
end if;
|
||||
return new;
|
||||
|
||||
@@ -7,7 +7,8 @@ begin;
|
||||
create table servers (
|
||||
private_id text,
|
||||
type text,
|
||||
name text not null unique,
|
||||
name text not null unique
|
||||
check(length(trim(name)) > 0),
|
||||
description text,
|
||||
address text,
|
||||
create_time wt_timestamp,
|
||||
|
||||
@@ -5,6 +5,9 @@ begin;
|
||||
create table auth_token (
|
||||
public_id wt_public_id primary key,
|
||||
token bytea not null unique,
|
||||
-- TODO: Make key_id a foreign key once we have DEKs
|
||||
key_id text not null
|
||||
check(length(trim(key_id)) > 0),
|
||||
auth_account_id wt_public_id not null
|
||||
references auth_account(public_id)
|
||||
on delete cascade
|
||||
|
||||
@@ -67,6 +67,9 @@ begin;
|
||||
check(length(salt) > 0),
|
||||
derived_key bytea not null
|
||||
check(length(derived_key) > 0),
|
||||
-- TODO: Make key_id a foreign key once we have DEKs
|
||||
key_id text not null
|
||||
check(length(trim(key_id)) > 0),
|
||||
foreign key (password_method_id, password_conf_id)
|
||||
references auth_password_argon2_conf (password_method_id, private_id)
|
||||
on delete cascade
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
begin;
|
||||
|
||||
drop function kms_version_column cascade;
|
||||
|
||||
commit;
|
||||
@@ -0,0 +1,29 @@
|
||||
begin;
|
||||
|
||||
-- kms_version_column() will increment the version column whenever row data
|
||||
-- is inserted and should only be used in an before insert trigger. This
|
||||
-- function will overwrite any explicit values to the version column.
|
||||
create or replace function
|
||||
kms_version_column()
|
||||
returns trigger
|
||||
as $$
|
||||
declare
|
||||
_key_id text;
|
||||
_max bigint;
|
||||
begin
|
||||
execute format('SELECT $1.%I', tg_argv[0]) into _key_id using new;
|
||||
execute format('select max(version) + 1 from %I where %I = $1', tg_relid::regclass, tg_argv[0]) using _key_id into _max;
|
||||
if _max is null then
|
||||
_max = 1;
|
||||
end if;
|
||||
new.version = _max;
|
||||
return new;
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
comment on function
|
||||
kms_version_column()
|
||||
is
|
||||
'function used in before insert triggers to properly set version columns for kms_* tables with a version column';
|
||||
|
||||
commit;
|
||||
@@ -0,0 +1,12 @@
|
||||
begin;
|
||||
|
||||
drop table kms_root_key cascade;
|
||||
drop table kms_root_key_version cascade;
|
||||
drop table kms_database_key cascade;
|
||||
drop table kms_database_key_version cascade;
|
||||
drop table kms_oplog_key cascade;
|
||||
drop table kms_oplog_key_version cascade;
|
||||
drop table kms_session_key cascade;
|
||||
drop table kms_session_key_version cascade;
|
||||
|
||||
commit;
|
||||
@@ -0,0 +1,267 @@
|
||||
begin;
|
||||
|
||||
/*
|
||||
┌────────────────────────────────────────────────────────────────────────────────────────────┐
|
||||
├────────────────────────────────────────────────────────────────┐ ○
|
||||
├────────────────────────────────────┐ ○ ┼
|
||||
│ ○ ┼ ┌────────────────────────┐
|
||||
┼ ┼ ┌────────────────────────┐ │ kms_session_key │
|
||||
┌────────────────────────┐ ┌────────────────────────┐ │ kms_oplog_key │ ├────────────────────────┤
|
||||
│ kms_root_key │ │ kms_database_key │ ├────────────────────────┤ │private_id │
|
||||
├────────────────────────┤ ├────────────────────────┤ │private_id │ │root_key_id │
|
||||
│private_id │ │private_id │ │root_key_id │ │ │
|
||||
│scope_id │ │root_key_id │ │ │ │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
└────────────────────────┘ └────────────────────────┘ └────────────────────────┘ └────────────────────────┘
|
||||
┼ ┼ ┼ ┼
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
┼ ┼ ┼ ┼
|
||||
╱│╲ ╱│╲ ╱│╲ ╱│╲
|
||||
┌────────────────────────┐ ┌────────────────────────┐ ┌────────────────────────┐ ┌────────────────────────┐
|
||||
│ kms_root_key_version │ │kms_database_key_version│ │ kms_oplog_key_version │ │kms_session_key_version │
|
||||
├────────────────────────┤ ├────────────────────────┤ ├────────────────────────┤ ├────────────────────────┤
|
||||
│private_id │ │private_id │ │private_id │ │private_id │
|
||||
│root_key_id │ │database_key_id │ │oplog_key_id │ │session_key_id │
|
||||
│key │ │root_key_id │ │root_key_id │ │root_key_id │
|
||||
│version │ │key │ │key │ │key │
|
||||
│ │ │version │ │version │ │version │
|
||||
└────────────────────────┘ └────────────────────────┘ │ │ │ │
|
||||
┼ ┼ └────────────────────────┘ │ │
|
||||
│ ○ ┼ └────────────────────────┘
|
||||
├────────────────────────────────────┘ ○ ┼
|
||||
├────────────────────────────────────────────────────────────────┘ ○
|
||||
└────────────────────────────────────────────────────────────────────────────────────────────┘
|
||||
*/
|
||||
|
||||
create table kms_root_key (
|
||||
private_id wt_private_id primary key,
|
||||
scope_id wt_scope_id not null unique -- there can only be one root key for a scope.
|
||||
references iam_scope(public_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_root_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_root_key
|
||||
for each row execute procedure immutable_columns('private_id', 'scope_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_root_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_root_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(root_key_id, version)
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_root_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_root_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_root_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_root_key_version
|
||||
for each row execute procedure kms_version_column('root_key_id');
|
||||
|
||||
create table kms_database_key (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_database_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_database_key
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_database_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_database_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
database_key_id wt_private_id not null
|
||||
references kms_database_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
root_key_version_id wt_private_id not null
|
||||
references kms_root_key_version(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(database_key_id, version)
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_database_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_database_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'database_key_id', 'root_key_version_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_database_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_database_key_version
|
||||
for each row execute procedure kms_version_column('database_key_id');
|
||||
|
||||
create table kms_oplog_key (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_oplog_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_oplog_key
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_oplog_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_oplog_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
oplog_key_id wt_private_id not null
|
||||
references kms_oplog_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
root_key_version_id wt_private_id not null
|
||||
references kms_root_key_version(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(oplog_key_id, version)
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_oplog_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_oplog_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'oplog_key_id', 'root_key_version_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_oplog_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_oplog_key_version
|
||||
for each row execute procedure kms_version_column('oplog_key_id');
|
||||
|
||||
create table kms_session_key (
|
||||
private_id wt_private_id primary key,
|
||||
root_key_id wt_private_id not null
|
||||
references kms_root_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
create_time wt_timestamp
|
||||
);
|
||||
|
||||
-- define the immutable fields for kms_oplog_key (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_session_key
|
||||
for each row execute procedure immutable_columns('private_id', 'root_key_id', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_session_key
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create table kms_session_key_version (
|
||||
private_id wt_private_id primary key,
|
||||
session_key_id wt_private_id not null
|
||||
references kms_session_key(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
root_key_version_id wt_private_id not null
|
||||
references kms_root_key_version(private_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
version wt_version,
|
||||
key bytea not null,
|
||||
create_time wt_timestamp,
|
||||
unique(session_key_id, version)
|
||||
);
|
||||
|
||||
|
||||
-- define the immutable fields for kms_session_key_version (all of them)
|
||||
create trigger
|
||||
immutable_columns
|
||||
before
|
||||
update on kms_session_key_version
|
||||
for each row execute procedure immutable_columns('private_id', 'session_key_id', 'root_key_version_id', 'version', 'key', 'create_time');
|
||||
|
||||
create trigger
|
||||
default_create_time_column
|
||||
before
|
||||
insert on kms_session_key_version
|
||||
for each row execute procedure default_create_time();
|
||||
|
||||
create trigger
|
||||
kms_version_column
|
||||
before insert on kms_session_key_version
|
||||
for each row execute procedure kms_version_column('session_key_id');
|
||||
|
||||
insert into oplog_ticket
|
||||
(name, version)
|
||||
values
|
||||
('kms_root_key', 1),
|
||||
('kms_root_key_version', 1);
|
||||
|
||||
commit;
|
||||
@@ -39,6 +39,7 @@ type Options struct {
|
||||
|
||||
withWhereClause string
|
||||
withWhereClauseArgs []interface{}
|
||||
withOrder string
|
||||
}
|
||||
|
||||
type oplogOpts struct {
|
||||
@@ -146,3 +147,11 @@ func WithWhere(whereClause string, args ...interface{}) Option {
|
||||
o.withWhereClauseArgs = append(o.withWhereClauseArgs, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// WithOrder provides an option to provide an order when searching and looking
|
||||
// up.
|
||||
func WithOrder(withOrder string) Option {
|
||||
return func(o *Options) {
|
||||
o.withOrder = withOrder
|
||||
}
|
||||
}
|
||||
@@ -162,4 +162,17 @@ func Test_getOpts(t *testing.T) {
|
||||
testOpts.withWhereClauseArgs = []interface{}{1234, "bar"}
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
t.Run("WithOrder", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
// test default of false
|
||||
opts := GetOpts()
|
||||
testOpts := getDefaultOptions()
|
||||
testOpts.withOrder = ""
|
||||
assert.Equal(opts, testOpts)
|
||||
|
||||
// try setting to false
|
||||
opts = GetOpts(WithOrder("version desc"))
|
||||
testOpts.withOrder = "version desc"
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
}
|
||||
@@ -914,7 +914,7 @@ func (rw *Db) LookupById(ctx context.Context, resourceWithIder interface{}, opt
|
||||
return fmt.Errorf("lookup by id: underlying db nil %w", ErrNilParameter)
|
||||
}
|
||||
if reflect.ValueOf(resourceWithIder).Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("lookup by id: interface parameter must to be a pointer %w", ErrInvalidParameter)
|
||||
return fmt.Errorf("lookup by id: interface parameter must to be a pointer: %w", ErrInvalidParameter)
|
||||
}
|
||||
primaryKey, where, err := primaryKeyWhere(resourceWithIder)
|
||||
if err != nil {
|
||||
@@ -973,7 +973,7 @@ func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where strin
|
||||
// SearchWhere will search for all the resources it can find using a where
|
||||
// clause with parameters. Supports the WithLimit option. If
|
||||
// WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then
|
||||
// default limits are used for results.
|
||||
// default limits are used for results. Supports the WithOrder option.
|
||||
func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error {
|
||||
opts := GetOpts(opt...)
|
||||
if rw.underlying == nil {
|
||||
@@ -985,11 +985,11 @@ func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where stri
|
||||
var err error
|
||||
switch {
|
||||
case opts.WithLimit < 0: // any negative number signals unlimited results
|
||||
err = rw.underlying.Where(where, args...).Find(resources).Error
|
||||
err = rw.underlying.Order(opts.withOrder).Where(where, args...).Find(resources).Error
|
||||
case opts.WithLimit == 0: // zero signals the default value and default limits
|
||||
err = rw.underlying.Limit(DefaultLimit).Where(where, args...).Find(resources).Error
|
||||
err = rw.underlying.Order(opts.withOrder).Limit(DefaultLimit).Where(where, args...).Find(resources).Error
|
||||
default:
|
||||
err = rw.underlying.Limit(opts.WithLimit).Where(where, args...).Find(resources).Error
|
||||
err = rw.underlying.Order(opts.withOrder).Limit(opts.WithLimit).Where(where, args...).Find(resources).Error
|
||||
}
|
||||
if err != nil {
|
||||
// searching with a slice parameter does not return a gorm.ErrRecordNotFound
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -949,7 +950,7 @@ func TestDb_LookupWhere(t *testing.T) {
|
||||
func TestDb_SearchWhere(t *testing.T) {
|
||||
t.Parallel()
|
||||
db, _ := TestSetup(t, "postgres")
|
||||
knownUser := testUser(t, db, "", "", "")
|
||||
knownUser := testUser(t, db, "zedUser", "", "")
|
||||
|
||||
type args struct {
|
||||
where string
|
||||
@@ -957,12 +958,13 @@ func TestDb_SearchWhere(t *testing.T) {
|
||||
opt []Option
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
db Db
|
||||
createCnt int
|
||||
args args
|
||||
wantCnt int
|
||||
wantErr bool
|
||||
name string
|
||||
db Db
|
||||
createCnt int
|
||||
args args
|
||||
wantCnt int
|
||||
wantErr bool
|
||||
wantNameOrder bool
|
||||
}{
|
||||
{
|
||||
name: "no-limit",
|
||||
@@ -970,10 +972,11 @@ func TestDb_SearchWhere(t *testing.T) {
|
||||
createCnt: 10,
|
||||
args: args{
|
||||
where: "1=1",
|
||||
opt: []Option{WithLimit(-1)},
|
||||
opt: []Option{WithLimit(-1), WithOrder("name asc")},
|
||||
},
|
||||
wantCnt: 11, // there's an additional knownUser
|
||||
wantErr: false,
|
||||
wantCnt: 11, // there's an additional knownUser
|
||||
wantErr: false,
|
||||
wantNameOrder: true,
|
||||
},
|
||||
{
|
||||
name: "custom-limit",
|
||||
@@ -1040,11 +1043,13 @@ func TestDb_SearchWhere(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
testUsers := []*db_test.TestUser{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
testUsers = append(testUsers, testUser(t, db, "", "", ""))
|
||||
testUsers = append(testUsers, testUser(t, db, tt.name+strconv.Itoa(i), "", ""))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(testUsers))
|
||||
|
||||
var foundUsers []db_test.TestUser
|
||||
db.LogMode(true)
|
||||
defer db.LogMode(false)
|
||||
err := tt.db.SearchWhere(context.Background(), &foundUsers, tt.args.where, tt.args.arg, tt.args.opt...)
|
||||
if tt.wantErr {
|
||||
require.Error(err)
|
||||
@@ -1052,6 +1057,14 @@ func TestDb_SearchWhere(t *testing.T) {
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.Equal(tt.wantCnt, len(foundUsers))
|
||||
if tt.wantNameOrder {
|
||||
assert.Equal(tt.name+strconv.Itoa(0), foundUsers[0].Name)
|
||||
for i, u := range foundUsers {
|
||||
if u.Name != "zedUser" {
|
||||
assert.Equal(tt.name+strconv.Itoa(i), u.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -47,6 +48,12 @@ func TestWrapper(t *testing.T) wrapping.Wrapper {
|
||||
t.Fatal(n)
|
||||
}
|
||||
root := aead.NewWrapper(nil)
|
||||
_, err = root.SetConfig(map[string]string{
|
||||
"key_id": base64.StdEncoding.EncodeToString(rootKey),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := root.SetAESGCMKeyBytes(rootKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -14,7 +15,8 @@ import (
|
||||
|
||||
func TestHostCatalog_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
type args struct {
|
||||
scopeId string
|
||||
@@ -107,7 +109,7 @@ func TestHostCatalog_New(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testCatalogs(t *testing.T, conn *gorm.DB, scopeId string, count int) []*HostCatalog {
|
||||
func testCatalogs(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, scopeId string, count int) []*HostCatalog {
|
||||
t.Helper()
|
||||
assert := assert.New(t)
|
||||
var cats []*HostCatalog
|
||||
@@ -128,9 +130,9 @@ func testCatalogs(t *testing.T, conn *gorm.DB, scopeId string, count int) []*Hos
|
||||
return cats
|
||||
}
|
||||
|
||||
func testCatalog(t *testing.T, conn *gorm.DB, scopeId string) *HostCatalog {
|
||||
func testCatalog(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, scopeId string) *HostCatalog {
|
||||
t.Helper()
|
||||
cats := testCatalogs(t, conn, scopeId, 1)
|
||||
cats := testCatalogs(t, conn, wrapper, scopeId, 1)
|
||||
return cats[0]
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,10 @@ import (
|
||||
func TestHostSetMember_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
conn.LogMode(false)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cats := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cats := testCatalogs(t, conn, wrapper, prj.PublicId, 2)
|
||||
|
||||
blueCat := cats[0]
|
||||
blueSets := testSets(t, conn, blueCat.GetPublicId(), 2)
|
||||
|
||||
@@ -14,8 +14,9 @@ import (
|
||||
|
||||
func TestHostSet_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
wrapper := db.TestWrapper(t)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
|
||||
conn.LogMode(false)
|
||||
type args struct {
|
||||
|
||||
@@ -15,8 +15,9 @@ import (
|
||||
|
||||
func TestHost_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
wrapper := db.TestWrapper(t)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
|
||||
conn.LogMode(false)
|
||||
type args struct {
|
||||
|
||||
@@ -18,11 +18,10 @@ func TestHostCatalog_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
w := db.New(conn)
|
||||
|
||||
_, proj := iam.TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
new := testCatalog(t, conn, prj.PublicId)
|
||||
o, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
new := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -51,7 +50,7 @@ func TestHostCatalog_ImmutableFields(t *testing.T) {
|
||||
name: "scope id",
|
||||
update: func() *HostCatalog {
|
||||
c := new.clone()
|
||||
c.ScopeId = proj.PublicId
|
||||
c.ScopeId = o.PublicId
|
||||
return c
|
||||
}(),
|
||||
fieldMask: []string{"ScopeId"},
|
||||
@@ -83,10 +82,11 @@ func TestStaticHost_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
w := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
hosts := testHosts(t, conn, cat.GetPublicId(), 1)
|
||||
|
||||
new := hosts[0]
|
||||
@@ -157,10 +157,11 @@ func TestStaticHostSet_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
w := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
sets := testSets(t, conn, cat.GetPublicId(), 1)
|
||||
|
||||
new := sets[0]
|
||||
@@ -231,9 +232,10 @@ func TestStaticHostSetMember_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
w := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
sets := testSets(t, conn, cat.GetPublicId(), 1)
|
||||
hosts := testHosts(t, conn, cat.GetPublicId(), 1)
|
||||
|
||||
|
||||
@@ -3,17 +3,16 @@ package static
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
)
|
||||
|
||||
// A Repository stores and retrieves the persistent types in the static
|
||||
// package. It is not safe to use a repository concurrently.
|
||||
type Repository struct {
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
kms *kms.Kms
|
||||
// defaultLimit provides a default for limiting the number of results
|
||||
// returned from the repo
|
||||
defaultLimit int
|
||||
@@ -23,14 +22,15 @@ type Repository struct {
|
||||
// only be used for one transaction and it is not safe for concurrent go
|
||||
// routines to access it. WithLimit option is used as a repo wide default
|
||||
// limit applied to all ListX methods.
|
||||
func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Option) (*Repository, error) {
|
||||
// routines to access it.
|
||||
func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, fmt.Errorf("db.Reader: %w", db.ErrNilParameter)
|
||||
case w == nil:
|
||||
return nil, fmt.Errorf("db.Writer: %w", db.ErrNilParameter)
|
||||
case wrapper == nil:
|
||||
return nil, fmt.Errorf("wrapping.Wrapper: %w", db.ErrNilParameter)
|
||||
case kms == nil:
|
||||
return nil, fmt.Errorf("kms: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -42,7 +42,7 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Op
|
||||
return &Repository{
|
||||
reader: r,
|
||||
writer: w,
|
||||
wrapper: wrapper,
|
||||
kms: kms,
|
||||
defaultLimit: opts.withLimit,
|
||||
}, nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbcommon "github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -20,7 +21,7 @@ import (
|
||||
//
|
||||
// Both h.Name and h.Description are optional. If h.Name is set, it must be
|
||||
// unique within h.CatalogId.
|
||||
func (r *Repository) CreateHost(ctx context.Context, h *Host, opt ...Option) (*Host, error) {
|
||||
func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, opt ...Option) (*Host, error) {
|
||||
if h == nil {
|
||||
return nil, fmt.Errorf("create: static host: %w", db.ErrNilParameter)
|
||||
}
|
||||
@@ -33,6 +34,9 @@ func (r *Repository) CreateHost(ctx context.Context, h *Host, opt ...Option) (*H
|
||||
if h.PublicId != "" {
|
||||
return nil, fmt.Errorf("create: static host: public id not empty: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("create: static host: no scopeId: %w", db.ErrNilParameter)
|
||||
}
|
||||
h = h.clone()
|
||||
|
||||
id, err := newHostId()
|
||||
@@ -41,11 +45,16 @@ func (r *Repository) CreateHost(ctx context.Context, h *Host, opt ...Option) (*H
|
||||
}
|
||||
h.PublicId = id
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var newHost *Host
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
newHost = h.clone()
|
||||
return w.Create(ctx, newHost, db.WithOplog(r.wrapper, h.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
return w.Create(ctx, newHost, db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
},
|
||||
)
|
||||
|
||||
@@ -75,7 +84,7 @@ func (r *Repository) CreateHost(ctx context.Context, h *Host, opt ...Option) (*H
|
||||
//
|
||||
// An attribute of h will be set to NULL in the database if the attribute
|
||||
// in h is the zero value and it is included in fieldMaskPaths.
|
||||
func (r *Repository) UpdateHost(ctx context.Context, h *Host, version uint32, fieldMaskPaths []string, opt ...Option) (*Host, int, error) {
|
||||
func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, version uint32, fieldMaskPaths []string, opt ...Option) (*Host, int, error) {
|
||||
if h == nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %w", db.ErrNilParameter)
|
||||
}
|
||||
@@ -88,6 +97,9 @@ func (r *Repository) UpdateHost(ctx context.Context, h *Host, version uint32, fi
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: no version supplied: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: no scopeId: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
for _, f := range fieldMaskPaths {
|
||||
switch {
|
||||
@@ -111,14 +123,19 @@ func (r *Repository) UpdateHost(ctx context.Context, h *Host, version uint32, fi
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %w", db.ErrEmptyFieldMask)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var rowsUpdated int
|
||||
var returnedHost *Host
|
||||
_, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
returnedHost = h.clone()
|
||||
var err error
|
||||
rowsUpdated, err = w.Update(ctx, returnedHost, dbMask, nullFields,
|
||||
db.WithOplog(r.wrapper, h.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithVersion(&version))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
@@ -181,19 +198,24 @@ func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Opt
|
||||
// DeleteHost deletes the host for the provided id from the repository
|
||||
// returning a count of the number of records deleted. All options are
|
||||
// ignored.
|
||||
func (r *Repository) DeleteHost(ctx context.Context, publicId string, opt ...Option) (int, error) {
|
||||
func (r *Repository) DeleteHost(ctx context.Context, scopeId string, publicId string, opt ...Option) (int, error) {
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host: missing public id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
h := allocHost()
|
||||
h.PublicId = publicId
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) (err error) {
|
||||
dh := h.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, dh, db.WithOplog(r.wrapper, h.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
rowsDeleted, err = w.Delete(ctx, dh, db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package static
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -39,6 +41,11 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
}
|
||||
c.PublicId = id
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_CREATE)
|
||||
|
||||
var newHostCatalog *HostCatalog
|
||||
@@ -51,7 +58,7 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
return w.Create(
|
||||
ctx,
|
||||
newHostCatalog,
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
},
|
||||
)
|
||||
@@ -87,6 +94,9 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
if c.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: missing public id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: missing scope id: %w", db.ErrNilParameter)
|
||||
}
|
||||
if len(fieldMask) == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %w", db.ErrEmptyFieldMask)
|
||||
}
|
||||
@@ -107,13 +117,19 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: field: %s: %w", f, db.ErrInvalidFieldMask)
|
||||
}
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
c = c.clone()
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_UPDATE)
|
||||
|
||||
var rowsUpdated int
|
||||
var returnedCatalog *HostCatalog
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
@@ -125,7 +141,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
returnedCatalog,
|
||||
dbMask,
|
||||
nullFields,
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
db.WithVersion(&version),
|
||||
)
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
@@ -172,12 +188,25 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option
|
||||
|
||||
c := allocCatalog()
|
||||
c.PublicId = id
|
||||
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
|
||||
if errors.Is(err, db.ErrRecordNotFound) {
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: failed %w for %s", err, id)
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: missing scope id: %w", db.ErrNilParameter)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_DELETE)
|
||||
|
||||
var rowsDeleted int
|
||||
var deleteCatalog *HostCatalog
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
@@ -187,7 +216,7 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option
|
||||
rowsDeleted, err = w.Delete(
|
||||
ctx,
|
||||
deleteCatalog,
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
dbassert "github.com/hashicorp/boundary/internal/db/assert"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -76,10 +77,11 @@ func TestRepository_CreateCatalog(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
if tt.in != nil && tt.in.HostCatalog != nil {
|
||||
tt.in.ScopeId = prj.GetPublicId()
|
||||
assert.Empty(tt.in.PublicId)
|
||||
@@ -103,11 +105,11 @@ func TestRepository_CreateCatalog(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
in := &HostCatalog{
|
||||
HostCatalog: &store.HostCatalog{
|
||||
ScopeId: prj.GetPublicId(),
|
||||
@@ -131,11 +133,11 @@ func TestRepository_CreateCatalog(t *testing.T) {
|
||||
|
||||
t.Run("valid-duplicate-names-diff-scopes", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
org, prj := iam.TestScopes(t, conn)
|
||||
org, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
in := &HostCatalog{
|
||||
HostCatalog: &store.HostCatalog{
|
||||
Name: "test-name-repo",
|
||||
@@ -432,10 +434,11 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
tt.orig.ScopeId = prj.GetPublicId()
|
||||
orig, err := repo.CreateCatalog(context.Background(), tt.orig)
|
||||
require.NoError(t, err)
|
||||
@@ -474,13 +477,14 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
name := "test-dup-name"
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cats := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cats := testCatalogs(t, conn, wrapper, prj.PublicId, 2)
|
||||
c1 := cats[0]
|
||||
c1.Name = name
|
||||
got1, gotCount1, err := repo.UpdateCatalog(context.Background(), c1, 1, []string{"name"})
|
||||
@@ -499,11 +503,11 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
|
||||
t.Run("valid-duplicate-names-diff-scopes", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
org, prj := iam.TestScopes(t, conn)
|
||||
org, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
in := &HostCatalog{
|
||||
HostCatalog: &store.HostCatalog{
|
||||
Name: "test-name-repo",
|
||||
@@ -537,13 +541,15 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
|
||||
t.Run("change-scope-id", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
_, prj1 := iam.TestScopes(t, conn)
|
||||
_, prj2 := iam.TestScopes(t, conn)
|
||||
c1, c2 := testCatalog(t, conn, prj1.PublicId), testCatalog(t, conn, prj2.PublicId)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, prj1 := iam.TestScopes(t, iamRepo)
|
||||
_, prj2 := iam.TestScopes(t, iamRepo)
|
||||
c1, c2 := testCatalog(t, conn, wrapper, prj1.PublicId), testCatalog(t, conn, wrapper, prj2.PublicId)
|
||||
assert.NotEqual(c1.ScopeId, c2.ScopeId)
|
||||
orig := c1.clone()
|
||||
|
||||
@@ -561,15 +567,14 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
|
||||
func TestRepository_LookupCatalog(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
badId, err := newHostCatalogId()
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, badId)
|
||||
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
id string
|
||||
@@ -598,7 +603,8 @@ func TestRepository_LookupCatalog(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
@@ -622,15 +628,15 @@ func TestRepository_LookupCatalog(t *testing.T) {
|
||||
|
||||
func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
cat := testCatalog(t, conn, prj.PublicId)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cat := testCatalog(t, conn, wrapper, prj.PublicId)
|
||||
badId, err := newHostCatalogId()
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, badId)
|
||||
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
id string
|
||||
@@ -659,7 +665,8 @@ func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(repo)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
dbassert "github.com/hashicorp/boundary/internal/db/assert"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"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"
|
||||
@@ -22,9 +23,10 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -144,10 +146,10 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateHost(context.Background(), tt.in, tt.opts...)
|
||||
got, err := repo.CreateHost(context.Background(), prj.GetPublicId(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
@@ -167,12 +169,12 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
|
||||
in := &Host{
|
||||
Host: &store.Host{
|
||||
@@ -182,7 +184,7 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
got, err := repo.CreateHost(context.Background(), in)
|
||||
got, err := repo.CreateHost(context.Background(), prj.GetPublicId(), in)
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, HostPrefix, got.PublicId)
|
||||
@@ -191,19 +193,19 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
assert.Equal(in.Description, got.Description)
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
got2, err := repo.CreateHost(context.Background(), in)
|
||||
got2, err := repo.CreateHost(context.Background(), prj.GetPublicId(), in)
|
||||
assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err)
|
||||
assert.Nil(got2)
|
||||
})
|
||||
|
||||
t.Run("valid-duplicate-names-diff-catalogs", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalogs := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalogs := testCatalogs(t, conn, wrapper, prj.PublicId, 2)
|
||||
|
||||
catalogA, catalogB := catalogs[0], catalogs[1]
|
||||
|
||||
@@ -216,7 +218,7 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
in2 := in.clone()
|
||||
|
||||
in.CatalogId = catalogA.PublicId
|
||||
got, err := repo.CreateHost(context.Background(), in)
|
||||
got, err := repo.CreateHost(context.Background(), prj.GetPublicId(), in)
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, HostPrefix, got.PublicId)
|
||||
@@ -226,7 +228,7 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
assert.Equal(got.CreateTime, got.UpdateTime)
|
||||
|
||||
in2.CatalogId = catalogB.PublicId
|
||||
got2, err := repo.CreateHost(context.Background(), in2)
|
||||
got2, err := repo.CreateHost(context.Background(), prj.GetPublicId(), in2)
|
||||
require.NoError(err)
|
||||
require.NotNil(got2)
|
||||
assertPublicId(t, HostPrefix, got2.PublicId)
|
||||
@@ -241,6 +243,8 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
changeAddress := func(s string) func(*Host) *Host {
|
||||
return func(h *Host) *Host {
|
||||
@@ -576,22 +580,22 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
|
||||
tt.orig.CatalogId = catalog.PublicId
|
||||
orig, err := repo.CreateHost(context.Background(), tt.orig)
|
||||
orig, err := repo.CreateHost(context.Background(), prj.GetPublicId(), tt.orig)
|
||||
assert.NoError(err)
|
||||
require.NotNil(orig)
|
||||
|
||||
if tt.chgFn != nil {
|
||||
orig = tt.chgFn(orig)
|
||||
}
|
||||
got, gotCount, err := repo.UpdateHost(context.Background(), orig, 1, tt.masks)
|
||||
got, gotCount, err := repo.UpdateHost(context.Background(), prj.GetPublicId(), orig, 1, tt.masks)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Equal(tt.wantCount, gotCount, "row count")
|
||||
@@ -624,19 +628,19 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
|
||||
t.Run("invalid-duplicate-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
name := "test-dup-name"
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
hs := testHosts(t, conn, catalog.PublicId, 2)
|
||||
|
||||
hA, hB := hs[0], hs[1]
|
||||
|
||||
hA.Name = name
|
||||
got1, gotCount1, err := repo.UpdateHost(context.Background(), hA, 1, []string{"name"})
|
||||
got1, gotCount1, err := repo.UpdateHost(context.Background(), prj.GetPublicId(), hA, 1, []string{"name"})
|
||||
assert.NoError(err)
|
||||
require.NotNil(got1)
|
||||
assert.Equal(name, got1.Name)
|
||||
@@ -644,7 +648,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
assert.NoError(db.TestVerifyOplog(t, rw, hA.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
|
||||
hB.Name = name
|
||||
got2, gotCount2, err := repo.UpdateHost(context.Background(), hB, 1, []string{"name"})
|
||||
got2, gotCount2, err := repo.UpdateHost(context.Background(), prj.GetPublicId(), hB, 1, []string{"name"})
|
||||
assert.Truef(errors.Is(err, db.ErrNotUnique), "want err: %v got: %v", db.ErrNotUnique, err)
|
||||
assert.Nil(got2)
|
||||
assert.Equal(db.NoRowsAffected, gotCount2, "row count")
|
||||
@@ -655,12 +659,12 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
|
||||
t.Run("valid-duplicate-names-diff-Catalogs", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalogs := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalogs := testCatalogs(t, conn, wrapper, prj.PublicId, 2)
|
||||
|
||||
catalogA, catalogB := catalogs[0], catalogs[1]
|
||||
|
||||
@@ -673,7 +677,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
in2 := in.clone()
|
||||
|
||||
in.CatalogId = catalogA.PublicId
|
||||
got, err := repo.CreateHost(context.Background(), in)
|
||||
got, err := repo.CreateHost(context.Background(), prj.GetPublicId(), in)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, HostPrefix, got.PublicId)
|
||||
@@ -683,11 +687,11 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
|
||||
in2.CatalogId = catalogB.PublicId
|
||||
in2.Name = "first-name"
|
||||
got2, err := repo.CreateHost(context.Background(), in2)
|
||||
got2, err := repo.CreateHost(context.Background(), prj.GetPublicId(), in2)
|
||||
assert.NoError(err)
|
||||
require.NotNil(got2)
|
||||
got2.Name = got.Name
|
||||
got3, gotCount3, err := repo.UpdateHost(context.Background(), got2, 1, []string{"name"})
|
||||
got3, gotCount3, err := repo.UpdateHost(context.Background(), prj.GetPublicId(), got2, 1, []string{"name"})
|
||||
assert.NoError(err)
|
||||
require.NotNil(got3)
|
||||
assert.NotSame(got2, got3)
|
||||
@@ -699,12 +703,12 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
|
||||
t.Run("change-scope-id", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalogs := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalogs := testCatalogs(t, conn, wrapper, prj.PublicId, 2)
|
||||
|
||||
catalogA, catalogB := catalogs[0], catalogs[1]
|
||||
|
||||
@@ -717,7 +721,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
hA.CatalogId = hB.CatalogId
|
||||
assert.Equal(hA.CatalogId, hB.CatalogId)
|
||||
|
||||
got1, gotCount1, err := repo.UpdateHost(context.Background(), hA, 1, []string{"name"})
|
||||
got1, gotCount1, err := repo.UpdateHost(context.Background(), prj.GetPublicId(), hA, 1, []string{"name"})
|
||||
|
||||
assert.NoError(err)
|
||||
require.NotNil(got1)
|
||||
@@ -731,9 +735,11 @@ func TestRepository_LookupHost(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
host := testHosts(t, conn, catalog.PublicId, 1)[0]
|
||||
|
||||
hostId, err := newHostId()
|
||||
@@ -763,7 +769,7 @@ func TestRepository_LookupHost(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.LookupHost(context.Background(), tt.in)
|
||||
@@ -782,9 +788,11 @@ func TestRepository_ListHosts(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalogs := testCatalogs(t, conn, prj.PublicId, 2)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalogs := testCatalogs(t, conn, wrapper, prj.PublicId, 2)
|
||||
catalogA, catalogB := catalogs[0], catalogs[1]
|
||||
|
||||
hosts := testHosts(t, conn, catalogA.PublicId, 3)
|
||||
@@ -816,7 +824,7 @@ func TestRepository_ListHosts(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListHosts(context.Background(), tt.in, tt.opts...)
|
||||
@@ -839,9 +847,11 @@ func TestRepository_ListHosts_Limits(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
count := 10
|
||||
hosts := testHosts(t, conn, catalog.PublicId, count)
|
||||
|
||||
@@ -893,7 +903,7 @@ func TestRepository_ListHosts_Limits(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, tt.repoOpts...)
|
||||
repo, err := NewRepository(rw, rw, kms, tt.repoOpts...)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListHosts(context.Background(), hosts[0].CatalogId, tt.listOpts...)
|
||||
@@ -907,9 +917,11 @@ func TestRepository_DeleteHost(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
_, prj := iam.TestScopes(t, conn)
|
||||
catalog := testCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := testCatalogs(t, conn, wrapper, prj.PublicId, 1)[0]
|
||||
host := testHosts(t, conn, catalog.PublicId, 1)[0]
|
||||
|
||||
newHostId, err := newHostId()
|
||||
@@ -940,10 +952,10 @@ func TestRepository_DeleteHost(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteHost(context.Background(), tt.in)
|
||||
got, err := repo.DeleteHost(context.Background(), catalog.ScopeId, tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
|
||||
@@ -4,23 +4,24 @@ import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
)
|
||||
|
||||
func TestRepository_New(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kmsCache := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
type args struct {
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
opts []Option
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
kms *kms.Kms
|
||||
opts []Option
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
@@ -32,38 +33,38 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "valid",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: kmsCache,
|
||||
defaultLimit: db.DefaultLimit,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-with-limit",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
opts: []Option{WithLimit(5)},
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
opts: []Option{WithLimit(5)},
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: kmsCache,
|
||||
defaultLimit: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nil-reader",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: nil,
|
||||
w: rw,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -71,19 +72,19 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "nil-writer",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: nil,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: nil,
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-wrapper",
|
||||
name: "nil-kms",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: nil,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -91,9 +92,9 @@ func TestRepository_New(t *testing.T) {
|
||||
{
|
||||
name: "all-nils",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: nil,
|
||||
wrapper: nil,
|
||||
r: nil,
|
||||
w: nil,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: db.ErrNilParameter,
|
||||
@@ -103,7 +104,7 @@ func TestRepository_New(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.wrapper, tt.args.opts...)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.kms, tt.args.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
|
||||
@@ -20,11 +20,13 @@ import (
|
||||
func Test_AccountUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
t.Run("simple-update", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
authMethodPublicId := testAuthMethod(t, conn, org.PublicId)
|
||||
acct := testAccount(t, conn, org.PublicId, authMethodPublicId, "")
|
||||
|
||||
@@ -45,12 +47,14 @@ func Test_AccountUpdate(t *testing.T) {
|
||||
func TestAccount_GetScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
t.Run("valid-org", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
authMethodPublicId := testAuthMethod(t, conn, org.PublicId)
|
||||
acct := testAccount(t, conn, org.PublicId, authMethodPublicId, u.PublicId)
|
||||
scope, err := acct.GetScope(context.Background(), w)
|
||||
@@ -62,10 +66,12 @@ func TestAccount_GetScope(t *testing.T) {
|
||||
func TestAccount_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
authMethodPublicId := testAuthMethod(t, conn, org.PublicId)
|
||||
acct := testAccount(t, conn, org.PublicId, authMethodPublicId, u.PublicId)
|
||||
cp := acct.Clone()
|
||||
@@ -74,7 +80,7 @@ func TestAccount_Clone(t *testing.T) {
|
||||
t.Run("not-equal", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
rw := db.New(conn)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
authMethodPublicId := testAuthMethod(t, conn, org.PublicId)
|
||||
acct := testAccount(t, conn, org.PublicId, authMethodPublicId, u.PublicId)
|
||||
acct2 := testAccount(t, conn, org.PublicId, authMethodPublicId, "")
|
||||
|
||||
@@ -15,13 +15,15 @@ import (
|
||||
func Test_NewGroupMember(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
org2, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
org2, _ := TestScopes(t, repo)
|
||||
|
||||
orgGroup := TestGroup(t, conn, org.PublicId)
|
||||
projGroup := TestGroup(t, conn, proj.PublicId)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
user2 := TestUser(t, conn, org2.PublicId)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
user2 := TestUser(t, repo, org2.PublicId)
|
||||
|
||||
type args struct {
|
||||
groupId string
|
||||
@@ -112,7 +114,9 @@ func Test_NewGroupMember(t *testing.T) {
|
||||
func Test_GroupMemberCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
type args struct {
|
||||
gm *GroupMemberUser
|
||||
}
|
||||
@@ -129,7 +133,7 @@ func Test_GroupMemberCreate(t *testing.T) {
|
||||
args: args{
|
||||
gm: func() *GroupMemberUser {
|
||||
g := TestGroup(t, conn, org.PublicId)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
gm, err := NewGroupMemberUser(g.PublicId, u.PublicId)
|
||||
require.NoError(t, err)
|
||||
return gm
|
||||
@@ -142,7 +146,7 @@ func Test_GroupMemberCreate(t *testing.T) {
|
||||
args: args{
|
||||
gm: func() *GroupMemberUser {
|
||||
g := TestGroup(t, conn, proj.PublicId)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
gm, err := NewGroupMemberUser(g.PublicId, u.PublicId)
|
||||
require.NoError(t, err)
|
||||
return gm
|
||||
@@ -155,7 +159,7 @@ func Test_GroupMemberCreate(t *testing.T) {
|
||||
args: args{
|
||||
gm: func() *GroupMemberUser {
|
||||
id := testId(t)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
gm, err := NewGroupMemberUser(id, u.PublicId)
|
||||
require.NoError(t, err)
|
||||
return gm
|
||||
@@ -182,7 +186,7 @@ func Test_GroupMemberCreate(t *testing.T) {
|
||||
name: "missing-group-id",
|
||||
args: args{
|
||||
gm: func() *GroupMemberUser {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
return &GroupMemberUser{
|
||||
GroupMemberUser: &store.GroupMemberUser{
|
||||
GroupId: "",
|
||||
@@ -215,7 +219,7 @@ func Test_GroupMemberCreate(t *testing.T) {
|
||||
args: args{
|
||||
gm: func() *GroupMemberUser {
|
||||
g := TestGroup(t, conn, org.PublicId)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
gm, err := NewGroupMemberUser(g.PublicId, u.PublicId)
|
||||
require.NoError(t, err)
|
||||
return gm
|
||||
@@ -259,14 +263,16 @@ func Test_GroupMemberCreate(t *testing.T) {
|
||||
func Test_GroupMemberUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
|
||||
t.Run("updates not allowed", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
g := TestGroup(t, conn, org.PublicId)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u2 := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
u2 := TestUser(t, repo, org.PublicId)
|
||||
gm := TestGroupMember(t, conn, g.PublicId, u.PublicId)
|
||||
updateGrpMember := gm.Clone().(*GroupMemberUser)
|
||||
updateGrpMember.MemberId = u2.PublicId
|
||||
@@ -279,10 +285,12 @@ func Test_GroupMemberUpdate(t *testing.T) {
|
||||
func Test_GroupMemberDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
rw := db.New(conn)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
org, _ := TestScopes(t, repo)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
g := TestGroup(t, conn, org.PublicId)
|
||||
|
||||
tests := []struct {
|
||||
@@ -333,8 +341,10 @@ func Test_GroupMemberDelete(t *testing.T) {
|
||||
func TestGroupMember_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
group := TestGroup(t, conn, org.PublicId)
|
||||
|
||||
@@ -20,7 +20,9 @@ import (
|
||||
func TestNewGroup(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
id := testId(t)
|
||||
|
||||
type args struct {
|
||||
@@ -96,7 +98,9 @@ func TestNewGroup(t *testing.T) {
|
||||
func Test_GroupCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
type args struct {
|
||||
group *Group
|
||||
}
|
||||
@@ -205,8 +209,10 @@ func Test_GroupCreate(t *testing.T) {
|
||||
func Test_GroupUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
type args struct {
|
||||
name string
|
||||
@@ -375,9 +381,11 @@ func Test_GroupUpdate(t *testing.T) {
|
||||
func Test_GroupDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
rw := db.New(conn)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -444,7 +452,9 @@ func TestGroup_ResourceType(t *testing.T) {
|
||||
func TestGroup_GetScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
t.Run("valid-org", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
@@ -467,7 +477,9 @@ func TestGroup_GetScope(t *testing.T) {
|
||||
func TestGroup_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@@ -16,11 +16,13 @@ import (
|
||||
func TestScope_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
w := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
new, _ := TestScopes(t, conn)
|
||||
new, _ := TestScopes(t, repo)
|
||||
var tests = []struct {
|
||||
name string
|
||||
update *Scope
|
||||
@@ -92,9 +94,11 @@ func TestConcreteScope_ImmutableFields(t *testing.T) {
|
||||
)
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
db := conn.DB()
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -142,12 +146,14 @@ func TestConcreteScope_ImmutableFields(t *testing.T) {
|
||||
func TestUser_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
w := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
new := TestUser(t, conn, org.PublicId)
|
||||
org, proj := TestScopes(t, repo)
|
||||
new := TestUser(t, repo, org.PublicId)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@@ -207,11 +213,13 @@ func TestUser_ImmutableFields(t *testing.T) {
|
||||
func TestRole_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
w := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
new := TestRole(t, conn, org.PublicId)
|
||||
|
||||
var tests = []struct {
|
||||
@@ -272,11 +280,13 @@ func TestRole_ImmutableFields(t *testing.T) {
|
||||
func TestGroup_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
w := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
new := TestGroup(t, conn, org.PublicId)
|
||||
|
||||
var tests = []struct {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package iam
|
||||
|
||||
import "io"
|
||||
|
||||
// getOpts - iterate the inbound Options and return a struct
|
||||
func getOpts(opt ...Option) options {
|
||||
opts := getDefaultOptions()
|
||||
@@ -23,6 +25,7 @@ type options struct {
|
||||
withGrantScopeId string
|
||||
withSkipVetForWrite bool
|
||||
withDisassociate bool
|
||||
withRandomReader io.Reader
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
@@ -106,3 +109,10 @@ func WithDisassociate(enable bool) Option {
|
||||
o.withDisassociate = enable
|
||||
}
|
||||
}
|
||||
|
||||
// WithRandomReader provides and option to specify a random reader.
|
||||
func WithRandomReader(reader io.Reader) Option {
|
||||
return func(o *options) {
|
||||
o.withRandomReader = reader
|
||||
}
|
||||
}
|
||||
@@ -15,10 +15,12 @@ import (
|
||||
func TestNewUserRole(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
orgRole := TestRole(t, conn, org.PublicId)
|
||||
projRole := TestRole(t, conn, proj.PublicId)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
|
||||
type args struct {
|
||||
roleId string
|
||||
@@ -97,8 +99,10 @@ func TestNewUserRole(t *testing.T) {
|
||||
func TestUserRole_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
org2, proj2 := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
org2, proj2 := TestScopes(t, repo)
|
||||
type args struct {
|
||||
role *UserRole
|
||||
}
|
||||
@@ -115,7 +119,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
role := TestRole(t, conn, org.PublicId)
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
principalRole, err := NewUserRole(role.PublicId, principal.PublicId)
|
||||
require.NoError(t, err)
|
||||
return principalRole
|
||||
@@ -128,7 +132,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
role := TestRole(t, conn, proj.PublicId)
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
principalRole, err := NewUserRole(role.PublicId, principal.PublicId)
|
||||
require.NoError(t, err)
|
||||
return principalRole
|
||||
@@ -141,7 +145,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
role := TestRole(t, conn, org2.PublicId)
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
principalRole, err := NewUserRole(role.PublicId, principal.PublicId)
|
||||
require.NoError(t, err)
|
||||
return principalRole
|
||||
@@ -153,7 +157,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
role := TestRole(t, conn, proj2.PublicId)
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
principalRole, err := NewUserRole(role.PublicId, principal.PublicId)
|
||||
require.NoError(t, err)
|
||||
return principalRole
|
||||
@@ -165,7 +169,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
id := testId(t)
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
principalRole, err := NewUserRole(id, principal.PublicId)
|
||||
require.NoError(t, err)
|
||||
return principalRole
|
||||
@@ -192,7 +196,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
name: "missing-role-id",
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
return &UserRole{
|
||||
UserRole: &store.UserRole{
|
||||
RoleId: "",
|
||||
@@ -227,7 +231,7 @@ func TestUserRole_Create(t *testing.T) {
|
||||
args: args{
|
||||
role: func() *UserRole {
|
||||
role := TestRole(t, conn, org.PublicId)
|
||||
principal := TestUser(t, conn, org.PublicId)
|
||||
principal := TestUser(t, repo, org.PublicId)
|
||||
principalRole, err := NewUserRole(role.PublicId, principal.PublicId)
|
||||
require.NoError(t, err)
|
||||
return principalRole
|
||||
@@ -271,14 +275,16 @@ func TestUserRole_Create(t *testing.T) {
|
||||
func TestUserRole_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
|
||||
t.Run("updates not allowed", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
r := TestRole(t, conn, org.PublicId)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u2 := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
u2 := TestUser(t, repo, org.PublicId)
|
||||
userRole := TestUserRole(t, conn, r.PublicId, u.PublicId)
|
||||
updateRole := userRole.Clone().(*UserRole)
|
||||
updateRole.PrincipalId = u2.PublicId
|
||||
@@ -291,10 +297,12 @@ func TestUserRole_Update(t *testing.T) {
|
||||
func TestUserRole_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
rw := db.New(conn)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
org, _ := TestScopes(t, repo)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
r := TestRole(t, conn, org.PublicId)
|
||||
|
||||
tests := []struct {
|
||||
@@ -345,8 +353,10 @@ func TestUserRole_Delete(t *testing.T) {
|
||||
func TestUserRole_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
role := TestRole(t, conn, org.PublicId)
|
||||
@@ -368,7 +378,9 @@ func TestUserRole_Clone(t *testing.T) {
|
||||
func TestNewGroupRole(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
orgRole := TestRole(t, conn, org.PublicId)
|
||||
projRole := TestRole(t, conn, proj.PublicId)
|
||||
group := TestGroup(t, conn, org.PublicId)
|
||||
@@ -450,8 +462,10 @@ func TestNewGroupRole(t *testing.T) {
|
||||
func TestGroupRole_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
org2, proj2 := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
org2, proj2 := TestScopes(t, repo)
|
||||
type args struct {
|
||||
role *GroupRole
|
||||
}
|
||||
@@ -639,7 +653,9 @@ func TestGroupRole_Create(t *testing.T) {
|
||||
func TestGroupRole_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
|
||||
t.Run("updates not allowed", func(t *testing.T) {
|
||||
@@ -659,9 +675,11 @@ func TestGroupRole_Update(t *testing.T) {
|
||||
func TestGroupRole_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
rw := db.New(conn)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
org, _ := TestScopes(t, repo)
|
||||
g := TestGroup(t, conn, org.PublicId)
|
||||
r := TestRole(t, conn, org.PublicId)
|
||||
|
||||
@@ -718,7 +736,9 @@ func TestGroupRole_Delete(t *testing.T) {
|
||||
func TestGroupRole_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
grp := TestGroup(t, conn, org.PublicId)
|
||||
|
||||
+44
-13
@@ -7,9 +7,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -18,9 +18,9 @@ var (
|
||||
|
||||
// Repository is the iam database repository
|
||||
type Repository struct {
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
kms *kms.Kms
|
||||
|
||||
// defaultLimit provides a default for limiting the number of results returned from the repo
|
||||
defaultLimit int
|
||||
@@ -28,15 +28,15 @@ type Repository struct {
|
||||
|
||||
// NewRepository creates a new iam Repository. Supports the options: WithLimit
|
||||
// which sets a default limit on results returned by repo operations.
|
||||
func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Option) (*Repository, error) {
|
||||
func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
|
||||
if r == nil {
|
||||
return nil, errors.New("error creating db repository with nil reader")
|
||||
}
|
||||
if w == nil {
|
||||
return nil, errors.New("error creating db repository with nil writer")
|
||||
}
|
||||
if wrapper == nil {
|
||||
return nil, errors.New("error creating db repository with nil wrapper")
|
||||
if kms == nil {
|
||||
return nil, errors.New("error creating db repository with nil kms")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
if opts.withLimit == 0 {
|
||||
@@ -46,7 +46,7 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Op
|
||||
return &Repository{
|
||||
reader: r,
|
||||
writer: w,
|
||||
wrapper: wrapper,
|
||||
kms: kms,
|
||||
defaultLimit: opts.withLimit,
|
||||
}, nil
|
||||
}
|
||||
@@ -78,6 +78,15 @@ func (r *Repository) create(ctx context.Context, resource Resource, opt ...Optio
|
||||
}
|
||||
metadata["op-type"] = []string{oplog.OpType_OP_TYPE_CREATE.String()}
|
||||
|
||||
scope, err := resource.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get scope: %w", err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var returnedResource interface{}
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -88,7 +97,7 @@ func (r *Repository) create(ctx context.Context, resource Resource, opt ...Optio
|
||||
return w.Create(
|
||||
ctx,
|
||||
returnedResource,
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
},
|
||||
)
|
||||
@@ -114,7 +123,6 @@ func (r *Repository) update(ctx context.Context, resource Resource, version uint
|
||||
metadata["op-type"] = []string{oplog.OpType_OP_TYPE_UPDATE.String()}
|
||||
|
||||
dbOpts := []db.Option{
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithVersion(&version),
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
@@ -122,6 +130,22 @@ func (r *Repository) update(ctx context.Context, resource Resource, version uint
|
||||
dbOpts = append(dbOpts, db.WithSkipVetForWrite(true))
|
||||
}
|
||||
|
||||
var scope *Scope
|
||||
switch t := resource.(type) {
|
||||
case *Scope:
|
||||
scope = t
|
||||
default:
|
||||
scope, err = resource.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("unable to get scope: %w", err)
|
||||
}
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
dbOpts = append(dbOpts, db.WithOplog(oplogWrapper, metadata))
|
||||
|
||||
var rowsUpdated int
|
||||
var returnedResource interface{}
|
||||
_, err = r.writer.DoTx(
|
||||
@@ -130,7 +154,6 @@ func (r *Repository) update(ctx context.Context, resource Resource, version uint
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
returnedResource = resourceCloner.Clone()
|
||||
var err error
|
||||
rowsUpdated, err = w.Update(
|
||||
ctx,
|
||||
returnedResource,
|
||||
@@ -163,6 +186,15 @@ func (r *Repository) delete(ctx context.Context, resource Resource, opt ...Optio
|
||||
}
|
||||
metadata["op-type"] = []string{oplog.OpType_OP_TYPE_DELETE.String()}
|
||||
|
||||
scope, err := resource.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("unable to get scope: %w", err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
var deleteResource interface{}
|
||||
_, err = r.writer.DoTx(
|
||||
@@ -171,11 +203,10 @@ func (r *Repository) delete(ctx context.Context, resource Resource, opt ...Optio
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
deleteResource = resourceCloner.Clone()
|
||||
var err error
|
||||
rowsDeleted, err = w.Delete(
|
||||
ctx,
|
||||
deleteResource,
|
||||
db.WithOplog(r.wrapper, metadata),
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
// return err, which will result in a rollback of the delete
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbcommon "github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -89,7 +90,7 @@ func (r *Repository) UpdateGroup(ctx context.Context, group *Group, version uint
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo, err := NewRepository(read, w, r.wrapper)
|
||||
repo, err := NewRepository(read, w, r.kms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update group: failed creating inner repo: %w for %s", err, group.PublicId)
|
||||
}
|
||||
@@ -126,7 +127,7 @@ func (r *Repository) LookupGroup(ctx context.Context, withPublicId string, opt .
|
||||
if err := read.LookupByPublicId(ctx, &g); err != nil {
|
||||
return fmt.Errorf("lookup group: failed %w for %s", err, withPublicId)
|
||||
}
|
||||
repo, err := NewRepository(read, w, r.wrapper)
|
||||
repo, err := NewRepository(read, w, r.kms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("lookup group: failed creating inner repo: %w for %s", err, withPublicId)
|
||||
}
|
||||
@@ -215,6 +216,11 @@ func (r *Repository) AddGroupMembers(ctx context.Context, groupId string, groupV
|
||||
newGroupMembers = append(newGroupMembers, gm)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add group members: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var currentMembers []*GroupMember
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -249,14 +255,14 @@ func (r *Repository) AddGroupMembers(ctx context.Context, groupId string, groupV
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{groupId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, groupTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, groupTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("add group members: unable to write oplog: %w", err)
|
||||
}
|
||||
// we need a new repo, that's using the same reader/writer as this TxHandler
|
||||
txRepo := Repository{
|
||||
reader: reader,
|
||||
writer: w,
|
||||
wrapper: r.wrapper,
|
||||
reader: reader,
|
||||
writer: w,
|
||||
kms: r.kms,
|
||||
// intentionally not setting the defaultLimit, so we'll get all
|
||||
// the members without a limit
|
||||
}
|
||||
@@ -302,6 +308,11 @@ func (r *Repository) DeleteGroupMembers(ctx context.Context, groupId string, gro
|
||||
deleteMembers = append(deleteMembers, member)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("add group members: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var totalRowsDeleted int
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -341,7 +352,7 @@ func (r *Repository) DeleteGroupMembers(ctx context.Context, groupId string, gro
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{groupId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, groupTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, groupTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("delete group members: unable to write oplog: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -414,6 +425,11 @@ func (r *Repository) SetGroupMembers(ctx context.Context, groupId string, groupV
|
||||
return currentMembers, db.NoRowsAffected, nil
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("add group members: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var totalRowsAffected int
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -470,14 +486,14 @@ func (r *Repository) SetGroupMembers(ctx context.Context, groupId string, groupV
|
||||
}
|
||||
// we're done with all the membership writes, so let's write the
|
||||
// group's update oplog message
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, groupTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, groupTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("set group members: unable to write oplog for additions: %w", err)
|
||||
}
|
||||
// we need a new repo, that's using the same reader/writer as this TxHandler
|
||||
txRepo := Repository{
|
||||
reader: reader,
|
||||
writer: w,
|
||||
wrapper: r.wrapper,
|
||||
reader: reader,
|
||||
writer: w,
|
||||
kms: r.kms,
|
||||
// intentionally not setting the defaultLimit, so we'll get all
|
||||
// the members without a limit
|
||||
}
|
||||
|
||||
@@ -21,11 +21,10 @@ func TestRepository_CreateGroup(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
group *Group
|
||||
@@ -177,12 +176,11 @@ func TestRepository_UpdateGroup(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
a.NoError(err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
a.NoError(err)
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
pubId := func(s string) *string { return &s }
|
||||
|
||||
type args struct {
|
||||
@@ -473,9 +471,8 @@ func TestRepository_DeleteGroup(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
a.NoError(err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
group *Group
|
||||
@@ -555,11 +552,9 @@ func TestRepository_ListGroups(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
withScopeId string
|
||||
@@ -651,11 +646,9 @@ func TestRepository_ListMembers(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org, proj := TestScopes(t, repo)
|
||||
pg := TestGroup(t, conn, proj.PublicId)
|
||||
og := TestGroup(t, conn, org.PublicId)
|
||||
|
||||
@@ -716,7 +709,7 @@ func TestRepository_ListMembers(t *testing.T) {
|
||||
require.NoError(conn.Where("1=1").Delete(allocGroupMember()).Error)
|
||||
gm := []*GroupMemberUser{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
gm = append(gm, TestGroupMember(t, conn, tt.args.withGroupId, u.PublicId))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(gm))
|
||||
@@ -745,14 +738,13 @@ func TestRepository_AddGroupMembers(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
group := TestGroup(t, conn, proj.PublicId)
|
||||
createUsersFn := func() []string {
|
||||
results := []string{}
|
||||
for i := 0; i < 5; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
results = append(results, u.PublicId)
|
||||
}
|
||||
return results
|
||||
@@ -864,9 +856,8 @@ func TestRepository_DeleteGroupMembers(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
group *Group
|
||||
@@ -959,7 +950,7 @@ func TestRepository_DeleteGroupMembers(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
userIds := make([]string, 0, tt.args.createUserCnt)
|
||||
for i := 0; i < tt.args.createUserCnt; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
userIds = append(userIds, u.PublicId)
|
||||
}
|
||||
members, err := repo.AddGroupMembers(context.Background(), tt.args.group.PublicId, 1, userIds, tt.args.opt...)
|
||||
@@ -1001,19 +992,16 @@ func TestRepository_DeleteGroupMembers(t *testing.T) {
|
||||
func TestRepository_SetGroupMembers(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
testUser := TestUser(t, conn, org.PublicId)
|
||||
org, proj := TestScopes(t, repo)
|
||||
testUser := TestUser(t, repo, org.PublicId)
|
||||
|
||||
createUsersFn := func() []string {
|
||||
results := []string{}
|
||||
for i := 0; i < 5; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
results = append(results, u.PublicId)
|
||||
}
|
||||
return results
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
)
|
||||
|
||||
@@ -29,13 +30,6 @@ func (r *Repository) AddPrincipalRoles(ctx context.Context, roleId string, roleV
|
||||
return nil, fmt.Errorf("add principal roles: missing either user or groups to add: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
|
||||
role := allocRole()
|
||||
role.PublicId = roleId
|
||||
roleScope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add principal roles: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
|
||||
newUserRoles := make([]interface{}, 0, len(userIds))
|
||||
for _, id := range userIds {
|
||||
usrRole, err := NewUserRole(roleId, id)
|
||||
@@ -52,6 +46,19 @@ func (r *Repository) AddPrincipalRoles(ctx context.Context, roleId string, roleV
|
||||
}
|
||||
newGrpRoles = append(newGrpRoles, grpRole)
|
||||
}
|
||||
|
||||
role := allocRole()
|
||||
role.PublicId = roleId
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add principal roles: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add principal roles: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var currentPrincipals []PrincipalRole
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -91,18 +98,18 @@ func (r *Repository) AddPrincipalRoles(ctx context.Context, roleId string, roleV
|
||||
}
|
||||
metadata := oplog.Metadata{
|
||||
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
|
||||
"scope-id": []string{roleScope.PublicId},
|
||||
"scope-type": []string{roleScope.Type},
|
||||
"scope-id": []string{scope.PublicId},
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{roleId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("add principal roles: unable to write oplog: %w", err)
|
||||
}
|
||||
// we need a new repo, that's using the same reader/writer as this TxHandler
|
||||
txRepo := &Repository{
|
||||
reader: reader,
|
||||
writer: w,
|
||||
wrapper: r.wrapper,
|
||||
reader: reader,
|
||||
writer: w,
|
||||
kms: r.kms,
|
||||
// intentionally not setting the defaultLimit, so we'll get all
|
||||
// the principal roles without a limit
|
||||
}
|
||||
@@ -133,10 +140,7 @@ func (r *Repository) SetPrincipalRoles(ctx context.Context, roleId string, roleV
|
||||
}
|
||||
role := allocRole()
|
||||
role.PublicId = roleId
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set principal roles: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
|
||||
// it's "safe" to do this lookup outside the DoTx transaction because we
|
||||
// have a roleVersion so the principals can’t change without the version
|
||||
// changing.
|
||||
@@ -154,6 +158,15 @@ func (r *Repository) SetPrincipalRoles(ctx context.Context, roleId string, roleV
|
||||
return toSet.unchangedPrincipalRoles, db.NoRowsAffected, nil
|
||||
}
|
||||
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set principal roles: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set principal roles: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var currentPrincipals []PrincipalRole
|
||||
var totalRowsAffected int
|
||||
_, err = r.writer.DoTx(
|
||||
@@ -234,14 +247,14 @@ func (r *Repository) SetPrincipalRoles(ctx context.Context, roleId string, roleV
|
||||
msgs = append(msgs, grpOplogMsgs...)
|
||||
}
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("set principal roles: unable to write oplog for additions: %w", err)
|
||||
}
|
||||
// we need a new repo, that's using the same reader/writer as this TxHandler
|
||||
txRepo := &Repository{
|
||||
reader: reader,
|
||||
writer: w,
|
||||
wrapper: r.wrapper,
|
||||
reader: reader,
|
||||
writer: w,
|
||||
kms: r.kms,
|
||||
// intentionally not setting the defaultLimit, so we'll get all
|
||||
// the principal roles without a limit
|
||||
}
|
||||
@@ -277,10 +290,7 @@ func (r *Repository) DeletePrincipalRoles(ctx context.Context, roleId string, ro
|
||||
}
|
||||
role := allocRole()
|
||||
role.PublicId = roleId
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete principal roles: unable to get role %s scope to create metadata: %w", roleId, err)
|
||||
}
|
||||
|
||||
deleteUserRoles := make([]interface{}, 0, len(userIds))
|
||||
for _, id := range userIds {
|
||||
usrRole, err := NewUserRole(roleId, id)
|
||||
@@ -298,6 +308,15 @@ func (r *Repository) DeletePrincipalRoles(ctx context.Context, roleId string, ro
|
||||
deleteGrpRoles = append(deleteGrpRoles, grpRole)
|
||||
}
|
||||
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete principal roles: unable to get role %s scope to create metadata: %w", roleId, err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete principal roles: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var totalRowsDeleted int
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -351,7 +370,7 @@ func (r *Repository) DeletePrincipalRoles(ctx context.Context, roleId string, ro
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{roleId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("delete principal roles: unable to write oplog: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -18,14 +18,13 @@ func TestRepository_AddPrincipalRoles(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
staticOrg, staticProj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
staticOrg, staticProj := TestScopes(t, repo)
|
||||
orgRole := TestRole(t, conn, staticOrg.PublicId)
|
||||
projRole := TestRole(t, conn, staticProj.PublicId)
|
||||
require.NoError(t, err)
|
||||
createScopesFn := func() (orgs []string, projects []string) {
|
||||
for i := 0; i < 5; i++ {
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
orgs = append(orgs, org.PublicId)
|
||||
projects = append(projects, proj.PublicId)
|
||||
}
|
||||
@@ -34,7 +33,7 @@ func TestRepository_AddPrincipalRoles(t *testing.T) {
|
||||
createUsersFn := func(orgs []string) []string {
|
||||
results := []string{}
|
||||
for org := 0; org < 5; org++ {
|
||||
u := TestUser(t, conn, orgs[org])
|
||||
u := TestUser(t, repo, orgs[org])
|
||||
results = append(results, u.PublicId)
|
||||
}
|
||||
return results
|
||||
@@ -128,7 +127,7 @@ func TestRepository_AddPrincipalRoles(t *testing.T) {
|
||||
|
||||
if tt.args.wantUserIds {
|
||||
userIds = createUsersFn(orgs)
|
||||
u := TestUser(t, conn, staticOrg.PublicId)
|
||||
u := TestUser(t, repo, staticOrg.PublicId)
|
||||
if roleId == orgRole.PublicId {
|
||||
userIds = append(userIds, u.PublicId)
|
||||
} else {
|
||||
@@ -186,11 +185,9 @@ func TestRepository_ListPrincipalRoles(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
withRoleId string
|
||||
@@ -260,7 +257,7 @@ func TestRepository_ListPrincipalRoles(t *testing.T) {
|
||||
userRoles := make([]string, 0, tt.createCnt)
|
||||
groupRoles := make([]string, 0, tt.createCnt)
|
||||
for i := 0; i < tt.createCnt/2; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
userRoles = append(userRoles, u.PublicId)
|
||||
g := TestGroup(t, conn, tt.createScopeId)
|
||||
groupRoles = append(groupRoles, g.PublicId)
|
||||
@@ -293,9 +290,8 @@ func TestRepository_DeletePrincipalRoles(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
role *Role
|
||||
@@ -426,7 +422,7 @@ func TestRepository_DeletePrincipalRoles(t *testing.T) {
|
||||
}
|
||||
createScopesFn := func() (orgs []string, projects []string) {
|
||||
for i := 0; i < 5; i++ {
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
orgs = append(orgs, org.PublicId)
|
||||
projects = append(projects, proj.PublicId)
|
||||
}
|
||||
@@ -438,10 +434,10 @@ func TestRepository_DeletePrincipalRoles(t *testing.T) {
|
||||
orgs, projects := createScopesFn()
|
||||
userIds := make([]string, 0, tt.args.createUserCnt)
|
||||
if tt.args.createUserCnt > 0 {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
userIds = append(userIds, u.PublicId)
|
||||
for i := 0; i < tt.args.createUserCnt-1; i++ {
|
||||
u := TestUser(t, conn, orgs[i])
|
||||
u := TestUser(t, repo, orgs[i])
|
||||
userIds = append(userIds, u.PublicId)
|
||||
}
|
||||
}
|
||||
@@ -506,19 +502,17 @@ func TestRepository_DeletePrincipalRoles(t *testing.T) {
|
||||
func TestRepository_SetPrincipalRoles(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
testUser := TestUser(t, conn, org.PublicId)
|
||||
org, proj := TestScopes(t, repo)
|
||||
testUser := TestUser(t, repo, org.PublicId)
|
||||
testGrp := TestGroup(t, conn, proj.PublicId)
|
||||
|
||||
createUsersFn := func() []string {
|
||||
results := []string{}
|
||||
for i := 0; i < 5; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
results = append(results, u.PublicId)
|
||||
}
|
||||
results = append(results, "u_anon")
|
||||
@@ -677,15 +671,13 @@ func TestRepository_SetPrincipalRoles(t *testing.T) {
|
||||
func TestRepository_principalsToSet(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
createUsersFn := func() []string {
|
||||
results := []string{}
|
||||
for i := 0; i < 5; i++ {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
results = append(results, u.PublicId)
|
||||
}
|
||||
return results
|
||||
@@ -799,7 +791,7 @@ func TestRepository_principalsToSet(t *testing.T) {
|
||||
wantDeleteGrps = append(wantDeleteGrps, id)
|
||||
}
|
||||
}
|
||||
newUser := TestUser(t, conn, org.PublicId)
|
||||
newUser := TestUser(t, repo, org.PublicId)
|
||||
newGrp := TestGroup(t, conn, proj.PublicId)
|
||||
wantSetUsers = append(wantSetUsers, newUser.PublicId)
|
||||
wantSetGrps = append(wantSetGrps, newGrp.PublicId)
|
||||
|
||||
@@ -92,7 +92,7 @@ func (r *Repository) UpdateRole(ctx context.Context, role *Role, version uint32,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo, err := NewRepository(read, w, r.wrapper)
|
||||
repo, err := NewRepository(read, w, r.kms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update role: failed creating inner repo: %w for %s", err, role.PublicId)
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func (r *Repository) LookupRole(ctx context.Context, withPublicId string, opt ..
|
||||
if err := read.LookupByPublicId(ctx, &role); err != nil {
|
||||
return fmt.Errorf("lookup role: failed %w for %s", err, withPublicId)
|
||||
}
|
||||
repo, err := NewRepository(read, w, r.wrapper)
|
||||
repo, err := NewRepository(read, w, r.kms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("lookup role: failed creating inner repo: %w for %s", err, withPublicId)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/hashicorp/boundary/internal/perms"
|
||||
)
|
||||
@@ -34,7 +35,16 @@ func (r *Repository) AddRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
newRoleGrants = append(newRoleGrants, roleGrant)
|
||||
}
|
||||
|
||||
_, err := r.writer.DoTx(
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add role grants: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add role grants: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
@@ -64,18 +74,13 @@ func (r *Repository) AddRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
}
|
||||
msgs = append(msgs, roleGrantOplogMsgs...)
|
||||
|
||||
s, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("add role grants: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
|
||||
metadata := oplog.Metadata{
|
||||
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
|
||||
"scope-id": []string{s.PublicId},
|
||||
"scope-type": []string{s.Type},
|
||||
"scope-id": []string{scope.PublicId},
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{roleId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("unable to write oplog: %w", err)
|
||||
}
|
||||
|
||||
@@ -109,8 +114,17 @@ func (r *Repository) DeleteRoleGrants(ctx context.Context, roleId string, roleVe
|
||||
role := allocRole()
|
||||
role.PublicId = roleId
|
||||
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete role grants: unable to get role %s scope to create metadata: %w", roleId, err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete role grants: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var totalRowsDeleted int
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
@@ -179,18 +193,13 @@ func (r *Repository) DeleteRoleGrants(ctx context.Context, roleId string, roleVe
|
||||
totalRowsDeleted = rowsDeleted
|
||||
msgs = append(msgs, roleGrantOplogMsgs...)
|
||||
|
||||
s, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete role grants: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
|
||||
metadata := oplog.Metadata{
|
||||
"op-type": []string{oplog.OpType_OP_TYPE_DELETE.String()},
|
||||
"scope-id": []string{s.PublicId},
|
||||
"scope-type": []string{s.Type},
|
||||
"scope-id": []string{scope.PublicId},
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{roleId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("delete role grants: unable to write oplog: %w", err)
|
||||
}
|
||||
|
||||
@@ -208,15 +217,15 @@ func (r *Repository) DeleteRoleGrants(ctx context.Context, roleId string, roleVe
|
||||
// value for the WithVersion option and will return an error.
|
||||
func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersion uint32, grants []string, opt ...Option) ([]*RoleGrant, int, error) {
|
||||
if roleId == "" {
|
||||
return nil, 0, fmt.Errorf("set role grants: missing role id %w", db.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: missing role id %w", db.ErrInvalidParameter)
|
||||
}
|
||||
if roleVersion == 0 {
|
||||
return nil, 0, fmt.Errorf("set role grants: version cannot be zero: %w", db.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: version cannot be zero: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
|
||||
// Explicitly set to zero clears, but treat nil as a mistake
|
||||
if grants == nil {
|
||||
return nil, 0, fmt.Errorf("set role grants: nil grants: %w", db.ErrNilParameter)
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: nil grants: %w", db.ErrNilParameter)
|
||||
}
|
||||
|
||||
role := allocRole()
|
||||
@@ -229,7 +238,7 @@ func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
// Find existing grants
|
||||
roleGrants := []*RoleGrant{}
|
||||
if err := r.reader.SearchWhere(ctx, &roleGrants, "role_id = ?", []interface{}{roleId}); err != nil {
|
||||
return nil, 0, fmt.Errorf("set role grants: unable to search for grants: %w", err)
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: unable to search for grants: %w", err)
|
||||
}
|
||||
found := map[string]*RoleGrant{}
|
||||
for _, rg := range roleGrants {
|
||||
@@ -244,7 +253,7 @@ func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
// Use a fake scope, just want to get out a canonical string
|
||||
perm, err := perms.Parse("o_abcd1234", "", grant)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("set role grants: error parsing grant string: %w", err)
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: error parsing grant string: %w", err)
|
||||
}
|
||||
canonicalString := perm.CanonicalString()
|
||||
|
||||
@@ -260,7 +269,7 @@ func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
// Not found, so add
|
||||
rg, err = NewRoleGrant(roleId, grant)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("set role grants: unable to create in memory role grant: %w", err)
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: unable to create in memory role grant: %w", err)
|
||||
}
|
||||
addRoleGrants = append(addRoleGrants, rg)
|
||||
currentRoleGrants = append(currentRoleGrants, rg)
|
||||
@@ -273,11 +282,20 @@ func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
}
|
||||
|
||||
if len(addRoleGrants) == 0 && len(deleteRoleGrants) == 0 {
|
||||
return currentRoleGrants, 0, nil
|
||||
return currentRoleGrants, db.NoRowsAffected, nil
|
||||
}
|
||||
|
||||
scope, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scope.GetPublicId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set role grants: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
var totalRowsDeleted int
|
||||
_, err := r.writer.DoTx(
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
@@ -323,18 +341,13 @@ func (r *Repository) SetRoleGrants(ctx context.Context, roleId string, roleVersi
|
||||
msgs = append(msgs, roleGrantOplogMsgs...)
|
||||
}
|
||||
|
||||
s, err := role.GetScope(ctx, r.reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set role grants: unable to get role %s scope: %w", roleId, err)
|
||||
}
|
||||
|
||||
metadata := oplog.Metadata{
|
||||
"op-type": []string{oplog.OpType_OP_TYPE_DELETE.String(), oplog.OpType_OP_TYPE_CREATE.String()},
|
||||
"scope-id": []string{s.PublicId},
|
||||
"scope-type": []string{s.Type},
|
||||
"scope-id": []string{scope.PublicId},
|
||||
"scope-type": []string{scope.Type},
|
||||
"resource-public-id": []string{roleId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("set role grants: unable to write oplog: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,9 +19,8 @@ func TestRepository_AddRoleGrants(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
_, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
_, proj := TestScopes(t, repo)
|
||||
role := TestRole(t, conn, proj.PublicId)
|
||||
createGrantsFn := func() []string {
|
||||
results := []string{}
|
||||
@@ -124,11 +123,9 @@ func TestRepository_ListRoleGrants(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
withRoleId string
|
||||
@@ -226,9 +223,8 @@ func TestRepository_DeleteRoleGrants(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
role *Role
|
||||
@@ -410,11 +406,9 @@ func TestRepository_SetRoleGrants_Randomize(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
role := TestRole(t, conn, org.PublicId)
|
||||
require.NoError(conn.Where("1=1").Delete(allocRoleGrant()).Error)
|
||||
|
||||
@@ -471,7 +465,7 @@ func TestRepository_SetRoleGrants_Randomize(t *testing.T) {
|
||||
}
|
||||
|
||||
// At the end, set to explicitly empty and make sure all are cleared out
|
||||
_, _, err = repo.SetRoleGrants(context.Background(), role.PublicId, uint32(totalCnt+1), []string{})
|
||||
_, _, err := repo.SetRoleGrants(context.Background(), role.PublicId, uint32(totalCnt+1), []string{})
|
||||
require.NoError(err)
|
||||
|
||||
roleGrants, err := repo.ListRoleGrants(context.Background(), role.PublicId)
|
||||
@@ -482,11 +476,9 @@ func TestRepository_SetRoleGrants_Randomize(t *testing.T) {
|
||||
func TestRepository_SetRoleGrants_Parameters(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
role := TestRole(t, conn, org.PublicId)
|
||||
type args struct {
|
||||
roleId string
|
||||
|
||||
@@ -22,11 +22,10 @@ func TestRepository_CreateRole(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
role *Role
|
||||
@@ -177,13 +176,12 @@ func TestRepository_UpdateRole(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
require.NoError(t, err)
|
||||
|
||||
org, proj := TestScopes(t, conn)
|
||||
u := TestUser(t, conn, org.GetPublicId())
|
||||
org, proj := TestScopes(t, repo)
|
||||
u := TestUser(t, repo, org.GetPublicId())
|
||||
|
||||
pubId := func(s string) *string { return &s }
|
||||
|
||||
@@ -470,9 +468,8 @@ func TestRepository_DeleteRole(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
role *Role
|
||||
@@ -552,11 +549,9 @@ func TestRepository_ListRoles(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, proj := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
withScopeId string
|
||||
|
||||
@@ -2,19 +2,24 @@ package iam
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbcommon "github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
kmsCommon "github.com/hashicorp/boundary/internal/kms/common"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
)
|
||||
|
||||
// CreateScope will create a scope in the repository and return the written
|
||||
// scope. Supported options include: WithPublicId.
|
||||
// scope. Supported options include: WithPublicId and WithRandomReader.
|
||||
func (r *Repository) CreateScope(ctx context.Context, s *Scope, userId string, opt ...Option) (*Scope, error) {
|
||||
if s == nil {
|
||||
return nil, fmt.Errorf("create scope: missing scope %w", db.ErrNilParameter)
|
||||
@@ -25,16 +30,32 @@ func (r *Repository) CreateScope(ctx context.Context, s *Scope, userId string, o
|
||||
if s.PublicId != "" {
|
||||
return nil, fmt.Errorf("create scope: public id not empty: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
|
||||
var parentOplogWrapper wrapping.Wrapper
|
||||
var externalWrappers *kms.ExternalWrappers
|
||||
var err error
|
||||
switch s.Type {
|
||||
case scope.Unknown.String():
|
||||
return nil, fmt.Errorf("create scope: unknown type: %w", db.ErrInvalidParameter)
|
||||
case scope.Global.String():
|
||||
return nil, fmt.Errorf("create scope: invalid type: %w", db.ErrInvalidParameter)
|
||||
default:
|
||||
switch s.ParentId {
|
||||
case "":
|
||||
return nil, fmt.Errorf("create scope: missing parent id: %w", db.ErrNilParameter)
|
||||
case scope.Global.String():
|
||||
parentOplogWrapper, err = r.kms.GetWrapper(ctx, scope.Global.String(), kms.KeyPurposeOplog)
|
||||
default:
|
||||
parentOplogWrapper, err = r.kms.GetWrapper(ctx, s.ParentId, kms.KeyPurposeOplog)
|
||||
}
|
||||
externalWrappers = r.kms.GetExternalWrappers()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create scope: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
var err error
|
||||
var scopePublicId string
|
||||
var scopeMetadata oplog.Metadata
|
||||
var scopeRaw interface{}
|
||||
@@ -98,19 +119,45 @@ func (r *Repository) CreateScope(ctx context.Context, s *Scope, userId string, o
|
||||
}
|
||||
}
|
||||
|
||||
reader := opts.withRandomReader
|
||||
if reader == nil {
|
||||
reader = rand.Reader
|
||||
}
|
||||
rootKey, err := uuid.GenerateRandomBytesWithReader(32, reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create scope: error generating random bytes for scope root key: %w", err)
|
||||
}
|
||||
|
||||
_, err = r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
func(dbr db.Reader, w db.Writer) error {
|
||||
if err := w.Create(
|
||||
ctx,
|
||||
scopeRaw,
|
||||
db.WithOplog(r.wrapper, scopeMetadata),
|
||||
db.WithOplog(parentOplogWrapper, scopeMetadata),
|
||||
); err != nil {
|
||||
return fmt.Errorf("error creating scope: %w", err)
|
||||
}
|
||||
|
||||
s := scopeRaw.(*Scope)
|
||||
|
||||
// Create the scope's root key
|
||||
_, _, err := kmsCommon.CreateRootKeyTx(ctx, w, externalWrappers.Root(), s.PublicId, rootKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating scope root key: %w", err)
|
||||
}
|
||||
|
||||
kmsRepo, err := kms.NewRepository(dbr, w)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new kms repo: %w", err)
|
||||
}
|
||||
childOplogWrapper, err := r.kms.GetWrapper(ctx, s.PublicId, kms.KeyPurposeOplog, kms.WithRepository(kmsRepo))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching new scope oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
// We create a new role, then set grants and principals on it. This
|
||||
// turns into a bunch of stuff sadly because the role is the
|
||||
// aggregate.
|
||||
@@ -118,7 +165,7 @@ func (r *Repository) CreateScope(ctx context.Context, s *Scope, userId string, o
|
||||
if err := w.Create(
|
||||
ctx,
|
||||
roleRaw,
|
||||
db.WithOplog(r.wrapper, roleMetadata),
|
||||
db.WithOplog(childOplogWrapper, roleMetadata),
|
||||
); err != nil {
|
||||
return fmt.Errorf("error creating role: %w", err)
|
||||
}
|
||||
@@ -163,15 +210,13 @@ func (r *Repository) CreateScope(ctx context.Context, s *Scope, userId string, o
|
||||
}
|
||||
msgs = append(msgs, roleUserOplogMsgs...)
|
||||
|
||||
s := scopeRaw.(*Scope)
|
||||
|
||||
metadata := oplog.Metadata{
|
||||
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
|
||||
"scope-id": []string{s.PublicId},
|
||||
"scope-type": []string{s.Type},
|
||||
"resource-public-id": []string{role.PublicId},
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, roleTicket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, childOplogWrapper, roleTicket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("unable to write oplog: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,13 @@ import (
|
||||
func Test_Repository_Scope_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s, err := NewOrg(WithName(id))
|
||||
require.NoError(err)
|
||||
s, err = repo.CreateScope(context.Background(), s, "")
|
||||
@@ -45,14 +44,11 @@ func Test_Repository_Scope_Create(t *testing.T) {
|
||||
})
|
||||
t.Run("valid-scope-withPublicId", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
publicId, err := newScopeId(scope.Org)
|
||||
require.NoError(err)
|
||||
s, err := NewOrg()
|
||||
require.NoError(err)
|
||||
|
||||
s, err = repo.CreateScope(context.Background(), s, "", WithPublicId(publicId))
|
||||
require.NoError(err)
|
||||
require.NotNil(s)
|
||||
@@ -66,14 +62,11 @@ func Test_Repository_Scope_Create(t *testing.T) {
|
||||
})
|
||||
t.Run("dup-org-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s, err := NewOrg(WithName(id))
|
||||
require.NoError(err)
|
||||
|
||||
s, err = repo.CreateScope(context.Background(), s, "")
|
||||
require.NoError(err)
|
||||
require.NotNil(s)
|
||||
@@ -88,10 +81,6 @@ func Test_Repository_Scope_Create(t *testing.T) {
|
||||
})
|
||||
t.Run("dup-proj-names", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s, err := NewOrg(WithName(id))
|
||||
@@ -119,15 +108,14 @@ func Test_Repository_Scope_Create(t *testing.T) {
|
||||
func Test_Repository_Scope_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s := testOrg(t, conn, id, "")
|
||||
s := testOrg(t, repo, id, "")
|
||||
assert.Equal(id, s.Name)
|
||||
|
||||
foundScope, err := repo.LookupScope(context.Background(), s.PublicId)
|
||||
@@ -167,13 +155,9 @@ func Test_Repository_Scope_Update(t *testing.T) {
|
||||
})
|
||||
t.Run("bad-parent-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s := testOrg(t, conn, id, "")
|
||||
s := testOrg(t, repo, id, "")
|
||||
assert.Equal(id, s.Name)
|
||||
|
||||
project, err := NewProject(s.PublicId)
|
||||
@@ -194,15 +178,13 @@ func Test_Repository_Scope_Update(t *testing.T) {
|
||||
func Test_Repository_Scope_Lookup(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("found-and-not-found", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s := testOrg(t, conn, id, "")
|
||||
s := testOrg(t, repo, id, "")
|
||||
require.NotEmpty(s.GetPublicId())
|
||||
assert.Equal(s.GetName(), id)
|
||||
|
||||
@@ -222,11 +204,10 @@ func Test_Repository_Scope_Delete(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
assert.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid-with-public-id", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
s, _ := TestScopes(t, conn)
|
||||
s, _ := TestScopes(t, repo)
|
||||
foundScope, err := repo.LookupScope(context.Background(), s.PublicId)
|
||||
require.NoError(err)
|
||||
assert.Equal(foundScope.GetPublicId(), s.GetPublicId())
|
||||
@@ -261,6 +242,7 @@ func TestRepository_UpdateScope(t *testing.T) {
|
||||
id := testId(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
publicId := testPublicId(t, "o")
|
||||
|
||||
type args struct {
|
||||
@@ -417,16 +399,11 @@ func TestRepository_UpdateScope(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
r := &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
}
|
||||
org := testOrg(t, conn, tt.name+"-orig-"+id, "orig-"+id)
|
||||
org := testOrg(t, repo, tt.name+"-orig-"+id, "orig-"+id)
|
||||
if tt.args.scope != nil {
|
||||
tt.args.scope.PublicId = org.PublicId
|
||||
}
|
||||
updatedScope, rowsUpdated, err := r.UpdateScope(context.Background(), tt.args.scope, 1, tt.args.fieldMaskPaths, tt.args.opt...)
|
||||
updatedScope, rowsUpdated, err := repo.UpdateScope(context.Background(), tt.args.scope, 1, tt.args.fieldMaskPaths, tt.args.opt...)
|
||||
if tt.wantErr {
|
||||
require.Error(err)
|
||||
assert.Equal(tt.wantUpdatedRows, rowsUpdated)
|
||||
@@ -458,13 +435,13 @@ func TestRepository_UpdateScope(t *testing.T) {
|
||||
t.Run("dup-name", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
r := &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
kms: repo.kms,
|
||||
}
|
||||
id := testId(t)
|
||||
_ = testOrg(t, conn, id, id)
|
||||
org2 := testOrg(t, conn, "dup-"+id, id)
|
||||
_ = testOrg(t, repo, id, id)
|
||||
org2 := testOrg(t, repo, "dup-"+id, id)
|
||||
org2.Name = id
|
||||
updatedScope, rowsUpdated, err := r.UpdateScope(context.Background(), org2, 1, []string{"Name"})
|
||||
require.Error(err)
|
||||
@@ -477,11 +454,9 @@ func Test_Repository_ListProjects(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org := testOrg(t, conn, "", "")
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org := testOrg(t, repo, "", "")
|
||||
|
||||
type args struct {
|
||||
withOrgId string
|
||||
@@ -539,7 +514,7 @@ func Test_Repository_ListProjects(t *testing.T) {
|
||||
require.NoError(conn.Where("public_id != ? and public_id != 'global'", org.PublicId).Delete(allocScope()).Error)
|
||||
testProjects := []*Scope{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
testProjects = append(testProjects, testProject(t, conn, org.PublicId))
|
||||
testProjects = append(testProjects, testProject(t, repo, org.PublicId))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(testProjects))
|
||||
got, err := repo.ListProjects(context.Background(), tt.args.withOrgId, tt.args.opt...)
|
||||
@@ -557,10 +532,8 @@ func Test_Repository_ListOrgs(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
type args struct {
|
||||
opt []Option
|
||||
}
|
||||
@@ -603,7 +576,7 @@ func Test_Repository_ListOrgs(t *testing.T) {
|
||||
require.NoError(conn.Where("type = 'org'").Delete(allocScope()).Error)
|
||||
testOrgs := []*Scope{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
testOrgs = append(testOrgs, testOrg(t, conn, "", ""))
|
||||
testOrgs = append(testOrgs, testOrg(t, repo, "", ""))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(testOrgs))
|
||||
got, err := repo.ListOrgs(context.Background(), tt.args.opt...)
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
iam_store "github.com/hashicorp/boundary/internal/iam/store"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/hashicorp/boundary/internal/oplog/store"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
@@ -24,10 +24,11 @@ func TestNewRepository(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
testKms := kms.TestKms(t, conn, wrapper)
|
||||
type args struct {
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
wrapper wrapping.Wrapper
|
||||
r db.Reader
|
||||
w db.Writer
|
||||
kms *kms.Kms
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -39,35 +40,35 @@ func TestNewRepository(t *testing.T) {
|
||||
{
|
||||
name: "valid",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: testKms,
|
||||
},
|
||||
want: &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
kms: testKms,
|
||||
defaultLimit: db.DefaultLimit,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "nil-wrapper",
|
||||
name: "nil-kms",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: rw,
|
||||
wrapper: nil,
|
||||
r: rw,
|
||||
w: rw,
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
wantErrString: "error creating db repository with nil wrapper",
|
||||
wantErrString: "error creating db repository with nil kms",
|
||||
},
|
||||
{
|
||||
name: "nil-writer",
|
||||
args: args{
|
||||
r: rw,
|
||||
w: nil,
|
||||
wrapper: wrapper,
|
||||
r: rw,
|
||||
w: nil,
|
||||
kms: testKms,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
@@ -76,9 +77,9 @@ func TestNewRepository(t *testing.T) {
|
||||
{
|
||||
name: "nil-reader",
|
||||
args: args{
|
||||
r: nil,
|
||||
w: rw,
|
||||
wrapper: wrapper,
|
||||
r: nil,
|
||||
w: rw,
|
||||
kms: testKms,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
@@ -88,7 +89,7 @@ func TestNewRepository(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.wrapper)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.kms)
|
||||
if tt.wantErr {
|
||||
require.Error(err)
|
||||
assert.Equal(err.Error(), tt.wantErrString)
|
||||
@@ -102,12 +103,10 @@ func TestNewRepository(t *testing.T) {
|
||||
func Test_Repository_create(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
id := testId(t)
|
||||
|
||||
s, err := NewOrg(WithName("fname-" + id))
|
||||
@@ -134,10 +133,6 @@ func Test_Repository_create(t *testing.T) {
|
||||
})
|
||||
t.Run("nil-resource", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
resource, err := repo.create(context.Background(), nil)
|
||||
require.Error(err)
|
||||
assert.Nil(resource)
|
||||
@@ -148,14 +143,13 @@ func Test_Repository_create(t *testing.T) {
|
||||
func Test_Repository_delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid-org", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
|
||||
s, _ := TestScopes(t, conn)
|
||||
s, _ := TestScopes(t, repo)
|
||||
|
||||
rowsDeleted, err := repo.delete(context.Background(), s)
|
||||
require.NoError(err)
|
||||
@@ -164,14 +158,12 @@ func Test_Repository_delete(t *testing.T) {
|
||||
err = db.TestVerifyOplog(t, rw, s.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_DELETE), db.WithCreateNotBefore(5*time.Second))
|
||||
require.NoError(err)
|
||||
})
|
||||
require.NoError(t, conn.Where("1=1").Delete(kms.AllocRootKey()).Error)
|
||||
t.Run("nil-resource", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
|
||||
deletedRows, err := repo.delete(context.Background(), nil, nil)
|
||||
assert.Error(err)
|
||||
require.Error(err)
|
||||
assert.Equal(0, deletedRows)
|
||||
assert.Equal(err.Error(), "error deleting resource that is nil")
|
||||
})
|
||||
@@ -181,8 +173,9 @@ func TestRepository_update(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
now := ×tamp.Timestamp{Timestamp: ptypes.TimestampNow()}
|
||||
id := testId(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
rw := db.New(conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
publicId := testPublicId(t, "o")
|
||||
|
||||
type args struct {
|
||||
@@ -297,16 +290,11 @@ func TestRepository_update(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
r := &Repository{
|
||||
reader: rw,
|
||||
writer: rw,
|
||||
wrapper: wrapper,
|
||||
}
|
||||
org := testOrg(t, conn, tt.name+"-orig-"+id, "orig-"+id)
|
||||
org := testOrg(t, repo, tt.name+"-orig-"+id, "orig-"+id)
|
||||
if tt.args.resource != nil {
|
||||
tt.args.resource.(*Scope).PublicId = org.PublicId
|
||||
}
|
||||
updatedResource, rowsUpdated, err := r.update(context.Background(), tt.args.resource, 1, tt.args.fieldMaskPaths, tt.args.setToNullPaths, tt.args.opt...)
|
||||
updatedResource, rowsUpdated, err := repo.update(context.Background(), tt.args.resource, 1, tt.args.fieldMaskPaths, tt.args.setToNullPaths, tt.args.opt...)
|
||||
if tt.wantErr {
|
||||
require.Error(err)
|
||||
assert.Equal(tt.wantUpdatedRows, rowsUpdated)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbcommon "github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
)
|
||||
@@ -159,6 +160,11 @@ func (r *Repository) LookupUserWithLogin(ctx context.Context, accountId string,
|
||||
"resource-type": []string{"auth-account"},
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, acct.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lookup user with login: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
// We will create a new user and associate the user with the account
|
||||
// within one retryable transaction using writer.DoTx
|
||||
var obtainedUser *User
|
||||
@@ -199,7 +205,7 @@ func (r *Repository) LookupUserWithLogin(ctx context.Context, accountId string,
|
||||
return fmt.Errorf("account update affected %d rows", updatedRows)
|
||||
}
|
||||
msgs = append(msgs, &updateMsg)
|
||||
if err := w.WriteOplogEntryWith(ctx, r.wrapper, ticket, metadata, msgs); err != nil {
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, ticket, metadata, msgs); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -280,6 +286,11 @@ func (r *Repository) AssociateUserWithAccount(ctx context.Context, userPublicId,
|
||||
}
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, acct.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("associate user with account: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
// validate, associated the user with the account, and then read the
|
||||
// user back in the same tx for consistency.
|
||||
_, err = r.writer.DoTx(
|
||||
@@ -287,7 +298,6 @@ func (r *Repository) AssociateUserWithAccount(ctx context.Context, userPublicId,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(txReader db.Reader, w db.Writer) error {
|
||||
|
||||
metadata := oplog.Metadata{
|
||||
"resource-public-id": []string{accountId},
|
||||
"scope-id": []string{acct.ScopeId},
|
||||
@@ -302,9 +312,9 @@ func (r *Repository) AssociateUserWithAccount(ctx context.Context, userPublicId,
|
||||
// transactions)
|
||||
switch {
|
||||
case opts.withDisassociate:
|
||||
updatedRows, err = w.Update(ctx, updatedAcct, []string{"IamUserId"}, nil, db.WithOplog(r.wrapper, metadata), db.WithWhere("iam_user_id = ?", acct.IamUserId))
|
||||
updatedRows, err = w.Update(ctx, updatedAcct, []string{"IamUserId"}, nil, db.WithOplog(oplogWrapper, metadata), db.WithWhere("iam_user_id = ?", acct.IamUserId))
|
||||
default:
|
||||
updatedRows, err = w.Update(ctx, updatedAcct, []string{"IamUserId"}, nil, db.WithOplog(r.wrapper, metadata), db.WithWhere("iam_user_id is NULL"))
|
||||
updatedRows, err = w.Update(ctx, updatedAcct, []string{"IamUserId"}, nil, db.WithOplog(oplogWrapper, metadata), db.WithWhere("iam_user_id is NULL"))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -355,6 +365,11 @@ func (r *Repository) DissociateUserWithAccount(ctx context.Context, userPublicId
|
||||
return nil, fmt.Errorf("dissociate user with account: %s account is not associated with a user: %w", accountId, db.ErrInvalidParameter)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, acct.GetScopeId(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("disassociate user with account: unable to get oplog wrapper: %w", err)
|
||||
}
|
||||
|
||||
// validate, dissociate the user with the account and then read the user back in
|
||||
// the same tx for consistency.
|
||||
_, err = r.writer.DoTx(
|
||||
@@ -373,7 +388,7 @@ func (r *Repository) DissociateUserWithAccount(ctx context.Context, userPublicId
|
||||
// set the user id to null and use WithWhere to ensure that the auth
|
||||
// account is associated with the user (handling race conditions
|
||||
// with other concurrent transactions)
|
||||
updatedRows, err := w.Update(ctx, updatedAcct, nil, []string{"IamUserId"}, db.WithOplog(r.wrapper, metadata), db.WithWhere("iam_user_id = ?", userPublicId))
|
||||
updatedRows, err := w.Update(ctx, updatedAcct, nil, []string{"IamUserId"}, db.WithOplog(oplogWrapper, metadata), db.WithWhere("iam_user_id = ?", userPublicId))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -21,10 +21,9 @@ func TestRepository_CreateUser(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
user *User
|
||||
@@ -113,10 +112,9 @@ func TestRepository_UpdateUser(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
pubId := func(s string) *string { return &s }
|
||||
|
||||
type args struct {
|
||||
@@ -298,13 +296,13 @@ func TestRepository_UpdateUser(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
if tt.wantDup {
|
||||
u := TestUser(t, conn, org.PublicId, tt.newUserOpts...)
|
||||
u := TestUser(t, repo, org.PublicId, tt.newUserOpts...)
|
||||
u.Name = tt.args.name
|
||||
_, _, err := repo.UpdateUser(context.Background(), u, 1, tt.args.fieldMaskPaths, tt.args.opt...)
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
u := TestUser(t, conn, org.PublicId, tt.newUserOpts...)
|
||||
u := TestUser(t, repo, org.PublicId, tt.newUserOpts...)
|
||||
|
||||
updateUser := allocUser()
|
||||
updateUser.PublicId = u.PublicId
|
||||
@@ -372,9 +370,8 @@ func TestRepository_DeleteUser(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
user *User
|
||||
@@ -390,7 +387,7 @@ func TestRepository_DeleteUser(t *testing.T) {
|
||||
{
|
||||
name: "valid",
|
||||
args: args{
|
||||
user: TestUser(t, conn, org.PublicId),
|
||||
user: TestUser(t, repo, org.PublicId),
|
||||
},
|
||||
wantRowsDeleted: 1,
|
||||
wantErr: false,
|
||||
@@ -454,11 +451,9 @@ func TestRepository_ListUsers(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
const testLimit = 10
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit))
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper, WithLimit(testLimit))
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
withOrgId string
|
||||
@@ -516,7 +511,7 @@ func TestRepository_ListUsers(t *testing.T) {
|
||||
require.NoError(conn.Where("public_id != 'u_anon' and public_id != 'u_auth'").Delete(allocUser()).Error)
|
||||
testUsers := []*User{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
testUsers = append(testUsers, TestUser(t, conn, org.PublicId))
|
||||
testUsers = append(testUsers, TestUser(t, repo, org.PublicId))
|
||||
}
|
||||
assert.Equal(tt.createCnt, len(testUsers))
|
||||
got, err := repo.ListUsers(context.Background(), tt.args.withOrgId, tt.args.opt...)
|
||||
@@ -535,16 +530,15 @@ func TestRepository_LookupUserWithLogin(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
org, _ := TestScopes(t, repo)
|
||||
authMethodId := testAuthMethod(t, conn, org.PublicId)
|
||||
newAuthAcct := testAccount(t, conn, org.PublicId, authMethodId, "")
|
||||
newAuthAcctWithoutVivify := testAccount(t, conn, org.PublicId, authMethodId, "")
|
||||
|
||||
user := TestUser(t, conn, org.PublicId, WithName("existing-"+id))
|
||||
user := TestUser(t, repo, org.PublicId, WithName("existing-"+id))
|
||||
existingAuthAcct := testAccount(t, conn, org.PublicId, authMethodId, user.PublicId)
|
||||
require.Equal(t, user.PublicId, existingAuthAcct.IamUserId)
|
||||
|
||||
@@ -676,9 +670,8 @@ func TestRepository_AssociateUserWithAccount(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
authMethodId := testAuthMethod(t, conn, org.PublicId)
|
||||
|
||||
type Ids struct {
|
||||
@@ -702,7 +695,7 @@ func TestRepository_AssociateUserWithAccount(t *testing.T) {
|
||||
name: "simple",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, "")
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -735,7 +728,7 @@ func TestRepository_AssociateUserWithAccount(t *testing.T) {
|
||||
name: "already-properly-assoc",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, u.PublicId)
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -746,8 +739,8 @@ func TestRepository_AssociateUserWithAccount(t *testing.T) {
|
||||
name: "assoc-with-diff-user",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
diffUser := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
diffUser := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, diffUser.PublicId)
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -759,8 +752,8 @@ func TestRepository_AssociateUserWithAccount(t *testing.T) {
|
||||
name: "assoc-with-diff-user-withDisassociateOption",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
diffUser := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
diffUser := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, diffUser.PublicId)
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -772,7 +765,7 @@ func TestRepository_AssociateUserWithAccount(t *testing.T) {
|
||||
name: "bad-acct-id",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
id := testId(t)
|
||||
return Ids{user: u.PublicId, acct: id}
|
||||
}(),
|
||||
@@ -821,9 +814,8 @@ func TestRepository_DissociateUserWithAccount(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
org, _ := TestScopes(t, conn)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
authMethodId := testAuthMethod(t, conn, org.PublicId)
|
||||
|
||||
type Ids struct {
|
||||
@@ -846,7 +838,7 @@ func TestRepository_DissociateUserWithAccount(t *testing.T) {
|
||||
name: "simple",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, u.PublicId)
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -879,7 +871,7 @@ func TestRepository_DissociateUserWithAccount(t *testing.T) {
|
||||
name: "already-properly-disassoc",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, "")
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -889,8 +881,8 @@ func TestRepository_DissociateUserWithAccount(t *testing.T) {
|
||||
name: "assoc-with-diff-user",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
diffUser := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
diffUser := TestUser(t, repo, org.PublicId)
|
||||
a := testAccount(t, conn, org.PublicId, authMethodId, diffUser.PublicId)
|
||||
return Ids{user: u.PublicId, acct: a.PublicId}
|
||||
}(),
|
||||
@@ -902,7 +894,7 @@ func TestRepository_DissociateUserWithAccount(t *testing.T) {
|
||||
name: "bad-acct-id",
|
||||
args: args{
|
||||
Ids: func() Ids {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
id := testId(t)
|
||||
return Ids{user: u.PublicId, acct: id}
|
||||
}(),
|
||||
|
||||
@@ -13,11 +13,13 @@ import (
|
||||
func Test_LookupScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
org, _ := TestScopes(t, conn)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
org, _ := TestScopes(t, repo)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
|
||||
foundScope, err := LookupScope(context.Background(), w, user)
|
||||
require.NoError(err)
|
||||
@@ -32,8 +34,8 @@ func Test_LookupScope(t *testing.T) {
|
||||
t.Run("bad-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
org, _ := TestScopes(t, conn)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
org, _ := TestScopes(t, repo)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
|
||||
s, err := LookupScope(context.Background(), nil, user)
|
||||
require.Error(err)
|
||||
|
||||
@@ -16,7 +16,9 @@ import (
|
||||
func TestRoleGrant_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
_, proj := TestScopes(t, repo)
|
||||
projRole := TestRole(t, conn, proj.PublicId)
|
||||
|
||||
type args struct {
|
||||
@@ -115,7 +117,9 @@ func TestRoleGrant_Create(t *testing.T) {
|
||||
func TestRoleGrant_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
|
||||
t.Run("updates not allowed", func(t *testing.T) {
|
||||
@@ -134,8 +138,9 @@ func TestRoleGrant_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
|
||||
_, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
_, proj := TestScopes(t, repo)
|
||||
projRole := TestRole(t, conn, proj.PublicId)
|
||||
|
||||
type args struct {
|
||||
@@ -218,9 +223,11 @@ func TestRoleGrant_Delete(t *testing.T) {
|
||||
func TestRoleGrant_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s := testOrg(t, conn, "", "")
|
||||
s := testOrg(t, repo, "", "")
|
||||
role := TestRole(t, conn, s.PublicId)
|
||||
|
||||
g, err := NewRoleGrant(role.PublicId, "id=*;actions=*")
|
||||
@@ -234,7 +241,7 @@ func TestRoleGrant_Clone(t *testing.T) {
|
||||
})
|
||||
t.Run("not-equal", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s := testOrg(t, conn, "", "")
|
||||
s := testOrg(t, repo, "", "")
|
||||
role := TestRole(t, conn, s.PublicId)
|
||||
|
||||
g, err := NewRoleGrant(role.PublicId, "id=*;actions=*")
|
||||
|
||||
@@ -20,7 +20,9 @@ import (
|
||||
func TestNewRole(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
id := testId(t)
|
||||
|
||||
type args struct {
|
||||
@@ -96,7 +98,9 @@ func TestNewRole(t *testing.T) {
|
||||
func Test_RoleCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
type args struct {
|
||||
role *Role
|
||||
}
|
||||
@@ -205,9 +209,11 @@ func Test_RoleCreate(t *testing.T) {
|
||||
func Test_RoleUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, proj := TestScopes(t, conn)
|
||||
org2, proj2 := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
org2, proj2 := TestScopes(t, repo)
|
||||
rw := db.New(conn)
|
||||
type args struct {
|
||||
name string
|
||||
@@ -474,9 +480,11 @@ func Test_RoleUpdate(t *testing.T) {
|
||||
func Test_RoleDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
rw := db.New(conn)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -549,7 +557,9 @@ func TestRole_ResourceType(t *testing.T) {
|
||||
func TestRole_GetScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
t.Run("valid-org", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
@@ -572,7 +582,9 @@ func TestRole_GetScope(t *testing.T) {
|
||||
func TestRole_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
role := TestRole(t, conn, org.PublicId, WithDescription("this is a test role"))
|
||||
|
||||
@@ -97,10 +97,12 @@ func TestScope_Create(t *testing.T) {
|
||||
func TestScope_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
s, _ := TestScopes(t, conn)
|
||||
s, _ := TestScopes(t, repo)
|
||||
|
||||
s.Name = testId(t)
|
||||
updatedRows, err := w.Update(context.Background(), s, []string{"Name"}, nil)
|
||||
@@ -110,7 +112,7 @@ func TestScope_Update(t *testing.T) {
|
||||
t.Run("type-update-not-allowed", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
s, _ := TestScopes(t, conn)
|
||||
s, _ := TestScopes(t, repo)
|
||||
|
||||
s.Type = scope.Project.String()
|
||||
updatedRows, err := w.Update(context.Background(), s, []string{"Type"}, nil)
|
||||
@@ -121,10 +123,12 @@ func TestScope_Update(t *testing.T) {
|
||||
func TestScope_GetScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
global := Scope{
|
||||
Scope: &store.Scope{Type: scope.Global.String(), PublicId: "global"},
|
||||
}
|
||||
@@ -171,16 +175,18 @@ func TestScope_ResourceType(t *testing.T) {
|
||||
func TestScope_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s, _ := TestScopes(t, conn)
|
||||
s, _ := TestScopes(t, repo)
|
||||
cp := s.Clone()
|
||||
assert.True(proto.Equal(cp.(*Scope).Scope, s.Scope))
|
||||
})
|
||||
t.Run("not-equal", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s, _ := TestScopes(t, conn)
|
||||
s2, _ := TestScopes(t, conn)
|
||||
s, _ := TestScopes(t, repo)
|
||||
s2, _ := TestScopes(t, repo)
|
||||
cp := s.Clone()
|
||||
assert.True(!proto.Equal(cp.(*Scope).Scope, s2.Scope))
|
||||
})
|
||||
|
||||
+43
-29
@@ -2,27 +2,55 @@ package iam
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/auth/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbassert "github.com/hashicorp/boundary/internal/db/assert"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestScopes creates an org and project suitable for testing.
|
||||
func TestScopes(t *testing.T, conn *gorm.DB) (org *Scope, prj *Scope) {
|
||||
t.Helper()
|
||||
// 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.T, conn *gorm.DB, rootWrapper wrapping.Wrapper, opt ...Option) *Repository {
|
||||
require := require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
kmsCache := kms.TestKms(t, conn, rootWrapper)
|
||||
wrapper, err := kmsCache.GetWrapper(context.Background(), scope.Global.String(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
var kmsRepo *kms.Repository
|
||||
// Assume the root hasn't been created and try to insert it
|
||||
kmsRepo, err = kms.NewRepository(rw, rw)
|
||||
require.NoError(err)
|
||||
rootKey, err := uuid.GenerateRandomBytesWithReader(32, rand.Reader)
|
||||
require.NoError(err)
|
||||
_, _, err = kmsRepo.CreateRootKey(context.Background(), rootWrapper, scope.Global.String(), rootKey)
|
||||
require.NoError(err)
|
||||
wrapper, err = kmsCache.GetWrapper(context.Background(), scope.Global.String(), kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
require.NoError(err)
|
||||
require.NotNil(wrapper)
|
||||
|
||||
org, err = NewOrg()
|
||||
repo, err := NewRepository(rw, rw, kmsCache, opt...)
|
||||
require.NoError(err)
|
||||
return repo
|
||||
}
|
||||
|
||||
// TestScopes creates an org and project suitable for testing.
|
||||
func TestScopes(t *testing.T, repo *Repository) (org *Scope, prj *Scope) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
|
||||
org, err := NewOrg()
|
||||
require.NoError(err)
|
||||
org, err = repo.CreateScope(context.Background(), org, "")
|
||||
require.NoError(err)
|
||||
@@ -39,15 +67,11 @@ func TestScopes(t *testing.T, conn *gorm.DB) (org *Scope, prj *Scope) {
|
||||
return
|
||||
}
|
||||
|
||||
func TestOrg(t *testing.T, conn *gorm.DB) (org *Scope) {
|
||||
func TestOrg(t *testing.T, repo *Repository, opt ...Option) (org *Scope) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
|
||||
org, err = NewOrg()
|
||||
org, err := NewOrg(opt...)
|
||||
require.NoError(err)
|
||||
org, err = repo.CreateScope(context.Background(), org, "")
|
||||
require.NoError(err)
|
||||
@@ -57,13 +81,9 @@ func TestOrg(t *testing.T, conn *gorm.DB) (org *Scope) {
|
||||
return
|
||||
}
|
||||
|
||||
func testOrg(t *testing.T, conn *gorm.DB, name, description string) (org *Scope) {
|
||||
func testOrg(t *testing.T, repo *Repository, name, description string) (org *Scope) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
|
||||
o, err := NewOrg(WithDescription(description), WithName(name))
|
||||
require.NoError(err)
|
||||
@@ -71,16 +91,13 @@ func testOrg(t *testing.T, conn *gorm.DB, name, description string) (org *Scope)
|
||||
require.NoError(err)
|
||||
require.NotNil(o)
|
||||
require.NotEmpty(o.GetPublicId())
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func testProject(t *testing.T, conn *gorm.DB, orgId string, opt ...Option) *Scope {
|
||||
func testProject(t *testing.T, repo *Repository, orgId string, opt ...Option) *Scope {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
|
||||
p, err := NewProject(orgId, opt...)
|
||||
require.NoError(err)
|
||||
@@ -88,6 +105,7 @@ func testProject(t *testing.T, conn *gorm.DB, orgId string, opt ...Option) *Scop
|
||||
require.NoError(err)
|
||||
require.NotNil(p)
|
||||
require.NotEmpty(p.GetPublicId())
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -106,13 +124,9 @@ func testPublicId(t *testing.T, prefix string) string {
|
||||
}
|
||||
|
||||
// TestUser creates a user suitable for testing.
|
||||
func TestUser(t *testing.T, conn *gorm.DB, scopeId string, opt ...Option) *User {
|
||||
func TestUser(t *testing.T, repo *Repository, scopeId string, opt ...Option) *User {
|
||||
t.Helper()
|
||||
require := assert.New(t)
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(err)
|
||||
require := require.New(t)
|
||||
|
||||
user, err := NewUser(scopeId, opt...)
|
||||
require.NoError(err)
|
||||
|
||||
@@ -12,9 +12,11 @@ import (
|
||||
func Test_testOrg(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
|
||||
org := testOrg(t, conn, id, id)
|
||||
org := testOrg(t, repo, id, id)
|
||||
assert.Equal(id, org.Name)
|
||||
assert.Equal(id, org.Description)
|
||||
assert.NotEmpty(org.PublicId)
|
||||
@@ -24,10 +26,12 @@ func Test_testProj(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
|
||||
org := testOrg(t, conn, id, id)
|
||||
proj := testProject(t, conn, org.PublicId, WithName(id), WithDescription(id))
|
||||
org := testOrg(t, repo, id, id)
|
||||
proj := testProject(t, repo, org.PublicId, WithName(id), WithDescription(id))
|
||||
assert.Equal(id, proj.Name)
|
||||
assert.Equal(id, proj.Description)
|
||||
assert.NotEmpty(proj.PublicId)
|
||||
@@ -47,7 +51,9 @@ func Test_testPublicId(t *testing.T) {
|
||||
func Test_TestScopes(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, prj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, prj := TestScopes(t, repo)
|
||||
|
||||
require.NotNil(org)
|
||||
assert.NotEmpty(org.GetPublicId())
|
||||
@@ -55,16 +61,31 @@ func Test_TestScopes(t *testing.T) {
|
||||
require.NotNil(prj)
|
||||
assert.NotEmpty(prj.GetPublicId())
|
||||
}
|
||||
|
||||
func Test_TestRepo(t *testing.T) {
|
||||
require := require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
require.NotNil(repo)
|
||||
|
||||
repo = TestRepo(t, conn, wrapper)
|
||||
require.NotNil(repo)
|
||||
}
|
||||
|
||||
func Test_TestUser(t *testing.T) {
|
||||
t.Helper()
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
require.NotNil(org)
|
||||
assert.NotEmpty(org.GetPublicId())
|
||||
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
require.NotNil(user)
|
||||
assert.NotEmpty(user.PublicId)
|
||||
}
|
||||
@@ -73,8 +94,10 @@ func Test_TestRole(t *testing.T) {
|
||||
t.Helper()
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
role := TestRole(t, conn, org.PublicId, WithDescription(id), WithName(id))
|
||||
require.NotNil(role)
|
||||
assert.Equal(id, role.Description)
|
||||
@@ -90,7 +113,9 @@ func Test_TestRoleGrant(t *testing.T) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
_, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
_, proj := TestScopes(t, repo)
|
||||
projRole := TestRole(t, conn, proj.PublicId)
|
||||
|
||||
grant := TestRoleGrant(t, conn, projRole.PublicId, "actions=*;id=*")
|
||||
@@ -104,14 +129,16 @@ func Test_TestUserRole(t *testing.T) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
org2, proj2 := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
org2, proj2 := TestScopes(t, repo)
|
||||
|
||||
orgRole := TestRole(t, conn, org.PublicId)
|
||||
projRole := TestRole(t, conn, proj.PublicId)
|
||||
org2Role := TestRole(t, conn, org2.PublicId)
|
||||
proj2Role := TestRole(t, conn, proj2.PublicId)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
|
||||
userRole := TestUserRole(t, conn, orgRole.PublicId, user.PublicId)
|
||||
require.NotNil(userRole)
|
||||
@@ -138,8 +165,10 @@ func Test_TestGroupRole(t *testing.T) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
org2, proj2 := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
org2, proj2 := TestScopes(t, repo)
|
||||
|
||||
orgRole := TestRole(t, conn, org.PublicId)
|
||||
orgGroup := TestGroup(t, conn, org.PublicId)
|
||||
@@ -174,10 +203,12 @@ func Test_TestGroupMember(t *testing.T) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, proj := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, proj := TestScopes(t, repo)
|
||||
og := TestGroup(t, conn, org.PublicId)
|
||||
pg := TestGroup(t, conn, proj.PublicId)
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
|
||||
gm := TestGroupMember(t, conn, og.PublicId, u.PublicId)
|
||||
require.NotNil(gm)
|
||||
|
||||
+19
-15
@@ -17,7 +17,8 @@ import (
|
||||
func TestNewUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
org, _ := TestScopes(t, TestRepo(t, conn, wrapper))
|
||||
id := testId(t)
|
||||
|
||||
type args struct {
|
||||
@@ -92,7 +93,8 @@ func Test_UserHardcoded(t *testing.T) {
|
||||
func Test_UserCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
org, _ := TestScopes(t, TestRepo(t, conn, wrapper))
|
||||
id := testId(t)
|
||||
t.Run("valid-user", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
@@ -131,10 +133,9 @@ func Test_UserUpdate(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, proj := TestScopes(t, conn)
|
||||
org, proj := TestScopes(t, repo)
|
||||
|
||||
type args struct {
|
||||
name string
|
||||
@@ -206,13 +207,13 @@ func Test_UserUpdate(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
if tt.wantDup {
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
u.Name = tt.args.name
|
||||
_, err := rw.Update(context.Background(), u, tt.args.fieldMaskPaths, nil)
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
u := TestUser(t, conn, org.PublicId)
|
||||
u := TestUser(t, repo, org.PublicId)
|
||||
|
||||
updateUser := allocUser()
|
||||
updateUser.PublicId = u.PublicId
|
||||
@@ -243,10 +244,9 @@ func Test_UserDelete(t *testing.T) {
|
||||
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
id := testId(t)
|
||||
org, _ := TestScopes(t, conn)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -257,7 +257,7 @@ func Test_UserDelete(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "valid",
|
||||
user: TestUser(t, conn, org.PublicId),
|
||||
user: TestUser(t, repo, org.PublicId),
|
||||
wantErr: false,
|
||||
wantRowsDeleted: 1,
|
||||
},
|
||||
@@ -302,11 +302,13 @@ func Test_UserDelete(t *testing.T) {
|
||||
func Test_UserGetScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
w := db.New(conn)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
userScope, err := user.GetScope(context.Background(), w)
|
||||
require.NoError(err)
|
||||
assert.True(proto.Equal(org, userScope))
|
||||
@@ -317,11 +319,13 @@ func Test_UserGetScope(t *testing.T) {
|
||||
func TestUser_Clone(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
org, _ := TestScopes(t, conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo := TestRepo(t, conn, wrapper)
|
||||
org, _ := TestScopes(t, repo)
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
user := TestUser(t, conn, org.PublicId)
|
||||
user := TestUser(t, repo, org.PublicId)
|
||||
cp := user.Clone()
|
||||
assert.True(proto.Equal(cp.(*User).User, user.User))
|
||||
})
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
)
|
||||
|
||||
// CreateRootKeyTx inserts into the db (via db.Writer) and returns the new root
|
||||
// key and root key version. This function encapsulates all the work required
|
||||
// within a db.TxHandler and allows this capability to be shared with the iam
|
||||
// repo via a common pkg without circular dependencies
|
||||
//
|
||||
// TODO (jimlambrt 8/2020) - refactor this... it's not needed now that we've
|
||||
// moved the tests into the kms_test package. We can do this refactor once
|
||||
// we've merged Jeff's sub-branch.
|
||||
func CreateRootKeyTx(ctx context.Context, w db.Writer, keyWrapper wrapping.Wrapper, scopeId string, key []byte) (*kms.RootKey, *kms.RootKeyVersion, error) {
|
||||
return kms.CreateRootKeyTx(ctx, w, keyWrapper, scopeId, key)
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package common_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/kms/common"
|
||||
"github.com/hashicorp/boundary/internal/kms/store"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func TestRepository_CreateRootKeyTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := kms.NewRepository(rw, rw)
|
||||
require.NoError(t, err)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
org, proj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
type args struct {
|
||||
scopeId string
|
||||
key []byte
|
||||
keyWrapper wrapping.Wrapper
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
wantIsError error
|
||||
}{
|
||||
{
|
||||
name: "valid-org",
|
||||
args: args{
|
||||
scopeId: org.PublicId,
|
||||
key: []byte("test key"),
|
||||
keyWrapper: wrapper,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid-global",
|
||||
args: args{
|
||||
scopeId: "global",
|
||||
key: []byte("valid-global"),
|
||||
keyWrapper: wrapper,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid-proj",
|
||||
args: args{
|
||||
scopeId: proj.PublicId,
|
||||
key: []byte("valid-proj"),
|
||||
keyWrapper: wrapper,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid-scope",
|
||||
args: args{
|
||||
scopeId: "o_notAValidScopeId",
|
||||
key: []byte("invalid-scope"),
|
||||
keyWrapper: wrapper,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty-scope",
|
||||
args: args{
|
||||
key: []byte("empty-scope"),
|
||||
keyWrapper: wrapper,
|
||||
},
|
||||
wantErr: true,
|
||||
wantIsError: db.ErrInvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-wrapper",
|
||||
args: args{
|
||||
scopeId: org.PublicId,
|
||||
key: []byte("test key"),
|
||||
keyWrapper: nil,
|
||||
},
|
||||
wantErr: true,
|
||||
wantIsError: db.ErrNilParameter,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
// need to delete root keys created by iam.TestScopes()
|
||||
require.NoError(conn.Where("1=1").Delete(&kms.RootKey{RootKey: &store.RootKey{}}).Error)
|
||||
rk, kv, err := common.CreateRootKeyTx(context.Background(), rw, tt.args.keyWrapper, tt.args.scopeId, tt.args.key)
|
||||
if tt.wantErr {
|
||||
assert.Error(err)
|
||||
assert.Nil(rk)
|
||||
if tt.wantIsError != nil {
|
||||
assert.True(errors.Is(err, tt.wantIsError))
|
||||
}
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.NotNil(rk.CreateTime)
|
||||
foundKey, err := repo.LookupRootKey(context.Background(), tt.args.keyWrapper, rk.PrivateId)
|
||||
assert.NoError(err)
|
||||
assert.True(proto.Equal(foundKey, rk))
|
||||
|
||||
// make sure there was no oplog written
|
||||
err = db.TestVerifyOplog(t, rw, rk.PrivateId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second))
|
||||
assert.Error(err)
|
||||
assert.True(errors.Is(err, db.ErrRecordNotFound))
|
||||
|
||||
assert.NotNil(kv.CreateTime)
|
||||
foundKeyVersion, err := repo.LookupRootKeyVersion(context.Background(), tt.args.keyWrapper, kv.PrivateId)
|
||||
assert.NoError(err)
|
||||
assert.True(proto.Equal(foundKeyVersion, kv))
|
||||
|
||||
// make sure there was no oplog written
|
||||
err = db.TestVerifyOplog(t, rw, kv.PrivateId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second))
|
||||
assert.Error(err)
|
||||
assert.True(errors.Is(err, db.ErrRecordNotFound))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package kms
|
||||
|
||||
// KeyPurpose allows an application to specify the reason they need a key; this
|
||||
// is used to select which DEK to return
|
||||
type KeyPurpose uint
|
||||
|
||||
const (
|
||||
// KeyPurposeUnknown is the default, and indicates that a correct purpose
|
||||
// wasn't specified
|
||||
KeyPurposeUnknown KeyPurpose = 0
|
||||
|
||||
// KeyPurposeDatabase is used for general encryption needs for most values
|
||||
// in the database, excluding the oplog
|
||||
KeyPurposeDatabase KeyPurpose = 1
|
||||
|
||||
// KeyPurposeOplog is used for oplogs
|
||||
KeyPurposeOplog KeyPurpose = 2
|
||||
)
|
||||
|
||||
// String returns the key purpose cast as a string, just so it can be called as
|
||||
// a function instead of direct casting elsewhere, yw
|
||||
func (k KeyPurpose) String() string {
|
||||
switch k {
|
||||
case KeyPurposeDatabase:
|
||||
return "database"
|
||||
case KeyPurposeOplog:
|
||||
return "oplog"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package kms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
)
|
||||
|
||||
const (
|
||||
RootKeyPrefix = "krk"
|
||||
RootKeyVersionPrefix = "krkv"
|
||||
)
|
||||
|
||||
func newRootKeyId() (string, error) {
|
||||
id, err := db.NewPublicId(RootKeyPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("new root key id: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func newRootKeyVersionId() (string, error) {
|
||||
id, err := db.NewPublicId(RootKeyVersionPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("new root key version id: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package kms
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Ids(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("krk", func(t *testing.T) {
|
||||
id, err := newRootKeyId()
|
||||
require.NoError(t, err)
|
||||
assert.True(t, strings.HasPrefix(id, RootKeyPrefix+"_"))
|
||||
})
|
||||
t.Run("krkv", func(t *testing.T) {
|
||||
id, err := newRootKeyVersionId()
|
||||
require.NoError(t, err)
|
||||
assert.True(t, strings.HasPrefix(id, RootKeyVersionPrefix+"_"))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package kms_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
// TODO (jimlambrt 8/2020) define tests for immutable fields in the remaining
|
||||
// kms tables:
|
||||
// kms_database_key
|
||||
// kms_database_key_version
|
||||
// kms_oplog_key
|
||||
// kms_oplog_key_version
|
||||
// kms_session_key
|
||||
// kms_session_key_version
|
||||
|
||||
func TestRootKeyVersion_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
rw := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
require.NoError(t, conn.Where("1=1").Delete(kms.AllocRootKey()).Error)
|
||||
require.NoError(t, conn.Where("1=1").Delete(kms.AllocRootKeyVersion()).Error)
|
||||
rk := kms.TestRootKey(t, conn, org.PublicId)
|
||||
new := kms.TestRootKeyVersion(t, conn, wrapper, rk.PrivateId, []byte("test key"))
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
update *kms.RootKeyVersion
|
||||
fieldMask []string
|
||||
}{
|
||||
{
|
||||
name: "private_id",
|
||||
update: func() *kms.RootKeyVersion {
|
||||
k := new.Clone().(*kms.RootKeyVersion)
|
||||
k.PrivateId = "o_thisIsNotAValidId"
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"PrivateId"},
|
||||
},
|
||||
{
|
||||
name: "create time",
|
||||
update: func() *kms.RootKeyVersion {
|
||||
k := new.Clone().(*kms.RootKeyVersion)
|
||||
k.CreateTime = &ts
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"CreateTime"},
|
||||
},
|
||||
{
|
||||
name: "root_key_id",
|
||||
update: func() *kms.RootKeyVersion {
|
||||
k := new.Clone().(*kms.RootKeyVersion)
|
||||
k.RootKeyId = "o_thisIsNotAValidId"
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"RootKeyId"},
|
||||
},
|
||||
{
|
||||
name: "version",
|
||||
update: func() *kms.RootKeyVersion {
|
||||
k := new.Clone().(*kms.RootKeyVersion)
|
||||
k.Version = uint32(22)
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"Version"},
|
||||
},
|
||||
{
|
||||
name: "key",
|
||||
update: func() *kms.RootKeyVersion {
|
||||
k := new.Clone().(*kms.RootKeyVersion)
|
||||
k.Key = []byte("updated key")
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"CtKey"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
orig := new.Clone()
|
||||
err := rw.LookupById(context.Background(), orig)
|
||||
require.NoError(err)
|
||||
|
||||
err = tt.update.Encrypt(context.Background(), wrapper)
|
||||
require.NoError(err)
|
||||
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil)
|
||||
require.Error(err)
|
||||
assert.Equal(0, rowsUpdated)
|
||||
|
||||
after := new.Clone()
|
||||
err = rw.LookupById(context.Background(), after)
|
||||
require.NoError(err)
|
||||
|
||||
assert.True(proto.Equal(orig.(*kms.RootKeyVersion), after.(*kms.RootKeyVersion)))
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRootKey_ImmutableFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
|
||||
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
||||
|
||||
wrapper := db.TestWrapper(t)
|
||||
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
require.NoError(t, conn.Where("1=1").Delete(kms.AllocRootKey()).Error)
|
||||
new := kms.TestRootKey(t, conn, org.PublicId)
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
update *kms.RootKey
|
||||
fieldMask []string
|
||||
}{
|
||||
{
|
||||
name: "private_id",
|
||||
update: func() *kms.RootKey {
|
||||
k := new.Clone().(*kms.RootKey)
|
||||
k.PrivateId = "o_thisIsNotAValidId"
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"PrivateId"},
|
||||
},
|
||||
{
|
||||
name: "create time",
|
||||
update: func() *kms.RootKey {
|
||||
k := new.Clone().(*kms.RootKey)
|
||||
k.CreateTime = &ts
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"CreateTime"},
|
||||
},
|
||||
{
|
||||
name: "scope_id",
|
||||
update: func() *kms.RootKey {
|
||||
k := new.Clone().(*kms.RootKey)
|
||||
k.ScopeId = "o_thisIsNotAValidId"
|
||||
return k
|
||||
}(),
|
||||
fieldMask: []string{"ScopeId"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
orig := new.Clone()
|
||||
err := rw.LookupById(context.Background(), orig)
|
||||
require.NoError(err)
|
||||
|
||||
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil)
|
||||
require.Error(err)
|
||||
assert.Equal(0, rowsUpdated)
|
||||
|
||||
after := new.Clone()
|
||||
err = rw.LookupById(context.Background(), after)
|
||||
require.NoError(err)
|
||||
|
||||
assert.True(proto.Equal(orig.(*kms.RootKey), after.(*kms.RootKey)))
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
package kms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/go-kms-wrapping/wrappers/aead"
|
||||
"github.com/hashicorp/go-kms-wrapping/wrappers/multiwrapper"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
// ExternalWrappers holds wrappers defined outside of Boundary, e.g. in its
|
||||
// configuration file.
|
||||
type ExternalWrappers struct {
|
||||
m sync.RWMutex
|
||||
root wrapping.Wrapper
|
||||
workerAuth wrapping.Wrapper
|
||||
}
|
||||
|
||||
// Root returns the wrapper for root keys
|
||||
func (e *ExternalWrappers) Root() wrapping.Wrapper {
|
||||
e.m.RLock()
|
||||
defer e.m.RUnlock()
|
||||
return e.root
|
||||
}
|
||||
|
||||
// WorkerAuth returns the wrapper for worker authentication
|
||||
func (e *ExternalWrappers) WorkerAuth() wrapping.Wrapper {
|
||||
e.m.RLock()
|
||||
defer e.m.RUnlock()
|
||||
return e.workerAuth
|
||||
}
|
||||
|
||||
// Kms is a way to access wrappers for a given scope and purpose. Since keys can
|
||||
// never change, only be added or (eventually) removed, it opportunistically
|
||||
// caches, going to the database as needed.
|
||||
type Kms struct {
|
||||
logger hclog.Logger
|
||||
|
||||
// scopePurposeCache holds a per-scope-purpose multiwrapper containing the
|
||||
// current encrypting key and all previous key versions, for decryption
|
||||
scopePurposeCache sync.Map
|
||||
|
||||
externalScopeCache map[string]*ExternalWrappers
|
||||
externalScopeCacheMutex sync.RWMutex
|
||||
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
// NewKms takes in a repo and returns a Kms. Supported options: WithLogger.
|
||||
func NewKms(repo *Repository, opt ...Option) (*Kms, error) {
|
||||
if repo == nil {
|
||||
return nil, errors.New("new kms created without an underlying repo")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
return &Kms{
|
||||
logger: opts.withLogger,
|
||||
externalScopeCache: make(map[string]*ExternalWrappers),
|
||||
repo: repo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetScopePurposeCache is used in test functions for validation. Since the
|
||||
// tests need to be in a different package to avoid circular dependencies, this
|
||||
// is exported.
|
||||
func (k *Kms) GetScopePurposeCache() *sync.Map {
|
||||
return &k.scopePurposeCache
|
||||
}
|
||||
|
||||
// AddExternalWrappers allows setting the external keys.
|
||||
//
|
||||
// TODO: If we support more than one, e.g. for encrypting against many in case
|
||||
// of a key loss, there will need to be some refactoring here to have the values
|
||||
// being stored in the struct be a multiwrapper, but that's for a later project.
|
||||
func (k *Kms) AddExternalWrappers(opt ...Option) error {
|
||||
k.externalScopeCacheMutex.Lock()
|
||||
defer k.externalScopeCacheMutex.Unlock()
|
||||
|
||||
ext := k.externalScopeCache[scope.Global.String()]
|
||||
if ext == nil {
|
||||
ext = &ExternalWrappers{}
|
||||
}
|
||||
ext.m.Lock()
|
||||
defer ext.m.Unlock()
|
||||
|
||||
opts := getOpts(opt...)
|
||||
if opts.withRootWrapper != nil {
|
||||
ext.root = opts.withRootWrapper
|
||||
if ext.root.KeyID() == "" {
|
||||
return fmt.Errorf("root wrapper has no key ID")
|
||||
}
|
||||
}
|
||||
if opts.withWorkerAuthWrapper != nil {
|
||||
ext.workerAuth = opts.withWorkerAuthWrapper
|
||||
if ext.workerAuth.KeyID() == "" {
|
||||
return fmt.Errorf("worker auth wrapper has no key ID")
|
||||
}
|
||||
}
|
||||
|
||||
k.externalScopeCache[scope.Global.String()] = ext
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *Kms) GetExternalWrappers() *ExternalWrappers {
|
||||
k.externalScopeCacheMutex.RLock()
|
||||
defer k.externalScopeCacheMutex.RUnlock()
|
||||
|
||||
ext := k.externalScopeCache[scope.Global.String()]
|
||||
ext.m.RLock()
|
||||
defer ext.m.RUnlock()
|
||||
|
||||
ret := &ExternalWrappers{
|
||||
root: ext.root,
|
||||
workerAuth: ext.workerAuth,
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func generateKeyId(scopeId string, purpose KeyPurpose, version uint32) string {
|
||||
return fmt.Sprintf("%s_%s_%d", scopeId, purpose, version)
|
||||
}
|
||||
|
||||
// GetWrapper returns a wrapper for the given scope and purpose. When a keyId is
|
||||
// passed, it will ensure that the returning wrapper has that key ID in the
|
||||
// multiwrapper. This is not necesary for encryption but should be supplied for
|
||||
// decryption.
|
||||
func (k *Kms) GetWrapper(ctx context.Context, scopeId string, purpose KeyPurpose, opt ...Option) (wrapping.Wrapper, error) {
|
||||
switch purpose {
|
||||
case KeyPurposeOplog, KeyPurposeDatabase:
|
||||
case KeyPurposeUnknown:
|
||||
return nil, errors.New("key purpose not specified")
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported purpose %q", purpose)
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
// Fast-path: we have a valid key at the scope/purpose. Verify the key with
|
||||
// that ID is in the multiwrapper; if not, fall through to reload from the
|
||||
// DB.
|
||||
val, ok := k.scopePurposeCache.Load(scopeId + purpose.String())
|
||||
if ok {
|
||||
wrapper := val.(*multiwrapper.MultiWrapper)
|
||||
if opts.withKeyId == "" || wrapper.WrapperForKeyID(opts.withKeyId) != nil {
|
||||
return wrapper, nil
|
||||
}
|
||||
// Fall through to refresh our multiwrapper for this scope/purpose from the DB
|
||||
}
|
||||
|
||||
// We don't have it cached, so we'll need to read from the database. Get the
|
||||
// root for the scope as we'll need it to decrypt the value coming from the
|
||||
// DB. We don't cache the roots as we expect that after a few calls the
|
||||
// scope-purpose cache will catch everything in steady-state.
|
||||
rootWrapper, err := k.loadRoot(ctx, scopeId, opt...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading root key for scope %s: %w", scopeId, err)
|
||||
}
|
||||
|
||||
// TODO: Look up dek in the db, then decrypt with the root wrapper. If we
|
||||
// have a key ID passed in verify that we find it. For now since we don't
|
||||
// have DEKs, derive a key.
|
||||
baseWrapper := rootWrapper.WrapperForKeyID("__base__").(*aead.Wrapper)
|
||||
derived, err := baseWrapper.NewDerivedWrapper(&aead.DerivedWrapperOptions{
|
||||
KeyID: generateKeyId(scopeId, purpose, 1),
|
||||
Hash: func() hash.Hash { b, _ := blake2b.New256(nil); return b },
|
||||
Salt: []byte(scopeId),
|
||||
Info: []byte(purpose.String()),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating derived wrapper: %w", err)
|
||||
}
|
||||
|
||||
// Store the looked-up value into the scope cache.
|
||||
multi := multiwrapper.NewMultiWrapper(derived)
|
||||
k.scopePurposeCache.Store(scopeId+purpose.String(), multi)
|
||||
|
||||
return multi, nil
|
||||
}
|
||||
|
||||
func (k *Kms) loadRoot(ctx context.Context, scopeId string, opt ...Option) (*multiwrapper.MultiWrapper, error) {
|
||||
opts := getOpts(opt...)
|
||||
repo := opts.withRepository
|
||||
if repo == nil {
|
||||
repo = k.repo
|
||||
}
|
||||
rootKeys, err := repo.ListRootKeys(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error listing root keys: %w", err)
|
||||
}
|
||||
var rootKeyId string
|
||||
for _, k := range rootKeys {
|
||||
if k.GetScopeId() == scopeId {
|
||||
rootKeyId = k.GetPrivateId()
|
||||
break
|
||||
}
|
||||
}
|
||||
if rootKeyId == "" {
|
||||
return nil, fmt.Errorf("error finding root key for scope %s", scopeId)
|
||||
}
|
||||
|
||||
// Now: find the external KMS that can be used to decrypt the root values
|
||||
// from the DB.
|
||||
k.externalScopeCacheMutex.Lock()
|
||||
externalWrappers := k.externalScopeCache[scope.Global.String()]
|
||||
k.externalScopeCacheMutex.Unlock()
|
||||
if externalWrappers == nil {
|
||||
return nil, errors.New("could not find kms information at either the needed scope or global fallback")
|
||||
}
|
||||
|
||||
externalWrappers.m.RLock()
|
||||
defer externalWrappers.m.RUnlock()
|
||||
|
||||
if externalWrappers.root == nil {
|
||||
return nil, fmt.Errorf("root key wrapper for scope %s is nil", scopeId)
|
||||
}
|
||||
rootKeyVersions, err := repo.ListRootKeyVersions(ctx, externalWrappers.root, rootKeyId, WithOrder("version desc"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error looking up root key versions for scope %s with key ID %s: %w", scopeId, externalWrappers.root.KeyID(), err)
|
||||
}
|
||||
if len(rootKeyVersions) == 0 {
|
||||
return nil, fmt.Errorf("no root key versions found for scope %s", scopeId)
|
||||
}
|
||||
|
||||
var multi *multiwrapper.MultiWrapper
|
||||
for i, key := range rootKeyVersions {
|
||||
wrapper := aead.NewWrapper(nil)
|
||||
if _, err := wrapper.SetConfig(map[string]string{
|
||||
"key_id": key.GetPrivateId(),
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("error setting config on aead root wrapper in scope %s: %w", scopeId, err)
|
||||
}
|
||||
if err := wrapper.SetAESGCMKeyBytes(key.GetKey()); err != nil {
|
||||
return nil, fmt.Errorf("error setting key bytes on aead root wrapper in scope %s: %w", scopeId, err)
|
||||
}
|
||||
if i == 0 {
|
||||
multi = multiwrapper.NewMultiWrapper(wrapper)
|
||||
} else {
|
||||
multi.AddWrapper(wrapper)
|
||||
}
|
||||
}
|
||||
|
||||
return multi, err
|
||||
}
|
||||
Loaded 100 of 140 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user