Skip to content

Commit

Permalink
feat: check disable secret creation flag in client
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed Feb 21, 2024
1 parent fe5b31f commit 8133ce8
Showing 1 changed file with 71 additions and 16 deletions.
87 changes: 71 additions & 16 deletions cmd/kubectl-testkube/commands/common/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package common

import (
"fmt"
"strconv"

"github.com/spf13/cobra"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)

func hasGitParamsInCmd(cmd *cobra.Command) bool {
var fields = []string{"git-uri", "git-branch", "git-commit", "git-path", "git-username", "git-token",
func hasGitParamsInCmd(cmd *cobra.Command, crdOnly bool) bool {
var fields = []string{"git-uri", "git-branch", "git-commit", "git-path",
"git-username-secret", "git-token-secret", "git-working-dir", "git-certificate-secret", "git-auth-type"}
if !crdOnly {
fields = append(fields, "git-username", "git-token")
}

for _, field := range fields {
if cmd.Flag(field).Changed {
return true
Expand All @@ -22,12 +28,30 @@ func hasGitParamsInCmd(cmd *cobra.Command) bool {

// NewRepositoryFromFlags creates repository from command flags
func NewRepositoryFromFlags(cmd *cobra.Command) (repository *testkube.Repository, err error) {
crdOnly, err := strconv.ParseBool(cmd.Flag("crd-only").Value.String())
if err != nil {
return nil, err
}

gitUri := cmd.Flag("git-uri").Value.String()
gitBranch := cmd.Flag("git-branch").Value.String()
gitCommit := cmd.Flag("git-commit").Value.String()
gitPath := cmd.Flag("git-path").Value.String()
gitUsername := cmd.Flag("git-username").Value.String()
gitToken := cmd.Flag("git-token").Value.String()

var gitUsername, gitToken string
if !crdOnly {
client, _, err := GetClient(cmd)
ui.ExitOnError("getting client", err)

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

if !info.DisableSecretCreation {
gitUsername = cmd.Flag("git-username").Value.String()
gitToken = cmd.Flag("git-token").Value.String()
}
}

gitUsernameSecret, err := cmd.Flags().GetStringToString("git-username-secret")
if err != nil {
return nil, err
Expand All @@ -42,7 +66,7 @@ func NewRepositoryFromFlags(cmd *cobra.Command) (repository *testkube.Repository
gitCertificateSecret := cmd.Flag("git-certificate-secret").Value.String()
gitAuthType := cmd.Flag("git-auth-type").Value.String()

hasGitParams := hasGitParamsInCmd(cmd)
hasGitParams := hasGitParamsInCmd(cmd, crdOnly)
if !hasGitParams {
return nil, nil
}
Expand Down Expand Up @@ -101,14 +125,6 @@ func NewRepositoryUpdateFromFlags(cmd *cobra.Command) (repository *testkube.Repo
"git-path",
&repository.Path,
},
{
"git-username",
&repository.Username,
},
{
"git-token",
&repository.Token,
},
{
"git-working-dir",
&repository.WorkingDir,
Expand All @@ -123,6 +139,27 @@ func NewRepositoryUpdateFromFlags(cmd *cobra.Command) (repository *testkube.Repo
},
}

client, _, err := GetClient(cmd)
ui.ExitOnError("getting client", err)

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

if !info.DisableSecretCreation {
fields = append(fields, []struct {
name string
destination **string
}{
{
"git-username",
&repository.Username,
},
{
"git-token",
&repository.Token,
}}...)
}

var nonEmpty bool
for _, field := range fields {
if cmd.Flag(field.name).Changed {
Expand Down Expand Up @@ -174,11 +211,29 @@ func NewRepositoryUpdateFromFlags(cmd *cobra.Command) (repository *testkube.Repo

// ValidateUpsertOptions validates upsert options
func ValidateUpsertOptions(cmd *cobra.Command, sourceName string) error {
crdOnly, err := strconv.ParseBool(cmd.Flag("crd-only").Value.String())
if err != nil {
return err
}

gitUri := cmd.Flag("git-uri").Value.String()
gitBranch := cmd.Flag("git-branch").Value.String()
gitCommit := cmd.Flag("git-commit").Value.String()
gitUsername := cmd.Flag("git-username").Value.String()
gitToken := cmd.Flag("git-token").Value.String()

var gitUsername, gitToken string
if !crdOnly {
client, _, err := GetClient(cmd)
ui.ExitOnError("getting client", err)

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

if !info.DisableSecretCreation {
gitUsername = cmd.Flag("git-username").Value.String()
gitToken = cmd.Flag("git-token").Value.String()
}
}

gitUsernameSecret, err := cmd.Flags().GetStringToString("git-username-secret")
if err != nil {
return err
Expand All @@ -193,7 +248,7 @@ func ValidateUpsertOptions(cmd *cobra.Command, sourceName string) error {
file := cmd.Flag("file").Value.String()
uri := cmd.Flag("uri").Value.String()

hasGitParams := hasGitParamsInCmd(cmd)
hasGitParams := hasGitParamsInCmd(cmd, crdOnly)
if hasGitParams && uri != "" {
return fmt.Errorf("found git params and `--uri` flag, please use `--git-uri` for git based repo or `--uri` without git based params")
}
Expand Down

0 comments on commit 8133ce8

Please sign in to comment.