Skip to content

Commit c0d4c51

Browse files
authored
Merge pull request #1542 from snyk/feat/handle_missing_azure_credentials
feat: Display better message when Azure credentials are missing
2 parents 95e1a82 + 28f1fb4 commit c0d4c51

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/remote/azurerm/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func Init(
2727
if err != nil {
2828
return err
2929
}
30+
err = provider.CheckCredentialsExist()
31+
if err != nil {
32+
return err
33+
}
3034
err = provider.Init()
3135
if err != nil {
3236
return err

pkg/remote/azurerm/provider.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package azurerm
22

33
import (
4+
"context"
5+
"errors"
46
"os"
57

8+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
9+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
610
"github.com/snyk/driftctl/pkg/output"
711
"github.com/snyk/driftctl/pkg/remote/azurerm/common"
812
"github.com/snyk/driftctl/pkg/remote/terraform"
@@ -70,3 +74,22 @@ func (p *AzureTerraformProvider) Name() string {
7074
func (p *AzureTerraformProvider) Version() string {
7175
return p.version
7276
}
77+
78+
func (p *AzureTerraformProvider) CheckCredentialsExist() error {
79+
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
80+
if err != nil {
81+
return err
82+
}
83+
84+
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{"https://management.azure.com//.default"}})
85+
if err != nil {
86+
return errors.New("Could not find any authentication method for Azure.\n" +
87+
"For more information, please check the official Azure documentation: https://docs.microsoft.com/en-us/azure/developer/go/azure-sdk-authorization#use-environment-based-authentication")
88+
}
89+
90+
if p.GetConfig().SubscriptionID == "" {
91+
return errors.New("Please provide an Azure subscription ID by setting the `AZURE_SUBSCRIPTION_ID` environment variable.")
92+
}
93+
94+
return nil
95+
}

0 commit comments

Comments
 (0)