Azure / azure-event-hubs-go

Golang client library for Azure Event Hubs https://azure.microsoft.com/services/event-hubs
MIT License
90 stars 69 forks source link

Unable to listen to the event hub in hello world example . #109

Open pikvik opened 5 years ago

pikvik commented 5 years ago

Expected Behavior

I am listening response to be received on pinging Event hub . Response should be : I m Listening

Actual Behavior

azure.BearerAuthorizer#WithAuthorization: Failed to refresh the Token for request to https://management.azure.com/subscriptions/**************************/resourceGroups/******/providers/Microsoft.EventHub/namespaces/*************/eventhubs/******************?api-version=2017-04-01: StatusCode=0 -- Original Error: adal:Failed to execute the refresh request. Error = 'Get http://*********/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F: dial tcp 169.254.169.254:80: i/o timeout' exit status 1

Environment

devigned commented 5 years ago

This appears to be error when trying to dial the Managed Identity provider on Azure virtual machine. Are you running this on an Azure VM with Managed Identity enabled? For more information on Managed Identities: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview.

Also, can you provide the code used to construct your Event Hub client?

pikvik commented 5 years ago

We are not trying on VM we are running on local and providing all the credentials and connection string as well . And I am just simply running the example script provided .

devigned commented 5 years ago

I'm not clear why the thumbs down?

The error you are providing is from the Azure Active Directory token provider when it tries to probe for managed identities. It does this as the last resort when it doesn't find Service Principal Credentials AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET env variables. Be aware, you will have to have RBAC rights to interact with the Event Hub entity.

provider, aadErr := aad.NewJWTProvider(aad.JWTProviderWithEnvironmentVars())
NewHub(namespace, hubName, provider, opts...)

If you are using a connection string, then you would be using a Signed Access Signature (SAS) token provider. Below is an example of using a Service Bus connection string. You can find many examples of this in the go docs (like: https://godoc.org/github.com/Azure/azure-event-hubs-go#example-Hub--HelloWorld). Also see the go docs for the SAS token provider.

connStr := os.Getenv("EVENTHUB_CONNECTION_STRING")
if connStr == "" {
    fmt.Println("FATAL: expected environment variable EVENTHUB_CONNECTION_STRING not set")
    return
}

// within this construction helper, a connection string is used to build a sas.TokenProviderWithKey
hub, err := eventhub.NewHubFromConnectionString(connStr + ";EntityPath=MyHub")
if err != nil {
    fmt.Println(err)
    return
}

My guess, without seeing your code, is that you are using: https://godoc.org/github.com/Azure/azure-event-hubs-go#NewHubFromEnvironment. If so, I would verify environment variables described in the documentation are properly set.

If you are still having issues getting this running after evaluating this guidance, please provide a code example replicating the issue.

pikvik commented 5 years ago

We are not pulling from .env file but hard coding . Everywhere we have hard coded . credential.go :

func AADSASCredentialWithEnvironmentVars() AADSASCredentialOption { return func(config *aad.TokenProviderConfiguration) error { config.TenantID = " " config.ClientID = " " config.ClientSecret = "***" // config.CertificatePath = os.Getenv("AZURE_CERTIFICATE_PATH") // config.CertificatePassword = os.Getenv("AZURE_CERTIFICATE_PASSWORD") .

func ExampleHub_helloWorld() { ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) defer cancel()

connStr := "Endpoint=sb://xyz-dev-eventhub.servicebus.windows.net/;SharedAccessKeyName=owner;SharedAccessKey=****************************=;EntityPath=xyz-device-remoteaccess"
if connStr == "" {
    fmt.Println("FATAL: expected environment variable EVENTHUB_CONNECTION_STRING not set")
    return
devigned commented 5 years ago

If you have a connection string, just use:

connStr := "Endpoint=sb://xyz-dev-eventhub.servicebus.windows.net/;SharedAccessKeyName=owner;SharedAccessKey=****************************=;EntityPath=xyz-device-remoteaccess"
hub, err := eventhub.NewHubFromConnectionString(connStr)
if err != nil {
    fmt.Println(err)
    return
}

I don't understand why you are using both Azure Active Directory (AAD) auth and a connection string at the same time. They are two separate, but equal, ways of authenticating.