Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TF config/testing script for cloudkms #617

Merged
merged 3 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions google/resource-snippets/cloudkms-v1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Cloudkms Snippets

## DM

Setup:

* Install [gcloud](https://cloud.google.com/sdk/docs/install)
* Create GCP project
* Setup [Credential](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started#adding-credentials)

```bash
DM_PROJECT_ID=[DM_PROJECT_ID]
gcloud config set project $DM_PROJECT_ID
gcloud services enable deploymentmanager.googleapis.com
gcloud deployment-manager deployments create d1 --config kms.yaml
```

## Terraform

Setup:

* Install [gcloud](https://cloud.google.com/sdk/docs/install)
* Install [terraform](https://www.terraform.io/downloads.html)
* Create GCP project
* Setup [Credential](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started#adding-credentials)

```bash
TF_PROJECT_ID=[TF_PROJECT_ID]
gcloud config set project $TF_PROJECT_ID
cd alternatives/tf
terraform init
terraform plan -var="deployment=d1" -var="project_id=${TF_PROJECT_ID}"
terraform apply -auto-approve -var="deployment=d1" -var="project_id=${TF_PROJECT_ID}"
```

## Testing

```bash
sh test_alternatives.sh
```
42 changes: 42 additions & 0 deletions google/resource-snippets/cloudkms-v1/alternatives/tf/cloudkms.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variable "project_id" {}
azhao155 marked this conversation as resolved.
Show resolved Hide resolved
variable "deployment" {}
variable "user" {}

provider "google" {
project = var.project_id
region = "us-central1"
zone = "us-central1-c"
}

resource "google_kms_key_ring" "default" {
name = "test-keyring"
location = "us-central1"
}

resource "google_kms_crypto_key" "default" {
name = format("%s-cryptoKey", var.deployment)
key_ring = google_kms_key_ring.default.id
purpose = "ENCRYPT_DECRYPT"
labels = {
foo = var.deployment
}
}

data "google_iam_policy" "admin" {
binding {
role = "roles/cloudkms.admin"

members = [
var.user,
]
}
}

resource "google_kms_crypto_key_iam_policy" "crypto_key" {
crypto_key_id = google_kms_crypto_key.default.id
policy_data = data.google_iam_policy.admin.policy_data
}

data "google_kms_crypto_key_version" "crypto-key-version" {
crypto_key = google_kms_crypto_key.default.self_link
}
2 changes: 1 addition & 1 deletion google/resource-snippets/cloudkms-v1/kms.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resources:
type: gcp-types/cloudkms-v1:projects.locations.keyRings
properties:
parent: projects/{{ env["project"] }}/locations/{{ properties["region"] }}
keyRingId: test-{{ properties["time"] }}-keyRing
keyRingId: test-keyRing
- name: cryptoKey
type: gcp-types/cloudkms-v1:projects.locations.keyRings.cryptoKeys
properties:
Expand Down
39 changes: 39 additions & 0 deletions google/resource-snippets/cloudkms-v1/test_alternatives.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
set -e

# assumes that the following projects are created and kubectl context is set to a cluster in KRM_PROJECT
TF_PROJECT_ID=[TF_PROJECT_ID]
DM_PROJECT_ID=[DM_PROJECT_ID]

gcloud auth application-default login
azhao155 marked this conversation as resolved.
Show resolved Hide resolved

# Create DM resources
gcloud config set project $DM_PROJECT_ID
gcloud services enable deploymentmanager.googleapis.com --project $DM_PROJECT_ID
gcloud services enable cloudkms.googleapis.com --project $DM_PROJECT_ID
gcloud deployment-manager deployments create d1 --config kms.yaml

# Create Terraform resources
pushd alternatives/tf
gcloud config set project $TF_PROJECT_ID
gcloud services enable cloudkms.googleapis.com --project $TF_PROJECT_ID
terraform plan -var="deployment=d1" -var="project_id=${TF_PROJECT_ID}" -var="user=<user_name>"
terraform apply -auto-approve -var="deployment=d1" -var="project_id=${TF_PROJECT_ID}"
popd

# Export DM and TF resources for comparison
gcloud kms keys list --keyring test-keyRing --location us-central1 --project $DM_PROJECT_ID | grep d1 | sed "s/${DM_PROJECT_ID}/PROJECT/" \
> /tmp/${DM_PROJECT_ID}.yaml

gcloud kms keys list --keyring test-keyRing --location us-central1 --project $TF_PROJECT_ID | grep d1 | sed "s/${TF_PROJECT_ID}/PROJECT/" \
> /tmp/${TF_PROJECT_ID}.yaml

diff /tmp/${TF_PROJECT_ID}.yaml /tmp/${DM_PROJECT_ID}.yaml

if [[ $(diff /tmp/${TF_PROJECT_ID}.yaml /tmp/${DM_PROJECT_ID}.yaml) ]]; then
echo "TF and DM outputs are NOT identical"
exit 1
else
echo "TF and DM outputs are identical"
fi

exit 0