Skip to content

Commit

Permalink
Contributor: dataproc encryption (#2840)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @chrisst
  • Loading branch information
modular-magician authored and chrisst committed Jan 10, 2019
1 parent 601f6cf commit cbfef5e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
34 changes: 34 additions & 0 deletions google/resource_dataproc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ func resourceDataprocCluster() *schema.Resource {
},
},
},
"encryption_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_name": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -502,6 +515,10 @@ func expandClusterConfig(d *schema.ResourceData, config *Config) (*dataproc.Clus
conf.InitializationActions = expandInitializationActions(v)
}

if cfg, ok := configOptions(d, "cluster_config.0.encryption_config"); ok {
conf.EncryptionConfig = expandEncryptionConfig(cfg)
}

if cfg, ok := configOptions(d, "cluster_config.0.master_config"); ok {
log.Println("[INFO] got master_config")
conf.MasterConfig = expandInstanceGroupConfig(cfg)
Expand Down Expand Up @@ -588,6 +605,14 @@ func expandSoftwareConfig(cfg map[string]interface{}) *dataproc.SoftwareConfig {
return conf
}

func expandEncryptionConfig(cfg map[string]interface{}) *dataproc.EncryptionConfig {
conf := &dataproc.EncryptionConfig{}
if v, ok := cfg["kms_key_name"]; ok {
conf.GcePdKmsKeyName = v.(string)
}
return conf
}

func expandInitializationActions(v interface{}) []*dataproc.NodeInitializationAction {
actionList := v.([]interface{})

Expand Down Expand Up @@ -796,6 +821,7 @@ func flattenClusterConfig(d *schema.ResourceData, cfg *dataproc.ClusterConfig) (
"master_config": flattenInstanceGroupConfig(d, cfg.MasterConfig),
"worker_config": flattenInstanceGroupConfig(d, cfg.WorkerConfig),
"preemptible_worker_config": flattenPreemptibleInstanceGroupConfig(d, cfg.SecondaryWorkerConfig),
"encryption_config": flattenEncryptionConfig(d, cfg.EncryptionConfig),
}

if len(cfg.InitializationActions) > 0 {
Expand All @@ -818,6 +844,14 @@ func flattenSoftwareConfig(d *schema.ResourceData, sc *dataproc.SoftwareConfig)
return []map[string]interface{}{data}
}

func flattenEncryptionConfig(d *schema.ResourceData, ec *dataproc.EncryptionConfig) []map[string]interface{} {
data := map[string]interface{}{
"kms_key_name": ec.GcePdKmsKeyName,
}

return []map[string]interface{}{data}
}

func flattenAccelerators(accelerators []*dataproc.AcceleratorConfig) interface{} {
acceleratorsTypeSet := schema.NewSet(schema.HashResource(acceleratorsSchema()), []interface{}{})
for _, accelerator := range accelerators {
Expand Down
49 changes: 48 additions & 1 deletion google/resource_dataproc_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,29 @@ func TestAccDataprocCluster_withNetworkRefs(t *testing.T) {
})
}

func TestAccDataprocCluster_KMS(t *testing.T) {
t.Parallel()

rnd := acctest.RandString(10)
kms := BootstrapKMSKey(t)
pid := getTestProjectFromEnv()

var cluster dataproc.Cluster
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDataprocClusterDestroy(),
Steps: []resource.TestStep{
{
Config: testAccDataprocCluster_KMS(pid, rnd, kms.CryptoKey.Name),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataprocClusterExists("google_dataproc_cluster.kms", &cluster),
),
},
},
})
}

func testAccCheckDataprocClusterDestroy() resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
Expand Down Expand Up @@ -777,7 +800,7 @@ resource "google_dataproc_cluster" "basic" {
name = "dproc-cluster-test-%s"
region = "us-central1"
depends_on = ["google_compute_firewall.dataproc_network_firewall"]
cluster_config {
gce_cluster_config {
subnetwork = "${google_compute_subnetwork.dataproc_subnetwork.name}"
Expand Down Expand Up @@ -1155,3 +1178,27 @@ resource "google_dataproc_cluster" "with_net_ref_by_url" {
`, netName, rnd, rnd, rnd)
}

func testAccDataprocCluster_KMS(pid, rnd, kmsKey string) string {
return fmt.Sprintf(`
data "google_project" "project" {
project_id = "%s"
}
resource "google_project_iam_member" "kms-project-binding" {
project = "${data.google_project.project.project_id}"
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
member = "serviceAccount:service-${data.google_project.project.number}@compute-system.iam.gserviceaccount.com"
}
resource "google_dataproc_cluster" "kms" {
name = "dproc-cluster-test-%s"
region = "us-central1"
cluster_config {
encryption_config {
kms_key_name = "%s"
}
}
}`, pid, rnd, kmsKey)
}
19 changes: 19 additions & 0 deletions website/docs/r/dataproc_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ The `cluster_config` block supports:
# You can define multiple initialization_action blocks
initialization_action { ... }
encryption_config { ... }
}
```

Expand Down Expand Up @@ -168,6 +169,8 @@ The `cluster_config` block supports:
* `initialization_action` (Optional) Commands to execute on each node after config is completed.
You can specify multiple versions of these. Structure defined below.

* `encryption_config` (Optional) The Customer managed encryption keys settings for the cluster.
Structure defined below.
- - -

The `cluster_config.gce_cluster_config` block supports:
Expand Down Expand Up @@ -418,6 +421,22 @@ The `initialization_action` block (Optional) can be specified multiple times and
allowed to take to execute its action. GCP will default to a predetermined
computed value if not set (currently 300).

- - -

The `encryption_config` block supports:

```hcl
cluster_config {
encryption_config {
kms_key_name = "projects/projectId/locations/region/keyRings/keyRingName/cryptoKeys/keyName"
}
}
}
```

* `kms_key_name` - (Required) The Cloud KMS key name to use for PD disk encryption for
all instances in the cluster.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down

0 comments on commit cbfef5e

Please sign in to comment.