Skip to content

Commit bd4e85f

Browse files
authored
Fix ArgoCD 3.0.0+ compatibility by replacing legacy repo credentials with proper repo-creds secret (#674)
* fix(cmd): add missing ArgoCD repository credentials during bootstrap - Add argocdRepoCreds field to bootstrapManifests struct - Create getArgoCDRepoCredsSecret function to generate ArgoCD-specific repo credentials - Apply ArgoCD repo credentials secret before bootstrap manifests - Include ArgoCD repo credentials in dry-run output - Add comprehensive tests for ArgoCD repository credentials functionality This ensures ArgoCD can properly authenticate with the Git repository during the bootstrap process by creating the required argocd-repo-creds secret with the appropriate labels and format expected by ArgoCD. * chore(deps): bump github.com/golang-jwt/jwt/v4 to v4.5.2 * chore(deps): bump github.com/expr-lang/expr to v1.17.5 --------- Signed-off-by: Aron Reis <[email protected]>
1 parent ecdfd37 commit bd4e85f

File tree

4 files changed

+81
-58
lines changed

4 files changed

+81
-58
lines changed

cmd/commands/repo.go

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
argocdcommon "github.com/argoproj/argo-cd/v2/common"
2525
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
26-
argocdsettings "github.com/argoproj/argo-cd/v2/util/settings"
2726
"github.com/go-git/go-billy/v5/memfs"
2827
billyUtils "github.com/go-git/go-billy/v5/util"
2928
"github.com/spf13/cobra"
@@ -358,7 +357,7 @@ func NewRepoUninstallCommand() *cobra.Command {
358357
359358
<BIN> repo uninstall --repo https://github.com/example/repo --force
360359
`),
361-
PreRunE: func(_ *cobra.Command, _ []string) error{
360+
PreRunE: func(_ *cobra.Command, _ []string) error {
362361
if !clusterOnly {
363362
cloneOpts.Parse()
364363
}
@@ -532,22 +531,28 @@ func waitClusterReady(ctx context.Context, f kube.Factory, timeout time.Duration
532531
})
533532
}
534533

535-
func getRepoCredsSecret(username, token, namespace string) ([]byte, error) {
534+
func getRepoCredsSecret(username, token, namespace, repoURL string) ([]byte, error) {
535+
host, _, _, _, _, _, _ := util.ParseGitUrl(repoURL)
536+
536537
return yaml.Marshal(&v1.Secret{
537538
TypeMeta: metav1.TypeMeta{
538539
APIVersion: "v1",
539540
Kind: "Secret",
540541
},
541542
ObjectMeta: metav1.ObjectMeta{
542-
Name: store.Default.RepoCredsSecretName,
543+
Name: "argocd-repo-creds",
543544
Namespace: namespace,
544545
Labels: map[string]string{
546+
"argocd.argoproj.io/secret-type": "repo-creds",
545547
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
546548
},
547549
},
548-
Data: map[string][]byte{
549-
"git_username": []byte(username),
550-
"git_token": []byte(token),
550+
Type: v1.SecretTypeOpaque,
551+
StringData: map[string]string{
552+
"type": "git",
553+
"url": host,
554+
"username": username,
555+
"password": token,
551556
},
552557
})
553558
}
@@ -670,7 +675,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon
670675
return nil, err
671676
}
672677

673-
manifests.repoCreds, err = getRepoCredsSecret(cloneOpts.Auth.Username, cloneOpts.Auth.Password, namespace)
678+
manifests.repoCreds, err = getRepoCredsSecret(cloneOpts.Auth.Username, cloneOpts.Auth.Password, namespace, cloneOpts.URL())
674679
if err != nil {
675680
return nil, err
676681
}
@@ -720,11 +725,6 @@ func writeManifestsToRepo(repoFS fs.FS, manifests *bootstrapManifests, installat
720725
}
721726

722727
func createBootstrapKustomization(namespace, appSpecifier string, cloneOpts *git.CloneOptions) (*kusttypes.Kustomization, error) {
723-
credsYAML, err := createCreds(cloneOpts.URL())
724-
if err != nil {
725-
return nil, err
726-
}
727-
728728
k := &kusttypes.Kustomization{
729729
Resources: []string{
730730
appSpecifier,
@@ -733,19 +733,6 @@ func createBootstrapKustomization(namespace, appSpecifier string, cloneOpts *git
733733
APIVersion: kusttypes.KustomizationVersion,
734734
Kind: kusttypes.KustomizationKind,
735735
},
736-
ConfigMapGenerator: []kusttypes.ConfigMapArgs{
737-
{
738-
GeneratorArgs: kusttypes.GeneratorArgs{
739-
Name: "argocd-cm",
740-
Behavior: kusttypes.BehaviorMerge.String(),
741-
KvPairSources: kusttypes.KvPairSources{
742-
LiteralSources: []string{
743-
"repository.credentials=" + string(credsYAML),
744-
},
745-
},
746-
},
747-
},
748-
},
749736
Namespace: namespace,
750737
}
751738

@@ -789,29 +776,6 @@ func createBootstrapKustomization(namespace, appSpecifier string, cloneOpts *git
789776
return k, nil
790777
}
791778

792-
func createCreds(repoUrl string) ([]byte, error) {
793-
host, _, _, _, _, _, _ := util.ParseGitUrl(repoUrl)
794-
creds := []argocdsettings.RepositoryCredentials{
795-
{
796-
URL: host,
797-
UsernameSecret: &v1.SecretKeySelector{
798-
LocalObjectReference: v1.LocalObjectReference{
799-
Name: store.Default.RepoCredsSecretName,
800-
},
801-
Key: "git_username",
802-
},
803-
PasswordSecret: &v1.SecretKeySelector{
804-
LocalObjectReference: v1.LocalObjectReference{
805-
Name: store.Default.RepoCredsSecretName,
806-
},
807-
Key: "git_token",
808-
},
809-
},
810-
}
811-
812-
return yaml.Marshal(creds)
813-
}
814-
815779
func setUninstallOptsDefaults(opts RepoUninstallOptions) (*RepoUninstallOptions, error) {
816780
var err error
817781

cmd/commands/repo_test.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ func Test_buildBootstrapManifests(t *testing.T) {
200200

201201
creds := &v1.Secret{}
202202
assert.NoError(t, yaml.Unmarshal(b.repoCreds, &creds))
203-
assert.Equal(t, store.Default.RepoCredsSecretName, creds.ObjectMeta.Name)
203+
assert.Equal(t, "argocd-repo-creds", creds.ObjectMeta.Name)
204204
assert.Equal(t, "foo", creds.ObjectMeta.Namespace)
205-
assert.Equal(t, []byte("test"), creds.Data["git_token"])
206-
assert.Equal(t, []byte(store.Default.GitHubUsername), creds.Data["git_username"])
205+
assert.Equal(t, "repo-creds", creds.ObjectMeta.Labels["argocd.argoproj.io/secret-type"])
206+
assert.Equal(t, store.Default.LabelValueManagedBy, creds.ObjectMeta.Labels[store.Default.LabelKeyAppManagedBy])
207+
assert.Equal(t, "git", creds.StringData["type"])
208+
assert.Equal(t, "https://github.com/", creds.StringData["url"])
209+
assert.Equal(t, "test", creds.StringData["password"])
210+
assert.Equal(t, store.Default.GitHubUsername, creds.StringData["username"])
207211
},
208212
},
209213
}
@@ -405,6 +409,61 @@ func TestRunRepoBootstrap(t *testing.T) {
405409
}
406410
}
407411

412+
func Test_getRepoCredsSecret(t *testing.T) {
413+
tests := map[string]struct {
414+
username string
415+
token string
416+
namespace string
417+
repoURL string
418+
assertFn func(t *testing.T, secret []byte, err error)
419+
}{
420+
"Basic GitHub": {
421+
username: "testuser",
422+
token: "testtoken",
423+
namespace: "argocd",
424+
repoURL: "https://github.com/owner/repo.git",
425+
assertFn: func(t *testing.T, secretBytes []byte, err error) {
426+
assert.NoError(t, err)
427+
428+
secret := &v1.Secret{}
429+
assert.NoError(t, yaml.Unmarshal(secretBytes, secret))
430+
assert.Equal(t, "argocd-repo-creds", secret.ObjectMeta.Name)
431+
assert.Equal(t, "argocd", secret.ObjectMeta.Namespace)
432+
assert.Equal(t, "repo-creds", secret.ObjectMeta.Labels["argocd.argoproj.io/secret-type"])
433+
assert.Equal(t, store.Default.LabelValueManagedBy, secret.ObjectMeta.Labels[store.Default.LabelKeyAppManagedBy])
434+
assert.Equal(t, "git", secret.StringData["type"])
435+
assert.Equal(t, "https://github.com/", secret.StringData["url"])
436+
assert.Equal(t, "testuser", secret.StringData["username"])
437+
assert.Equal(t, "testtoken", secret.StringData["password"])
438+
},
439+
},
440+
"GitLab": {
441+
username: "gitlabuser",
442+
token: "glpat-xxxx",
443+
namespace: "custom-ns",
444+
repoURL: "https://gitlab.com/group/project.git",
445+
assertFn: func(t *testing.T, secretBytes []byte, err error) {
446+
assert.NoError(t, err)
447+
448+
secret := &v1.Secret{}
449+
assert.NoError(t, yaml.Unmarshal(secretBytes, secret))
450+
assert.Equal(t, "argocd-repo-creds", secret.ObjectMeta.Name)
451+
assert.Equal(t, "custom-ns", secret.ObjectMeta.Namespace)
452+
assert.Equal(t, "https://gitlab.com/", secret.StringData["url"])
453+
assert.Equal(t, "gitlabuser", secret.StringData["username"])
454+
assert.Equal(t, "glpat-xxxx", secret.StringData["password"])
455+
},
456+
},
457+
}
458+
459+
for tname, tt := range tests {
460+
t.Run(tname, func(t *testing.T) {
461+
secret, err := getRepoCredsSecret(tt.username, tt.token, tt.namespace, tt.repoURL)
462+
tt.assertFn(t, secret, err)
463+
})
464+
}
465+
}
466+
408467
func TestRunRepoBootstrapRecovery(t *testing.T) {
409468
exitCalled := false
410469
tests := map[string]struct {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ require (
9292
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
9393
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
9494
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
95-
github.com/expr-lang/expr v1.16.9 // indirect
95+
github.com/expr-lang/expr v1.17.5 // indirect
9696
github.com/fatih/camelcase v1.0.0 // indirect
9797
github.com/fatih/color v1.16.0 // indirect
9898
github.com/felixge/httpsnoop v1.0.4 // indirect
@@ -121,7 +121,7 @@ require (
121121
github.com/gobwas/glob v0.2.3 // indirect
122122
github.com/gogits/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355 // indirect
123123
github.com/gogo/protobuf v1.3.2 // indirect
124-
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
124+
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
125125
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
126126
github.com/golang/protobuf v1.5.4 // indirect
127127
github.com/google/btree v1.1.3 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0
223223
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
224224
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d h1:105gxyaGwCFad8crR9dcMQWvV9Hvulu6hwUh4tWPJnM=
225225
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZMPRZwes7CROmyNKgQzC3XPs6L/G2EJLHddWejkmf4=
226-
github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI=
227-
github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
226+
github.com/expr-lang/expr v1.17.5 h1:i1WrMvcdLF249nSNlpQZN1S6NXuW9WaOfF5tPi3aw3k=
227+
github.com/expr-lang/expr v1.17.5/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
228228
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
229229
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
230230
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
@@ -359,8 +359,8 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
359359
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
360360
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
361361
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
362-
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
363-
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
362+
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
363+
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
364364
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
365365
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
366366
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=

0 commit comments

Comments
 (0)