Skip to content

Commit b9cb715

Browse files
jprieto92iru
and
iru
authored
feat: add deploy_aws_iam_user optional for example/single-k8s (#124)
For k8s single account, we make optional the creation of aws IAM credentials. Co-authored-by: iru <[email protected]>
1 parent 38270a4 commit b9cb715

File tree

7 files changed

+104
-15
lines changed

7 files changed

+104
-15
lines changed

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ repos:
4646
rev: v1.64.0
4747
hooks:
4848
- id: terraform_fmt
49-
# https://github.com/antonbabenko/pre-commit-terraform#terraform_validate
5049
- id: terraform_docs
5150
args:
5251
- '--args=--sort-by required'
53-
- id: terraform_validate
54-
exclude: (test)|(examples-internal)\/.*$
5552
- id: terraform_tflint
5653
exclude: (test)|(examples-internal)\/.*$
5754
args:
@@ -70,6 +67,9 @@ repos:
7067
- '--args=--only=terraform_unused_declarations'
7168
- '--args=--only=terraform_unused_required_providers'
7269
- '--args=--only=terraform_workspace_remote'
70+
# https://github.com/antonbabenko/pre-commit-terraform#terraform_validate
71+
- id: terraform_validate
72+
exclude: (test)|(examples-internal)\/.*$
7373
- repo: https://github.com/pre-commit/pre-commit-hooks
7474
rev: v4.1.0
7575
hooks:

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ This would be an overall schema of the **created resources**, for the default se
6666
- Sysdig Workload: ECS / AppRunner creation (K8s cluster is pre-required, not created)
6767
- each compute solution require a role to assume for execution
6868
- CodeBuild for on-demand image scanning
69-
- Sysdig role for [Compliance](./modules/services/cloud-bench)
69+
- Sysdig role for [Compliance](https://github.com/sysdiglabs/terraform-aws-secure-for-cloud/blob/master/modules/services/cloud-bench)
7070

7171
### Runtime Permissions
7272

73-
**General Permissions**
73+
**Threat-Detection specific**
7474

7575
```shell
7676
ssm: GetParameters
@@ -85,8 +85,14 @@ s3: GetObject
8585
**Image-Scanning specific**
8686

8787
```shell
88+
89+
# all type scanning
8890
codebuild: StartBuild
8991

92+
# deploy_image_scanning_ecr
93+
ecs:DescribeTaskDefinition
94+
95+
# deploy_image_scanning_ecs
9096
ecr: GetAuthorizationToken
9197
ecr: BatchCheckLayerAvailability
9298
ecr: GetDownloadUrlForLayer
@@ -99,14 +105,13 @@ ecr: GetLifecyclePolicy
99105
ecr: GetLifecyclePolicyPreview
100106
ecr: ListTagsForResource
101107
ecr: DescribeImageScanFindings
102-
103-
ecs:DescribeTaskDefinition
104108
```
105109
- Other Notes:
110+
- [Runtime AWS IAM permissions on JSON Statement format](https://github.com/sysdiglabs/terraform-aws-secure-for-cloud/blob/master/resources/policy-single-account-k8s-aws.json)
106111
- only Sysdig workload related permissions are specified above; infrastructure internal resource permissions (such as Cloudtrail permissions to publish on SNS, or SNS-SQS Subscription)
107112
are not detailed.
108113
- For a better security, permissions are resource pinned, instead of `*`
109-
- Check [Organizational Use Case - Role Summary](./examples/organizational/README.md#role-summary) for more details
114+
- Check [Organizational Use Case - Role Summary](https://github.com/sysdiglabs/terraform-aws-secure-for-cloud/blob/master/examples/organizational/README.md#role-summary) for more details
110115

111116

112117
<br/>
@@ -155,6 +160,14 @@ When scanning is activated, should see following lines on the cloud-connector co
155160
```
156161

157162
- For ECR image scanning, upload any image to an ECR repository of AWS. Can find CLI instructions within the UI of AWS
163+
164+
It may take some time, but you should see logs detecting the new image in the ECR repository
165+
```
166+
{"component":"ecr-action","message":"processing detection {\"account\":\"***\",\"image\":\"***.dkr.ecr.us-east-1.amazonaws.com/myimage:tag\",\"region\":\"us-east-1\"}. source=aws_cloudtrail"}
167+
{"component":"ecr-action","message":"starting ECR scanning for ***.dkr.ecr.us-east-1.amazonaws.com/myimage:tag at account ‘***’ region ‘us-east-1’"}
168+
```
169+
and a CodeBuild project being launched successfully
170+
158171
- For ECS running image scanning, deploy any task in your own cluster, or the one that we create to deploy our workload (ex.`amazon/amazon-ecs-sample` image).
159172
160173
It may take some time, but you should see logs detecting the new image in the ECS cloud-connector task

examples/single-account-k8s/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ provider "aws" {
5151
provider "helm" {
5252
kubernetes {
5353
config_path = "~/.kube/config"
54+
# optional: if you have multiple k8s contexts and desire specify your eks context
55+
config_context = "arn:aws:eks:<AWS-REGION>:<AWS-MANAGEMENT-ACCOUNT-ID>:cluster/<AWS-EKS-CLUSTER-NAME>"
5456
}
5557
}
5658
@@ -116,6 +118,7 @@ $ terraform apply
116118
| <a name="input_cloudtrail_is_multi_region_trail"></a> [cloudtrail\_is\_multi\_region\_trail](#input\_cloudtrail\_is\_multi\_region\_trail) | true/false whether cloudtrail will ingest multiregional events. testing/economization purpose. | `bool` | `true` | no |
117119
| <a name="input_cloudtrail_kms_enable"></a> [cloudtrail\_kms\_enable](#input\_cloudtrail\_kms\_enable) | true/false whether s3 should be encrypted. testing/economization purpose. | `bool` | `true` | no |
118120
| <a name="input_cloudtrail_sns_arn"></a> [cloudtrail\_sns\_arn](#input\_cloudtrail\_sns\_arn) | ARN of a pre-existing cloudtrail\_sns. If defaulted, a new cloudtrail will be created. If specified, deployment region must match Cloudtrail S3 bucket region | `string` | `"create"` | no |
121+
| <a name="input_deploy_aws_iam_user"></a> [deploy\_aws\_iam\_user](#input\_deploy\_aws\_iam\_user) | true/false whether to deploy an iam user. if set to false, check [required role permissions](https://github.com/sysdiglabs/terraform-aws-secure-for-cloud/blob/master/resources/policy-single-account-k8s-aws.json) | `bool` | `true` | no |
119122
| <a name="input_deploy_benchmark"></a> [deploy\_benchmark](#input\_deploy\_benchmark) | Whether to deploy or not the cloud benchmarking | `bool` | `true` | no |
120123
| <a name="input_deploy_image_scanning_ecr"></a> [deploy\_image\_scanning\_ecr](#input\_deploy\_image\_scanning\_ecr) | true/false whether to deploy the image scanning on ECR pushed images | `bool` | `false` | no |
121124
| <a name="input_deploy_image_scanning_ecs"></a> [deploy\_image\_scanning\_ecs](#input\_deploy\_image\_scanning\_ecs) | true/false whether to deploy the image scanning on ECS running images | `bool` | `false` | no |

examples/single-account-k8s/cloud-connector.tf

+13-7
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ resource "helm_release" "cloud_connector" {
4646
value = data.sysdig_secure_connection.current.secure_api_token
4747
}
4848

49-
set_sensitive {
50-
name = "aws.accessKeyId"
51-
value = module.iam_user.sfc_user_access_key_id
49+
dynamic "set_sensitive" {
50+
for_each = var.deploy_aws_iam_user ? [true] : []
51+
content {
52+
name = "aws.accessKeyId"
53+
value = module.iam_user[0].sfc_user_access_key_id
54+
}
5255
}
5356

54-
set_sensitive {
55-
name = "aws.secretAccessKey"
56-
value = module.iam_user.sfc_user_secret_access_key
57+
dynamic "set_sensitive" {
58+
for_each = var.deploy_aws_iam_user ? [true] : []
59+
content {
60+
name = "aws.secretAccessKey"
61+
value = module.iam_user[0].sfc_user_secret_access_key
62+
}
5763
}
5864

5965
set {
@@ -91,5 +97,5 @@ resource "helm_release" "cloud_connector" {
9197
] : []
9298
})
9399
]
94-
depends_on = [module.iam_user]
100+
depends_on = [module.iam_user[0]]
95101
}

examples/single-account-k8s/credentials.tf

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module "iam_user" {
22
source = "../../modules/infrastructure/permissions/iam-user"
3+
count = var.deploy_aws_iam_user ? 1 : 0
34
name = var.name
45

56
deploy_image_scanning = local.deploy_image_scanning

examples/single-account-k8s/variables.tf

+9
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ variable "benchmark_regions" {
6868
description = "List of regions in which to run the benchmark. If empty, the task will contain all aws regions by default."
6969
default = []
7070
}
71+
72+
#
73+
# aws iam user configuration
74+
#
75+
variable "deploy_aws_iam_user" {
76+
type = bool
77+
description = "true/false whether to deploy an iam user. if set to false, check [required role permissions](https://github.com/sysdiglabs/terraform-aws-secure-for-cloud/blob/master/resources/policy-single-account-k8s-aws.json)"
78+
default = true
79+
}

resources/sfc-policy.json

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Sid": "AllowSysdigSQSDeleteAndReceive",
6+
"Effect": "Allow",
7+
"Action": [
8+
"sqs:DeleteMessage",
9+
"sqs:ReceiveMessage"
10+
],
11+
"Resource": "<SYSDIG_CLOUDTRAIL_SNS_SQS_ARN>"
12+
},
13+
{
14+
"Sid": "AllowSysdigReadS3",
15+
"Effect": "Allow",
16+
"Action": [
17+
"s3:GetObject"
18+
],
19+
"Resource": "<SYSDIG_CLOUDTRAIL_S3_ARN>/*"
20+
},
21+
{
22+
"Sid": "AllowSysdigECSTaskDefinition",
23+
"Effect": "Allow",
24+
"Action": [
25+
"ecs:DescribeTaskDefinition"
26+
],
27+
"Resource": "*"
28+
},
29+
{
30+
"Sid": "AllowSysdigCodeBuildStartBuild",
31+
"Effect": "Allow",
32+
"Action": [
33+
"codebuild:StartBuild"
34+
],
35+
"Resource": "<SYSDIG_CODEBUILD_ARN>"
36+
},
37+
{
38+
"Sid": "AllowSysdigECRActions",
39+
"Effect": "Allow",
40+
"Action": [
41+
"ecr:GetAuthorizationToken",
42+
"ecr:BatchCheckLayerAvailability",
43+
"ecr:GetDownloadUrlForLayer",
44+
"ecr:GetRepositoryPolicy",
45+
"ecr:DescribeRepositories",
46+
"ecr:ListImages",
47+
"ecr:DescribeImages",
48+
"ecr:BatchGetImage",
49+
"ecr:GetLifecyclePolicy",
50+
"ecr:GetLifecyclePolicyPreview",
51+
"ecr:ListTagsForResource",
52+
"ecr:DescribeImageScanFindings"
53+
],
54+
"Resource": "*"
55+
}
56+
]
57+
}

0 commit comments

Comments
 (0)