Skip to content

Commit 31e9bdf

Browse files
authored
fix: fixes 216 (again) (#220)
1 parent 1b53a6e commit 31e9bdf

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

cmd/alzlibtool/command/check/library.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/Azure/alzlib/internal/auth"
1111
"github.com/Azure/alzlib/internal/tools/checker"
1212
"github.com/Azure/alzlib/internal/tools/checks"
13+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
14+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1315
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armpolicy"
1416
"github.com/spf13/cobra"
1517
)
@@ -27,7 +29,11 @@ var libraryCmd = cobra.Command{
2729
cmd.PrintErrf("%s could not get Azure credential: %v\n", cmd.ErrPrefix(), err)
2830
os.Exit(1)
2931
}
30-
cf, err := armpolicy.NewClientFactory("", creds, nil)
32+
cf, err := armpolicy.NewClientFactory("", creds, &arm.ClientOptions{
33+
ClientOptions: policy.ClientOptions{
34+
Cloud: auth.GetCloudFromEnv(),
35+
},
36+
})
3137
if err != nil {
3238
cmd.PrintErrf("%s could not create Azure policy client factory: %v\n", cmd.ErrPrefix(), err)
3339
os.Exit(1)

cmd/alzlibtool/command/generate/architecture.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/Azure/alzlib"
1313
"github.com/Azure/alzlib/deployment"
1414
"github.com/Azure/alzlib/internal/auth"
15+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
16+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1517
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armpolicy"
1618
"github.com/spf13/cobra"
1719
)
@@ -28,8 +30,8 @@ var generateArchitectureBaseCmd = cobra.Command{
2830
`This enables deployment with a tool of your choosing.`,
2931
Args: cobra.ExactArgs(RequiredArchitectureArgs),
3032
Run: func(cmd *cobra.Command, args []string) {
31-
thislib := alzlib.NewCustomLibraryReference(args[0])
32-
alllibs, err := thislib.FetchWithDependencies(cmd.Context())
33+
thisLib := alzlib.NewCustomLibraryReference(args[0])
34+
allLibs, err := thisLib.FetchWithDependencies(cmd.Context())
3335
if err != nil {
3436
cmd.PrintErrf(
3537
"%s could not fetch all libraries with dependencies: %v\n",
@@ -39,18 +41,22 @@ var generateArchitectureBaseCmd = cobra.Command{
3941
os.Exit(1)
4042
}
4143
az := alzlib.NewAlzLib(nil)
42-
cred, err := auth.NewToken()
44+
creds, err := auth.NewToken()
4345
if err != nil {
4446
cmd.PrintErrf("%s could not get Azure credential: %v\n", cmd.ErrPrefix(), err)
4547
os.Exit(1)
4648
}
47-
cf, err := armpolicy.NewClientFactory("", cred, nil)
49+
cf, err := armpolicy.NewClientFactory("", creds, &arm.ClientOptions{
50+
ClientOptions: policy.ClientOptions{
51+
Cloud: auth.GetCloudFromEnv(),
52+
},
53+
})
4854
if err != nil {
4955
cmd.PrintErrf("%s could not add client to alzlib: %v\n", cmd.ErrPrefix(), err)
5056
os.Exit(1)
5157
}
5258
az.AddPolicyClient(cf)
53-
if err := az.Init(cmd.Context(), alllibs...); err != nil {
59+
if err := az.Init(cmd.Context(), allLibs...); err != nil {
5460
cmd.PrintErrf("%s could not initialize alzlib: %v\n", cmd.ErrPrefix(), err)
5561
os.Exit(1)
5662
}

internal/auth/auth.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ var environmentToCloud = map[string]cloud.Configuration{
1919
"china": cloud.AzureChina,
2020
}
2121

22-
// NewToken creates a new Entra token credential.
23-
// It uses well-known Terraform ARM environment variables to configure the token acquisition.
24-
func NewToken() (azcore.TokenCredential, error) {
22+
// GetCloudFromEnv retrieves the Azure cloud configuration based on environment variables.
23+
// It checks the ARM_ENVIRONMENT and AZURE_ENVIRONMENT variables to determine the appropriate cloud.
24+
// If neither variable is set or recognized, it defaults to AzurePublic.
25+
func GetCloudFromEnv() cloud.Configuration {
2526
cld := cloud.AzurePublic
2627

2728
if env := getFirstSetEnvVar("ARM_ENVIRONMENT", "AZURE_ENVIRONMENT"); env != "" {
@@ -30,10 +31,16 @@ func NewToken() (azcore.TokenCredential, error) {
3031
}
3132
}
3233

34+
return cld
35+
}
36+
37+
// NewToken creates a new Entra token credential.
38+
// It uses well-known Terraform ARM environment variables to configure the token acquisition.
39+
func NewToken() (azcore.TokenCredential, error) {
3340
opts := aztfauth.Option{
3441
UseAzureCLI: true,
3542
ClientOptions: azcore.ClientOptions{
36-
Cloud: cld,
43+
Cloud: GetCloudFromEnv(),
3744
},
3845
}
3946

0 commit comments

Comments
 (0)