@@ -26,11 +26,28 @@ import (
26
26
"k8s.io/klog/v2"
27
27
)
28
28
29
+ // AzureCloudEnv defines the different Azure cloud environments
30
+ type AzureCloudEnv string
31
+
32
+ const (
33
+ AzurePublicCloud AzureCloudEnv = "AzurePublicCloud"
34
+ AzureUSGovernmentCloud AzureCloudEnv = "AzureUSGovernment"
35
+ // Add other environments if needed, e.g., AzureChinaCloud, AzureGermanyCloud
36
+ )
37
+
38
+ // keyVaultScopes maps AzureCloudEnv to its corresponding Key Vault default scope
39
+ var keyVaultScopes = map [AzureCloudEnv ]string {
40
+ AzurePublicCloud : "https://vault.azure.net/.default" ,
41
+ AzureUSGovernmentCloud : "https://vault.usgovcloudapi.net/.default" ,
42
+ // Add other environments as they are needed
43
+ }
44
+
29
45
type AuthService struct {
30
- kubeclient kubernetes.Interface
31
- credentials azure.LegacyTokenCredential
32
- caCert []byte
33
- caKey []byte
46
+ kubeclient kubernetes.Interface
47
+ credentials azure.LegacyTokenCredential
48
+ caCert []byte
49
+ caKey []byte
50
+ keyVaultScope string // Added field for configurable Key Vault scope
34
51
}
35
52
36
53
func fileExists (filename string ) bool {
@@ -87,11 +104,26 @@ func NewAuthService(kubeclient kubernetes.Interface, credentials azure.LegacyTok
87
104
return nil , fmt .Errorf ("file %s is empty" , caKeyFile )
88
105
}
89
106
107
+ // Determine the Azure environment from environment variable
108
+ azureEnvStr := os .Getenv ("AZURE_ENVIRONMENT" )
109
+ if azureEnvStr == "" {
110
+ azureEnvStr = string (AzurePublicCloud ) // Default to Azure Public Cloud if not set
111
+ }
112
+
113
+ azureEnv := AzureCloudEnv (azureEnvStr )
114
+ keyVaultScope , found := keyVaultScopes [azureEnv ]
115
+ if ! found {
116
+ klog .InfoS ("unsupported AZURE_ENVIRONMENT specified, defaulting to Azure Public Cloud Key Vault scope" , "environment" , azureEnvStr )
117
+ keyVaultScope = keyVaultScopes [AzurePublicCloud ]
118
+ }
119
+ klog .InfoS ("using Azure Key Vault scope" , "environment" , azureEnvStr , "scope" , keyVaultScope )
120
+
90
121
return & AuthService {
91
- kubeclient : kubeclient ,
92
- credentials : credentials ,
93
- caCert : caCert ,
94
- caKey : caKey ,
122
+ kubeclient : kubeclient ,
123
+ credentials : credentials ,
124
+ caCert : caCert ,
125
+ caKey : caKey ,
126
+ keyVaultScope : keyVaultScope , // Initialize the new field
95
127
}, nil
96
128
}
97
129
@@ -136,10 +168,12 @@ func (a AuthService) AuthHandler(w http.ResponseWriter, r *http.Request) {
136
168
137
169
w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
138
170
w .WriteHeader (http .StatusOK )
139
- token , err := a .credentials .GetToken (context .TODO (), policy.TokenRequestOptions {Scopes : []string {"https://vault.azure.net/.default" }})
171
+ token , err := a .credentials .GetToken (context .TODO (), policy.TokenRequestOptions {Scopes : []string {a . keyVaultScope }})
140
172
if err != nil {
141
173
klog .ErrorS (err , "failed to get token" , "pod" , pod .name , "namespace" , pod .namespace )
142
- return
174
+ // Ensure an error response is sent to the client
175
+ http .Error (w , "Failed to get Azure token" , http .StatusInternalServerError )
176
+ return // Added return to prevent further execution after error
143
177
}
144
178
if err := json .NewEncoder (w ).Encode (map [string ]string {"oauth_token" : token .Token }); err != nil {
145
179
klog .ErrorS (err , "failed to json encode token" , "pod" , pod .name , "namespace" , pod .namespace )
0 commit comments