Skip to content

Commit c3734e5

Browse files
✨ Expose azure cred functions as opts. (#5401)
Signed-off-by: Preslav <preslav@mondoo.com>
1 parent e5b48e8 commit c3734e5

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

providers-sdk/v1/util/azauth/token.go

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,63 @@ import (
1717
"go.mondoo.com/cnquery/v11/providers-sdk/v1/vault"
1818
)
1919

20-
// sometimes we run into a 'managed identity timed out' error when using a managed identity.
21-
// according to https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/azidentity/TROUBLESHOOTING.md#troubleshoot-defaultazurecredential-authentication-issues
22-
// we should instead use the NewManagedIdentityCredential directly.
23-
// This function mimics the behavior of the DefaultAzureCredential, but with a higher timeout on the managed identity
24-
func GetChainedToken(options *azidentity.DefaultAzureCredentialOptions) (*azidentity.ChainedTokenCredential, error) {
25-
if options == nil {
26-
options = &azidentity.DefaultAzureCredentialOptions{}
27-
}
20+
type TokenResolverFn (func() (azcore.TokenCredential, error))
2821

29-
chain := []azcore.TokenCredential{}
30-
31-
cli, err := azidentity.NewAzureCLICredential(&azidentity.AzureCLICredentialOptions{
32-
AdditionallyAllowedTenants: []string{"*"},
33-
})
34-
if err == nil {
35-
chain = append(chain, cli)
22+
func WithCliCredentials(opts *azidentity.AzureCLICredentialOptions) TokenResolverFn {
23+
return func() (azcore.TokenCredential, error) {
24+
return azidentity.NewAzureCLICredential(opts)
3625
}
37-
envCred, err := azidentity.NewEnvironmentCredential(&azidentity.EnvironmentCredentialOptions{ClientOptions: options.ClientOptions})
38-
if err == nil {
39-
chain = append(chain, envCred)
26+
}
27+
28+
func WithEnvCredentials(opts *azidentity.EnvironmentCredentialOptions) TokenResolverFn {
29+
return func() (azcore.TokenCredential, error) {
30+
return azidentity.NewEnvironmentCredential(opts)
4031
}
41-
mic, err := azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{ClientOptions: options.ClientOptions})
42-
if err == nil {
43-
retryableMic := &retryableManagedIdentityCredential{mic: *mic, timeout: 5 * time.Second, attempts: 3}
44-
chain = append(chain, retryableMic)
32+
}
33+
34+
// sometimes we run into a 'managed identity timed out' error when using a managed identity.
35+
// This function mimics the behavior of the NewManagedIdentityCredential, but with a higher timeout and retries
36+
func WithRetryableManagedIdentityCredentials(timeout time.Duration, attempts int, opts *azidentity.ManagedIdentityCredentialOptions) TokenResolverFn {
37+
return func() (azcore.TokenCredential, error) {
38+
mic, err := azidentity.NewManagedIdentityCredential(opts)
39+
if err != nil {
40+
return nil, err
41+
}
42+
return &retryableManagedIdentityCredential{mic: *mic, timeout: timeout, attempts: attempts}, nil
4543
}
46-
wic, err := azidentity.NewWorkloadIdentityCredential(&azidentity.WorkloadIdentityCredentialOptions{
47-
ClientOptions: options.ClientOptions,
48-
DisableInstanceDiscovery: options.DisableInstanceDiscovery,
49-
TenantID: options.TenantID,
50-
})
51-
if err == nil {
52-
chain = append(chain, wic)
44+
}
45+
46+
func WithWorkloadIdentityCredentials(opts *azidentity.WorkloadIdentityCredentialOptions) TokenResolverFn {
47+
return func() (azcore.TokenCredential, error) {
48+
return azidentity.NewWorkloadIdentityCredential(opts)
5349
}
50+
}
5451

52+
func BuildChainedToken(opts ...TokenResolverFn) (*azidentity.ChainedTokenCredential, error) {
53+
chain := []azcore.TokenCredential{}
54+
for _, fn := range opts {
55+
cred, err := fn()
56+
if err == nil {
57+
chain = append(chain, cred)
58+
}
59+
}
5560
return azidentity.NewChainedTokenCredential(chain, nil)
5661
}
5762

63+
func GetChainedToken(options *azidentity.DefaultAzureCredentialOptions) (*azidentity.ChainedTokenCredential, error) {
64+
opts := []TokenResolverFn{
65+
WithCliCredentials(&azidentity.AzureCLICredentialOptions{AdditionallyAllowedTenants: []string{"*"}}),
66+
WithEnvCredentials(&azidentity.EnvironmentCredentialOptions{ClientOptions: options.ClientOptions}),
67+
WithRetryableManagedIdentityCredentials(5*time.Second, 3, &azidentity.ManagedIdentityCredentialOptions{ClientOptions: options.ClientOptions}),
68+
WithWorkloadIdentityCredentials(&azidentity.WorkloadIdentityCredentialOptions{
69+
ClientOptions: options.ClientOptions,
70+
DisableInstanceDiscovery: options.DisableInstanceDiscovery,
71+
TenantID: options.TenantID,
72+
}),
73+
}
74+
return BuildChainedToken(opts...)
75+
}
76+
5877
type retryableManagedIdentityCredential struct {
5978
mic azidentity.ManagedIdentityCredential
6079
attempts int

providers/os/connection/container/acr/acr_auth_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ const (
2828
// implemented according to https://github.com/Azure/acr/blob/main/docs/AAD-OAuth.md
2929
type acrAuthHelper struct {
3030
httpClient *http.Client
31-
tokenFn func() (azcore.TokenCredential, error)
31+
tokenFn azauth.TokenResolverFn
3232
cache map[string]string
3333
}
3434

35-
func NewAcrAuthHelperFromToken(tokenFn func() (azcore.TokenCredential, error)) *acrAuthHelper {
35+
func NewAcrAuthHelperFromToken(tokenFn azauth.TokenResolverFn) *acrAuthHelper {
3636
return &acrAuthHelper{
3737
httpClient: http.DefaultClient,
3838
tokenFn: tokenFn,

0 commit comments

Comments
 (0)