Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: Unauthorized Access (401) to Blob Storage #814

Closed
jubueche opened this issue Jan 17, 2019 · 11 comments
Closed

ESP32: Unauthorized Access (401) to Blob Storage #814

jubueche opened this issue Jan 17, 2019 · 11 comments

Comments

@jubueche
Copy link

jubueche commented Jan 17, 2019

  • OS and version used: Ubuntu 18.04

  • SDK version used: Release Dec. 13, 2018

  • Target: ESP32.

Description of the issue:

I am trying to connect the ESP32 to my Blob storage. I am getting an HTTP error 401 (unauthorized access).
I am using the example: iothub_client_sample_upload_to_blob_mb.
I tried connecting using just the Shared Access Key in my connection string, but this did not work (no connection). After that I generated an SAS token in Azure (Storage Accounts -> -> Shared Access Signature) and plugged that in into my connection string.
My connection string looks like this:
static const char* connectionString = "HostName=<Host name>;DeviceId=<Device ID>;SharedAccessSignature=<inserted here without the "?" at the beginning>";
Q1: Why is there a "?" in front of the token? When I look at the connection string, at SharedAccessSignature=.. I don't see the "?".

I also set up the Endpoint in Azure under IoT Hub -> Upload files.
In the example, I am using the option SET_TRUSTED_CERT_IN_SAMPLES.

Q2: What does that mean? I am not so familiar with basic encryption and should probably read up on that.

Q3: Why am I getting an 401 error? What could be a possible solution?

Code sample exhibiting the issue:

Console log of the issue:

Initializing SNTP
ESP platform sntp inited!
Time is not set yet. Connecting to WiFi and getting time over NTP. timeinfo.tm_year:70
Waiting for system time to be set... tm_year:0[times:1]
Starting the IoTHub client sample upload to blob with multiple blocks...

Info: Waiting for TLS connection
Info: Waiting for TLS connection
Info: Waiting for TLS connection
Info: Waiting for TLS connection

Error: Time:Thu Jan 17 22:06:00 2019 File:/home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/iothub_client/src/iothub_client_ll_uploadtoblob.c Func:send_http_request Line:142 HTTP code was 401

Error: Time:Thu Jan 17 22:06:00 2019 File:/home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/iothub_client/src/iothub_client_ll_uploadtoblob.c Func:IoTHubClient_LL_UploadToBlob_step1and2 Line:494 unable to HTTPAPIEX_ExecuteRequest

Error: Time:Thu Jan 17 22:06:00 2019 File:/home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/iothub_client/src/iothub_client_ll_uploadtoblob.c Func:IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl Line:768 error in IoTHubClient_LL_UploadToBlob_step1
Received unexpected result FILE_UPLOAD_ERROR

hello world failed to upload
Press any key to continue

@jubueche
Copy link
Author

I changed to using just the Shared Access key with the option SET_TRUSTED_CERT_IN_SAMPLES and the upload worked one time, then my esp paniced and when I retry I get a 403 error. Why could that be? This is essentially this issue.

@asergaz
Copy link
Contributor

asergaz commented Jan 24, 2019

@asergaz asergaz self-assigned this Jan 24, 2019
@jubueche
Copy link
Author

In your answer you pointed me to an Arduino example that sends messages. This works fine for me. I ham trouble with the example sketch for uploading block blob data.

Here is the trace of the first exception that I get:

 _svfprintf_r at /Users/ivan/e/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vfprintf.c:1529

0x4015ee16: _vsnprintf_r at /Users/ivan/e/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:72

0x4015ee52: vsnprintf at /Users/ivan/e/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:41

0x400f4136: STRING_construct_sprintf at /home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/c-utility/src/strings.c:786

0x400ecbf0: IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl at /home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/iothub_client/src/iothub_client_ll_uploadtoblob.c:911

0x400ebcbd: IoTHubClientCore_LL_UploadMultipleBlocksToBlobEx at /home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/iothub_client/src/iothub_client_core_ll.c:2595

0x400ea6a6: IoTHubDeviceClient_LL_UploadMultipleBlocksToBlob at /home/julian/eclipse-workspace/chaze-esp32/components/esp-azure/azure-iot-sdk-c/iothub_client/src/iothub_device_client_ll.c:127

0x40091336: upload_to_blob_block at /home/julian/eclipse-workspace/chaze-esp32/components/upload_to_blob_block/upload_to_blob_block.c:120

