@@ -2,11 +2,14 @@ package defaultazurecredential
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "os"
5
7
6
8
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
7
9
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
8
10
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
9
11
"github.com/Azure/go-autorest/autorest"
12
+ "github.com/Azure/go-autorest/autorest/azure"
10
13
"k8s.io/klog/v2"
11
14
)
12
15
@@ -26,20 +29,34 @@ func NewAuthorizer() (autorest.Authorizer, error) {
26
29
return nil , err
27
30
}
28
31
32
+ scope := tokenScopeFromEnvironment ()
33
+ klog .V (7 ).Infof ("Fetching token with scope %s" , scope )
29
34
return autorest .NewBearerAuthorizer (& tokenCredentialWrapper {
30
- cred : cred ,
35
+ cred : cred ,
36
+ scope : scope ,
31
37
}), nil
32
38
}
33
39
40
+ func tokenScopeFromEnvironment () string {
41
+ cloud := os .Getenv ("AZURE_ENVIRONMENT" )
42
+ env , err := azure .EnvironmentFromName (cloud )
43
+ if err != nil {
44
+ env = azure .PublicCloud
45
+ }
46
+
47
+ return fmt .Sprintf ("%s.default" , env .TokenAudience )
48
+ }
49
+
34
50
type tokenCredentialWrapper struct {
35
- cred azcore.TokenCredential
51
+ cred azcore.TokenCredential
52
+ scope string
36
53
}
37
54
38
55
func (w * tokenCredentialWrapper ) OAuthToken () string {
39
56
klog .V (7 ).Info ("Getting Azure token using DefaultAzureCredential" )
40
57
41
58
token , err := w .cred .GetToken (context .Background (), policy.TokenRequestOptions {
42
- Scopes : []string {"https://management.azure.com/.default" },
59
+ Scopes : []string {w . scope },
43
60
})
44
61
45
62
if err != nil {
0 commit comments