|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package azure |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 26 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 27 | +) |
| 28 | + |
| 29 | +// newDefaultAzureCredential is like azidentity.NewDefaultAzureCredential(), |
| 30 | +// but does not call the functions that shell out to Azure CLIs. |
| 31 | +func newDefaultAzureCredential(options azidentity.DefaultAzureCredentialOptions) (azcore.TokenCredential, error) { |
| 32 | + const ( |
| 33 | + azureClientID = "AZURE_CLIENT_ID" |
| 34 | + azureFederatedTokenFile = "AZURE_FEDERATED_TOKEN_FILE" |
| 35 | + azureAuthorityHost = "AZURE_AUTHORITY_HOST" |
| 36 | + azureTenantID = "AZURE_TENANT_ID" |
| 37 | + ) |
| 38 | + |
| 39 | + var errorMessages []string |
| 40 | + |
| 41 | + envCred, err := azidentity.NewEnvironmentCredential(&azidentity.EnvironmentCredentialOptions{ |
| 42 | + ClientOptions: options.ClientOptions, DisableInstanceDiscovery: options.DisableInstanceDiscovery}, |
| 43 | + ) |
| 44 | + if err == nil { |
| 45 | + return envCred, nil |
| 46 | + } else { |
| 47 | + errorMessages = append(errorMessages, "EnvironmentCredential: "+err.Error()) |
| 48 | + } |
| 49 | + |
| 50 | + // workload identity requires values for AZURE_AUTHORITY_HOST, AZURE_CLIENT_ID, AZURE_FEDERATED_TOKEN_FILE, AZURE_TENANT_ID |
| 51 | + haveWorkloadConfig := false |
| 52 | + clientID, haveClientID := os.LookupEnv(azureClientID) |
| 53 | + if haveClientID { |
| 54 | + if file, ok := os.LookupEnv(azureFederatedTokenFile); ok { |
| 55 | + if _, ok := os.LookupEnv(azureAuthorityHost); ok { |
| 56 | + if tenantID, ok := os.LookupEnv(azureTenantID); ok { |
| 57 | + haveWorkloadConfig = true |
| 58 | + workloadCred, err := azidentity.NewWorkloadIdentityCredential(&azidentity.WorkloadIdentityCredentialOptions{ |
| 59 | + ClientID: clientID, |
| 60 | + TenantID: tenantID, |
| 61 | + TokenFilePath: file, |
| 62 | + ClientOptions: options.ClientOptions, |
| 63 | + DisableInstanceDiscovery: options.DisableInstanceDiscovery, |
| 64 | + }) |
| 65 | + if err == nil { |
| 66 | + return workloadCred, nil |
| 67 | + } else { |
| 68 | + errorMessages = append(errorMessages, "Workload Identity"+": "+err.Error()) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + if !haveWorkloadConfig { |
| 75 | + err := errors.New("missing environment variables for workload identity. Check webhook and pod configuration") |
| 76 | + errorMessages = append(errorMessages, fmt.Sprintf("Workload Identity: %s", err)) |
| 77 | + } |
| 78 | + |
| 79 | + o := &azidentity.ManagedIdentityCredentialOptions{ClientOptions: options.ClientOptions} |
| 80 | + if haveClientID { |
| 81 | + o.ID = azidentity.ClientID(clientID) |
| 82 | + } |
| 83 | + miCred, err := azidentity.NewManagedIdentityCredential(o) |
| 84 | + if err == nil { |
| 85 | + return miCred, nil |
| 86 | + } else { |
| 87 | + errorMessages = append(errorMessages, "ManagedIdentity"+": "+err.Error()) |
| 88 | + } |
| 89 | + |
| 90 | + return nil, errors.New(strings.Join(errorMessages, "\n")) |
| 91 | +} |
0 commit comments