From 4dfc41514b9a56f9278eb62d707d43e890e7af62 Mon Sep 17 00:00:00 2001 From: Jim Date: Wed, 15 Jul 2020 13:32:36 -0400 Subject: [PATCH] refactor direct sql into query.go (#192) --- internal/iam/query.go | 24 ++++++++++++++++++++++++ internal/iam/repository_user.go | 16 ++-------------- internal/iam/testing.go | 6 ++---- 3 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 internal/iam/query.go diff --git a/internal/iam/query.go b/internal/iam/query.go new file mode 100644 index 000000000..f133caba5 --- /dev/null +++ b/internal/iam/query.go @@ -0,0 +1,24 @@ +package iam + +// query.go contains "raw sql" for the iam package that goes directly against +// the db via sql.DB vs the standard pattern of using the internal/db package to +// interact with the db. +const ( + // whereUserAuthAccount - given an auth account id, return the associated user. + whereUserAuthAccount = ` + select iam_user.* + from iam_user + inner join auth_account + on iam_user.public_id = auth_account.iam_user_id + where + iam_user.scope_id = auth_account.scope_id and + auth_account.public_id = $1` + + // whereValidAuthMethod - determine if an auth method public_id within a scope_id + // is valid by returning a count of matching rows. + whereValidAuthMethod = `select count(*) from auth_method where public_id = $1 and scope_id = $2` + + // insertAuthMethod - insert a row directly into auth_method (TODO - this + // should be replaced with calls to the auth method repo) + insertAuthMethod = `insert into auth_method (public_id, scope_id) values ($1, $2)` +) diff --git a/internal/iam/repository_user.go b/internal/iam/repository_user.go index e61c00967..8e307a418 100644 --- a/internal/iam/repository_user.go +++ b/internal/iam/repository_user.go @@ -219,18 +219,7 @@ func (r *Repository) getUserWithAuthAccount(ctx context.Context, withAuthAccount if err != nil { return nil, fmt.Errorf("unable to get underlying db for auth account search: %w", err) } - userTable := func() *User { u := allocUser(); return &u }().TableName() - acctTable := func() *AuthAccount { a := allocAuthAccount(); return &a }().TableName() - - where := fmt.Sprintf(` - select %[1]s.* - from %[1]s - inner join %[2]s - on %[1]s.public_id = %[2]s.iam_user_id - where - %[1]s.scope_id = %[2]s.scope_id and - %[2]s.public_id = $1`, userTable, acctTable) - rows, err := underlying.Query(where, withAuthAccountId) + rows, err := underlying.Query(whereUserAuthAccount, withAuthAccountId) if err != nil { return nil, fmt.Errorf("unable to query auth account %s", withAuthAccountId) } @@ -291,13 +280,12 @@ func (r *Repository) validateAuthMethodId(ctx context.Context, scopeId, authMeth if authMethodId == "" { return false, fmt.Errorf("auth method id is unset: %w", db.ErrInvalidParameter) } - const where = `select count(*) from auth_method where public_id = $1 and scope_id = $2` db, err := r.reader.DB() if err != nil { return false, err } var cnt int - if err := db.QueryRow(where, authMethodId, scopeId).Scan(&cnt); err != nil { + if err := db.QueryRow(whereValidAuthMethod, authMethodId, scopeId).Scan(&cnt); err != nil { return false, err } if cnt == 0 { diff --git a/internal/iam/testing.go b/internal/iam/testing.go index 13d513683..da5ea50ef 100644 --- a/internal/iam/testing.go +++ b/internal/iam/testing.go @@ -197,7 +197,6 @@ func TestGroupRole(t *testing.T, conn *gorm.DB, roleId, grpId string, opt ...Opt func testAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId string) *AuthAccount { const ( authAccountPrefix = "aa_" - where = `select count(*) from auth_method where public_id = $1 and scope_id = $2` ) t.Helper() rw := db.New(conn) @@ -215,7 +214,7 @@ func testAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId } var count int - err := conn.DB().QueryRow(where, authMethodId, scopeId).Scan(&count) + err := conn.DB().QueryRow(whereValidAuthMethod, authMethodId, scopeId).Scan(&count) require.NoError(err) require.Equal(1, count) @@ -246,7 +245,6 @@ func testAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId func testAuthMethod(t *testing.T, conn *gorm.DB, scopeId string) string { const ( authMethodPrefix = "am_" - insert = `insert into auth_method (public_id, scope_id) values ($1, $2)` ) t.Helper() require := require.New(t) @@ -255,7 +253,7 @@ func testAuthMethod(t *testing.T, conn *gorm.DB, scopeId string) string { id, err := db.NewPublicId(authMethodPrefix) require.NoError(err) - _, err = conn.DB().Exec(insert, id, scopeId) + _, err = conn.DB().Exec(insertAuthMethod, id, scopeId) require.NoError(err) return id }