Skip to content

Commit b6a9e1a

Browse files
giuspenGiuseppe PenonekstribrnAmzn
authored
New HTTP demo to generate a pre-signed URL to an S3 object file (#1901)
* added demo to generate a presigned URL --------- Co-authored-by: Giuseppe Penone <[email protected]> Co-authored-by: Kody Stribrny <[email protected]>
1 parent 02504fe commit b6a9e1a

File tree

22 files changed

+1819
-552
lines changed

22 files changed

+1819
-552
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ You can generate the presigned urls using [demos/http/common/src/presigned_urls_
479479

480480
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.
481481

482+
#### Configure S3 Generate Pre-signed URL using SigV4 Library:
483+
484+
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.
485+
482486
#### Setup for AWS IoT Jobs demo
483487

484488
1. The demo requires the Linux platform to contain curl and libmosquitto. On a Debian platform, these dependencies can be installed with:
@@ -550,6 +554,7 @@ http_demo_basic_tls
550554
http_demo_mutual_auth
551555
http_demo_plaintext
552556
http_demo_s3_download
557+
http_demo_s3_generate_presigned_url
553558
http_demo_s3_download_multithreaded
554559
http_demo_s3_upload
555560
jobs_demo_mosquitto

demos/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ if(NOT ${OpenSSL_FOUND})
4040
"http_demo_basic_tls"
4141
"http_demo_mutual_auth"
4242
"http_demo_s3_download"
43+
"http_demo_s3_generate_presigned_url"
4344
"http_demo_s3_download_multithreaded"
4445
"http_demo_s3_upload"
4546
"mqtt_demo_basic_tls"
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* AWS IoT Device SDK for Embedded C 202211.00
3+
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#ifndef HTTP_DEMO_S3_UTILS_H_
24+
#define HTTP_DEMO_S3_UTILS_H_
25+
26+
/* Standard includes. */
27+
#include <stdlib.h>
28+
#include <stdbool.h>
29+
30+
/* *INDENT-OFF* */
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
/* *INDENT-ON* */
35+
36+
/* Other HTTP utils header. */
37+
#include "http_demo_utils.h"
38+
39+
/* JSON API header. */
40+
#include "core_json.h"
41+
42+
/* SIGV4 API header. */
43+
#include "sigv4.h"
44+
45+
/**
46+
* @brief Length in bytes of hex encoded hash digest.
47+
*/
48+
#define HEX_ENCODED_SHA256_HASH_DIGEST_LENGTH ( ( ( uint16_t ) 64 ) )
49+
50+
/**
51+
* @brief Length in bytes of SHA256 hash digest.
52+
*/
53+
#define SHA256_HASH_DIGEST_LENGTH ( HEX_ENCODED_SHA256_HASH_DIGEST_LENGTH / 2 )
54+
55+
/**
56+
* @brief Maximum Length for AWS IOT Credential provider server host name.
57+
*
58+
* @note length of the AWS IOT Credential provider server host name string
59+
* cannot exceed this value.
60+
*/
61+
#define SERVER_HOST_NAME_MAX_LENGTH 65U
62+
63+
/**
64+
* @brief The host address string extracted from the AWS IOT CREDENTIAL PROVIDER URL.
65+
*
66+
* @note SERVER_HOST_NAME_MAX_LENGTH is set as the array length here as the
67+
* length of the host name string cannot exceed this value.
68+
*/
69+
extern char serverHost[ SERVER_HOST_NAME_MAX_LENGTH ];
70+
71+
/**
72+
* @brief The length of the host address found in the the AWS IOT CREDENTIAL PROVIDER URL.
73+
*/
74+
extern size_t serverHostLength;
75+
76+
/**
77+
* @brief The security token retrieved from AWS IoT credential provider
78+
* required for making HTTP requests to AWS S3.
79+
*/
80+
extern const char * pSecurityToken;
81+
82+
/**
83+
* @brief Length of security token retrieved from AWS IoT credential provider
84+
* required for making HTTP requests to AWS S3.
85+
*/
86+
extern size_t securityTokenLen;
87+
88+
/**
89+
* @brief The expiration time for the temporary credentials retrieved
90+
* from AWS IoT credential provider service.
91+
*/
92+
extern const char * pExpiration;
93+
94+
/**
95+
* @brief Length of expiration time for the temporary credentials retrieved
96+
* from AWS IoT credential provider service.
97+
*/
98+
size_t expirationLen;
99+
100+
/**
101+
* @brief Retrieve the temporary credentials from AWS IOT Credential Provider.
102+
*
103+
* @param[in] pTransportInterface The transport interface for performing network send/recv operations.
104+
* @param[out] pDateISO8601 Buffer to store the ISO8601 formatted date.
105+
* @param[in] pDateISO8601Len Length of the buffer provided to store ISO8601 formatted date.
106+
* @param[in,out] response Response buffer to store the HTTP response received.
107+
* @param[out] sigvCreds Buffer to store the parsed credentials.
108+
*
109+
* @return `true` if credentials are retrieved successfully otherwise 'false`.
110+
*/
111+
bool getTemporaryCredentials( TransportInterface_t * transportInterface,
112+
char * pDateISO8601,
113+
size_t pDateISO8601Len,
114+
HTTPResponse_t * response,
115+
SigV4Credentials_t * sigvCreds );
116+
117+
/**
118+
* @brief Calculate SHA256 digest.
119+
*
120+
* @param[in] pInput Input string to hash.
121+
* @param[in] ilen Length of input string.
122+
* @param[out] pOutput Buffer to store the generated hash.
123+
*/
124+
int32_t sha256( const char * pInput,
125+
size_t ilen,
126+
char * pOutput );
127+
128+
/**
129+
* @brief Application-defined Hash Initialization function provided
130+
* to the SigV4 library.
131+
*
132+
* @note Refer to SigV4CryptoInterface_t interface documentation for this function.
133+
*/
134+
int32_t sha256Init( void * hashContext );
135+
136+
/**
137+
* @brief Application-defined Hash Update function provided to the SigV4 library.
138+
*
139+
* @note Refer to SigV4CryptoInterface_t interface documentation for this function.
140+
*/
141+
int32_t sha256Update( void * hashContext,
142+
const uint8_t * pInput,
143+
size_t inputLen );
144+
145+
/**
146+
* @brief Application-defined Hash Final function provided to the SigV4 library.
147+
*
148+
* @note Refer to SigV4CryptoInterface_t interface documentation for this function.
149+
*/
150+
int32_t sha256Final( void * hashContext,
151+
uint8_t * pOutput,
152+
size_t outputLen );
153+
154+
/**
155+
* @brief Connect to AWS IOT Credential Provider server with reconnection retries.
156+
*
157+
* @param[out] pNetworkContext The output parameter to return the created
158+
* network context.
159+
*
160+
* @return EXIT_FAILURE on failure; EXIT_SUCCESS on successful connection.
161+
*/
162+
int32_t connectToIotServer( NetworkContext_t * pNetworkContext );
163+
164+
/* *INDENT-OFF* */
165+
#ifdef __cplusplus
166+
}
167+
#endif
168+
/* *INDENT-ON* */
169+
170+
#endif /* ifndef HTTP_DEMO_S3_UTILS_H_ */

demos/http/common/include/http_demo_utils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ typedef int32_t ( * TransportConnect_t )( NetworkContext_t * pNetworkContext );
6363
int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
6464
NetworkContext_t * pNetworkContext );
6565

66+
/**
67+
* @brief Get the starting location of HTTP header in an HTTP request.
68+
*
69+
* @param[in] requestHeaders HTTP request headers that contains the HTTP request information.
70+
* @param[out] pStartHeaderLoc Buffer to store the start Location of the HTTP header.
71+
* @param[out] pHeadersDataLen Length of @p pStartHeaderLoc.
72+
*/
73+
void getHeaderStartLocFromHttpRequest( HTTPRequestHeaders_t requestHeaders,
74+
char ** pStartHeaderLoc,
75+
size_t * pHeadersDataLen );
76+
6677
/**
6778
* @brief Get the current time in milliseconds.
6879
*

0 commit comments

Comments
 (0)