Skip to content

Commit b2e7b8d

Browse files
Normalize structure: Generate IAM Policies with IAM Policy (#258)
Co-authored-by: totesforlife <toteally.notifications@gmail.com>
1 parent 2f2e205 commit b2e7b8d

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
32.1 KB
Loading

src/content/docs/aws/tutorials/iam-policy-stream.mdx

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ Additionally, it serves as a useful learning tool, helping users understand the
4848
- [LocalStack Web Application account](https://app.localstack.cloud/sign-up)
4949
- [`jq`](https://jqlang.github.io/jq/download/)
5050

51+
## Architecture diagram
52+
53+
The following diagram illustrates the architecture of this tutorial:
54+
55+
![LocalStack Environment Architecture](/images/aws/iam-policy-stream-architecture.png)
56+
57+
In this architecture:
58+
1. An **S3 bucket** is configured to send event notifications when objects are created
59+
2. An **SQS queue** receives these notifications
60+
3. The **IAM Enforcement Engine** intercepts API calls and checks for proper permissions
61+
4. The **IAM Policy Stream Dashboard** captures all API requests and generates the necessary IAM policies
62+
5. When a file is uploaded to the S3 bucket, S3 attempts to send a message to SQS, which initially fails due to missing permissions
63+
6. The IAM Policy Stream automatically generates the required policy, which can then be applied to resolve the violation
64+
5165
## Tutorial: Configure an S3 bucket for event notifications using SQS
5266

5367
In this tutorial, you will configure a LocalStack S3 bucket to send event notifications to an SQS queue.
@@ -253,6 +267,147 @@ For larger AWS applications, you would be able to find multiple roles and multip
253267

254268
![Required resource based policy](/images/aws/require-resource-based-policy.png)
255269

270+
## Testing the application
271+
272+
This section demonstrates how to test your IAM policies and verify both deny and allow scenarios using LocalStack's IAM enforcement.
273+
274+
### Testing Scenario 1: Deny (Without IAM Policy)
275+
276+
When you first upload a file to the S3 bucket without the proper SQS queue policy in place, the S3 service will be denied permission to send messages to the SQS queue.
277+
278+
**Upload a test file:**
279+
280+
```bash
281+
echo "Test file content" > test-file.log
282+
awslocal s3 cp test-file.log s3://s3-event-notification-bucket/
283+
```
284+
285+
**Expected output - IAM Violation in LocalStack logs:**
286+
287+
```shell
288+
2024-07-09T05:30:33.583 INFO --- [et.reactor-4] l.s.i.p.handler : Request for service 'sqs' by principal 's3.amazonaws.com' for operation 'SendMessage' denied.
289+
2024-07-09T05:30:33.583 DEBUG --- [et.reactor-4] l.s.i.p.handler : Necessary permissions for this action: ["Action 'sqs:SendMessage' for 'arn:aws:sqs:us-east-1:000000000000:s3-event-notification-queue'"]
290+
2024-07-09T05:30:33.583 DEBUG --- [et.reactor-4] l.s.i.p.handler : 0 permissions have been explicitly denied: []
291+
2024-07-09T05:30:33.583 DEBUG --- [et.reactor-4] l.s.i.p.handler : 0 permissions have been explicitly allowed: []
292+
2024-07-09T05:30:33.583 DEBUG --- [et.reactor-4] l.s.i.p.handler : 1 permissions have been implicitly denied: ["Action 'sqs:SendMessage' for 'arn:aws:sqs:us-east-1:000000000000:s3-event-notification-queue'"]
293+
```
294+
295+
**IAM Policy Stream Dashboard showing the violation:**
296+
297+
![IAM Policy Stream showcasing an IAM violation](/images/aws/iam-policy-stream-violation.png)
298+
299+
The dashboard clearly shows:
300+
- **Action**: `SQS.SendMessage`
301+
- **Status**: `Denied` (shown in red)
302+
- **Principal**: `s3.amazonaws.com`
303+
- **Resource**: `arn:aws:sqs:us-east-1:000000000000:s3-event-notification-queue`
304+
305+
**Attempting to receive messages from the queue:**
306+
307+
```bash
308+
awslocal sqs receive-message \
309+
--queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/s3-event-notification-queue
310+
```
311+
312+
**Expected output - No messages (because S3 was denied):**
313+
314+
```json
315+
{
316+
"Messages": []
317+
}
318+
```
319+
320+
Or you may receive no output at all, indicating an empty queue.
321+
322+
### Testing Scenario 2: Allow (With IAM Policy)
323+
324+
After applying the IAM policy generated by the Policy Stream to your SQS queue, the S3 service will be granted permission to send messages.
325+
326+
**The required policy (already applied via Terraform):**
327+
328+
```json
329+
{
330+
"Version": "2012-10-17",
331+
"Statement": [
332+
{
333+
"Sid": "Test22bf6867",
334+
"Effect": "Allow",
335+
"Action": "sqs:SendMessage",
336+
"Resource": "arn:aws:sqs:us-east-1:000000000000:s3-event-notification-queue",
337+
"Principal": {
338+
"Service": [
339+
"s3.amazonaws.com"
340+
]
341+
},
342+
"Condition": {
343+
"ArnEquals": {
344+
"aws:SourceArn": "arn:aws:s3:::s3-event-notification-bucket"
345+
}
346+
}
347+
}
348+
]
349+
}
350+
```
351+
352+
**Upload another test file:**
353+
354+
```bash
355+
echo "Test file with policy" > test-file-2.log
356+
awslocal s3 cp test-file-2.log s3://s3-event-notification-bucket/
357+
```
358+
359+
**Expected output - Success (no IAM violation):**
360+
361+
```shell
362+
upload: ./test-file-2.log to s3://s3-event-notification-bucket/test-file-2.log
363+
```
364+
365+
**LocalStack logs showing successful permission:**
366+
367+
```shell
368+
2024-07-09T05:35:22.123 DEBUG --- [et.reactor-2] l.s.i.p.handler : Request for service 'sqs' by principal 's3.amazonaws.com' for operation 'SendMessage' allowed.
369+
2024-07-09T05:35:22.123 DEBUG --- [et.reactor-2] l.s.i.p.handler : 1 permissions have been explicitly allowed: ["Action 'sqs:SendMessage' for 'arn:aws:sqs:us-east-1:000000000000:s3-event-notification-queue'"]
370+
```
371+
372+
**IAM Policy Stream Dashboard showing no violations:**
373+
374+
![IAM Policy Stream showcasing no violations](/images/aws/iam-policy-stream-no-violations.png)
375+
376+
The dashboard shows all actions with green checkmarks, indicating successful execution.
377+
378+
**Receive the message from the queue:**
379+
380+
```bash
381+
awslocal sqs receive-message \
382+
--queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/s3-event-notification-queue
383+
```
384+
385+
**Expected output - Message successfully received:**
386+
387+
```json
388+
{
389+
"Messages": [
390+
{
391+
"MessageId": "7c9d6b22-cb35-4a66-98dc-6f48dfc78f33",
392+
"ReceiptHandle": "MTM4ZTg2NTYtMGIwNC00ZWE2LWIyM2EtNWNlZTIyOTZmOGE1IGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6czMtZXZlbnQtbm90aWZpY2F0aW9uLXF1ZXVlIDdjOWQ2YjIyLWNiMzUtNGE2Ni05OGRjLTZmNDhkZmM3OGYzMyAxNzIwNTAzNjEyLjU2NDEyOTQ=",
393+
"MD5OfBody": "10eacb105ec11badc56f7e0198e0c4ad",
394+
"Body": "{\"Service\": \"Amazon S3\", \"Event\": \"s3:TestEvent\", \"Time\": \"2024-07-09T05:29:55.923Z\", \"Bucket\": \"s3-event-notification-bucket\", \"RequestId\": \"bfa882c0-a3b0-4549-b4c5-ac34167b3076\", \"HostId\": \"eftixk72aD6Ap51TnqcoF8eFidJG9Z/2\"}"
395+
}
396+
]
397+
}
398+
```
399+
400+
The message body contains the S3 event notification with details about the uploaded file, confirming that the IAM policy is working correctly.
401+
402+
### Verification Checklist
403+
404+
To ensure your IAM policies are correctly configured:
405+
406+
- **No IAM violations** appear in the IAM Policy Stream dashboard
407+
- **Messages are successfully delivered** to the SQS queue
408+
- **LocalStack logs show "allowed"** for the `SendMessage` operation
409+
- **All API calls display green checkmarks** in the Policy Stream dashboard
410+
256411
## Conclusion
257412

258413
IAM Policy Stream streamlines your development process by minimizing the manual creation of policies and confirming the necessity of granted permissions.

0 commit comments

Comments
 (0)