-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurernm_postgresql_aad_administrator - prevent invalid usernames for…
- Loading branch information
1 parent
92700b8
commit fd1e43b
Showing
5 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
azurerm/internal/services/postgres/validate/admin_usernames.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func AdminUsernames(i interface{}, k string) (_ []string, errors []error) { | ||
disallowedLogins := [7]string{"azure_superuser", "azure_pg_admin", "admin", "administrator", "root", "guest", "public"} | ||
for _, v := range disallowedLogins { | ||
if v == strings.ToLower(i.(string)) { | ||
return nil, append(errors, fmt.Errorf("Error - PostgreSQL AD Administrator login can not be %q", i.(string))) | ||
} | ||
} | ||
if strings.HasPrefix(strings.ToLower(i.(string)), "pg_") { | ||
return nil, append(errors, fmt.Errorf("Error - PostgreSQL AD Administrator login can not start with 'pg_'")) | ||
} | ||
|
||
return nil, nil | ||
} |
48 changes: 48 additions & 0 deletions
48
azurerm/internal/services/postgres/validate/admin_usernames_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package validate | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestValidateAdminUsernames(t *testing.T) { | ||
testData := []struct { | ||
input string | ||
expected bool | ||
}{ | ||
{ | ||
// guest | ||
input: "guest", | ||
expected: false, | ||
}, | ||
{ | ||
// basic example | ||
input: "blah", | ||
expected: true, | ||
}, | ||
{ | ||
// contains pg_ | ||
input: "pg_blah", | ||
expected: false, | ||
}, | ||
{ | ||
// azure_pg_admin | ||
input: "azure_pg_admin", | ||
expected: false, | ||
}, | ||
{ | ||
// Capitalised example | ||
input: "Azure_superuser", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q..", v.input) | ||
|
||
_, errors := AdminUsernames(v.input, "name") | ||
actual := len(errors) == 0 | ||
if v.expected != actual { | ||
t.Fatalf("Expected %t but got %t", v.expected, actual) | ||
} | ||
} | ||
} |