From 116a671279121171385dca2a9a6cc2bfdd15a612 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 21 Aug 2020 20:58:27 -0400 Subject: [PATCH] resource/aws_service_discovery_http_namespace: Correct name validation (#14749) --- ...ce_aws_service_discovery_http_namespace.go | 2 +- ...service_discovery_private_dns_namespace.go | 7 ++-- ..._service_discovery_public_dns_namespace.go | 7 ++-- aws/validators.go | 26 +++------------ aws/validators_test.go | 33 +++++++++++++++++++ 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/aws/resource_aws_service_discovery_http_namespace.go b/aws/resource_aws_service_discovery_http_namespace.go index 25b9f8a5369..33c50b81834 100644 --- a/aws/resource_aws_service_discovery_http_namespace.go +++ b/aws/resource_aws_service_discovery_http_namespace.go @@ -27,7 +27,7 @@ func resourceAwsServiceDiscoveryHttpNamespace() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validateServiceDiscoveryHttpNamespaceName, + ValidateFunc: validateServiceDiscoveryNamespaceName, }, "description": { Type: schema.TypeString, diff --git a/aws/resource_aws_service_discovery_private_dns_namespace.go b/aws/resource_aws_service_discovery_private_dns_namespace.go index 542e72ab7f6..bb7d984729e 100644 --- a/aws/resource_aws_service_discovery_private_dns_namespace.go +++ b/aws/resource_aws_service_discovery_private_dns_namespace.go @@ -32,9 +32,10 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespace() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateServiceDiscoveryNamespaceName, }, "description": { Type: schema.TypeString, diff --git a/aws/resource_aws_service_discovery_public_dns_namespace.go b/aws/resource_aws_service_discovery_public_dns_namespace.go index f26fea7623f..8955ef963e0 100644 --- a/aws/resource_aws_service_discovery_public_dns_namespace.go +++ b/aws/resource_aws_service_discovery_public_dns_namespace.go @@ -24,9 +24,10 @@ func resourceAwsServiceDiscoveryPublicDnsNamespace() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateServiceDiscoveryNamespaceName, }, "description": { Type: schema.TypeString, diff --git a/aws/validators.go b/aws/validators.go index 12ae9401c9e..c9b3fd4b164 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -2354,27 +2354,6 @@ func validateCloudFrontPublicKeyNamePrefix(v interface{}, k string) (ws []string return } -func validateServiceDiscoveryHttpNamespaceName(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - if !regexp.MustCompile(`^[0-9A-Za-z_-]+$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "only alphanumeric characters, underscores and hyphens allowed in %q", k)) - } - if !regexp.MustCompile(`^[a-zA-Z]`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "first character of %q must be a letter", k)) - } - if !regexp.MustCompile(`[a-zA-Z]$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "last character of %q must be a letter", k)) - } - if len(value) > 1024 { - errors = append(errors, fmt.Errorf( - "%q cannot be greater than 1024 characters", k)) - } - return -} - func validateLbTargetGroupName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if len(value) > 32 { @@ -2478,3 +2457,8 @@ func validateRoute53ResolverName(v interface{}, k string) (ws []string, errors [ return } + +var validateServiceDiscoveryNamespaceName = validation.All( + validation.StringLenBetween(1, 1024), + validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._-]+$`), ""), +) diff --git a/aws/validators_test.go b/aws/validators_test.go index 9439b384c91..8c566d3bffa 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -3146,3 +3146,36 @@ func TestValidateRoute53ResolverName(t *testing.T) { } } } + +func TestValidateServiceDiscoveryNamespaceName(t *testing.T) { + validNames := []string{ + "ValidName", + "V_-.dN01e", + "0", + ".", + "-", + "_", + strings.Repeat("x", 1024), + } + for _, v := range validNames { + _, errors := validateServiceDiscoveryNamespaceName(v, "name") + if len(errors) != 0 { + t.Fatalf("%q should be a valid namespace name: %q", v, errors) + } + } + + invalidNames := []string{ + "Inval:dName", + "Invalid Name", + "*", + "", + // length > 512 + strings.Repeat("x", 1025), + } + for _, v := range invalidNames { + _, errors := validateServiceDiscoveryNamespaceName(v, "name") + if len(errors) == 0 { + t.Fatalf("%q should be an invalid namespace name", v) + } + } +}