-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
190 changed files
with
6,281 additions
and
1,467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
|
||
"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 { | ||
policy.Statements = append(policy.Statements, &awsIamPolicyStatement{ | ||
Effect: "Allow", | ||
Actions: []string{ | ||
"kms:Decrypt", | ||
"kms:Encrypt", | ||
"kms:GenerateDataKey*", | ||
}, | ||
Resources: []string{ | ||
fmt.Sprintf("arn:aws:kms:%s", kmsKey), | ||
}, | ||
}) | ||
} | ||
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, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile(`^[0-9a-zA-Z/_-]+$`), | ||
"must contain only alphanumeric, hyphens, forward slashes, and underscores characters"), | ||
}, | ||
"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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package aws | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/databricks/terraform-provider-databricks/qa" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDataAwsUnityCatalogPolicy(t *testing.T) { | ||
d, err := qa.ResourceFixture{ | ||
Read: true, | ||
Resource: DataAwsUnityCatalogPolicy(), | ||
NonWritable: true, | ||
ID: ".", | ||
HCL: ` | ||
aws_account_id = "123456789098" | ||
bucket_name = "databricks-bucket" | ||
role_name = "databricks-role" | ||
kms_name = "databricks-kms" | ||
`, | ||
}.Apply(t) | ||
assert.NoError(t, err) | ||
j := d.Get("json").(string) | ||
p := `{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:GetObject", | ||
"s3:PutObject", | ||
"s3:DeleteObject", | ||
"s3:ListBucket", | ||
"s3:GetBucketLocation" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::databricks-bucket/*", | ||
"arn:aws:s3:::databricks-bucket" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"sts:AssumeRole" | ||
], | ||
"Resource": [ | ||
"arn:aws:iam::123456789098:role/databricks-role" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"kms:Decrypt", | ||
"kms:Encrypt", | ||
"kms:GenerateDataKey*" | ||
], | ||
"Resource": [ | ||
"arn:aws:kms:databricks-kms" | ||
] | ||
} | ||
] | ||
}` | ||
compareJSON(t, j, p) | ||
} | ||
|
||
func TestDataAwsUnityCatalogPolicyWithoutKMS(t *testing.T) { | ||
d, err := qa.ResourceFixture{ | ||
Read: true, | ||
Resource: DataAwsUnityCatalogPolicy(), | ||
NonWritable: true, | ||
ID: ".", | ||
HCL: ` | ||
aws_account_id = "123456789098" | ||
bucket_name = "databricks-bucket" | ||
role_name = "databricks-role" | ||
`, | ||
}.Apply(t) | ||
assert.NoError(t, err) | ||
j := d.Get("json").(string) | ||
p := `{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:GetObject", | ||
"s3:PutObject", | ||
"s3:DeleteObject", | ||
"s3:ListBucket", | ||
"s3:GetBucketLocation" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::databricks-bucket/*", | ||
"arn:aws:s3:::databricks-bucket" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"sts:AssumeRole" | ||
], | ||
"Resource": [ | ||
"arn:aws:iam::123456789098:role/databricks-role" | ||
] | ||
} | ||
] | ||
}` | ||
compareJSON(t, j, p) | ||
} | ||
|
||
func compareJSON(t *testing.T, json1 string, json2 string) { | ||
var i1 interface{} | ||
var i2 interface{} | ||
err := json.Unmarshal([]byte(json1), &i1) | ||
assert.NoError(t, err, "error while unmarshalling") | ||
err = json.Unmarshal([]byte(json2), &i2) | ||
assert.NoError(t, err, "error while unmarshalling") | ||
assert.Equal(t, i1, i2) | ||
} |
Oops, something went wrong.