Skip to content

Commit 5036e73

Browse files
committed
feat: add support for separate GitHub app credentials stored as Kubernetes secrets
Signed-off-by: Dustin Lactin <[email protected]>
1 parent 0252dae commit 5036e73

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

docs/basics/update-methods.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ Example:
123123
argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd-image-updater/git-creds
124124
```
125125

126-
If the repository is accessed using HTTPS, the secret must contain two fields:
126+
If the repository is accessed using HTTPS, the secret must contain either user credentials or GitHub app credentials.
127+
128+
If the repository is accessed using user credentials, the secret requires two fields
127129
`username` which holds the Git username, and `password` which holds the user's
128130
password or a private access token (PAT) with write access to the repository.
129131
You can generate such a secret using `kubectl`, e.g.:
@@ -134,6 +136,16 @@ kubectl -n argocd-image-updater create secret generic git-creds \
134136
--from-literal=password=somepassword
135137
```
136138

139+
If the repository is accessed using GitHub app credentials, the secret requires three fields `githubAppID` which holds the GitHub Application ID, `githubAppInstallationID` which holds the GitHub Organization Installation ID, and `githubAppPrivateKey` which holds the GitHub Application private key. The GitHub Application must be installed into the target repository with write access.
140+
You can generate such a secret using `kubectl`, e.g.:
141+
142+
```bash
143+
kubectl -n argocd-image-updater create secret generic git-creds \
144+
--from-literal=githubAppID=applicationid \
145+
--from-literal=githubAppInstallationID=installationid \
146+
--from-literal=githubAppPrivateKey='-----BEGIN RSA PRIVATE KEY-----PRIVATEKEYDATA-----END RSA PRIVATE KEY-----'
147+
```
148+
137149
If the repository is accessed using SSH, the secret must contain the field
138150
`sshPrivateKey`, which holds a SSH private key in OpenSSH-compatible PEM
139151
format. To create such a secret from an existing private key, you can use

pkg/argocd/gitcreds.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocd
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
"strings"
78

89
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
@@ -66,14 +67,25 @@ func getCredsFromSecret(wbc *WriteBackConfig, credentialsSecret string, kubeClie
6667
}
6768
return git.NewSSHCreds(string(sshPrivateKey), "", true), nil
6869
} else if git.IsHTTPSURL(wbc.GitRepo) {
69-
var username, password []byte
70-
if username, ok = credentials["username"]; !ok {
71-
return nil, fmt.Errorf("invalid secret %s: does not contain field username", credentialsSecret)
70+
var username, password, githubAppID, githubAppInstallationID, githubAppPrivateKey []byte
71+
if githubAppID, ok = credentials["githubAppID"]; ok {
72+
if githubAppInstallationID, ok = credentials["githubAppInstallationID"]; !ok {
73+
return nil, fmt.Errorf("invalid secret %s: does not contain field githubAppInstallationID", credentialsSecret)
74+
}
75+
if githubAppPrivateKey, ok = credentials["githubAppPrivateKey"]; !ok {
76+
return nil, fmt.Errorf("invalid secret %s: does not contain field githubAppPrivateKey", credentialsSecret)
77+
}
78+
// converting byte array to string and ultimately int64 for NewGitHubAppCreds
79+
intGithubAppID, _ := strconv.ParseInt(string(githubAppID), 10, 64)
80+
intGithubAppInstallationID, _ := strconv.ParseInt(string(githubAppInstallationID), 10, 64)
81+
return git.NewGitHubAppCreds(intGithubAppID, intGithubAppInstallationID, string(githubAppPrivateKey), "", "", "", "", true), nil
82+
} else if username, ok = credentials["username"]; ok {
83+
if password, ok = credentials["password"]; !ok {
84+
return nil, fmt.Errorf("invalid secret %s: does not contain field password", credentialsSecret)
85+
}
86+
return git.NewHTTPSCreds(string(username), string(password), "", "", true, ""), nil
7287
}
73-
if password, ok = credentials["password"]; !ok {
74-
return nil, fmt.Errorf("invalid secret %s: does not contain field password", credentialsSecret)
75-
}
76-
return git.NewHTTPSCreds(string(username), string(password), "", "", true, ""), nil
88+
return nil, fmt.Errorf("invalid repository credentials in secret %s: does not contain githubAppID or username", credentialsSecret)
7789
}
7890
return nil, fmt.Errorf("unknown repository type")
7991
}

0 commit comments

Comments
 (0)