Skip to content

Commit

Permalink
Add validations for K8s resource fields (#2772)
Browse files Browse the repository at this point in the history
This PR adds some validation methods for K8s resource fields.

Fixes #1405.
  • Loading branch information
metacpp authored and katbyte committed Jan 30, 2019
1 parent 74ba853 commit 0aedd77
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 131 deletions.
39 changes: 39 additions & 0 deletions azurerm/helpers/validate/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package validate

import (
"fmt"
"regexp"
)

func KubernetesAdminUserName(i interface{}, k string) (warnings []string, errors []error) {
adminUserName := i.(string)

re := regexp.MustCompile(`^[A-Za-z][-A-Za-z0-9_]*$`)
if re != nil && !re.MatchString(adminUserName) {
errors = append(errors, fmt.Errorf("%s must start with alphabet and/or continue with alphanumeric characters, underscores, hyphens. Got %q.", k, adminUserName))
}

return warnings, errors
}

func KubernetesAgentPoolName(i interface{}, k string) (warnings []string, errors []error) {
agentPoolName := i.(string)

re := regexp.MustCompile(`^[a-z]{1}[a-z0-9]{0,11}$`)
if re != nil && !re.MatchString(agentPoolName) {
errors = append(errors, fmt.Errorf("%s must start with a lowercase letter, have max length of 12, and only have characters a-z0-9. Got %q.", k, agentPoolName))
}

return warnings, errors
}

func KubernetesDNSPrefix(i interface{}, k string) (warnings []string, errors []error) {
dnsPrefix := i.(string)

re := regexp.MustCompile(`^[a-zA-Z][-a-zA-Z0-9]{0,43}[a-zA-Z0-9]$`)
if re != nil && !re.MatchString(dnsPrefix) {
errors = append(errors, fmt.Errorf("%s must contain between 2 and 45 characters. The name can contain only letters, numbers, and hyphens. The name must start with a letter and must end with an alphanumeric character.. Got %q.", k, dnsPrefix))
}

return warnings, errors
}
135 changes: 135 additions & 0 deletions azurerm/helpers/validate/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package validate

import (
"testing"
)

func TestKubernetesAdminUserName(t *testing.T) {
cases := []struct {
AdminUserName string
Errors int
}{
{
AdminUserName: "",
Errors: 1,
},
{
AdminUserName: "Abc-123_abc",
Errors: 0,
},
{
AdminUserName: "123abc",
Errors: 1,
},
}

for _, tc := range cases {
t.Run(tc.AdminUserName, func(t *testing.T) {
_, errors := KubernetesAdminUserName(tc.AdminUserName, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected AdminUserName to return %d error(s) not %d", tc.Errors, len(errors))
}
})
}
}

func TestKubernetesAgentPoolName(t *testing.T) {
cases := []struct {
AgentPoolName string
Errors int
}{
{
AgentPoolName: "",
Errors: 1,
},
{
AgentPoolName: "ABC123",
Errors: 1,
},
{
AgentPoolName: "abc123",
Errors: 0,
},
{
AgentPoolName: "123abc",
Errors: 1,
},
{
AgentPoolName: "hi",
Errors: 0,
},
{
AgentPoolName: "hello",
Errors: 0,
},
{
AgentPoolName: "hello-world",
Errors: 1,
},
{
AgentPoolName: "helloworld123",
Errors: 1,
},
{
AgentPoolName: "hello_world",
Errors: 1,
},
{
AgentPoolName: "Hello-World",
Errors: 1,
},
{
AgentPoolName: "20202020",
Errors: 1,
},
{
AgentPoolName: "h20202020",
Errors: 0,
},
{
AgentPoolName: "ABC123!@£",
Errors: 1,
},
}

for _, tc := range cases {
t.Run(tc.AgentPoolName, func(t *testing.T) {
_, errors := KubernetesAgentPoolName(tc.AgentPoolName, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected AgentPoolName to return %d error(s) not %d", tc.Errors, len(errors))
}
})
}
}

func TestKubernetesDNSPrefix(t *testing.T) {
cases := []struct {
DNSPrefix string
Errors int
}{
{
DNSPrefix: "",
Errors: 1,
},
{
DNSPrefix: "a",
Errors: 1,
},
{
DNSPrefix: "aBc-123abc",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.DNSPrefix, func(t *testing.T) {
_, errors := KubernetesDNSPrefix(tc.DNSPrefix, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected DNSPrefix to return %d error(s) not %d", tc.Errors, len(errors))
}
})
}
}
12 changes: 12 additions & 0 deletions azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validate
import (
"fmt"
"net"
"regexp"
)

func IPv6Address(i interface{}, k string) (warnings []string, errors []error) {
Expand All @@ -29,6 +30,17 @@ func validateIpv6Address(i interface{}, k string, allowEmpty bool) (warnings []s

}

func CIDR(i interface{}, k string) (warnings []string, errors []error) {
cidr := i.(string)

re := regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$`)
if re != nil && !re.MatchString(cidr) {
errors = append(errors, fmt.Errorf("%s must start with IPV4 address and/or slash, number of bits (0-32) as prefix. Example: 127.0.0.1/8. Got %q.", k, cidr))
}

return warnings, errors
}

func IPv4Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv4Address(i, k, false)
}
Expand Down
38 changes: 38 additions & 0 deletions azurerm/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ import (
"testing"
)

func TestCIDR(t *testing.T) {
cases := []struct {
CIDR string
Errors int
}{
{
CIDR: "",
Errors: 1,
},
{
CIDR: "0.0.0.0",
Errors: 0,
},
{
CIDR: "127.0.0.1/8",
Errors: 0,
},
{
CIDR: "127.0.0.1/33",
Errors: 1,
},
{
CIDR: "127.0.0.1/-1",
Errors: 1,
},
}

for _, tc := range cases {
t.Run(tc.CIDR, func(t *testing.T) {
_, errors := CIDR(tc.CIDR, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected CIDR to return %d error(s) not %d", tc.Errors, len(errors))
}
})
}
}

func TestIPv6Address(t *testing.T) {
cases := []struct {
IP string
Expand Down
Loading

0 comments on commit 0aedd77

Please sign in to comment.