Skip to content

Commit 73b1513

Browse files
ramdaspotaleBentleyRamdasandreasthuen
authored
fix: make keyvault endpoint/dns suffix configurable depending on clou… (#824)
* fix: make keyvault endpoint/dns suffix configurable depending on cloud environment * correct go formatting with newline at end of the file --------- Co-authored-by: Ramdas Potale <ramdas.potale@bentley.com> Co-authored-by: Andreas Thuen <andreas.thuen@spv.no>
1 parent a4cd4b5 commit 73b1513

File tree

1 file changed

+44
-10
lines changed
  • cmd/azure-keyvault-secrets-webhook/auth

1 file changed

+44
-10
lines changed

cmd/azure-keyvault-secrets-webhook/auth/auth.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,28 @@ import (
2626
"k8s.io/klog/v2"
2727
)
2828

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+
2945
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
3451
}
3552

3653
func fileExists(filename string) bool {
@@ -87,11 +104,26 @@ func NewAuthService(kubeclient kubernetes.Interface, credentials azure.LegacyTok
87104
return nil, fmt.Errorf("file %s is empty", caKeyFile)
88105
}
89106

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+
90121
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
95127
}, nil
96128
}
97129

@@ -136,10 +168,12 @@ func (a AuthService) AuthHandler(w http.ResponseWriter, r *http.Request) {
136168

137169
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
138170
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}})
140172
if err != nil {
141173
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
143177
}
144178
if err := json.NewEncoder(w).Encode(map[string]string{"oauth_token": token.Token}); err != nil {
145179
klog.ErrorS(err, "failed to json encode token", "pod", pod.name, "namespace", pod.namespace)

0 commit comments

Comments
 (0)