Skip to content

Commit 0048672

Browse files
committed
fix(s3-blobstore): add code to create user,role and bucket
Signed-off-by: Steffen Tautenhahn <[email protected]>
1 parent e33895d commit 0048672

File tree

7 files changed

+68
-57
lines changed

7 files changed

+68
-57
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
},
1010
"go.buildFlags": [
1111
"-v"
12-
]
12+
],
13+
"go.inferGopath": false
1314
}

examples/blobstore-s3/README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
#
1+
# Nexus with AWS s3 blobstore
22

3+
To use the AWS credential from the terraform code example below you need to do the following:
4+
5+
- login with iam user credentials
6+
- assume role "terraform-provider-nexus-s3"
7+
- access s3 bucket "terraform-provider-nexus-example.datadrivers.de"
38

49
## Terraform
510

11+
Here you can find terraform example code to create iam user, iam role and s3-bucket to be used with nexus.
12+
613
```bash
714
cd terraform
815
terraform init
9-
terraform plan -var-file=./example.tfvars
10-
terraform apply -var-file=./example.tfvars
16+
terraform plan -var-file=./example.tfvars -out example.tfplan
17+
terraform apply example.tfvars
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.tfplan
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.12.23
1+
0.12.26
+17-34
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,32 @@
1-
bucket_name = "terrafor-provider-nexus--example"
2-
iam_name_prefix = "terrafor-provider-nexus"
1+
// general
2+
tags = {
3+
provisioning = "terraform"
4+
use-case = "terraform-provider-nexus"
5+
github = "https://github.com/datadrivers/terraform-provider-nexus"
6+
owner = "datadrivers"
7+
}
38

4-
acl = "private"
9+
// iam settings
10+
iam_user_name_prefix = "terraform-provider-nexus--github-action"
11+
iam_role_name = "terraform-provider-nexus-s3"
512

13+
// s3 settings
14+
bucket_name = "terraform-provider-nexus-example.datadrivers.de"
15+
acl = "private"
616
force_destroy = true
7-
817
versioning = {
918
enabled = false
1019
}
11-
12-
lifecycle_rule = [
13-
{
14-
id = "save-costs"
15-
enabled = true
16-
17-
abort_incomplete_multipart_upload_days = 7
18-
19-
transition = [
20-
{
21-
days = 30
22-
storage_class = "STANDARD_IA" # (requires >=30 days)
23-
},
24-
]
25-
},
26-
]
27-
20+
lifecycle_rule = [] # disable lifecycle rules since it will conflict with nexus lifecycle policys
2821
server_side_encryption_configuration = {
2922
rule = {
3023
apply_server_side_encryption_by_default = {
3124
sse_algorithm = "AES256"
3225
}
3326
}
3427
}
35-
36-
tags = {
37-
provisioning = "terraform"
38-
use-case = "terrafor-provider-nexus"
39-
owner = "datadrivers"
40-
}
41-
4228
// S3 bucket-level Public Access Block configuration
43-
block_public_acls = true
44-
45-
block_public_policy = true
46-
47-
ignore_public_acls = true
48-
29+
block_public_acls = true
30+
block_public_policy = true
31+
ignore_public_acls = true
4932
restrict_public_buckets = true

examples/blobstore-s3/terraform/main.tf

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
// iam user
12
resource "aws_iam_user" "login_for_external_user" {
2-
name = var.iam_name_prefix
3-
tags = var.tags
3+
count = var.iam_user_name_prefix != null ? 1 : 0
4+
name = var.iam_user_name_prefix
5+
tags = var.tags
46
}
57

8+
resource "aws_iam_user_policy" "assume_nexus_role" {
9+
count = var.iam_user_name_prefix != null ? 1 : 0
10+
name = "assume_role_nexus"
11+
user = aws_iam_user.login_for_external_user.0.name
12+
13+
policy = <<EOF
14+
{
15+
"Version": "2012-10-17",
16+
"Statement": [
17+
{
18+
"Action": "sts:AssumeRole",
19+
"Effect": "Allow",
20+
"Resource": "${aws_iam_role.access_s3_bucket_nexus.arn}"
21+
}
22+
]
23+
}
24+
EOF
25+
}
626

27+
// iam role
728
resource "aws_iam_role" "access_s3_bucket_nexus" {
8-
name_prefix = var.iam_name_prefix
29+
name = var.iam_role_name
930

1031
assume_role_policy = <<EOF
1132
{
@@ -14,18 +35,9 @@ resource "aws_iam_role" "access_s3_bucket_nexus" {
1435
{
1536
"Action": "sts:AssumeRole",
1637
"Principal": {
17-
"AWS": "${aws_iam_user.login_for_external_user.arn}"
38+
"AWS": ${jsonencode(aws_iam_user.login_for_external_user.*.arn)}
1839
},
19-
"Effect": "Allow",
20-
"Sid": "iam_user"
21-
},
22-
{
23-
"Action": "sts:AssumeRole",
24-
"Principal": {
25-
"Service": "ec2.amazonaws.com"
26-
},
27-
"Effect": "Allow",
28-
"Sid": "ec2_instance"
40+
"Effect": "Allow"
2941
}
3042
]
3143
}
@@ -60,8 +72,8 @@ data "aws_iam_policy_document" "bucket_policy" {
6072
}
6173

6274
resource "aws_iam_policy" "policy_s3_bucket" {
63-
name_prefix = var.iam_name_prefix
64-
description = "A test policy"
75+
name_prefix = var.iam_role_name
76+
description = "Nexus Access to S3 bucket"
6577

6678
policy = data.aws_iam_policy_document.bucket_policy.json
6779
}
@@ -71,6 +83,7 @@ resource "aws_iam_role_policy_attachment" "attach-s3-policy" {
7183
policy_arn = aws_iam_policy.policy_s3_bucket.arn
7284
}
7385

86+
// s3 bucket
7487
module "s3_bucket" {
7588
source = "terraform-aws-modules/s3-bucket/aws"
7689

examples/blobstore-s3/terraform/variables.tf

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
variable "iam_name_prefix" {
2-
description = "The name prefix of the iam resoruces to access the bucket. If omitted, Terraform will assign a random, unique name."
1+
variable "iam_user_name_prefix" {
2+
description = "The name prefix of the iam user resoruces to access the bucket. If omitted, Terraform will skip user creation"
3+
type = string
4+
default = null
5+
}
6+
7+
variable "iam_role_name" {
8+
description = "The name of the iam role to access the bucket. If omitted, Terraform will assign a random, unique name."
39
type = string
410
default = null
511
}

0 commit comments

Comments
 (0)