0x400d39ae: azure_task at /home/julian/eclipse-workspace/chaze-esp32/main/main.c:87

0x4008e585: vPortTaskWrapper at /home/julian/Documents/esp/esp-idf/components/freertos/port.c:403

So I guess it is something with vfprintf.

@jubueche
Copy link
Author

When I first upload, it works, but afterwards I get an error (403). I am pretty sure I setup everything correctly in Azure. On question though: When configuring the blob format, it says: {iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm} With partition, what is meant? Some table I created? And must the files names be equivalent to this?
I used: "chaze-iot-hub/0/2019/01/24/21/53-01.txt"

This is the line where it fails when the first upload was not successful:


if (send_http_sas_request(upload_data, STRING_c_str(uri_resource), iotHubHttpApiExHandle, STRING_c_str(relativePath), requestHttpHeaders, blobBuffer, responseContent) != 0)
                                        {
                                            /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadMultipleBlocksToBlob(Ex) shall fail and return IOTHUB_CLIENT_ERROR. ]*/
                                            result = __FAILURE__;
                                            LogError("unable to HTTPAPIEX_ExecuteRequest");
                                        }

@asergaz
Copy link
Contributor

asergaz commented Jan 25, 2019

If you use the sample as is you don't need to care about with the blob format. You should just point to where the file is:

Line 133

@jubueche
Copy link
Author

I see. But that is not the problem. The problem is:

  1. First time uploading generates an exception in vprintf. I think this is board related.
  2. After an unsuccessful upload, I get an error (403) when trying to upload again. And I believe that should not happen. The 1st error should appear over and over again. Would it help if I post my source code?

Is anybody else having the same issue with ESP32 and azure? And @sergaz-msft maybe you have an ESP and can try to reproduce the error. I greatly appreciate any help.

@jubueche
Copy link
Author

jubueche commented Feb 2, 2019

Quick update: Seems now that I am getting the first error over and over again. I deleted the #ifdefs for the certificate and just used the MSFT Baltimore certificate. Seems that this was the issue.
I am still getting an error when the uplaod is done. After setting the data pointer to NULL and the size also to 0, the program panics. Maybe this has something to do with the esp?

Also I am using the release of 13th Decdember.
Update: The error just occured again. I am trying to debug but for some reason my debugger keeps failing at random lines.

@jubueche
Copy link
Author

jubueche commented Feb 4, 2019

There is a new update: espressif/esp-azure#23
After the last block upload, the callback sets the size of the data to zero and this Buffer_new() in iothub_client_ll_uploadtoblob.c Line and thus generates a panic. I am trying to fix this, but I am getting the good old 403 right now. Have to wait a couple minutes. (I think this is an issue with a time zone and the expiry time)
Q: What should be the IoTHubResponse in case the last block was uploaded?
Is what I stated above correct?
Why does it (seem to) work for others?

@jubueche
Copy link
Author

jubueche commented Feb 4, 2019

Ok that fixed the problem. It is just a minor change. I added:

unsigned char * tmp = BUFFER_u_char(responseToIoTHub);
                                        	STRING_HANDLE req_string;
                                        	if(tmp == NULL){
                                        		req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d}", ((httpResponse < 300) ? "true" : "false"), httpResponse);
                                        	} else{
                                        		req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}", ((httpResponse < 300) ? "true" : "false"), httpResponse, tmp);
                                        	}

@jubueche jubueche closed this as completed Feb 4, 2019
@az-iot-builder-01
Copy link
Collaborator

@jubueche, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey

@massand
Copy link
Contributor

massand commented Feb 5, 2019

@jubueche - thanks for sharing your resolution. It seems you had to patch the iothub_client\src\iothub_client_ll_uploadtoblob.c file with the NULL check above. It would be awesome if you could submit PR for this into the iothub_ll_uploadtoblob.c code, so you are credited for this change :) . Our current tests running on Ubuntu VM didn't catch this. Espressif libraries have a panic behavior for the vsnprintf call when the %s parameter is null, and I believe most embedded C libraries will run into this.

