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

Interactive template auth #613

Merged
merged 10 commits into from
Nov 7, 2024
97 changes: 91 additions & 6 deletions cyctl/internal/create/template_auth_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package create

import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
v1Spec "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -24,16 +26,89 @@ cyctl create templateauthrule demo-templateauthrule --repo='https://github.com/c
)

var (
username string
password string
username string
password string
usernameName string
passwordName string
usernameKey string
passwordKey string
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remove these four from here. username and password are here because those are used to bind flag values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this I have a doubt here that we are taking the individuals and should we need to add the values the same as username secret name: username secret key , and password secret name: password secret key

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Abiji-2020 I would just move these variables to createTemplateAuthRule function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@petar-cvit could you check it

Copy link
Contributor Author

@Abiji-2020 Abiji-2020 Oct 31, 2024

Choose a reason for hiding this comment

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

@petar-cvit Modified it.

)

func getTeamplateAuthRulesFromPromt() (string, string, string, string, string, error) {
RepoPrompt := promptui.Prompt{
Label: "repo",
}
repoValue, err := RepoPrompt.Run()
if err != nil {
return "", "", "", "", "", fmt.Errorf("Prompt failed: %v", err)
}
usernameNamePrompt := promptui.Prompt{
Label: "Username: Name",
Validate: func(input string) error {
if input == "" {
return errors.New("The username Name cannot be empty")
}
return nil
},
}
usernameName, err := usernameNamePrompt.Run()
if err != nil {
return "", "", "", "", "", fmt.Errorf("Prompt failed: %v", err)
}

usernameKeyPrompt := promptui.Prompt{
Label: "Username: Key",
Validate: func(input string) error {
if input == "" {
return errors.New("The username key cannot be empty")
}
return nil
},
}
usernameKey, err := usernameKeyPrompt.Run()
if err != nil {
return "", "", "", "", "", fmt.Errorf("Prompt failed: %v", err)
}

passwordNamePrompt := promptui.Prompt{
Label: "Password: Name",
Validate: func(input string) error {
if input == "" {
return errors.New("The password name cannot be empty")
}
return nil
},
}
passwordName, err := passwordNamePrompt.Run()
if err != nil {
return "", "", "", "", "", fmt.Errorf("Prompt failed: %v", err)
}

passwordKeyPrompt := promptui.Prompt{
Label: "Password: Key",
Validate: func(input string) error {
if input == "" {
return errors.New("The password key cannot be empty")
}
return nil
},
}
passwordKey, err := passwordKeyPrompt.Run()
if err != nil {
return "", "", "", "", "", fmt.Errorf("Prompt failed: %v", err)
}

return usernameName, usernameKey, passwordName, passwordKey, repoValue, nil

}

func validateSecretKeySelector(username, password string) (string, string, string, string, error) {
usernameName, usernameKey := splitNameKey(username)
passwordName, passwordKey := splitNameKey(password)

// Ensure both name and key are present
if usernameName == "" || usernameKey == "" || passwordName == "" || passwordKey == "" {

return "", "", "", "", fmt.Errorf("invalid format for username or password. Expected 'name:key'")
}

Expand All @@ -50,10 +125,20 @@ func splitNameKey(input string) (string, string) {

// createTemplateAuthRule allows you to create TemplateAuthRule Custom Resource.
func createTemplateAuthRule(clientset *client.CyclopsV1Alpha1Client, templateAuthRuleName string) {
usernameName, usernameKey, passwordName, passwordKey, err := validateSecretKeySelector(username, password)
if err != nil {
fmt.Println(err)
return
if username == "" && password == "" && repo == "" {
var err error
usernameName, usernameKey, passwordName, passwordKey, repo, err = getTeamplateAuthRulesFromPromt()
if err != nil {
fmt.Println(err)
return
}
} else {
var err error
usernameName, usernameKey, passwordName, passwordKey, err = validateSecretKeySelector(username, password)
if err != nil {
fmt.Println(err)
return
}
}

var localObjectNameRef, localObjectPasswordRef v1Spec.LocalObjectReference
Expand Down
Loading