Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROX-19609: add cluster name validation to infractl #980

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion cmd/infractl/cluster/create/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ func run(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, args []
displayUserNotes(cmd, args, &req)

if len(args) > 1 {
req.Parameters["name"] = args[1]
name := args[1]
err := validateName(name)
if err != nil {
return nil, err
}
req.Parameters["name"] = name
} else {
name, err := determineName(ctx, conn, args[0])
if err != nil {
Expand Down Expand Up @@ -146,6 +151,25 @@ func run(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, args []
return prettyResourceByID(*clusterID), nil
}

func validateName(name string) error {
if len(name) < 3 {
return errors.New("cluster name too short")
}
if len(name) > 28 {
return errors.New("cluster name too long")
}

match, err := regexp.MatchString(`^(?:[a-z](?:[-a-z0-9]{0,28}[a-z0-9])?)$`, name)
if err != nil {
return err
}
if !match {
return errors.New("The name does not match the requirements. Only lowercase letters, numbers, and '-' allowed, must start with a letter and end with a letter or number.")
}

return nil
}

func determineWorkingEnvironment() {
workingEnvironment.gitTopLevel = ""
workingEnvironment.tag = ""
Expand Down
18 changes: 18 additions & 0 deletions cmd/infractl/cluster/create/create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ setup() {
assert_output --partial "parameter \"main-image\" was not provided"
}

@test "provided name failed validation because too short" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦇

run infractl create test-qa-demo ab
assert_failure
assert_output --partial "Error: cluster name too short"
}

@test "provided name failed validation because too long" {
run infractl create test-qa-demo this-name-will-be-too-loooooooooooooooooooong
assert_failure
assert_output --partial "Error: cluster name too long"
}

@test "provided name failed validation because does not match regex" {
run infractl create test-qa-demo THIS-IN-INVALID
assert_failure
assert_output --partial "Error: The name does not match the requirements."
}

infractl() {
"$ROOT"/bin/infractl -e localhost:8443 -k "$@"
}
Expand Down