Skip to content

Commit 9c45a96

Browse files
committed
Improve Azure implmentation
1 parent 035c13a commit 9c45a96

1 file changed

Lines changed: 37 additions & 29 deletions

File tree

pkg/postgres/azure.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,34 @@ import (
88
"github.com/lib/pq"
99
)
1010

11+
type AzureType string
12+
13+
const (
14+
// Azure Database for PostgreSQL Flexible Server uses default convention for login, but has not full superuser privileges
15+
FLEXIBLE AzureType = "flexible"
16+
// Azure Database for PostgreSQL Single Server uses <username>@<servername> convention
17+
SINGLE AzureType = "single"
18+
)
19+
1120
type azurepg struct {
1221
serverName string
22+
azureType AzureType
1323
pg
1424
}
1525

1626
func newAzurePG(postgres *pg) PG {
1727
splitUser := strings.Split(postgres.user, "@")
1828
serverName := ""
19-
// We need to know the server name for Azure Database for PostgreSQL Single Server
29+
azureType := FLEXIBLE
2030
if len(splitUser) > 1 {
31+
// If a servername is found, we are using Azure Database for PostgreSQL Single Server
2132
serverName = splitUser[1]
33+
azureType = SINGLE
2234
}
2335
return &azurepg{
24-
serverName,
25-
*postgres,
36+
serverName: serverName,
37+
azureType: azureType,
38+
pg: *postgres,
2639
}
2740
}
2841

@@ -31,23 +44,29 @@ func (azpg *azurepg) CreateUserRole(role, password string) (string, error) {
3144
if err != nil {
3245
return "", err
3346
}
34-
if azpg.serverName == "" {
47+
48+
// For Flexible Server, just return the role name as-is
49+
if azpg.azureType == FLEXIBLE {
3550
return returnedRole, nil
3651
}
37-
// Azure Database for PostgreSQL Single Server offering uses <username>@<servername> convention
52+
53+
// For Single Server, format as <username>@<servername>
3854
return fmt.Sprintf("%s@%s", returnedRole, azpg.serverName), nil
3955
}
4056

4157
func (azpg *azurepg) GetRoleForLogin(login string) string {
42-
splitUser := strings.Split(azpg.user, "@")
43-
if len(splitUser) > 1 {
44-
return splitUser[0]
58+
// For Azure Flexible Server, the login name is the same as the role name
59+
if azpg.azureType == FLEXIBLE {
60+
return login
4561
}
46-
return login
62+
63+
// For Azure Single Server, extract the username part before the '@' symbol
64+
splitUser := strings.Split(azpg.user, "@")
65+
return splitUser[0]
4766
}
4867

4968
func (azpg *azurepg) CreateDB(dbname, role string) error {
50-
// Have to add the master role to the group role before we can transfer the database owner
69+
// This step is necessary before we can set the specified role as the database owner
5170
err := azpg.GrantRole(role, azpg.GetRoleForLogin(azpg.user))
5271
if err != nil {
5372
return err
@@ -57,32 +76,21 @@ func (azpg *azurepg) CreateDB(dbname, role string) error {
5776
}
5877

5978
func (azpg *azurepg) DropRole(role, newOwner, database string, logger logr.Logger) error {
60-
if azpg.serverName != "" {
61-
// Logic for Single Server
62-
azNewOwner := azpg.GetRoleForLogin(newOwner)
63-
return azpg.pg.DropRole(role, azNewOwner, database, logger)
64-
} else {
65-
// Logic for Flexible Server (same as AWS)
66-
// to REASSIGN OWNED BY unless he belongs to both roles
67-
err := azpg.pg.GrantRole(role, azpg.user)
68-
if err != nil && err.(*pq.Error).Code != "0LP01" {
69-
if err.(*pq.Error).Code == "42704" {
70-
// The group role does not exist, no point in continuing
71-
return nil
72-
}
73-
return err
74-
}
75-
err = azpg.pg.GrantRole(newOwner, azpg.user)
79+
if azpg.azureType == FLEXIBLE {
80+
// Grant the role to the user first
81+
err := azpg.GrantRole(role, azpg.user)
7682
if err != nil && err.(*pq.Error).Code != "0LP01" {
7783
if err.(*pq.Error).Code == "42704" {
78-
// The group role does not exist, no point of granting roles
79-
logger.Info(fmt.Sprintf("not granting %s to %s as %s does not exist", role, newOwner, newOwner))
8084
return nil
8185
}
8286
return err
8387
}
84-
defer azpg.pg.RevokeRole(newOwner, azpg.pg.user)
8588

89+
// Delegate to parent implementation to perform the actual drop
8690
return azpg.pg.DropRole(role, newOwner, database, logger)
8791
}
92+
93+
// For Azure Single Server, format the new owner correctly
94+
azNewOwner := azpg.GetRoleForLogin(newOwner)
95+
return azpg.pg.DropRole(role, azNewOwner, database, logger)
8896
}

0 commit comments

Comments
 (0)