Skip to content

Commit dbf64fa

Browse files
david-becherpull[bot]
authored andcommitted
fix: Use CredsStore for GoogleCloudCreds (argoproj#12391)
git-ask-pass.sh is no longer supported for credentials Signed-off-by: David Becher <[email protected]>
1 parent e6f189d commit dbf64fa

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

pkg/apis/application/v1alpha1/repository_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (repo *Repository) GetGitCreds(store git.CredsStore) git.Creds {
202202
return git.NewGitHubAppCreds(repo.GithubAppId, repo.GithubAppInstallationId, repo.GithubAppPrivateKey, repo.GitHubAppEnterpriseBaseURL, repo.Repo, repo.TLSClientCertData, repo.TLSClientCertKey, repo.IsInsecure(), repo.Proxy, store)
203203
}
204204
if repo.GCPServiceAccountKey != "" {
205-
return git.NewGoogleCloudCreds(repo.GCPServiceAccountKey)
205+
return git.NewGoogleCloudCreds(repo.GCPServiceAccountKey, store)
206206
}
207207
return git.NopCreds{}
208208
}

util/git/creds.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,16 @@ func (g GitHubAppCreds) GetClientCertKey() string {
456456
// GoogleCloudCreds to authenticate to Google Cloud Source repositories
457457
type GoogleCloudCreds struct {
458458
creds *google.Credentials
459+
store CredsStore
459460
}
460461

461-
func NewGoogleCloudCreds(jsonData string) GoogleCloudCreds {
462+
func NewGoogleCloudCreds(jsonData string, store CredsStore) GoogleCloudCreds {
462463
creds, err := google.CredentialsFromJSON(context.Background(), []byte(jsonData), "https://www.googleapis.com/auth/cloud-platform")
463464
if err != nil {
464465
// Invalid JSON
465466
log.Errorf("Failed reading credentials from JSON: %+v", err)
466467
}
467-
return GoogleCloudCreds{creds}
468+
return GoogleCloudCreds{creds, store}
468469
}
469470

470471
func (c GoogleCloudCreds) Environ() (io.Closer, []string, error) {
@@ -477,9 +478,13 @@ func (c GoogleCloudCreds) Environ() (io.Closer, []string, error) {
477478
return NopCloser{}, nil, fmt.Errorf("failed to get access token from creds: %w", err)
478479
}
479480

480-
env := []string{fmt.Sprintf("GIT_ASKPASS=%s", "git-ask-pass.sh"), fmt.Sprintf("GIT_USERNAME=%s", username), fmt.Sprintf("GIT_PASSWORD=%s", token)}
481+
nonce := c.store.Add(username, token)
482+
env := getGitAskPassEnv(nonce)
481483

482-
return NopCloser{}, env, nil
484+
return argoioutils.NewCloser(func() error {
485+
c.store.Remove(nonce)
486+
return NopCloser{}.Close()
487+
}), env, nil
483488
}
484489

485490
func (c GoogleCloudCreds) getUsername() (string, error) {

util/git/creds_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"github.com/google/uuid"
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
15+
"golang.org/x/oauth2"
16+
"golang.org/x/oauth2/google"
1517

1618
"github.com/argoproj/argo-cd/v2/util/cert"
1719
"github.com/argoproj/argo-cd/v2/util/io"
18-
"golang.org/x/oauth2"
19-
"golang.org/x/oauth2/google"
2020
)
2121

2222
type cred struct {
@@ -251,12 +251,14 @@ const invalidJSON = `{
251251
`
252252

253253
func TestNewGoogleCloudCreds(t *testing.T) {
254-
googleCloudCreds := NewGoogleCloudCreds(gcpServiceAccountKeyJSON)
254+
store := &memoryCredsStore{creds: make(map[string]cred)}
255+
googleCloudCreds := NewGoogleCloudCreds(gcpServiceAccountKeyJSON, store)
255256
assert.NotNil(t, googleCloudCreds)
256257
}
257258

258259
func TestNewGoogleCloudCreds_invalidJSON(t *testing.T) {
259-
googleCloudCreds := NewGoogleCloudCreds(invalidJSON)
260+
store := &memoryCredsStore{creds: make(map[string]cred)}
261+
googleCloudCreds := NewGoogleCloudCreds(invalidJSON, store)
260262
assert.Nil(t, googleCloudCreds.creds)
261263

262264
token, err := googleCloudCreds.getAccessToken()
@@ -273,17 +275,25 @@ func TestNewGoogleCloudCreds_invalidJSON(t *testing.T) {
273275
assert.NotNil(t, err)
274276
}
275277

276-
func TestGoogleCloudCreds_Environ(t *testing.T) {
278+
func TestGoogleCloudCreds_Environ_cleanup(t *testing.T) {
279+
store := &memoryCredsStore{creds: make(map[string]cred)}
277280
staticToken := &oauth2.Token{AccessToken: "token"}
278281
googleCloudCreds := GoogleCloudCreds{&google.Credentials{
279282
ProjectID: "my-google-project",
280283
TokenSource: oauth2.StaticTokenSource(staticToken),
281284
JSON: []byte(gcpServiceAccountKeyJSON),
282-
}}
285+
}, store}
283286

284287
closer, env, err := googleCloudCreds.Environ()
285288
assert.NoError(t, err)
286-
defer func() { _ = closer.Close() }()
287-
288-
assert.Equal(t, []string{"GIT_ASKPASS=git-ask-pass.sh", "GIT_USERNAME=argocd-service-account@my-google-project.iam.gserviceaccount.com", "GIT_PASSWORD=token"}, env)
289+
var nonce string
290+
for _, envVar := range env {
291+
if strings.HasPrefix(envVar, ASKPASS_NONCE_ENV) {
292+
nonce = envVar[len(ASKPASS_NONCE_ENV)+1:]
293+
break
294+
}
295+
}
296+
assert.Contains(t, store.creds, nonce)
297+
io.Close(closer)
298+
assert.NotContains(t, store.creds, nonce)
289299
}

0 commit comments

Comments
 (0)