README.md
1# Configure HTTP S3 Download Demo using SigV4 Library
2
3Following steps needs to be followed to configure HTTP S3 Download Demo to use SigV4 library for authenticating the requests sent to AWS S3.
4
5## Prerequisites
6
71. You will need an AWS Account with S3 access before beginning. You must be familiar with
8AWS IoT and IAM to perform steps using the AWS CLI. You must install and configure the AWS
9CLI in order to follow the steps.
10
11 * For information on AWS S3 please refer to the
12[Welcome Guide](https://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html)
13
14 * [AWS CLI Installation guide](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)
15
16 * [AWS CLI Configuration Guide](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
17
18 ```sh
19 aws configure
20 ```
21
22### Detailed Steps
23
24#### 1. Create an AWS IoT thing
25
26You may utilize an already existing AWS IoT Thing or create a new one in the IoT Core
27section of the AWS Management Console UI.
28
29You may also use the AWS CLI with the following command to create a Thing,
30keeping track of its name:
31
32```sh
33aws iot create-thing --thing-name device_thing_name
34```
35
36#### 2. Register a certificate
37
38If your AWS IoT Thing already has a certificate attached to it, then that certificate's
39ARN can be used in [step 5](#5-attach-a-policy). Otherwise, you can create a certificate
40and attach it to the thing through IoT Core in the AWS Management Console UI. By doing
41any of these, you may skip to [step 3](#3-configure-an-iam-role).
42
43It is also possible to sign the Thing's certificate using your own Certificate Authority
44(CA) certificate, and register both certificates with AWS IoT before your device can
45authenticate to AWS IoT. If you do not already have a CA certificate, you can use OpenSSL
46to create a CA certificate, as described in
47[Use Your Own Certificate](https://docs.aws.amazon.com/iot/latest/developerguide/device-certs-your-own.html).
48To register your CA certificate with AWS IoT, follow the steps on
49[Registering Your CA Certificate](https://docs.aws.amazon.com/iot/latest/developerguide/device-certs-your-own.html#register-CA-cert).
50
51You then have to create a device certificate signed by the CA certificate and register it
52with AWS IoT, which you can do by following the steps on
53[Creating a Device Certificate Using Your CA Certificate](https://docs.aws.amazon.com/iot/latest/developerguide/device-certs-your-own.html#create-device-cert).
54Save the certificate and the corresponding key pair; you will use them when you request a
55security token later. Also, remember the password you provide when you create the
56 certificate.
57
58Run the following command in the AWS CLI to attach the device certificate to your thing
59so that you can use thing attributes in policy variables.
60
61```sh
62aws iot attach-thing-principal --thing-name device_thing_name --principal <certificate-arn>
63```
64
65#### 3. Configure an IAM role
66
67Next, configure an IAM role in your AWS account that will be assumed by the credentials
68provider on behalf of your device. You are required to associate two policies with the
69role: a trust policy that controls who can assume the role, and an access policy that
70controls which actions can be performed on which resources by assuming the role.
71
72The following trust policy grants the credentials provider permission to assume the role.
73Put it in a text document and save the document with the name, trustpolicyforiot.json.
74
75```json
76{
77 "Version": "2012-10-17",
78 "Statement": {
79 "Effect": "Allow",
80 "Principal": {"Service": "credentials.iot.amazonaws.com"},
81 "Action": "sts:AssumeRole"
82 }
83}
84```
85
86Run the following command in the AWS CLI to create an IAM role with the preceding trust
87policy.
88
89```sh
90aws iam create-role --role-name s3-access-role --assume-role-policy-document file://trustpolicyforiot.json
91```
92
93The following s3 access policy allows you to perform actions on S3. Put the
94following policy in a text document and save the document with the name
95`accesspolicyfors3.json`. Make Sure to replace "BUCKET_NAME" with the name
96of the S3 bucket you are using for this demo.
97
98```json
99{
100 "Version": "2012-10-17",
101 "Statement": {
102 "Effect": "Allow",
103 "Action": [
104 "s3:GetObject"
105 ],
106 "Resource": "arn:aws:s3:::BUCKET_NAME/*"
107 }
108}
109```
110
111Run the following command in the AWS CLI to create the access policy.
112
113```sh
114aws iam create-policy --policy-name accesspolicyfors3 --policy-document file://accesspolicyfors3.json
115```
116
117Finally, run the following command in the AWS CLI to attach the access policy to your role.
118
119```sh
120aws iam attach-role-policy --role-name s3-access-role --policy-arn arn:aws:iam::<your_aws_account_id>:policy/accesspolicyfors3
121```
122
123Configure the PassRole permissions
124
125The IAM role that you have created must be passed to AWS IoT to create a role alias, as
126described in Step 4. The IAM user who performs the operation requires `iam:PassRole`
127permission to authorize this action. You also should add permission for the `iam:GetRole`
128action to allow the IAM user to retrieve information about the specified role. Create the
129following policy to grant `iam:PassRole` and `iam:GetRole` permissions. Name this policy
130`passrolepermission.json`.
131
132```json
133{
134 "Version": "2012-10-17",
135 "Statement": {
136 "Effect": "Allow",
137 "Action": [
138 "iam:GetRole",
139 "iam:PassRole"
140 ],
141 "Resource": "arn:aws:iam::<your_aws_account_id>:role/s3-access-role"
142 }
143}
144```
145
146Run the following command in the AWS CLI to create the policy in your AWS account.
147
148```sh
149aws iam create-policy --policy-name passrolepermission --policy-document file://passrolepermission.json
150```
151
152Now, run the following command to attach the policy to the IAM user.
153
154```sh
155aws iam attach-user-policy --policy-arn arn:aws:iam::<your_aws_account_id>:policy/passrolepermission --user-name <user_name>
156```
157
158#### 4. Create a role alias
159
160Now that you have configured the IAM role, you will create a role alias with AWS IoT.
161You must provide the following pieces of information when creating a role alias:
162
163RoleAlias: This is the primary key of the role alias data model and hence a mandatory
164attribute. It is a string; the minimum length is 1 character, and the maximum length is
165128 characters.
166
167RoleArn: This is the
168[Amazon Resource Name (ARN)](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html)
169 of the IAM role you have created. This is also a mandatory attribute.
170
171CredentialDurationSeconds: This is an optional attribute specifying the validity
172(in seconds) of the security token. The minimum value is 900 seconds (15 minutes),
173and the maximum value is 43,200 seconds (12 hours); the default value is 3,600 seconds,
174if not specified.
175
176**Note**: The credentialDurationSeconds value must be less than or equal to the
177 maximum session duration of the IAM role that the role alias references,
178 otherwise the request will be rejected by the credentials provider.
179
180Run the following command in the AWS CLI to create a role alias. Use the credentials of
181the user to whom you have given the iam:PassRole permission.
182
183```sh
184aws iot create-role-alias --role-alias name-s3-access-role-alias --role-arn arn:aws:iam::<your_aws_account_id>:role/s3-access-role --credential-duration-seconds 3600
185```
186
187#### 5. Attach a policy
188
189You created and registered a certificate with AWS IoT earlier for successful authentication of your device. Now, you need to create and attach a policy to the certificate to authorize the request for the security token.
190
191```json
192{
193 "Version": "2012-10-17",
194 "Statement": [
195 {
196 "Effect": "Allow",
197 "Action": "iot:AssumeRoleWithCertificate",
198 "Resource": "arn:aws:iot:<aws_region_name>:<your_aws_account_id>:rolealias/name-s3-access-role-alias"
199 }
200 ]
201}
202```
203
204Run the following command in the AWS CLI to create the policy in your AWS IoT database.
205
206```sh
207aws iot create-policy --policy-name Thing_Policy_Name --policy-document file://thingpolicy.json
208```
209
210Use the following command to attach the policy with the certificate you registered earlier.
211
212```sh
213aws iot attach-policy --policy-name Thing_Policy_Name --target <certificate-arn>
214```
215
216#### 6. Obtain the Credentials Provider Endpoint
217
218Run the following command in the AWS CLI to obtain your AWS account-specific
219endpoint for the credentials provider. See the
220[DescribeEndpoint API documentation](https://docs.aws.amazon.com/iot/latest/apireference/API_DescribeEndpoint.html)
221for further details.
222
223```sh
224aws iot describe-endpoint --endpoint-type iot:CredentialProvider
225```
226
227The following is sample output of the describe-endpoint command. It contains the endpointAddress.
228
229```json
230{
231 "endpointAddress": "<your_aws_account_specific_prefix>.credentials.iot.us-east-1.amazonaws.com"
232}
233```
234
235Next, copy this endpoint to the macro below in `demo_config.h`.
236
237```c
238#define democonfigIOT_CREDENTIAL_PROVIDER_ENDPOINT "<your_aws_account_specific_prefix>.credentials.iot.us-east-1.amazonaws.com"
239```
240
241#### 7. After the following the above steps, configure the below macros in `demo_config.h`
242
243```c
244#define democonfigIOT_THING_NAME "Name of IOT Thing that you provided in STEP 1"
245#define democonfigIOT_CREDENTIAL_PROVIDER_ROLE "Name of ROLE ALIAS that you provided in STEP 4"
246#define democonfigS3_BUCKET_NAME "Name of Bucket that contains the object that needs to be downloaded"
247#define democonfigS3_BUCKET_REGION "Region where Bucket is located"
248#define democonfigS3_OBJECT_NAME "Name of object that needs to be downloaded from AWS S3"
249```
250