profile
viewpoint

Ask questionsAuthentication example needed

I've seen a bunch of examples on how to authenticate but non of those worked for me. Maybe someone could look at my code and provide a hint on what I'm doing wrong:

func retrieveAllResources(appID string, tenantID string, subscription string, key string, request *http.Request) {

	log.Printf("Started background worker to retrieve data from Azure subscription %s", subscription)

	os.Setenv("AZURE_CLIENT_ID", appID)
	os.Setenv("AZURE_CLIENT_SECRET", key)
	os.Setenv("AZURE_TENANT_ID", tenantID)
	os.Setenv("AZURE_SUBSCRIPTION_ID", subscription)

	vmClient := compute.NewVirtualMachinesClient(subscription)
	authorizer, err := auth.NewAuthorizerFromEnvironment()
	if err != nil {
		log.Printf("Error trying to authenticate against Azure: %+v", err)
		return
	}

	vmClient.Authorizer = authorizer

	vmIterator, err := vmClient.ListAllComplete(request.Context())
	if err != nil {
		log.Printf("Error trying to retrieve all VMs from Azure. %+v", err)
		return
	}

This is the error I get:

Error trying to retrieve all VMs from Azure. azure.BearerAuthorizer#WithAuthorization: Failed to refresh the Token for request to https://management.azure.com/subscriptions/my_subscription/providers/Microsoft.Compute/virtualMachines?api-version=2018-10-01: StatusCode=0 -- Original Error: adal: Failed to execute the refresh request. Error = 'Post https://login.microsoftonline.com/my_tenatnId/oauth2/token?api-version=1.0: context canceled'

Thanks

Azure/azure-sdk-for-go

Answer questions ksemba

@jhendrixMSFT the auth.NewAuthorizerFromEnvironment() doesn't work for me because I cannot get it to work ;) which could be related to the context that I don't know how to properly provide. Any suggestions?

Maybe some more background on why I want to use that "anti-pattern": I'm writing a service that pulls resource data from any Azure account. The consumer of that service just needs to provide the auth creds. So I'm not intending to encode/store any Azure credentials wthin my service. It receives those during the http get request, uses them and then forgets them.

useful!

Related questions

SDK equivalent for CLI "az aks install-connector" hot 1
storage.ManagementPoliciesClient.CreateOrUpdate Failed with RequestIsNotValidJson hot 1
`go get` latest fails due to UUID usage hot 1
[Recovery Services] Vague error messages when updating a policy hot 1
Cannot unmarshal ServicePrincipalListResult in call to graphrbac client.List hot 1
[Recovery Services] Vague error messages when updating a policy hot 1
go get module no matching versions for query "latest hot 1
Key Management API for Function App hot 1
Application insights frustration hot 1
AAD Domain Service create/update fails with Request_InvalidDataContractVersion hot 1
Github User Rank List