Skip to content

fix(cli): preserve password-only secret in flux create source git#5861

Open
SAY-5 wants to merge 1 commit intofluxcd:mainfrom
SAY-5:fix/create-source-git-password-only-3892
Open

fix(cli): preserve password-only secret in flux create source git#5861
SAY-5 wants to merge 1 commit intofluxcd:mainfrom
SAY-5:fix/create-source-git-password-only-3892

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 22, 2026

Closes #3892.

Problem

sourcesecret.buildGitSecret wrote the username / password secret keys only when both fields were non-empty:

```go
if options.Username != "" && options.Password != "" {
secret.StringData[UsernameSecretKey] = options.Username
secret.StringData[PasswordSecretKey] = options.Password
}
```

With an Azure DevOps PAT, the token is the credential and there is no username. Running

```
flux create source git podinfo --password=${PAT} --url=${repo} …
```

therefore produced a GitRepository backed by a secret with an empty StringData, and the reconciler failed to authenticate. Manually editing the secret to add the password key made it work again — confirming the value was just being dropped on create.

Fix

Write the two fields independently, so:

flags result
--username + --password username + password keys (unchanged)
--password alone password key only (this PR)
--username alone username key only

The SSH-passphrase case also becomes simpler: the duplicated if options.Password != \"\" inside the keypair branch collapses into the top-level write, since Password is written exactly once regardless of whether a keypair is also present.

Tests

Added Test_buildGitSecret_BasicAuthFields to sourcesecret_test.go covering all four credential shapes (both, password-only, username-only, none). Existing tests still pass:

```
$ go test ./pkg/manifestgen/sourcesecret/...
ok github.com/fluxcd/flux2/v2/pkg/manifestgen/sourcesecret 1.463s
```

Signed-off-by: SAY-5 SAY-5@users.noreply.github.com

`sourcesecret.buildGitSecret` previously wrote the Username and Password
secret fields only when *both* were set. With the Azure DevOps PAT flow,
`flux create source git --password=<pat>` has no username (the token is
the credential), so both fields were silently dropped and the resulting
secret was empty, breaking authentication.

Write the two fields independently, so:
- `--username` + `--password` -> username + password keys (unchanged);
- `--password` alone       -> password key only (fixes fluxcd#3892);
- `--username` alone       -> username key only.

The SSH-passphrase case also becomes simpler: the duplicated
`if options.Password != ""` inside the keypair branch collapses into the
top-level write.

Adds a `buildGitSecret` unit test covering all four credential shapes,
including the Azure DevOps PAT scenario.

Closes fluxcd#3892.

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flux create source git with --password fails to create secret

1 participant