Skip to content

Commit 01c14ef

Browse files
akuitybotkrancour
andauthored
chore(backport release-1.9): fix(controller): fix bug that wrongly judges workload identity to be available (#5653)
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com> Co-authored-by: Kent Rancourt <kent.rancourt@gmail.com>
1 parent c7cb7ef commit 01c14ef

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

pkg/credentials/acr/workload_identity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func NewWorkloadIdentityProvider(ctx context.Context) credentials.Provider {
6060
logger := logging.LoggerFromContext(ctx)
6161

6262
// Try to create a DefaultAzureCredential which supports workload identity
63-
credential, err := azidentity.NewDefaultAzureCredential(nil)
63+
credential, err := azidentity.NewWorkloadIdentityCredential(nil)
6464
if err != nil {
6565
logger.Info("Azure workload identity not available", "error", err.Error())
6666
return nil

pkg/credentials/acr/workload_identity_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package acr
33
import (
44
"context"
55
"errors"
6+
"os"
67
"testing"
78
"time"
89

@@ -15,6 +16,31 @@ import (
1516
"github.com/akuity/kargo/pkg/credentials"
1617
)
1718

19+
func TestNewWorkloadIdentityProvider(t *testing.T) {
20+
const azFederatedTokenFile = "AZURE_FEDERATED_TOKEN_FILE"
21+
const azClientID = "AZURE_CLIENT_ID"
22+
const azTenantID = "AZURE_TENANT_ID"
23+
t.Run("workload identity not available", func(t *testing.T) {
24+
// Make it look unavailable by ensuring key env vars are unset
25+
t.Setenv(azFederatedTokenFile, "") // Ensures cleanup
26+
os.Unsetenv(azFederatedTokenFile) // Actually unsets
27+
t.Setenv(azClientID, "") // Ensures cleanup
28+
os.Unsetenv(azClientID) // Actually unsets
29+
t.Setenv(azTenantID, "") // Ensures cleanup
30+
os.Unsetenv(azTenantID) // Actually unsets
31+
require.Nil(t, NewWorkloadIdentityProvider(t.Context()))
32+
})
33+
t.Run("workload identity available", func(t *testing.T) {
34+
// Make it look available by ensuring key env vars are set, albeit with
35+
// nonsense values.
36+
const nonsense = "nonsense"
37+
t.Setenv(azFederatedTokenFile, nonsense)
38+
t.Setenv(azClientID, nonsense)
39+
t.Setenv(azTenantID, nonsense)
40+
require.NotNil(t, NewWorkloadIdentityProvider(t.Context()))
41+
})
42+
}
43+
1844
func TestWorkloadIdentityProvider_Supports(t *testing.T) {
1945
const testOCIRepoURL = "myregistry.azurecr.io/my-repo"
2046
const testHTTPSRepoURL = "https://myregistry.azurecr.io/my-repo"

0 commit comments

Comments
 (0)