forked from GoogleCloudPlatform/kms-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tink encryption migration from symmetric to envelope example (
GoogleCloudPlatform#72) Co-authored-by: Alessio Buraggina <[email protected]>
- Loading branch information
1 parent
8b9bf51
commit a02fedd
Showing
10 changed files
with
332 additions
and
11 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
46 changes: 46 additions & 0 deletions
46
examples/tink-envelope-encryption-sample/symmetric_encrypt_process.tf
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,46 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// This file provides all the required resources to demonstrate a encryption key changing from a symmetric encryption to envelope encryption. | ||
|
||
module "symmetric_kms" { | ||
source = "terraform-google-modules/kms/google" | ||
version = "2.3.0" | ||
|
||
keyring = local.keyring | ||
location = local.location | ||
project_id = var.project_id | ||
keys = [local.key] | ||
prevent_destroy = false | ||
} | ||
|
||
resource "null_resource" "encrypt_symmetric" { | ||
|
||
provisioner "local-exec" { | ||
when = create | ||
command = <<EOF | ||
gcloud kms encrypt \ | ||
--key ${local.key} \ | ||
--keyring ${local.keyring} \ | ||
--location ${local.location} \ | ||
--ciphertext-file ./symmetric_encrypted_file \ | ||
--plaintext-file ./secret_file_sample.txt \ | ||
--project ${var.project_id} | ||
EOF | ||
} | ||
|
||
depends_on = [module.symmetric_kms] | ||
} |
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
59 changes: 59 additions & 0 deletions
59
tink-envelope-encryption-sample/3-reencrypt-symmetric-to-envelope/README.md
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,59 @@ | ||
# [3-reencrypt-symmetric-to-envelope module] Envelope encryption with Tink | ||
|
||
## Overview | ||
|
||
This module decrypts a file that was symmetrically encrypted using an existing GCP KMS CryptoKey, and re-encrypts it with envelope encryption using Tink and Terraform. | ||
|
||
## Prerequisites | ||
|
||
- [Terraform](https://developer.hashicorp.com/terraform/downloads); | ||
- [Google Cloud CLI (`gcloud`)](https://cloud.google.com/sdk/docs/install-sdk); | ||
- You must be authenticated in your GCP account. If you're not you should run `gcloud auth login`; | ||
- An existing [GCP project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project) with a KMS symmetric key created; | ||
- And a file encrypted with this KMS symmetric key; | ||
- [Go 1.22+](https://go.dev/dl/); | ||
|
||
## Deploy infrastructure | ||
|
||
1. Rename `terraform.example.tfvars` to `terraform.tfvars`: | ||
```sh | ||
mv terraform.example.tfvars terraform.tfvars | ||
``` | ||
|
||
1. Update `terraform.tfvars` file with the required values. | ||
|
||
1. Create the infrastructure. | ||
|
||
```sh | ||
terraform init | ||
terraform plan | ||
terraform apply | ||
``` | ||
|
||
1. The desired file is now decrypted. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| associated\_data | The associated data in Authenticated Encryption with Associated Data (AEAD) is used to tie ciphertext to specific associated data. Associated data is authenticated but NOT encrypted. | `string` | `"associated_data_sample"` | no | | ||
| cli\_path | CLI base path. | `string` | `"../"` | no | | ||
| current\_encrypted\_file\_path | Path to the symmetric encrypted file to be used. | `string` | n/a | yes | | ||
| current\_key | Key encryption name used to symmetric encryption. | `string` | n/a | yes | | ||
| current\_keyring | Keyring name used to symmetric encryption. | `string` | n/a | yes | | ||
| current\_project\_id | GCP project ID of the KMS used to symmetric encryption. | `string` | n/a | yes | | ||
| encrypted\_file\_path | Path to the encrypted file to be output by terraform. | `string` | `"./envelope_encrypted_file"` | no | | ||
| kek\_uri | KMS Key Encryption Key (KEK) URI. | `string` | n/a | yes | | ||
| location | Location for the resources used to symmetric encryption. | `string` | `"us-central1"` | no | | ||
| rotate\_encrypted\_file\_path | Path to the encrypted file to be used to envelope encryption on rotation. | `string` | `"./file_to_be_envelope_encrypted"` | no | | ||
| tink\_keyset\_file | Tink keyset file name. | `string` | n/a | yes | | ||
| tink\_sa\_credentials\_file | Service accounts credential file path required by Tink. | `string` | n/a | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| encrypted\_file\_path | Path to the encrypted file created by terraform. | | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
51 changes: 51 additions & 0 deletions
51
tink-envelope-encryption-sample/3-reencrypt-symmetric-to-envelope/main.tf
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,51 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
resource "null_resource" "decrypt_current_file" { | ||
|
||
triggers = { | ||
current_encrypted_file_path = var.current_encrypted_file_path | ||
rotate_encrypted_file_path = var.rotate_encrypted_file_path | ||
} | ||
|
||
provisioner "local-exec" { | ||
when = create | ||
command = <<EOF | ||
gcloud kms decrypt \ | ||
--key ${var.current_key} \ | ||
--keyring ${var.current_keyring} \ | ||
--location ${var.location} \ | ||
--ciphertext-file ${var.current_encrypted_file_path} \ | ||
--plaintext-file ${var.rotate_encrypted_file_path} \ | ||
--project ${var.current_project_id} | ||
EOF | ||
} | ||
|
||
} | ||
|
||
module "tink_encrypt" { | ||
source = "../1-encrypt" | ||
|
||
tink_keyset_file = var.tink_keyset_file | ||
kek_uri = var.kek_uri | ||
tink_sa_credentials_file = var.tink_sa_credentials_file | ||
input_file_path = var.rotate_encrypted_file_path | ||
encrypted_file_path = var.encrypted_file_path | ||
cli_path = var.cli_path | ||
associated_data = var.associated_data | ||
|
||
depends_on = [null_resource.decrypt_current_file] | ||
} |
20 changes: 20 additions & 0 deletions
20
tink-envelope-encryption-sample/3-reencrypt-symmetric-to-envelope/outputs.tf
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,20 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
output "encrypted_file_path" { | ||
description = "Path to the encrypted file created by terraform." | ||
value = module.tink_encrypt.encrypted_file_path | ||
} |
26 changes: 26 additions & 0 deletions
26
tink-envelope-encryption-sample/3-reencrypt-symmetric-to-envelope/terraform.example.tfvars
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,26 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
# You can find all these inputs values in 0-bootstrap and 1-encrypt modules outputs by running `terraform output` at the module path. | ||
|
||
current_project_id = "REPLACE-WITH-THE-PROJECT-ID-OF-YOUR-KMS-symmetric-KEY" | ||
current_key = "REPLACE-WITH-THE-NAME-OF-YOUR-symmetric-KEY" | ||
current_keyring = "REPLACE-WITH-THE-NAME-OF-YOUR-symmetric-KEYRING" | ||
current_encrypted_file_path = "REPLACE-WITH-YOUR-ENCRYPTED-FILE-PATH" | ||
kek_uri = "REPLACE-WITH-YOUR-KEK-URI" # Format expected: "gcp-kms://projects/PROJECT-ID/locations/us-central1/keyRings/KEYRING/cryptoKeys/CRYPTOKEY" | ||
tink_keyset_file = "REPLACE-WITH-YOUR-KEYSET-FILE-PATH" | ||
tink_sa_credentials_file = "REPLACE-WITH-YOUR-SA-CREDENTIALS-KEY-FILE-PATH" | ||
associated_data = "associated_data_sample" |
78 changes: 78 additions & 0 deletions
78
tink-envelope-encryption-sample/3-reencrypt-symmetric-to-envelope/variables.tf
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,78 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "tink_sa_credentials_file" { | ||
description = "Service accounts credential file path required by Tink." | ||
type = string | ||
} | ||
|
||
variable "rotate_encrypted_file_path" { | ||
description = "Path to the encrypted file to be used to envelope encryption on rotation." | ||
type = string | ||
default = "./file_to_be_envelope_encrypted" | ||
} | ||
|
||
variable "current_encrypted_file_path" { | ||
description = "Path to the symmetric encrypted file to be used." | ||
type = string | ||
} | ||
|
||
variable "cli_path" { | ||
description = "CLI base path." | ||
default = "../" | ||
} | ||
|
||
variable "associated_data" { | ||
description = "The associated data in Authenticated Encryption with Associated Data (AEAD) is used to tie ciphertext to specific associated data. Associated data is authenticated but NOT encrypted." | ||
default = "associated_data_sample" | ||
} | ||
|
||
variable "tink_keyset_file" { | ||
description = "Tink keyset file name." | ||
type = string | ||
} | ||
|
||
variable "kek_uri" { | ||
description = "KMS Key Encryption Key (KEK) URI." | ||
type = string | ||
} | ||
|
||
variable "current_project_id" { | ||
description = "GCP project ID of the KMS used to symmetric encryption." | ||
type = string | ||
} | ||
|
||
variable "location" { | ||
description = "Location for the resources used to symmetric encryption." | ||
type = string | ||
default = "us-central1" | ||
} | ||
|
||
variable "current_keyring" { | ||
description = "Keyring name used to symmetric encryption." | ||
type = string | ||
} | ||
|
||
variable "current_key" { | ||
description = "Key encryption name used to symmetric encryption." | ||
type = string | ||
} | ||
|
||
variable "encrypted_file_path" { | ||
description = "Path to the encrypted file to be output by terraform." | ||
type = string | ||
default = "./envelope_encrypted_file" | ||
} |
29 changes: 29 additions & 0 deletions
29
tink-envelope-encryption-sample/3-reencrypt-symmetric-to-envelope/versions.tf
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,29 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
terraform { | ||
required_version = ">= 1.5.7" | ||
required_providers { | ||
null = { | ||
source = "hashicorp/null" | ||
version = "3.2.2" | ||
} | ||
} | ||
|
||
provider_meta "google" { | ||
module_name = "blueprints/terraform/kms-solutions:tink-envelope-encryption-sample-decrypt/v0.1.0" | ||
} | ||
} |
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