From 95574651bac98a1be2a5b32889eb7bfc2994597d Mon Sep 17 00:00:00 2001 From: Noam Gal Date: Fri, 10 Sep 2021 16:35:37 +0300 Subject: [PATCH] Bitbucket (#172) * use --git-user in gith auth --- cmd/commands/repo.go | 6 +++--- cmd/commands/repo_test.go | 2 +- docs/commands/argocd-autopilot_application.md | 1 + .../argocd-autopilot_application_create.md | 2 ++ .../argocd-autopilot_application_delete.md | 1 + .../commands/argocd-autopilot_application_list.md | 1 + docs/commands/argocd-autopilot_project.md | 1 + docs/commands/argocd-autopilot_project_create.md | 1 + docs/commands/argocd-autopilot_project_delete.md | 1 + docs/commands/argocd-autopilot_project_list.md | 1 + docs/commands/argocd-autopilot_repo_bootstrap.md | 1 + docs/commands/argocd-autopilot_repo_uninstall.md | 1 + pkg/git/repository.go | 15 +++++++++------ pkg/git/repository_test.go | 15 +++++---------- pkg/store/store.go | 4 ++-- 15 files changed, 31 insertions(+), 22 deletions(-) diff --git a/cmd/commands/repo.go b/cmd/commands/repo.go index 01687b8f..e3d57d14 100644 --- a/cmd/commands/repo.go +++ b/cmd/commands/repo.go @@ -446,7 +446,7 @@ func waitClusterReady(ctx context.Context, f kube.Factory, timeout time.Duration }) } -func getRepoCredsSecret(token, namespace string) ([]byte, error) { +func getRepoCredsSecret(username, token, namespace string) ([]byte, error) { return yaml.Marshal(&v1.Secret{ TypeMeta: metav1.TypeMeta{ APIVersion: "v1", @@ -460,7 +460,7 @@ func getRepoCredsSecret(token, namespace string) ([]byte, error) { }, }, Data: map[string][]byte{ - "git_username": []byte(store.Default.GitUsername), + "git_username": []byte(username), "git_token": []byte(token), }, }) @@ -586,7 +586,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon return nil, err } - manifests.repoCreds, err = getRepoCredsSecret(cloneOpts.Auth.Password, namespace) + manifests.repoCreds, err = getRepoCredsSecret(cloneOpts.Auth.Username, cloneOpts.Auth.Password, namespace) if err != nil { return nil, err } diff --git a/cmd/commands/repo_test.go b/cmd/commands/repo_test.go index 63ea67e0..f53bfefe 100644 --- a/cmd/commands/repo_test.go +++ b/cmd/commands/repo_test.go @@ -205,7 +205,7 @@ func Test_buildBootstrapManifests(t *testing.T) { assert.Equal(t, store.Default.RepoCredsSecretName, creds.ObjectMeta.Name) assert.Equal(t, "foo", creds.ObjectMeta.Namespace) assert.Equal(t, []byte("test"), creds.Data["git_token"]) - assert.Equal(t, []byte(store.Default.GitUsername), creds.Data["git_username"]) + assert.Equal(t, []byte(store.Default.GitHubUsername), creds.Data["git_username"]) }, }, } diff --git a/docs/commands/argocd-autopilot_application.md b/docs/commands/argocd-autopilot_application.md index 9aa4c2fa..a8892457 100644 --- a/docs/commands/argocd-autopilot_application.md +++ b/docs/commands/argocd-autopilot_application.md @@ -10,6 +10,7 @@ argocd-autopilot application [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) -h, --help help for application --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_application_create.md b/docs/commands/argocd-autopilot_application_create.md index 6af59a75..ae8d76fa 100644 --- a/docs/commands/argocd-autopilot_application_create.md +++ b/docs/commands/argocd-autopilot_application_create.md @@ -50,6 +50,7 @@ argocd-autopilot application create [APP_NAME] [flags] ``` --app string The application specifier (e.g. github.com/argoproj/argo-workflows/manifests/cluster-install/?ref=v3.0.3) --apps-git-token string Your git provider api token [APPS_GIT_TOKEN] + --apps-git-user string Your git provider user name [APPS_GIT_USER] (not required in GitHub) --apps-repo string Repository URL [APPS_GIT_REPO] --dest-namespace string K8s target namespace (overrides the namespace specified in the kustomization.yaml) --dest-server string K8s cluster URL (e.g. https://kubernetes.default.svc) (default "https://kubernetes.default.svc") @@ -67,6 +68,7 @@ argocd-autopilot application create [APP_NAME] [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_application_delete.md b/docs/commands/argocd-autopilot_application_delete.md index 95e957af..237660ff 100644 --- a/docs/commands/argocd-autopilot_application_delete.md +++ b/docs/commands/argocd-autopilot_application_delete.md @@ -38,6 +38,7 @@ argocd-autopilot application delete [APP_NAME] [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_application_list.md b/docs/commands/argocd-autopilot_application_list.md index a3f879cb..d02b07ce 100644 --- a/docs/commands/argocd-autopilot_application_list.md +++ b/docs/commands/argocd-autopilot_application_list.md @@ -36,6 +36,7 @@ argocd-autopilot application list [PROJECT_NAME] [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_project.md b/docs/commands/argocd-autopilot_project.md index 02fd3b15..325a487d 100644 --- a/docs/commands/argocd-autopilot_project.md +++ b/docs/commands/argocd-autopilot_project.md @@ -10,6 +10,7 @@ argocd-autopilot project [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) -h, --help help for project --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_project_create.md b/docs/commands/argocd-autopilot_project_create.md index dee3c8a0..83de3058 100644 --- a/docs/commands/argocd-autopilot_project_create.md +++ b/docs/commands/argocd-autopilot_project_create.md @@ -82,6 +82,7 @@ argocd-autopilot project create [PROJECT] [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_project_delete.md b/docs/commands/argocd-autopilot_project_delete.md index eed4c575..51436e0a 100644 --- a/docs/commands/argocd-autopilot_project_delete.md +++ b/docs/commands/argocd-autopilot_project_delete.md @@ -36,6 +36,7 @@ argocd-autopilot project delete [PROJECT_NAME] [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_project_list.md b/docs/commands/argocd-autopilot_project_list.md index b150fa6f..4927f40b 100644 --- a/docs/commands/argocd-autopilot_project_list.md +++ b/docs/commands/argocd-autopilot_project_list.md @@ -36,6 +36,7 @@ argocd-autopilot project list [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) --repo string Repository URL [GIT_REPO] ``` diff --git a/docs/commands/argocd-autopilot_repo_bootstrap.md b/docs/commands/argocd-autopilot_repo_bootstrap.md index 7aed9000..41d89cc3 100644 --- a/docs/commands/argocd-autopilot_repo_bootstrap.md +++ b/docs/commands/argocd-autopilot_repo_bootstrap.md @@ -37,6 +37,7 @@ argocd-autopilot repo bootstrap [flags] --app string The application specifier (e.g. github.com/argoproj-labs/argocd-autopilot/manifests?ref=v0.2.5), overrides the default installation argo-cd manifests --dry-run If true, print manifests instead of applying them to the cluster (nothing will be commited to git) -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) -h, --help help for bootstrap --hide-password If true, will not print initial argo cd password --insecure Run Argo-CD server without TLS diff --git a/docs/commands/argocd-autopilot_repo_uninstall.md b/docs/commands/argocd-autopilot_repo_uninstall.md index 7db6abc8..63ac074a 100644 --- a/docs/commands/argocd-autopilot_repo_uninstall.md +++ b/docs/commands/argocd-autopilot_repo_uninstall.md @@ -35,6 +35,7 @@ argocd-autopilot repo uninstall [flags] ``` -t, --git-token string Your git provider api token [GIT_TOKEN] + -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) -h, --help help for uninstall --kubeconfig string Path to the kubeconfig file to use for CLI requests. -n, --namespace string If present, the namespace scope for this CLI request diff --git a/pkg/git/repository.go b/pkg/git/repository.go index 5cb84f9b..aabd1c41 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -12,6 +12,7 @@ import ( "github.com/argoproj-labs/argocd-autopilot/pkg/fs" "github.com/argoproj-labs/argocd-autopilot/pkg/git/gogit" "github.com/argoproj-labs/argocd-autopilot/pkg/log" + "github.com/argoproj-labs/argocd-autopilot/pkg/store" "github.com/argoproj-labs/argocd-autopilot/pkg/util" billy "github.com/go-git/go-billy/v5" @@ -105,13 +106,16 @@ func AddFlags(cmd *cobra.Command, opts *AddFlagsOptions) *CloneOptions { envPrefix := strings.ReplaceAll(strings.ToUpper(opts.Prefix), "-", "_") cmd.PersistentFlags().StringVar(&co.Auth.Password, opts.Prefix+"git-token", "", fmt.Sprintf("Your git provider api token [%sGIT_TOKEN]", envPrefix)) + cmd.PersistentFlags().StringVar(&co.Auth.Username, opts.Prefix+"git-user", "", fmt.Sprintf("Your git provider user name [%sGIT_USER] (not required in GitHub)", envPrefix)) cmd.PersistentFlags().StringVar(&co.Repo, opts.Prefix+"repo", "", fmt.Sprintf("Repository URL [%sGIT_REPO]", envPrefix)) util.Die(viper.BindEnv(opts.Prefix+"git-token", envPrefix+"GIT_TOKEN")) + util.Die(viper.BindEnv(opts.Prefix+"git-user", envPrefix+"GIT_USER")) util.Die(viper.BindEnv(opts.Prefix+"repo", envPrefix+"GIT_REPO")) if opts.Prefix == "" { cmd.Flag("git-token").Shorthand = "t" + cmd.Flag("git-user").Shorthand = "u" } if opts.CreateIfNotExist { @@ -135,6 +139,10 @@ func (o *CloneOptions) Parse() { host, orgRepo, o.path, o.revision, _, suffix, _ = util.ParseGitUrl(o.Repo) o.url = host + orgRepo + suffix + + if o.Auth.Username == "" { + o.Auth.Username = store.Default.GitHubUsername + } } func (o *CloneOptions) GetRepo(ctx context.Context) (Repository, fs.FS, error) { @@ -377,13 +385,8 @@ func getAuth(auth Auth) transport.AuthMethod { return nil } - username := auth.Username - if username == "" { - username = "git" - } - return &http.BasicAuth{ - Username: username, + Username: auth.Username, Password: auth.Password, } } diff --git a/pkg/git/repository_test.go b/pkg/git/repository_test.go index f250be48..d4c527ec 100644 --- a/pkg/git/repository_test.go +++ b/pkg/git/repository_test.go @@ -85,16 +85,7 @@ func Test_getAuth(t *testing.T) { auth Auth want transport.AuthMethod }{ - "Basic": { - auth: Auth{ - Password: "123", - }, - want: &http.BasicAuth{ - Username: "git", - Password: "123", - }, - }, - "Username": { + "Should use the supplied username": { auth: Auth{ Username: "test", Password: "123", @@ -104,6 +95,10 @@ func Test_getAuth(t *testing.T) { Password: "123", }, }, + "Should return nil if no password is supplied": { + auth: Auth{}, + want: nil, + }, } for tname, tt := range tests { t.Run(tname, func(t *testing.T) { diff --git a/pkg/store/store.go b/pkg/store/store.go index f87122e6..aa6ab26a 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -45,7 +45,7 @@ var Default = struct { DestServer string DummyName string DestServerAnnotation string - GitUsername string + GitHubUsername string LabelKeyAppName string LabelKeyAppManagedBy string LabelValueManagedBy string @@ -66,7 +66,7 @@ var Default = struct { DestServer: "https://kubernetes.default.svc", DestServerAnnotation: "argocd-autopilot.argoproj-labs.io/default-dest-server", DummyName: "DUMMY", - GitUsername: "username", + GitHubUsername: "username", LabelKeyAppName: "app.kubernetes.io/name", LabelKeyAppManagedBy: "app.kubernetes.io/managed-by", LabelValueManagedBy: "argocd-autopilot",