Skip to content

Commit 9e0e8bc

Browse files
committed
[RFC-0010] Add azure auth library
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent 45fbfee commit 9e0e8bc

23 files changed

+1029
-376
lines changed

auth/aws/credentials_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func NewCredentialsProvider(ctx context.Context, opts ...auth.Option) aws.Creden
3838
// Retrieve implements aws.CredentialsProvider.
3939
// The context is ignored, use the constructor to set the context.
4040
// This is because some callers of the library pass context.Background()
41-
// when calling this method, so to ensure we have a real context we pass
42-
// it in the constructor.
41+
// when calling this method (e.g. SOPS), so to ensure we have a real
42+
// context we pass it in the constructor.
4343
func (c *credentialsProvider) Retrieve(context.Context) (aws.Credentials, error) {
4444
token, err := auth.GetToken(c.ctx, Provider{}, c.opts...)
4545
if err != nil {

auth/azure/client.go

Lines changed: 0 additions & 118 deletions
This file was deleted.

auth/azure/client_test.go

Lines changed: 0 additions & 116 deletions
This file was deleted.

auth/azure/fake_credential.go

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)