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

OCM-6879 | fix: allow user specify cluster-admin as admin username in day-1 #1869

Merged
merged 1 commit into from
Mar 22, 2024
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
2 changes: 1 addition & 1 deletion cmd/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ func run(cmd *cobra.Command, _ []string) {
os.Exit(1)
}
if isClusterAdmin {
clusterAdminUser = idp.GetIdpUserNameFromPrompt(cmd, r, "cluster-admin-user", clusterAdminUser)
clusterAdminUser = idp.GetIdpUserNameFromPrompt(cmd, r, "cluster-admin-user", clusterAdminUser, true)
isCustomAdminPassword, err := interactive.GetBool(interactive.Input{
Question: "Create custom password for cluster admin",
Default: false,
Expand Down
37 changes: 24 additions & 13 deletions cmd/create/idp/htpasswd.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,24 @@ func getUserList(cmd *cobra.Command, r *rosa.Runtime) (userList map[string]strin

func GetUserDetails(cmd *cobra.Command, r *rosa.Runtime,
usernameKey, passwordKey, defaultUsername, defaultPassword string) (string, string) {
return GetIdpUserNameFromPrompt(cmd, r, usernameKey, defaultUsername),
return GetIdpUserNameFromPrompt(cmd, r, usernameKey, defaultUsername, false),
GetIdpPasswordFromPrompt(cmd, r, passwordKey, defaultPassword)
}

func GetIdpUserNameFromPrompt(cmd *cobra.Command, r *rosa.Runtime,
usernameKey, defaultUsername string) string {
usernameKey, defaultUsername string, acceptClusterAdmin bool) string {
validators := []interactive.Validator{
UsernameValidator,
}
if !acceptClusterAdmin {
validators = append(validators, clusterAdminValidator)
}
username, err := interactive.GetString(interactive.Input{
Question: "Username",
Help: cmd.Flags().Lookup(usernameKey).Usage,
Default: defaultUsername,
Required: true,
Validators: []interactive.Validator{
UsernameValidator,
},
Question: "Username",
Help: cmd.Flags().Lookup(usernameKey).Usage,
Default: defaultUsername,
Required: true,
Validators: validators,
})
if err != nil {
exitHTPasswdCreate("Expected a valid username: %s", r.ClusterKey, err, r)
Expand Down Expand Up @@ -244,10 +248,6 @@ func exitHTPasswdCreate(format, clusterKey string, err error, r *rosa.Runtime) {

func UsernameValidator(val interface{}) error {
if username, ok := val.(string); ok {
if username == ClusterAdminUsername {
return fmt.Errorf("username '%s' is not allowed. It is preserved for cluster admin creation. "+
"Run `rosa create admin -c <cluster_id>` to create user '%s'", username, username)
}
if strings.ContainsAny(username, "/:%") {
return fmt.Errorf("invalid username '%s': "+
"username must not contain /, :, or %%", username)
Expand All @@ -257,6 +257,17 @@ func UsernameValidator(val interface{}) error {
return fmt.Errorf("can only validate strings, got '%v'", val)
}

func clusterAdminValidator(val interface{}) error {
if username, ok := val.(string); ok {
if username == ClusterAdminUsername {
return fmt.Errorf("username '%s' is not allowed. It is preserved for cluster admin creation. "+
"Run `rosa create admin -c <cluster_id>` to create user '%s'", username, username)
}
return nil
}
return fmt.Errorf("can only validate strings, got '%v'", val)
}

func parseHtpasswordFile(usersList *map[string]string, filePath string) error {

//A standard wellformed htpasswd file has rows of colon separated usernames and passwords
Expand Down
16 changes: 16 additions & 0 deletions cmd/create/idp/htpasswd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ var _ = Describe("IDP Tests", func() {
)
})

Describe("Username Validators Tests", func() {
It("username with `:` cannot pass clusterAdminValidator", func() {
username := "my:admin"
err := UsernameValidator(username)
Expect(err).To(HaveOccurred())
err = clusterAdminValidator(username)
Expect(err).NotTo(HaveOccurred())
})
It("username `cluster-admin` cannot pass clusterAdminValidator", func() {
username := "cluster-admin"
err := UsernameValidator(username)
Expect(err).NotTo(HaveOccurred())
err = clusterAdminValidator(username)
Expect(err).To(HaveOccurred())
})
})
})

func CreateTmpFile(content string) (*os.File, error) {
Expand Down