@jubueche jubueche mentioned this issue Feb 5, 2019
6 tasks
bikamani pushed a commit that referenced this issue Mar 22, 2019
bikamani added a commit that referenced this issue Mar 29, 2019
…nse (#918)

Bring in PR #847 (issue #814) change to fix uploadtoblob null pointer IoTHubResponse, add unit tests.
jadeiceman pushed a commit to jadeiceman/azure-iot-sdk-c that referenced this issue Apr 12, 2019
…Response (Azure#918)

Bring in PR Azure#847 (issue Azure#814) change to fix uploadtoblob null pointer IoTHubResponse, add unit tests.
jebrando added a commit that referenced this issue Apr 16, 2019
* Updating WolfSSL image to return errors that happen in the script (#896)

* Fix that mqtt connects twice on x509 cert (#920)

* Rename macro_utils DEC to CS_DEC (fix Arduino name collision) (#876)

* Merge PR 847 - (issue #814) Fix uploadtoblob null pointer IoTHubResponse (#918)

Bring in PR #847 (issue #814) change to fix uploadtoblob null pointer IoTHubResponse, add unit tests.

* Add authentication details to connection reliability doc

* Add authentication details to connection reliability doc [ACR]

* refactor (#925)

* release_2019_04_11_after_bump_version
jebrando added a commit that referenced this issue May 30, 2019
* Updating WolfSSL image to return errors that happen in the script (#896)

* Fix that mqtt connects twice on x509 cert (#920)

* Rename macro_utils DEC to CS_DEC (fix Arduino name collision) (#876)

* Merge PR 847 - (issue #814) Fix uploadtoblob null pointer IoTHubResponse (#918)

Bring in PR #847 (issue #814) change to fix uploadtoblob null pointer IoTHubResponse, add unit tests.

* Add authentication details to connection reliability doc

* Add authentication details to connection reliability doc [ACR]

* 4KB SSL support

4KB SSL incoming limit support on IoT Hub server side certificate renewal

* Added RAM

* refactor (#925)

* release_2019_04_11_after_bump_version

* Adding the macro_utils repo (#936)

* Update submodules to the latest update (#961)

* added MOCKABLE_FUNCTION to prov transport headers (#958)

* Fix for gh issue 775 (twin/amqp not resubscribed after reconnection)

* [ACR] Fix for gh issue 775 (twin/amqp not resubscribed after reconnection)

* Add Device Twin service fault injection tests over AMQP

* Raspi pipeline (#975)

* dockerfile changes

specify branch

pls work

move dockerfile

connection set trace

pi

clean up

update steps

fixed bugs and cleaned script

dockerfile work

add build

fix bugs

remove parameters

bugfix

remove run on raspi

remove e2e script

fix horton syntax

remove parameters

fix job name

patch condition values

fix python path

fix conditions

clean yaml

added pip

fix no checkout

fix job

fix indent

add condition

fix pip step

remove image variants

remove script_dir variable

add parser

remove force flag

fix dockerfile

remove sudo

add wget

add mkdirs

update syntax

fix tars

remove z from tar

fix workdirs

fix linaro path

fix tar

squashed bugs

dockerfile patch

Update Dockerfile

Update Dockerfile

Update sdk_build.sh

Update raspi.yaml for Azure Pipelines

Update raspi.yaml for Azure Pipelines

dockerfile tweak

add back check python

Update Dockerfile

Update Dockerfile

Update Dockerfile

Update Dockerfile

add publish artifacts

fix path for source_artifacts

container images

added docker image

raspi updates

listing stuff

tweak script

raspi tweak

try fixing copyfiles

add manual copy because tasks suck

remove env vars and add keyvault

environment variables

fix drop path

update dockerfile curl config

move curl down further

change openssl version

removed call to jenkins

dockfile tweaks

add setcap

clean steps to success

clean up the code

remove sudo

adjust bash script line

Update raspi.yaml for Azure Pipelines

this is a hack might wanna redo later

bump down versions

add ca-certificates package

fix linaro wget

fix dockerfile

dockerfile apt patch

change up

fix link

clean dockerfile

Update raspi.yaml for Azure Pipelines

Update raspi.yaml for Azure Pipelines

Update raspi.yaml for Azure Pipelines

move to raspberry pi folder

incorporate new cp for raspi

remove fake docker files

fix pathing

move to jenkins

script instead of inline

remove old lines

remove branch specific code for merge

patch some stuff

more sanitization

more cleanup

remove trace stuff

more sanitization

add copyright

* fix link

* remove space

* newline

* newline

* Update Dockerfile

* Update run_this_to_setup_a_pi_for_e2e_tests.sh

* remove reference to run_tests since we don't clone

* only run MQTT

* switch copy order

* update script

* add the sudo

* chmod

* dockerfile fix

* Run back compat test on the branch(#980)

* Adding script to run back compat tests

* Adding linux_back_compat.yml to enable DevOps test integration with new linux_back_compat.sh script.

* add Mxchip YAML and script

* Update .mxchip-ci.yml

* Add comment per previous PR

* Add comment

* Update build.sh

* Reworked build/.mxchip-ci.yml to split build and execute tasks. Also placed longer setup shell logic into build_all/mbed5/setup.sh.

* Cleaning up and testing build/.mxchip-ci.yml

* Changed /media path on client pc.

* ...

* Playing with git checkout steps in build_all/mbed5/build.sh.

* Update .mxchip-ci.yml for Azure Pipelines

* Update .mxchip-ci.yml for Azure Pipelines

* Update linux back compat scripts to speed builds (#985)

* Update .mxchip-ci.yml attempt to fix permission issues with git checkout

* Update .mxchip-ci.yml for drop to client pc

* Update .mxchip-ci.yml for Azure Pipelines

* release_2019_04_30_after_bump_version

* Update .mxchip-ci.yml for Azure Pipelines

* Cleanup of  .mxchip-ci.yml for PR

* Update .mxchip-ci.yml to give DevOps relative path for Artifact Directory

* Update setup.sh

* Update setup.sh

* Update build.sh

* Removed script that was transfered back into build_all/mbed5/build.sh

* Patch to copy command

* Test .mxchip-ci.yml with local chmod change

* Update .mxchip-ci.yml for Azure Pipelines

* Update .mxchip-ci.yml for Azure Pipelines

* update raspberry pi tests for PR validation (#995)

* Reenable rsync in mbed5/build.sh

* Update utpm submodule (#1000)

* Implementing Prov custom payload (#984)

* Update prov_auth_client.c to use renamed Azure_Base32

* Update submodules

* Add script gate build to verify building with installed dependencies

* Fix prov_auth_client_ut to use Azure_Base32

* Fix amqp property value that was being release early

* Update unittest to for new changes

* Submodule update

* fix submodule patches (#1008)

* Add extended platform info to product info string

* Change function to have 1 return

* Update transports to correctly expose GetPlatformInfo, plus tests

* Change name for AMQP function definition

* Fix memory leak that was discovered in new feature failure

* add use_mbedtls option (#862)

* remove installs (#1010)

* Additional fixes for vcpkg installation

* Update utpm with fix for log arg

* New mxchip uart interface (#1002)

* Adding initial mxchip serial interface for version control.

* Update to serial_connect.py and serial_settings.py to expand error checking and variable wait times between reads based on baud rate

* Improved tracking of Serial output from device by checking read before write. Also added better handling of exit command.

* Create input.sh which Generates a new input txt file to be run on mxchip Serial interface.

* Update build.sh, Revert git clone changes and replace with massand/GetStarted repo for mxchip.

* Update .mxchip-ci.yml to copy correct bin file to mxchip

* Update input.sh, Added setting upstream IoT Hub connection string.

* Adding updates to serial_connect.py to intelligently wait for WiFi and IoT Hub setup to complete.

* Made serial_connect.py more robust in several ways.

* Added usage dictionary file to print out serial_connect.py usage

* optionized clone of mxchip repo branch.

* fix sas handling logic error (#972)

* release_2019_05_16_after_bump_version

* Jbobotek patch 2 (#1015)

* Update build.sh, re-added rsync line with correct filepath.

* Fix Base32/64 related files (#1009)

* Raspberrypi service updates (#1013)

* update yaml

* add daemon information

* fix spacing

* increase sleep time to 10 minutes

* increase to 20 minutes

* add AZURECR to names

* move reboot to after test

* syntax fix

* add back in working directory

* move reboot before the run again

* only publish exe files

* improved steps for devops-agent

* remove pattern matching

* cp is more selective

* flat copy means happiness

* more setup explanation

* OPTION_DO_WORK_FREQUENCY_IN_MS (#1023)

* update samples

* ifndef for http

* add unit tests

* unsigned int

* update typing to be clear

* fix unit tests

* Update iothub_convenience_sample.c

* update requirement messages

* bigger comment

* fix bug

* bugfix again

* update pointer syntax

* for requirements update to iothubclient invalid arg

* add logging for deprecated option

* add negative unit test for setoption overlap

* update unit testing

* update unit tests

* unit test fix

* requirements doc

* remove logInfo

* more parenthesis

* revert changes

* remove unit test for keep alive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants