Skip to content

Commit 3e648e0

Browse files
athewseyanajmi07
andauthored
Merge pull request #16 from aws-samples/fix/security-findings
Enable access logging on provisioned S3 bucket and scope down some IAM role permissions. Co-authored-by: Aamna Najmi <anajmi@amazon.de>
2 parents 966aef3 + b466748 commit 3e648e0

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

infra/modules/agentcore-iam-role/bedrock-agentcore-policy.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ resource "aws_iam_policy" "bedrock_permissions" {
1111
"bedrock:InvokeModel",
1212
"bedrock:InvokeModelWithResponseStream"
1313
]
14-
Resource = "*"
14+
Resource = [
15+
"arn:aws:bedrock:${data.aws_region.current.name}::foundation-model/anthropic.*",
16+
"arn:aws:bedrock:${data.aws_region.current.name}::foundation-model/amazon.*",
17+
"arn:aws:bedrock:${data.aws_region.current.name}::foundation-model/meta.*"
18+
]
1519
}
1620
]
1721
})
@@ -28,8 +32,7 @@ resource "aws_iam_policy" "ecr_permissions" {
2832
Effect = "Allow"
2933
Action = [
3034
"ecr:BatchGetImage",
31-
"ecr:GetDownloadUrlForLayer",
32-
"ecr:GetAuthorizationToken"
35+
"ecr:GetDownloadUrlForLayer"
3336
]
3437
Resource = [
3538
"arn:aws:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/*"
@@ -41,6 +44,8 @@ resource "aws_iam_policy" "ecr_permissions" {
4144
Action = [
4245
"ecr:GetAuthorizationToken"
4346
]
47+
# This action does not accept any restrictions on the resource, per the docs:
48+
# https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonelasticcontainerregistry.html
4449
Resource = "*"
4550
}
4651
]
@@ -100,9 +105,14 @@ resource "aws_iam_policy" "monitoring_permissions" {
100105
"xray:GetSamplingRules",
101106
"xray:GetSamplingTargets"
102107
]
103-
Resource = "*"
108+
Resource = [
109+
"arn:aws:xray:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trace/*"
110+
]
104111
},
105112
{
113+
# WILDCARD JUSTIFICATION: CloudWatch PutMetricData requires Resource="*"
114+
# as per AWS documentation. Condition restricts to bedrock-agentcore namespace only.
115+
# Reference: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html
106116
Effect = "Allow"
107117
Resource = "*"
108118
Action = "cloudwatch:PutMetricData"

infra/modules/kb-stack/main.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,49 @@ resource "aws_s3_bucket_public_access_block" "kb_bucket_pab" {
1212
restrict_public_buckets = true
1313
}
1414

15+
# Access logging bucket
16+
resource "aws_s3_bucket" "access_logs" {
17+
bucket_prefix = "${var.name}-access-logs"
18+
}
19+
20+
resource "aws_s3_bucket_public_access_block" "access_logs_pab" {
21+
bucket = aws_s3_bucket.access_logs.id
22+
23+
block_public_acls = true
24+
block_public_policy = true
25+
ignore_public_acls = true
26+
restrict_public_buckets = true
27+
}
28+
29+
# Enable access logging
30+
resource "aws_s3_bucket_logging" "kb_bucket_logging" {
31+
bucket = aws_s3_bucket.kb_bucket.id
32+
33+
target_bucket = aws_s3_bucket.access_logs.id
34+
target_prefix = "access-logs/"
35+
}
36+
37+
# Lifecycle configuration
38+
resource "aws_s3_bucket_lifecycle_configuration" "kb_bucket_lifecycle" {
39+
bucket = aws_s3_bucket.kb_bucket.id
40+
41+
rule {
42+
id = "knowledge_base_lifecycle"
43+
status = "Enabled"
44+
45+
filter {}
46+
transition {
47+
days = 30
48+
storage_class = "STANDARD_IA"
49+
}
50+
51+
transition {
52+
days = 90
53+
storage_class = "GLACIER"
54+
}
55+
}
56+
}
57+
1558
# IAM Role for Bedrock
1659
resource "aws_iam_role" "bedrock_role" {
1760
name = "${var.name}-bedrock-role"

infra/modules/knowledge-base/main.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ resource "aws_iam_role_policy" "bedrock_kb_sample_kb_model" {
77
Version = "2012-10-17"
88
Statement = [
99
{
10-
Action = ["aoss:*"]
10+
Action = [
11+
"aoss:CreateIndex",
12+
"aoss:DescribeIndex",
13+
"aoss:UpdateIndex",
14+
"aoss:DeleteIndex",
15+
"aoss:WriteDocument",
16+
"aoss:ReadDocument",
17+
"aoss:SearchDocument",
18+
"aoss:DeleteDocument"
19+
]
1120
Effect = "Allow"
1221
Resource = [var.opensearch_arn]
1322
},

0 commit comments

Comments
 (0)