Skip to content

Commit

Permalink
Align HostCredentials and ServiceCredentials on User/Name field (#282)
Browse files Browse the repository at this point in the history
* Do not validate admin credentials and hardcode database for admin connection

* Align HostCredentials and ServiceCredentials on User/Name field
  • Loading branch information
tmablunar authored Mar 13, 2024
1 parent cb0fa63 commit 531a401
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h *hostCredentials) String() string {
}
records := make([]string, 0, len(*h.value)>>1)
for k, v := range *h.value {
pair := k + "=" + v.Name
pair := k + "=" + v.User
if v.Password != "" {
pair += ":********"
}
Expand Down
14 changes: 7 additions & 7 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHostCredentials_Set(t *testing.T) {
err: nil,
output: map[string]postgres.Credentials{
"host:5432": {
Name: "user",
User: "user",
Password: "pass",
},
},
Expand All @@ -50,11 +50,11 @@ func TestHostCredentials_Set(t *testing.T) {
err: nil,
output: map[string]postgres.Credentials{
"host1:5432": {
Name: "user1",
User: "user1",
Password: "pass1",
},
"host2:5432": {
Name: "user2",
User: "user2",
Password: "pass2",
},
},
Expand All @@ -71,7 +71,7 @@ func TestHostCredentials_Set(t *testing.T) {
err: nil,
output: map[string]postgres.Credentials{
"host1:5432": {
Name: "user1",
User: "user1",
Password: "pass1",
Params: "sslmode=enabled",
},
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestHostCredentials_String(t *testing.T) {
name: "single host",
value: map[string]postgres.Credentials{
"host:5432": {
Name: "user",
User: "user",
Password: "pass",
},
},
Expand All @@ -127,11 +127,11 @@ func TestHostCredentials_String(t *testing.T) {
name: "multiple hosts",
value: map[string]postgres.Credentials{
"host1:5432": {
Name: "user1",
User: "user1",
Password: "pass1",
},
"host2:5432": {
Name: "user2",
User: "user2",
Password: "pass2",
},
},
Expand Down
11 changes: 5 additions & 6 deletions controllers/postgresqluser_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestReconcile_badConfigmapReference(t *testing.T) {
Now: time.Now,
HostCredentials: map[string]postgres.Credentials{
host: {
Name: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
},
},
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestReconcile_rolePrefix(t *testing.T) {
Now: time.Now,
HostCredentials: map[string]postgres.Credentials{
host: {
Name: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
},
},
Expand Down Expand Up @@ -374,7 +374,7 @@ func TestReconcile_dotInName(t *testing.T) {
Now: time.Now,
HostCredentials: map[string]postgres.Credentials{
host: {
Name: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
},
},
Expand Down Expand Up @@ -522,7 +522,7 @@ func TestReconcile_multipleDatabaseResources(t *testing.T) {
Now: time.Now,
HostCredentials: map[string]postgres.Credentials{
host: {
Name: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
},
},
Expand Down Expand Up @@ -577,9 +577,8 @@ func seededDatabase(t *testing.T, host, databaseName, userName string, managerRo
require.NoErrorf(t, err, "failed to create managerRole for dbConn during seedDatabase")

err = postgres.Database(logf.Log, host, postgres.Credentials{
Name: "postgres",
Password: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Name: databaseName,
Password: databaseName,
Expand Down
6 changes: 3 additions & 3 deletions pkg/grants/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ func TestGranter_connectToHosts(t *testing.T) {
name: "single host with credentials",
credentials: map[string]postgres.Credentials{
"localhost:5432": {
Name: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
},
},
Expand All @@ -800,11 +800,11 @@ func TestGranter_connectToHosts(t *testing.T) {
name: "multiple hosts without upstream",
credentials: map[string]postgres.Credentials{
"localhost:5432": {
Name: "iam_creator",
User: "iam_creator",
Password: "iam_creator",
},
"unknown": {
Name: "iam_creator",
User: "iam_creator",
Password: "12345678",
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/grants/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *Granter) connectToHosts(log logr.Logger, accesses HostAccess) (map[stri
connectionString := postgres.ConnectionString{
Host: host,
Database: database,
User: credentials.Name,
User: credentials.User,
Password: credentials.Password,
}
db, err := postgres.Connect(log, connectionString)
Expand Down
10 changes: 3 additions & 7 deletions pkg/postgres/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ParseUsernamePassword(s string) (Credentials, error) {
return Credentials{}, fmt.Errorf("username empty")
}
c := Credentials{
Name: pair[0],
User: pair[0],
}
if len(pair) == 2 {
c.Password = pair[1]
Expand All @@ -62,11 +62,7 @@ func Database(log logr.Logger, host string, adminCredentials, serviceCredentials
if managerRole == "" {
return fmt.Errorf("managerRole required")
}
err := adminCredentials.Validate()
if err != nil {
return fmt.Errorf("adminCredentials not valid: %w", err)
}
err = serviceCredentials.Validate()
err := serviceCredentials.Validate()
if err != nil {
return fmt.Errorf("serviceCredentials not valid: %w", err)
}
Expand Down Expand Up @@ -204,7 +200,7 @@ func createDatabase(log logr.Logger, host string, adminCredentials Credentials,

connectionString := ConnectionString{
Host: host,
Database: adminCredentials.Name,
Database: "postgres",
User: adminCredentials.User,
Password: adminCredentials.Password,
Params: adminCredentials.Params,
Expand Down
13 changes: 3 additions & 10 deletions pkg/postgres/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestParseUsernamePassword(t *testing.T) {
name: "complete",
input: "user:password",
output: postgres.Credentials{
Name: "user",
User: "user",
Password: "password",
},
err: nil,
Expand All @@ -42,7 +42,7 @@ func TestParseUsernamePassword(t *testing.T) {
name: "no password",
input: "user",
output: postgres.Credentials{
Name: "user",
User: "user",
Password: "",
},
err: nil,
Expand All @@ -51,7 +51,7 @@ func TestParseUsernamePassword(t *testing.T) {
name: "empty password",
input: "user:",
output: postgres.Credentials{
Name: "user",
User: "user",
Password: "",
},
err: nil,
Expand Down Expand Up @@ -100,7 +100,6 @@ func TestDatabase_sunshine(t *testing.T) {

err = postgres.Database(logf.Log, postgresqlHost,
postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand Down Expand Up @@ -194,7 +193,6 @@ func TestDatabase_existingResourcePrivilegesForReadWriteRoles(t *testing.T) {
log.Info("TC: Run controller database creation")
err = postgres.Database(log, postgresqlHost,
postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand Down Expand Up @@ -261,7 +259,6 @@ func TestDatabase_defaultDatabaseName(t *testing.T) {
log.Info("TC: Create a legacy database that will be shared with other services")
err = postgres.Database(log, postgresqlHost,
postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand All @@ -278,7 +275,6 @@ func TestDatabase_defaultDatabaseName(t *testing.T) {
log.Info("TC: Request new database using default postgres database (postgres)")
err = postgres.Database(log, postgresqlHost,
postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand Down Expand Up @@ -364,7 +360,6 @@ func TestDatabase_mixedOwnershipOnSharedDatabase(t *testing.T) {
log.Info("TC: Create new_user database on shared database")
err = postgres.Database(log, postgresqlHost,
postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand Down Expand Up @@ -462,7 +457,6 @@ func TestDatabase_idempotency(t *testing.T) {
password := "test"

err = postgres.Database(log, postgresqlHost, postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand All @@ -476,7 +470,6 @@ func TestDatabase_idempotency(t *testing.T) {

// Invoke again with same name
err = postgres.Database(log, postgresqlHost, postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand Down
1 change: 0 additions & 1 deletion pkg/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ func createServiceDatabase(t *testing.T, log logr.Logger, host, service string)
managerRole := "postgres_manager_role"
err := postgres.Database(log, host,
postgres.Credentials{
Name: "postgres",
User: "iam_creator",
Password: "iam_creator",
}, postgres.Credentials{
Expand Down

0 comments on commit 531a401

Please sign in to comment.