forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_aws_unity_catalog_policy.go
107 lines (102 loc) · 2.44 KB
/
data_aws_unity_catalog_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package aws
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func generateReadContext(ctx context.Context, d *schema.ResourceData, m *common.DatabricksClient) error {
bucket := d.Get("bucket_name").(string)
awsAccountId := d.Get("aws_account_id").(string)
roleName := d.Get("role_name").(string)
policy := awsIamPolicy{
Version: "2012-10-17",
Statements: []*awsIamPolicyStatement{
{
Effect: "Allow",
Actions: []string{
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket",
"s3:GetBucketLocation",
},
Resources: []string{
fmt.Sprintf("arn:aws:s3:::%s/*", bucket),
fmt.Sprintf("arn:aws:s3:::%s", bucket),
},
},
{
Effect: "Allow",
Actions: []string{
"sts:AssumeRole",
},
Resources: []string{
fmt.Sprintf("arn:aws:iam::%s:role/%s", awsAccountId, roleName),
},
},
},
}
if kmsKey, ok := d.GetOk("kms_name"); ok {
kmsArn := fmt.Sprintf("arn:aws:kms:%s", kmsKey)
if strings.HasPrefix(kmsKey.(string), "arn:aws") {
kmsArn = kmsKey.(string)
}
policy.Statements = append(policy.Statements, &awsIamPolicyStatement{
Effect: "Allow",
Actions: []string{
"kms:Decrypt",
"kms:Encrypt",
"kms:GenerateDataKey*",
},
Resources: []string{kmsArn},
})
}
policyJSON, err := json.MarshalIndent(policy, "", " ")
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s-%s-%s", bucket, awsAccountId, roleName))
err = d.Set("json", string(policyJSON))
if err != nil {
return err
}
return nil
}
func validateSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"kms_name": {
Type: schema.TypeString,
Optional: true,
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[0-9a-zA-Z_-]+$`),
"must contain only alphanumeric, underscore, and hyphen characters"),
},
"role_name": {
Type: schema.TypeString,
Required: true,
},
"aws_account_id": {
Type: schema.TypeString,
Required: true,
},
"json": {
Type: schema.TypeString,
Computed: true,
},
}
}
func DataAwsUnityCatalogPolicy() common.Resource {
return common.Resource{
Read: generateReadContext,
Schema: validateSchema(),
}
}