Skip to content

Commit

Permalink
Make username optional for create secret git
Browse files Browse the repository at this point in the history
Git authentication with basic auth doesn't necessarily require a
username to be present.

Signed-off-by: talife <[email protected]>
Signed-off-by: Max Jonas Werner <[email protected]>
  • Loading branch information
talife authored and Max Jonas Werner committed Aug 23, 2023
1 parent 525bd21 commit a6cb593
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd/flux/create_secret_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ func createSecretGitCmdRun(cmd *cobra.Command, args []string) error {
opts.ECDSACurve = secretGitArgs.ecdsaCurve.Curve
opts.Password = secretGitArgs.password
case "http", "https":
if (secretGitArgs.username == "" || secretGitArgs.password == "") && secretGitArgs.bearerToken == "" {
if secretGitArgs.password == "" && secretGitArgs.bearerToken == "" {
return fmt.Errorf("for Git over HTTP/S the username and password, or a bearer token is required")
}
opts.Username = secretGitArgs.username
opts.Password = secretGitArgs.password
opts.BearerToken = secretGitArgs.bearerToken
if secretGitArgs.username != "" && secretGitArgs.password != "" && secretGitArgs.bearerToken != "" {
if secretGitArgs.password != "" && secretGitArgs.bearerToken != "" {
return fmt.Errorf("user credentials and bearer token cannot be used together")
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/flux/create_secret_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func TestCreateGitSecret(t *testing.T) {
args: "create secret git podinfo-auth --url=https://github.com/stefanprodan/podinfo --username=aaa --password=zzzz --bearer-token=aaaa --namespace=my-namespace --export",
assert: assertError("user credentials and bearer token cannot be used together"),
},
{
name: "git authentication with basic auth consisting of only one password without a username",
args: "create secret git podinfo-auth --url=https://github.com/stefanprodan/podinfo --password=my-password --namespace=my-namespace --export",
assert: assertGoldenFile("./testdata/create_secret/git/secret-git-only-pwd.yaml"),
},
}

for _, tt := range tests {
Expand Down
9 changes: 9 additions & 0 deletions cmd/flux/testdata/create_secret/git/secret-git-only-pwd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: v1
kind: Secret
metadata:
name: podinfo-auth
namespace: my-namespace
stringData:
password: my-password

6 changes: 4 additions & 2 deletions pkg/manifestgen/sourcesecret/sourcesecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ func buildSecret(keypair *ssh.KeyPair, hostKey, dockerCfg []byte, options Option
return
}

if options.Username != "" && options.Password != "" {
secret.StringData[UsernameSecretKey] = options.Username
if options.Password != "" {
if options.Username != "" {
secret.StringData[UsernameSecretKey] = options.Username
}
secret.StringData[PasswordSecretKey] = options.Password
}
if options.BearerToken != "" {
Expand Down

0 comments on commit a6cb593

Please sign in to comment.