Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion azure/credential_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ func (c *credentialCache) GetOrStoreClientCert(tenantID, clientID string, cert,
}

func (c *credentialCache) GetOrStoreManagedIdentity(opts *azidentity.ManagedIdentityCredentialOptions) (azcore.TokenCredential, error) {
var clientID string
if opts.ID != nil {
clientID = opts.ID.String()
}
return c.getOrStore(
credentialCacheKey{
authorityHost: opts.Cloud.ActiveDirectoryAuthorityHost,
credentialType: CredentialTypeManagedIdentity,
// tenantID not used for managed identity
clientID: opts.ID.String(),
clientID: clientID,
},
func() (azcore.TokenCredential, error) {
return c.credFactory.newManagedIdentityCredential(opts)
Expand Down
4 changes: 3 additions & 1 deletion azure/scope/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ func (p *AzureCredentialsProvider) GetTokenCredential(ctx context.Context, resou
ClientOptions: azcore.ClientOptions{
TracingProvider: tracingProvider,
},
ID: azidentity.ClientID(p.Identity.Spec.ClientID),
}
if p.Identity.Spec.ClientID != "" {
options.ID = azidentity.ClientID(p.Identity.Spec.ClientID)
}
cred, authErr = p.cache.GetOrStoreManagedIdentity(&options)

Expand Down
22 changes: 22 additions & 0 deletions azure/scope/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,28 @@ func TestGetTokenCredential(t *testing.T) {
}))
},
},
{
name: "system-assigned identity",
cluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
IdentityRef: &corev1.ObjectReference{
Kind: infrav1.AzureClusterIdentityKind,
},
},
},
},
identity: &infrav1.AzureClusterIdentity{
Spec: infrav1.AzureClusterIdentitySpec{
Type: infrav1.UserAssignedMSI,
},
},
cacheExpect: func(cache *mock_azure.MockCredentialCache) {
cache.EXPECT().GetOrStoreManagedIdentity(gomock.Cond(func(opts *azidentity.ManagedIdentityCredentialOptions) bool {
return opts.ID == nil
}))
},
},
{
name: "UserAssignedIdentityCredential",
cluster: &infrav1.AzureCluster{
Expand Down