Skip to content

Commit

Permalink
cli: fix invalid upper case name on AWS (#2546)
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead authored Nov 3, 2023
1 parent d67f1a0 commit eaec73c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cli/internal/terraform/terraform/aws/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ variable "name" {
condition = length(var.name) <= 10
error_message = "The length of the name of the Constellation must be <= 10 characters"
}
validation {
condition = var.name == lower(var.name)
error_message = "The name of the Constellation must be in lowercase"
}
}

variable "node_groups" {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ func (c *Config) Validate(force bool) error {
return err
}

if err := validate.RegisterTranslation("valid_name", trans, registerValidateNameError, c.translateValidateNameError); err != nil {
if err := validate.RegisterTranslation("valid_name", trans, c.registerValidateNameError, c.translateValidateNameError); err != nil {
return err
}

Expand Down
10 changes: 10 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@ func TestValidate(t *testing.T) {
wantErr: true,
wantErrCount: awsErrCount,
},
"AWS config with upper case name": {
cnf: func() *Config {
cnf := Default()
cnf.RemoveProviderAndAttestationExcept(cloudprovider.AWS)
cnf.Name = "testAWS"
return cnf
}(),
wantErr: true,
wantErrCount: awsErrCount + 1,
},
"AWS config with correct region and zone format": {
cnf: func() *Config {
cnf := Default()
Expand Down
8 changes: 6 additions & 2 deletions internal/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ func returnsTrue(_ validator.FieldLevel) bool {
return true
}

func registerValidateNameError(ut ut.Translator) error {
func (c *Config) registerValidateNameError(ut ut.Translator) error {
if c.Provider.AWS != nil {
return ut.Add("validate_name", "{0} must be no more than {1} characters long and contain only lowercase letters", true)
}
return ut.Add("validate_name", "{0} must be no more than {1} characters long", true)
}

Expand All @@ -731,7 +734,8 @@ func (c *Config) translateValidateNameError(ut ut.Translator, fe validator.Field
// This also allows us to eventually add more validation rules for constellation names if necessary.
func (c *Config) validateName(fl validator.FieldLevel) bool {
if c.Provider.AWS != nil {
return len(fl.Field().String()) <= constants.AWSConstellationNameLength
name := fl.Field().String()
return strings.ToLower(name) == name && len(name) <= constants.AWSConstellationNameLength
}
return len(fl.Field().String()) <= constants.ConstellationNameLength
}
Expand Down

0 comments on commit eaec73c

Please sign in to comment.