Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvz committed Apr 23, 2023
1 parent 51b5ac7 commit a312af9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/provider/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ func ParseConfig(configReader io.Reader) (*Config, error) {
config.AADClientID = clientID
}
config.AADFederatedTokenFile = os.Getenv("AZURE_FEDERATED_TOKEN_FILE")
config.UseFederatedWorkloadIdentityExtension = config.AADFederatedTokenFile != ""
return &config, nil
}

Expand Down
32 changes: 18 additions & 14 deletions pkg/provider/config/azure_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type AzureAuthConfig struct {
// The AAD federated token file
AADFederatedTokenFile string `json:"aadFederatedTokenFile,omitempty" yaml:"aadFederatedTokenFile,omitempty"`
// Use workload identity federation for the virtual machine to access Azure ARM APIs
UseWorkloadIdentityExtension bool `json:"useWorkloadIdentityExtension,omitempty" yaml:"useWorkloadIdentityExtension,omitempty"`
UseFederatedWorkloadIdentityExtension bool `json:"useFederatedWorkloadIdentityExtension,omitempty" yaml:"useFederatedWorkloadIdentityExtension,omitempty"`
}

// GetServicePrincipalToken creates a new service principal token based on the configuration.
Expand All @@ -104,6 +104,23 @@ func GetServicePrincipalToken(config *AzureAuthConfig, env *azure.Environment, r
resource = env.ServiceManagementEndpoint
}

if config.UseFederatedWorkloadIdentityExtension {
klog.V(2).Infoln("azure: using workload identity extension to retrieve access token")
oauthConfig, err := adal.NewOAuthConfigWithAPIVersion(env.ActiveDirectoryEndpoint, tenantID, nil)
if err != nil {
return nil, fmt.Errorf("failed to create the OAuth config: %w", err)
}
jwt, err := os.ReadFile(config.AADFederatedTokenFile)
if err != nil {
return nil, fmt.Errorf("failed to read a file with a federated token: %w", err)
}
token, err := adal.NewServicePrincipalTokenFromFederatedToken(*oauthConfig, config.AADClientID, string(jwt), env.ResourceManagerEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to create a workload identity token: %w", err)
}
return token, nil
}

if config.UseManagedIdentityExtension {
klog.V(2).Infoln("azure: using managed identity extension to retrieve access token")
msiEndpoint, err := adal.GetMSIVMEndpoint()
Expand Down Expand Up @@ -165,19 +182,6 @@ func GetServicePrincipalToken(config *AzureAuthConfig, env *azure.Environment, r
resource)
}

if config.UseWorkloadIdentityExtension {
klog.V(2).Infoln("azure: using workload identity extension to retrieve access token")
jwt, err := os.ReadFile(config.AADFederatedTokenFile)
if err != nil {
return nil, fmt.Errorf("failed to read a file with a federated token: %w", err)
}
token, err := adal.NewServicePrincipalTokenFromFederatedToken(*oauthConfig, config.AADClientID, string(jwt), env.ResourceManagerEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to create a workload identity token: %w", err)
}
return token, nil
}

return nil, ErrorNoAuth
}

Expand Down

0 comments on commit a312af9

Please sign in to comment.