github.com/terraform-modules-krish/terratest@v0.29.0/modules/azure/authorizer.go (about)

     1  package azure
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/Azure/go-autorest/autorest"
     7  	az "github.com/Azure/go-autorest/autorest/azure"
     8  	"github.com/Azure/go-autorest/autorest/azure/auth"
     9  )
    10  
    11  const (
    12  	// AuthFromEnvClient is an env variable supported by the Azure SDK
    13  	AuthFromEnvClient = "AZURE_CLIENT_ID"
    14  
    15  	// AuthFromEnvTenant is an env variable supported by the Azure SDK
    16  	AuthFromEnvTenant = "AZURE_TENANT_ID"
    17  
    18  	// AuthFromFile is an env variable supported by the Azure SDK
    19  	AuthFromFile = "AZURE_AUTH_LOCATION"
    20  )
    21  
    22  // NewAuthorizer creates an Azure authorizer adhering to standard auth mechanisms provided by the Azure Go SDK
    23  // See Azure Go Auth docs here: https://docs.microsoft.com/en-us/go/azure/azure-sdk-go-authorization
    24  func NewAuthorizer() (*autorest.Authorizer, error) {
    25  	// Carry out env var lookups
    26  	_, clientIDExists := os.LookupEnv(AuthFromEnvClient)
    27  	_, tenantIDExists := os.LookupEnv(AuthFromEnvTenant)
    28  	_, fileAuthSet := os.LookupEnv(AuthFromFile)
    29  
    30  	// Execute logic to return an authorizer from the correct method
    31  	if clientIDExists && tenantIDExists {
    32  		authorizer, err := auth.NewAuthorizerFromEnvironment()
    33  		return &authorizer, err
    34  	} else if fileAuthSet {
    35  		authorizer, err := auth.NewAuthorizerFromFile(az.PublicCloud.ResourceManagerEndpoint)
    36  		return &authorizer, err
    37  	} else {
    38  		authorizer, err := auth.NewAuthorizerFromCLI()
    39  		return &authorizer, err
    40  	}
    41  }