Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(azure): sovereign cloud support #3942

Merged
Merged
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
7 changes: 4 additions & 3 deletions provider/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ func NewAzureProvider(configFile string, domainFilter endpoint.DomainFilter, zon
if err != nil {
return nil, fmt.Errorf("failed to read Azure config file '%s': %v", configFile, err)
}
cred, err := getCredentials(*cfg)
cred, clientOpts, err := getCredentials(*cfg)
if err != nil {
return nil, fmt.Errorf("failed to get credentials: %w", err)
}
zonesClient, err := dns.NewZonesClient(cfg.SubscriptionID, cred, nil)

zonesClient, err := dns.NewZonesClient(cfg.SubscriptionID, cred, clientOpts)
if err != nil {
return nil, err
}
recordSetsClient, err := dns.NewRecordSetsClient(cfg.SubscriptionID, cred, nil)
recordSetsClient, err := dns.NewRecordSetsClient(cfg.SubscriptionID, cred, clientOpts)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions provider/azure/azure_private_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ func NewAzurePrivateDNSProvider(configFile string, domainFilter endpoint.DomainF
if err != nil {
return nil, fmt.Errorf("failed to read Azure config file '%s': %v", configFile, err)
}
cred, err := getCredentials(*cfg)
cred, clientOpts, err := getCredentials(*cfg)
if err != nil {
return nil, fmt.Errorf("failed to get credentials: %w", err)
}
zonesClient, err := privatedns.NewPrivateZonesClient(cfg.SubscriptionID, cred, nil)

zonesClient, err := privatedns.NewPrivateZonesClient(cfg.SubscriptionID, cred, clientOpts)
if err != nil {
return nil, err
}
recordSetsClient, err := privatedns.NewRecordSetsClient(cfg.SubscriptionID, cred, nil)
recordSetsClient, err := privatedns.NewRecordSetsClient(cfg.SubscriptionID, cred, clientOpts)
if err != nil {
return nil, err
}
Expand Down
37 changes: 19 additions & 18 deletions provider/azure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -65,10 +66,16 @@ func getConfig(configFile, resourceGroup, userAssignedIdentityClientID string) (
}

// getAccessToken retrieves Azure API access token.
func getCredentials(cfg config) (azcore.TokenCredential, error) {
func getCredentials(cfg config) (azcore.TokenCredential, *arm.ClientOptions, error) {
cloudCfg, err := getCloudConfiguration(cfg.Cloud)
if err != nil {
return nil, fmt.Errorf("failed to get cloud configuration: %w", err)
return nil, nil, fmt.Errorf("failed to get cloud configuration: %w", err)
}
clientOpts := azcore.ClientOptions{
Cloud: cloudCfg,
}
armClientOpts := &arm.ClientOptions{
ClientOptions: clientOpts,
}

// Try to retrieve token with service principal credentials.
Expand All @@ -83,25 +90,21 @@ func getCredentials(cfg config) (azcore.TokenCredential, error) {
!strings.EqualFold(cfg.ClientSecret, "msi") {
log.Info("Using client_id+client_secret to retrieve access token for Azure API.")
opts := &azidentity.ClientSecretCredentialOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloudCfg,
},
ClientOptions: clientOpts,
}
cred, err := azidentity.NewClientSecretCredential(cfg.TenantID, cfg.ClientID, cfg.ClientSecret, opts)
if err != nil {
return nil, fmt.Errorf("failed to create service principal token: %w", err)
return nil, nil, fmt.Errorf("failed to create service principal token: %w", err)
}
return cred, nil
return cred, armClientOpts, nil
}

// Try to retrieve token with Workload Identity.
if cfg.UseWorkloadIdentityExtension {
log.Info("Using workload identity extension to retrieve access token for Azure API.")

wiOpt := azidentity.WorkloadIdentityCredentialOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloudCfg,
},
ClientOptions: clientOpts,
// In a standard scenario, Client ID and Tenant ID are expected to be read from environment variables.
// Though, in certain cases, it might be important to have an option to override those (e.g. when AZURE_TENANT_ID is not set
// through a webhook or azure.workload.identity/client-id service account annotation is absent). When any of those values are
Expand All @@ -112,31 +115,29 @@ func getCredentials(cfg config) (azcore.TokenCredential, error) {

cred, err := azidentity.NewWorkloadIdentityCredential(&wiOpt)
if err != nil {
return nil, fmt.Errorf("failed to create a workload identity token: %w", err)
return nil, nil, fmt.Errorf("failed to create a workload identity token: %w", err)
}

return cred, nil
return cred, armClientOpts, nil
}

// Try to retrieve token with MSI.
if cfg.UseManagedIdentityExtension {
log.Info("Using managed identity extension to retrieve access token for Azure API.")
msiOpt := azidentity.ManagedIdentityCredentialOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloudCfg,
},
ClientOptions: clientOpts,
}
if cfg.UserAssignedIdentityID != "" {
msiOpt.ID = azidentity.ClientID(cfg.UserAssignedIdentityID)
}
cred, err := azidentity.NewManagedIdentityCredential(&msiOpt)
if err != nil {
return nil, fmt.Errorf("failed to create the managed service identity token: %w", err)
return nil, nil, fmt.Errorf("failed to create the managed service identity token: %w", err)
}
return cred, nil
return cred, armClientOpts, nil
}

return nil, fmt.Errorf("no credentials provided for Azure API")
return nil, nil, fmt.Errorf("no credentials provided for Azure API")
}

func getCloudConfiguration(name string) (cloud.Configuration, error) {
Expand Down
Loading