Skip to content

Commit bafe40a

Browse files
authored
Restricting access to S3 bucket by custom tag on the IAM identity according to security email in databricks_aws_bucket_policy data resource (#1694)
1 parent 3f8e519 commit bafe40a

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

aws/data_aws_bucket_policy.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func DataAwsBucketPolicy() *schema.Resource {
3939
},
4040
},
4141
}
42+
e2AccountId := d.Get("databricks_e2_account_id").(string)
43+
if e2AccountId != "" {
44+
policy.Statements[0].Condition = map[string]map[string]string{
45+
"StringEquals": {
46+
"aws:PrincipalTag/DatabricksAccountId": e2AccountId,
47+
},
48+
}
49+
}
4250
if v, ok := d.GetOk("full_access_role"); ok {
4351
policy.Statements[0].Principal["AWS"] = v.(string)
4452
}
@@ -57,6 +65,10 @@ func DataAwsBucketPolicy() *schema.Resource {
5765
Default: "414351767826",
5866
Optional: true,
5967
},
68+
"databricks_e2_account_id": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
},
6072
"full_access_role": {
6173
Type: schema.TypeString,
6274
Optional: true,

aws/data_aws_bucket_policy_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@ func TestDataAwsBucketPolicy_FullAccessRole(t *testing.T) {
3737
j := d.Get("json")
3838
assert.Lenf(t, j, 413, "Strange length for policy: %s", j)
3939
}
40+
41+
func TestDataAwsBucketPolicyConfusedDeputyProblem(t *testing.T) {
42+
d, err := qa.ResourceFixture{
43+
Read: true,
44+
Resource: DataAwsBucketPolicy(),
45+
NonWritable: true,
46+
ID: ".",
47+
HCL: `
48+
bucket = "abc"
49+
databricks_e2_account_id = "my_e2_account_id"
50+
`,
51+
}.Apply(t)
52+
assert.NoError(t, err)
53+
j := d.Get("json")
54+
assert.Lenf(t, j, 575, "Strange length for policy: %s", j)
55+
}

docs/data-sources/aws_bucket_policy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ resource "aws_s3_bucket_policy" "ds" {
7474

7575
* `bucket` - (Required) AWS S3 Bucket name for which to generate the policy document.
7676
* `full_access_role` - (Optional) Data access role that can have full access for this bucket
77+
* `databricks_e2_account_id` - (Optional) Your Databricks E2 account ID. Used to generate restrictive IAM policies that will increase the security of your root bucket
7778

7879
## Attribute Reference
7980

0 commit comments

Comments
 (0)