@@ -10,35 +10,32 @@ import (
1010 "os"
1111
1212 "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
13+ "github.com/Azure/azure-sdk-for-go/sdk/azidentity/internal/exported"
1314)
1415
1516const (
16- AzureKubernetesCAData = "AZURE_KUBERNETES_CA_DATA"
17- AzureKubernetesCAFile = "AZURE_KUBERNETES_CA_FILE"
18- AzureKubernetesSNIName = "AZURE_KUBERNETES_SNI_NAME"
19-
20- AzureKubernetesTokenProxy = "AZURE_KUBERNETES_TOKEN_PROXY"
17+ EnvAzureKubernetesCAData = "AZURE_KUBERNETES_CA_DATA"
18+ EnvAzureKubernetesCAFile = "AZURE_KUBERNETES_CA_FILE"
19+ EnvAzureKubernetesSNIName = "AZURE_KUBERNETES_SNI_NAME"
20+ EnvAzureKubernetesTokenProxy = "AZURE_KUBERNETES_TOKEN_PROXY"
2121)
2222
23- // Options contains optional parameters for custom token proxy configuration.
24- type Options struct {
25- // AzureKubernetesCAData specifies the CA certificate data for the Kubernetes cluster.
26- // Corresponds to the AZURE_KUBERNETES_CA_DATA environment variable.
27- // At most one of AzureKubernetesCAData or AzureKubernetesCAFile should be set.
28- AzureKubernetesCAData string
29-
30- // AzureKubernetesCAFile specifies the path to the CA certificate file for the Kubernetes cluster.
31- // This field corresponds to the AZURE_KUBERNETES_CA_FILE environment variable.
32- // At most one of AzureKubernetesCAData or AzureKubernetesCAFile should be set.
33- AzureKubernetesCAFile string
34-
35- // AzureKubernetesSNIName specifies the name of the SNI for Kubernetes cluster.
36- // This field corresponds to the AZURE_KUBERNETES_SNI_NAME environment variable.
37- AzureKubernetesSNIName string
38-
39- // AzureKubernetesTokenProxy specifies the URL of the custom token proxy for the Kubernetes cluster.
40- // This field corresponds to the AZURE_KUBERNETES_TOKEN_PROXY environment variable.
41- AzureKubernetesTokenProxy string
23+ func readOptionsFromEnv () * exported.CustomTokenProxyOptions {
24+ return & exported.CustomTokenProxyOptions {
25+ TokenProxy : os .Getenv (EnvAzureKubernetesTokenProxy ),
26+ SNIName : os .Getenv (EnvAzureKubernetesSNIName ),
27+ CAFile : os .Getenv (EnvAzureKubernetesCAFile ),
28+ CAData : os .Getenv (EnvAzureKubernetesCAData ),
29+ }
30+ }
31+
32+ func backfillOptionsFromEnv (opts * exported.CustomTokenProxyOptions ) {
33+ if opts .CAData != "" || opts .CAFile != "" || opts .SNIName != "" || opts .TokenProxy != "" {
34+ return
35+ }
36+
37+ // only backfill if all fields are empty
38+ * opts = * readOptionsFromEnv ()
4239}
4340
4441func parseTokenProxyURL (endpoint string ) (* url.URL , error ) {
@@ -65,21 +62,6 @@ func parseTokenProxyURL(endpoint string) (*url.URL, error) {
6562 return tokenProxy , nil
6663}
6764
68- func (o * Options ) defaults () {
69- if o .AzureKubernetesTokenProxy == "" {
70- o .AzureKubernetesTokenProxy = os .Getenv (AzureKubernetesTokenProxy )
71- }
72- if o .AzureKubernetesSNIName == "" {
73- o .AzureKubernetesSNIName = os .Getenv (AzureKubernetesSNIName )
74- }
75- if o .AzureKubernetesCAFile == "" {
76- o .AzureKubernetesCAFile = os .Getenv (AzureKubernetesCAFile )
77- }
78- if o .AzureKubernetesCAData == "" {
79- o .AzureKubernetesCAData = os .Getenv (AzureKubernetesCAData )
80- }
81- }
82-
8365var (
8466 errCustomEndpointSetWithoutTokenProxy = errors .New (
8567 "AZURE_KUBERNETES_TOKEN_PROXY is not set but other custom endpoint-related settings are present" ,
@@ -93,40 +75,40 @@ func noopConfigure(*policy.ClientOptions) {
9375 // no-op
9476}
9577
96- // Apply returns a function that configures the client options to use the custom token proxy.
97- func Apply (opts * Options ) (func (* policy.ClientOptions ), error ) {
78+ // GetClientOptionsConfigurer returns a function that configures the client options to use the custom token proxy.
79+ func GetClientOptionsConfigurer (opts * exported. CustomTokenProxyOptions ) (func (* policy.ClientOptions ), error ) {
9880 if opts == nil {
9981 return noopConfigure , nil
10082 }
10183
102- opts . defaults ( )
84+ backfillOptionsFromEnv ( opts )
10385
104- if opts .AzureKubernetesTokenProxy == "" {
86+ if opts .TokenProxy == "" {
10587 // custom token proxy is not set, while other Kubernetes-related environment variables are present,
10688 // this is likely a configuration issue so erroring out to avoid misconfiguration
107- if opts .AzureKubernetesSNIName != "" || opts .AzureKubernetesCAFile != "" || opts .AzureKubernetesCAData != "" {
89+ if opts .SNIName != "" || opts .CAFile != "" || opts .CAData != "" {
10890 return nil , errCustomEndpointSetWithoutTokenProxy
10991 }
11092
11193 return noopConfigure , nil
11294 }
11395
114- tokenProxy , err := parseTokenProxyURL (opts .AzureKubernetesTokenProxy )
96+ tokenProxy , err := parseTokenProxyURL (opts .TokenProxy )
11597 if err != nil {
11698 return nil , err
11799 }
118100
119101 // CAFile and CAData are mutually exclusive, at most one can be set.
120102 // If none of CAFile or CAData are set, the default system CA pool will be used.
121- if opts .AzureKubernetesCAFile != "" && opts .AzureKubernetesCAData != "" {
103+ if opts .CAFile != "" && opts .CAData != "" {
122104 return nil , errCustomEndpointMultipleCASourcesSet
123105 }
124106
125107 // preload the transport
126108 t := & transport {
127- caFile : opts .AzureKubernetesCAFile ,
128- caData : []byte (opts .AzureKubernetesCAData ),
129- sniName : opts .AzureKubernetesSNIName ,
109+ caFile : opts .CAFile ,
110+ caData : []byte (opts .CAData ),
111+ sniName : opts .SNIName ,
130112 tokenProxy : tokenProxy ,
131113 }
132114 if _ , err := t .getTokenTransporter (); err != nil {
0 commit comments