77}
88
99resource "aws_s3_bucket" "this" {
10+ provider = aws. exoscale-s3
11+
1012 for_each = toset (local. s3_buckets )
1113
1214 bucket = " ${ local . cluster_name } -${ each . key } "
@@ -20,48 +22,44 @@ resource "aws_s3_bucket" "this" {
2022 }
2123}
2224
23- resource "exoscale_iam_access_key" "s3_iam_key" {
25+ # Role based on the example available https://github.com/exoscale/terraform-provider-exoscale/blob/28da8e40dca37d93e4f3438f3bf906ef400f5b07/examples/iam-bucket-access/main.tf
26+ resource "exoscale_iam_role" "s3_role" {
2427 for_each = toset (local. s3_buckets )
2528
26- name = " ${ local . cluster_name } -${ each . key } -s3-iam-key"
27- resources = [" sos/bucket:${ local . cluster_name } -${ each . key } " ]
29+ name = " ${ local . cluster_name } -${ each . key } -s3-role"
30+ description = " Role for SOS bucket ${ each . key } for the ${ local . cluster_name } SKS cluster. Created using Terraform."
31+ editable = true
2832
29- # Probably not all these permissions are needed. However, these IAM keys are resource-scoped, so there should be no
30- # issue. The only SOS permissions commented out are the ones related to the creation and deletion of an SOS bucket.
31- operations = [
32- " abort-sos-multipart-upload" ,
33- " by-pass-sos-governance-retention" ,
34- # "create-sos-bucket",
35- # "delete-sos-bucket",
36- " delete-sos-object" ,
37- " get-sos-bucket-acl" ,
38- " get-sos-bucket-cors" ,
39- " get-sos-bucket-location" ,
40- " get-sos-bucket-object-lock-configuration" ,
41- " get-sos-bucket-ownership-controls" ,
42- " get-sos-bucket-versioning" ,
43- " get-sos-object" ,
44- " get-sos-object-acl" ,
45- " get-sos-object-legal-hold" ,
46- " get-sos-object-retention" ,
47- " get-sos-presigned-url" ,
48- " list-sos-bucket" ,
49- " list-sos-bucket-multipart-uploads" ,
50- " list-sos-bucket-versions" ,
51- " list-sos-buckets" ,
52- " list-sos-buckets-usage" ,
53- " put-sos-bucket-acl" ,
54- " put-sos-bucket-cors" ,
55- " put-sos-bucket-object-lock-configuration" ,
56- " put-sos-bucket-ownership-controls" ,
57- " put-sos-bucket-versioning" ,
58- " put-sos-object" ,
59- " put-sos-object-acl" ,
60- " put-sos-object-legal-hold" ,
61- " put-sos-object-retention" ,
62- ]
33+ policy = {
34+ default_service_strategy = " deny"
35+ services = {
36+ sos = {
37+ # These rules are used in order, so if a rule does not match, the following rules are NOT evaluated.
38+ # In these settings, we first allow all operations except create-bucket and delete-bucket, then we deny all
39+ # operations on buckets that are not the one that the role relates to.
40+ type = " rules"
41+ rules = [
42+ {
43+ expression = " !(operation in ['create-bucket', 'delete-bucket'])"
44+ action = " allow"
45+ },
46+ {
47+ expression = " !(parameters.bucket in ['${ each . key } '])"
48+ action = " deny"
49+ },
50+ ]
51+ }
52+ }
53+ }
54+ }
55+
56+ resource "exoscale_iam_api_key" "s3_iam_api_key" {
57+ for_each = toset (local. s3_buckets )
58+
59+ name = " ${ local . cluster_name } -${ each . key } -s3-iam-key"
60+ role_id = resource. exoscale_iam_role . s3_role [each . key ]. id
6361
6462 depends_on = [
65- aws_s3_bucket . this ,
63+ resource . aws_s3_bucket . this ,
6664 ]
6765}
0 commit comments