cli: fix invalid upper case name on AWS (#2546)

This commit is contained in:
Adrian Stobbe 2023-11-03 10:09:43 +01:00 committed by GitHub
parent d67f1a035f
commit eaec73cca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 3 deletions

View File

@ -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" {

View File

@ -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
}

View File

@ -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()

View File

@ -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)
}
@ -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
}