Skip to content

Commit

Permalink
New HTTP demo to generate a pre-signed URL to an S3 object file (#1901)
Browse files Browse the repository at this point in the history
* added demo to generate a presigned URL
---------

Co-authored-by: Giuseppe Penone <[email protected]>
Co-authored-by: Kody Stribrny <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent 02504fe commit b6a9e1a
Show file tree
Hide file tree
Showing 22 changed files with 1,819 additions and 552 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ You can generate the presigned urls using [demos/http/common/src/presigned_urls_

Refer this [demos/http/http_demo_s3_download/README.md](demos/http/http_demo_s3_download/README.md) to follow the steps needed to configure and run the S3 Download HTTP Demo using SigV4 Library that generates the authorization HTTP header needed to authenticate the HTTP requests send to S3.

#### Configure S3 Generate Pre-signed URL using SigV4 Library:

Refer this [demos/http/http_demo_s3_generate_presigned_url/README.md](demos/http/http_demo_s3_generate_presigned_url/README.md) to follow the steps needed to configure and run the S3 Generate Pre-signed URL HTTP Demo using SigV4 Library that generates the authorization HTTP header needed to authenticate the HTTP requests send to S3.

#### Setup for AWS IoT Jobs demo

1. The demo requires the Linux platform to contain curl and libmosquitto. On a Debian platform, these dependencies can be installed with:
Expand Down Expand Up @@ -550,6 +554,7 @@ http_demo_basic_tls
http_demo_mutual_auth
http_demo_plaintext
http_demo_s3_download
http_demo_s3_generate_presigned_url
http_demo_s3_download_multithreaded
http_demo_s3_upload
jobs_demo_mosquitto
Expand Down
1 change: 1 addition & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if(NOT ${OpenSSL_FOUND})
"http_demo_basic_tls"
"http_demo_mutual_auth"
"http_demo_s3_download"
"http_demo_s3_generate_presigned_url"
"http_demo_s3_download_multithreaded"
"http_demo_s3_upload"
"mqtt_demo_basic_tls"
Expand Down
170 changes: 170 additions & 0 deletions demos/http/common/include/http_demo_s3_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* AWS IoT Device SDK for Embedded C 202211.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef HTTP_DEMO_S3_UTILS_H_
#define HTTP_DEMO_S3_UTILS_H_

/* Standard includes. */
#include <stdlib.h>
#include <stdbool.h>

/* *INDENT-OFF* */
#ifdef __cplusplus
extern "C" {
#endif
/* *INDENT-ON* */

/* Other HTTP utils header. */
#include "http_demo_utils.h"

/* JSON API header. */
#include "core_json.h"

/* SIGV4 API header. */
#include "sigv4.h"

/**
* @brief Length in bytes of hex encoded hash digest.
*/
#define HEX_ENCODED_SHA256_HASH_DIGEST_LENGTH ( ( ( uint16_t ) 64 ) )

/**
* @brief Length in bytes of SHA256 hash digest.
*/
#define SHA256_HASH_DIGEST_LENGTH ( HEX_ENCODED_SHA256_HASH_DIGEST_LENGTH / 2 )

/**
* @brief Maximum Length for AWS IOT Credential provider server host name.
*
* @note length of the AWS IOT Credential provider server host name string
* cannot exceed this value.
*/
#define SERVER_HOST_NAME_MAX_LENGTH 65U

/**
* @brief The host address string extracted from the AWS IOT CREDENTIAL PROVIDER URL.
*
* @note SERVER_HOST_NAME_MAX_LENGTH is set as the array length here as the
* length of the host name string cannot exceed this value.
*/
extern char serverHost[ SERVER_HOST_NAME_MAX_LENGTH ];

/**
* @brief The length of the host address found in the the AWS IOT CREDENTIAL PROVIDER URL.
*/
extern size_t serverHostLength;

/**
* @brief The security token retrieved from AWS IoT credential provider
* required for making HTTP requests to AWS S3.
*/
extern const char * pSecurityToken;

/**
* @brief Length of security token retrieved from AWS IoT credential provider
* required for making HTTP requests to AWS S3.
*/
extern size_t securityTokenLen;

/**
* @brief The expiration time for the temporary credentials retrieved
* from AWS IoT credential provider service.
*/
extern const char * pExpiration;

/**
* @brief Length of expiration time for the temporary credentials retrieved
* from AWS IoT credential provider service.
*/
size_t expirationLen;

/**
* @brief Retrieve the temporary credentials from AWS IOT Credential Provider.
*
* @param[in] pTransportInterface The transport interface for performing network send/recv operations.
* @param[out] pDateISO8601 Buffer to store the ISO8601 formatted date.
* @param[in] pDateISO8601Len Length of the buffer provided to store ISO8601 formatted date.
* @param[in,out] response Response buffer to store the HTTP response received.
* @param[out] sigvCreds Buffer to store the parsed credentials.
*
* @return `true` if credentials are retrieved successfully otherwise 'false`.
*/
bool getTemporaryCredentials( TransportInterface_t * transportInterface,
char * pDateISO8601,
size_t pDateISO8601Len,
HTTPResponse_t * response,
SigV4Credentials_t * sigvCreds );

/**
* @brief Calculate SHA256 digest.
*
* @param[in] pInput Input string to hash.
* @param[in] ilen Length of input string.
* @param[out] pOutput Buffer to store the generated hash.
*/
int32_t sha256( const char * pInput,
size_t ilen,
char * pOutput );

/**
* @brief Application-defined Hash Initialization function provided
* to the SigV4 library.
*
* @note Refer to SigV4CryptoInterface_t interface documentation for this function.
*/
int32_t sha256Init( void * hashContext );

/**
* @brief Application-defined Hash Update function provided to the SigV4 library.
*
* @note Refer to SigV4CryptoInterface_t interface documentation for this function.
*/
int32_t sha256Update( void * hashContext,
const uint8_t * pInput,
size_t inputLen );

/**
* @brief Application-defined Hash Final function provided to the SigV4 library.
*
* @note Refer to SigV4CryptoInterface_t interface documentation for this function.
*/
int32_t sha256Final( void * hashContext,
uint8_t * pOutput,
size_t outputLen );

/**
* @brief Connect to AWS IOT Credential Provider server with reconnection retries.
*
* @param[out] pNetworkContext The output parameter to return the created
* network context.
*
* @return EXIT_FAILURE on failure; EXIT_SUCCESS on successful connection.
*/
int32_t connectToIotServer( NetworkContext_t * pNetworkContext );

/* *INDENT-OFF* */
#ifdef __cplusplus
}
#endif
/* *INDENT-ON* */

#endif /* ifndef HTTP_DEMO_S3_UTILS_H_ */
11 changes: 11 additions & 0 deletions demos/http/common/include/http_demo_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ typedef int32_t ( * TransportConnect_t )( NetworkContext_t * pNetworkContext );
int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
NetworkContext_t * pNetworkContext );

/**
* @brief Get the starting location of HTTP header in an HTTP request.
*
* @param[in] requestHeaders HTTP request headers that contains the HTTP request information.
* @param[out] pStartHeaderLoc Buffer to store the start Location of the HTTP header.
* @param[out] pHeadersDataLen Length of @p pStartHeaderLoc.
*/
void getHeaderStartLocFromHttpRequest( HTTPRequestHeaders_t requestHeaders,
char ** pStartHeaderLoc,
size_t * pHeadersDataLen );

/**
* @brief Get the current time in milliseconds.
*
Expand Down
Loading

0 comments on commit b6a9e1a

Please sign in to comment.