forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_aws_assume_role_policy.go
90 lines (85 loc) · 2.68 KB
/
data_aws_assume_role_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
package aws
import (
"context"
"encoding/json"
"fmt"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type awsIamPolicy struct {
Version string `json:"Version,omitempty"`
ID string `json:"Id,omitempty"`
Statements []*awsIamPolicyStatement `json:"Statement"`
}
type awsIamPolicyStatement struct {
Sid string `json:"Sid,omitempty"`
Effect string `json:"Effect,omitempty"`
Actions any `json:"Action,omitempty"`
NotActions any `json:"NotAction,omitempty"`
Resources any `json:"Resource,omitempty"`
NotResources any `json:"NotResource,omitempty"`
Principal map[string]string `json:"Principal,omitempty"`
Condition map[string]map[string]string `json:"Condition,omitempty"`
}
// DataAwsAssumeRolePolicy ...
func DataAwsAssumeRolePolicy() common.Resource {
return common.Resource{
Read: func(ctx context.Context, d *schema.ResourceData, m *common.DatabricksClient) error {
externalID := d.Get("external_id").(string)
policy := awsIamPolicy{
Version: "2012-10-17",
Statements: []*awsIamPolicyStatement{
{
Effect: "Allow",
Actions: "sts:AssumeRole",
Condition: map[string]map[string]string{
"StringEquals": {
"sts:ExternalId": externalID,
},
},
Principal: map[string]string{
"AWS": fmt.Sprintf("arn:aws:iam::%s:root", d.Get("databricks_account_id").(string)),
},
},
},
}
if v, ok := d.GetOk("for_log_delivery"); ok {
if v.(bool) {
// this is production UsageDelivery IAM role, that is considered a constant
logDeliveryARN := "arn:aws:iam::414351767826:role/SaasUsageDeliveryRole-prod-IAMRole-3PLHICCRR1TK"
policy.Statements[0].Principal["AWS"] = logDeliveryARN
}
}
policyJSON, err := json.MarshalIndent(policy, "", " ")
if err != nil {
return err
}
d.SetId(externalID)
// nolint
d.Set("json", string(policyJSON))
return nil
},
Schema: map[string]*schema.Schema{
"databricks_account_id": {
Type: schema.TypeString,
Default: "414351767826",
Optional: true,
},
"for_log_delivery": {
Type: schema.TypeBool,
Description: "Grant AssumeRole to Databricks SaasUsageDeliveryRole instead of root account",
Optional: true,
Default: false,
},
"external_id": {
Type: schema.TypeString,
Required: true,
},
"json": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
},
}
}