-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix - MariaDB firewall rule name restrictions does not match official (…
- Loading branch information
Showing
5 changed files
with
90 additions
and
12 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
25 changes: 25 additions & 0 deletions
25
azurerm/internal/services/mariadb/validate/firewall_rule_name.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,25 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func FirewallRuleName(i interface{}, k string) (warnings []string, errors []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %s to be string", k)) | ||
return | ||
} | ||
|
||
// The name attribute rules are : | ||
// 1. can contain only letters, numbers, underscore and hyphen. | ||
// 2. The value must be between 1 and 128 characters long | ||
|
||
if !regexp.MustCompile(`^[a-zA-Z\d-_]{1,128}$`).MatchString(v) { | ||
errors = append(errors, fmt.Errorf("%s can contain only letters, numbers, underscore and hyphen, and be between 1 and 128 characters long", k)) | ||
return | ||
} | ||
|
||
return warnings, errors | ||
} |
58 changes: 58 additions & 0 deletions
58
azurerm/internal/services/mariadb/validate/firewall_rule_name_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,58 @@ | ||
package validate | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFirewallRuleName(t *testing.T) { | ||
testData := []struct { | ||
input string | ||
expected bool | ||
}{ | ||
{ | ||
// empty | ||
input: "", | ||
expected: false, | ||
}, | ||
{ | ||
// basic example | ||
input: "abc123", | ||
expected: true, | ||
}, | ||
{ | ||
// can contain underscore | ||
input: "aBc_123", | ||
expected: true, | ||
}, | ||
{ | ||
// can contain hyphen | ||
input: "ab-c", | ||
expected: true, | ||
}, | ||
{ | ||
// can't contain `*` | ||
input: "abcon*demand", | ||
expected: false, | ||
}, | ||
{ | ||
// 128 chars | ||
input: "abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef", | ||
expected: true, | ||
}, | ||
{ | ||
// 129 chars | ||
input: "abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefg", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q..", v.input) | ||
|
||
_, errors := FirewallRuleName(v.input, "name") | ||
actual := len(errors) == 0 | ||
if v.expected != actual { | ||
t.Fatalf("Expected %t but got %t", v.expected, actual) | ||
} | ||
} | ||
} |
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