-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow reuse of Argo CD repo credentials (#141)
Signed-off-by: Alexander Matyushentsev <[email protected]>
- Loading branch information
Alexander Matyushentsev
authored
Jan 22, 2021
1 parent
c0d7e1d
commit 74e9d3f
Showing
12 changed files
with
284 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ mypass | |
otherapp | ||
wildcard | ||
wildcards | ||
credref | ||
repocreds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ rules: | |
- '' | ||
resources: | ||
- secrets | ||
- configmaps | ||
verbs: | ||
- get | ||
- list | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ rules: | |
- "" | ||
resources: | ||
- secrets | ||
- configmaps | ||
verbs: | ||
- get | ||
- list | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package argocd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" | ||
"github.com/argoproj/argo-cd/util/db" | ||
"github.com/argoproj/argo-cd/util/settings" | ||
|
||
"github.com/argoproj-labs/argocd-image-updater/ext/git" | ||
"github.com/argoproj-labs/argocd-image-updater/pkg/kube" | ||
) | ||
|
||
// getGitCredsSource returns git credentials source that loads credentials from the secret or from Argo CD settings | ||
func getGitCredsSource(creds string, kubeClient *kube.KubernetesClient) (GitCredsSource, error) { | ||
switch { | ||
case creds == "repocreds": | ||
return func(app *v1alpha1.Application) (git.Creds, error) { | ||
return getCredsFromArgoCD(app, kubeClient) | ||
}, nil | ||
case strings.HasPrefix(creds, "secret:"): | ||
return func(app *v1alpha1.Application) (git.Creds, error) { | ||
return getCredsFromSecret(app, creds[len("secret:"):], kubeClient) | ||
}, nil | ||
} | ||
return nil, fmt.Errorf("unexpected credentials format. Expected 'repocreds' or 'secret:<namespace>/<secret>' but got '%s'", creds) | ||
} | ||
|
||
// getCredsFromArgoCD loads repository credentials from Argo CD settings | ||
func getCredsFromArgoCD(app *v1alpha1.Application, kubeClient *kube.KubernetesClient) (git.Creds, error) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
settingsMgr := settings.NewSettingsManager(ctx, kubeClient.Clientset, kubeClient.Namespace) | ||
argocdDB := db.NewDB(kubeClient.Namespace, settingsMgr, kubeClient.Clientset) | ||
repo, err := argocdDB.GetRepository(ctx, app.Spec.Source.RepoURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !repo.HasCredentials() { | ||
return nil, fmt.Errorf("credentials for '%s' are not configured in Argo CD settings", app.Spec.Source.RepoURL) | ||
} | ||
return repo.GetGitCreds(), nil | ||
} | ||
|
||
// getCredsFromSecret loads repository credentials from secret | ||
func getCredsFromSecret(app *v1alpha1.Application, credentialsSecret string, kubeClient *kube.KubernetesClient) (git.Creds, error) { | ||
var credentials map[string][]byte | ||
var err error | ||
s := strings.SplitN(credentialsSecret, "/", 2) | ||
if len(s) == 2 { | ||
credentials, err = kubeClient.GetSecretData(s[0], s[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
return nil, fmt.Errorf("secret ref must be in format 'namespace/name', but is '%s'", credentialsSecret) | ||
} | ||
|
||
if ok, _ := git.IsSSHURL(app.Spec.Source.RepoURL); ok { | ||
var sshPrivateKey []byte | ||
if sshPrivateKey, ok = credentials["sshPrivateKey"]; !ok { | ||
return nil, fmt.Errorf("invalid secret %s: does not contain field sshPrivateKey", credentialsSecret) | ||
} | ||
return git.NewSSHCreds(string(sshPrivateKey), "", true), nil | ||
} else if git.IsHTTPSURL(app.Spec.Source.RepoURL) { | ||
var username, password []byte | ||
if username, ok = credentials["username"]; !ok { | ||
return nil, fmt.Errorf("invalid secret %s: does not contain field username", credentialsSecret) | ||
} | ||
if password, ok = credentials["password"]; !ok { | ||
return nil, fmt.Errorf("invalid secret %s: does not contain field password", credentialsSecret) | ||
} | ||
return git.NewHTTPSCreds(string(username), string(password), "", "", true), nil | ||
} | ||
return nil, fmt.Errorf("unknown repository type") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.