0
votes

Working with the event hub and found something I find quite odd.

How can I send data to the eventhub acting as a device which i'm not.

private static Task<HttpResponseMessage> PostTelemetryAsync(string test)
        {
            var serviceNamespace = "dev-hub";
            var hubName = "eventhub";
            var url = string.Format("/{0}/publishers/testdevice/messages/", hubName);

            // Create client.
            var httpClient = new HttpClient
            {
                BaseAddress = new Uri(string.Format("https://{0}.servicebus.windows.net/", serviceNamespace))
            };

            var payload = JsonConvert.SerializeObject(test);

            var sas = createToken("dev-hub", "anotherDevice", "IdmUSeHmcrLfjSfc2ssJVvLcsMIHM/uqG1xSLUIh5t4=");


            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", sas);

            var content = new StringContent(payload, Encoding.UTF8, "application/json");

            content.Headers.Add("ContentType", "application/json");

            return httpClient.PostAsync(url, content);
        }
        private static string createToken(string resourceUri, string keyName, string key)
        {
            TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
            var week = 60 * 60 * 24 * 7;
            var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + week);
            string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
            HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
            var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
            var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
            return sasToken;
        }

In the code above I´m generating a SAS token for the device anotherDevice but posting to the url ...publishers/testdevice/messages/. That is a different device.

The eventprocessor I´m using thinks that the data is sent from the testdevice but the SAS token is generated for anotherDevice.

Is it supposed to work like this? How can I use a SAS token for a different device to send data to the hub or am I missing something here?

1
You should regenerate your keys since you posted it here. ;)Gabriel Monteiro Nepomuceno
No worries.. Not the correct keys or names.. :)Jonas K
Did the answer clarify the problem or you need more info?Gabriel Monteiro Nepomuceno
Think I got it.. Problem is if you share the key across many devices it´s hard to shut down a specific device if needed. I thought there was an auth mechanism in the eventHub checking that the SAS key is only valid for the device your sending data as.... I think that the IoT hub works like that....but not 100% sure. :)Jonas K

1 Answers

0
votes

The sas token it is to authorize your application to send data. You can have hundreads of different publishers all of them using the same sas token. Think as the authorization being this shared key that you distribute to your devices, you don't need to register the publishers before you send. Use the token just as a key to send metrics not as a way to register which device sent the data.