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

feat: Add usage example for monitoring alerts on KMS key versions #162

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fd6ff3d
Introduce autokey feature
romanini-ciandt Aug 29, 2024
fba57c4
Adjusts on setup
romanini-ciandt Aug 29, 2024
8c4df14
Merge branch 'master' of github.com:romanini-ciandt/terraform-google-…
romanini-ciandt Aug 29, 2024
68dc943
Adjust docs
romanini-ciandt Aug 29, 2024
8a94f7e
Adjust permissions
romanini-ciandt Aug 30, 2024
275bed5
Make module more readable
romanini-ciandt Aug 30, 2024
4a018d5
Add tests
romanini-ciandt Aug 30, 2024
b173b10
Add more tests
romanini-ciandt Aug 30, 2024
35b48a9
Turn autokey an independent submodule
romanini-ciandt Sep 5, 2024
f0874c1
README adjusts
romanini-ciandt Sep 5, 2024
f5eb555
Merge branch 'master' into feat/add-autokey
romanini-ciandt Sep 6, 2024
78bddd7
Description updates
romanini-ciandt Sep 9, 2024
ad5d895
Merge branch 'feat/add-autokey' of github.com:romanini-ciandt/terrafo…
romanini-ciandt Sep 9, 2024
8d665de
Adjust tag value
romanini-ciandt Sep 9, 2024
e2598d0
Update output type
romanini-ciandt Sep 9, 2024
b4bd45f
Add autokey README
romanini-ciandt Sep 10, 2024
5c4d195
Fix typo
romanini-ciandt Sep 10, 2024
07c34f5
Hardcode location in tests
romanini-ciandt Sep 10, 2024
2c7e6fd
Add an importing plan README
romanini-ciandt Sep 12, 2024
73bfbba
Add missing init
romanini-ciandt Sep 12, 2024
c6c99a2
Merge branch 'master' into feat/add-autokey-plus-migration
romanini-ciandt Sep 24, 2024
ed14225
Add specific instructions to import existing state
romanini-ciandt Sep 26, 2024
e769c63
Partial working version
romanini-ciandt Sep 27, 2024
e587109
Turn bash commands into scripts
romanini-ciandt Sep 27, 2024
0a3dbda
Remove comments
romanini-ciandt Sep 27, 2024
72efd29
Fix lint errors
romanini-ciandt Sep 27, 2024
91eccaf
Revert an unwanted change
romanini-ciandt Sep 27, 2024
9172091
Fix typo
romanini-ciandt Oct 4, 2024
c44d574
Add comment to explain string op
romanini-ciandt Oct 15, 2024
61448ee
Merge branch 'master' of github.com:romanini-ciandt/terraform-google-kms
romanini-ciandt Oct 15, 2024
1d90759
Merge branch 'master' of github.com:romanini-ciandt/terraform-google-kms
romanini-ciandt Nov 7, 2024
090cf4a
Alert implemented on examples
romanini-ciandt Nov 7, 2024
67f2d65
Add integration test
romanini-ciandt Nov 8, 2024
693ac7c
Fix versions
romanini-ciandt Nov 8, 2024
1b19ca7
Implement a custom backend
romanini-ciandt Nov 8, 2024
3cb7cfc
Simplify filter string
romanini-ciandt Nov 8, 2024
144ff16
Remove default from example
romanini-ciandt Nov 8, 2024
699f2ed
Rollback file
romanini-ciandt Nov 8, 2024
95469f2
Turn params into a map
romanini-ciandt Nov 8, 2024
091d93a
Attempt without additional index
romanini-ciandt Nov 11, 2024
8f2a480
Adjust type
romanini-ciandt Nov 11, 2024
ac332a1
Fix broken lint
romanini-ciandt Nov 11, 2024
416b34d
Change approach to use fixtures on tests
romanini-ciandt Nov 12, 2024
375d688
Adjust test
romanini-ciandt Nov 12, 2024
90ad6c7
Add project into kms destroy command
romanini-ciandt Nov 12, 2024
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
2 changes: 1 addition & 1 deletion examples/autokey_example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

module "autokey" {
source = "terraform-google-modules/kms/google//modules/autokey"
version = "3.1.0"
version = "~> 3.2"

project_id = var.project_id
autokey_folder_number = var.folder_id
Expand Down
3 changes: 2 additions & 1 deletion examples/import_only_example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ resource "random_pet" "main" {
}

module "kms" {
source = "../.."
source = "terraform-google-modules/kms/google"
version = "~> 3.2"

project_id = var.project_id
keyring = random_pet.main.id
Expand Down
23 changes: 23 additions & 0 deletions examples/monitoring_alerts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Monitoring Alert Example

This example provides monitoring e-mail alerts for KMS key versions scheduled for destruction. If multiple key versions are deleted in less than 5 minutes a single notification will be sent.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| email\_addresses\_to\_be\_notified | Email addresses used for sending notifications to. | `list(string)` | n/a | yes |
| monitor\_all\_keys\_in\_the\_project | True for all KMS key versions under the same project to be monitored, false for only the KMS key version created in this example to be monitored. Default: false. | `bool` | n/a | yes |
| project\_id | The ID of the project in which to provision resources. | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| key | The version of the created KMS key. |
| keyring | The keyring created. |
| notification\_channel\_names | Notification channel names. |
| project\_id | GCP Project ID where key version was created. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
80 changes: 80 additions & 0 deletions examples/monitoring_alerts/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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.
*/

/**
* Send a warning email when a KMS key version is scheduled for destruction.
* If multiple key versions are deleted in less than 5 minutes a single notification will be sent.
*/

locals {
all_keys_filter = "protoPayload.request.@type=\"type.googleapis.com/google.cloud.kms.v1.DestroyCryptoKeyVersionRequest\""
single_key_filter = "${local.all_keys_filter} AND protoPayload.request.name=~\"${values(module.kms.keys)[0]}/.*\""
}

resource "random_string" "suffix" {
length = 4
special = false
upper = false
}

module "kms" {
source = "terraform-google-modules/kms/google"
version = "~> 3.2"

project_id = var.project_id
keyring = "alert-keyring-${random_string.suffix.result}"
location = "us-central1"
keys = ["alert-key"]
prevent_destroy = false
}

resource "google_monitoring_alert_policy" "main" {
project = var.project_id
display_name = "KMS Key Version Destruction Alert"
documentation {
content = "KMS Key Version alert: one or more key versions from ${var.project_id} project were scheduled for destruction."
}
combiner = "OR"
conditions {
display_name = "Destroy condition"
condition_matched_log {
filter = var.monitor_all_keys_in_the_project ? local.all_keys_filter : local.single_key_filter
}
}

alert_strategy {
notification_rate_limit {
period = "300s"
}
}

notification_channels = [for email_ch in google_monitoring_notification_channel.email_channel : email_ch.name]

severity = "WARNING"
}

resource "google_monitoring_notification_channel" "email_channel" {
for_each = toset(var.email_addresses_to_be_notified)

project = var.project_id
display_name = "KMS version scheduled for destruction alert channel"
type = "email"
description = "Sends email notifications for KMS key versions scheduled for destruction alerts"

labels = {
email_address = each.value
}
}
35 changes: 35 additions & 0 deletions examples/monitoring_alerts/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 "key" {
value = values(module.kms.keys)[0]
description = "The version of the created KMS key."
}

output "keyring" {
value = module.kms.keyring_name
description = "The keyring created."
}

output "project_id" {
value = var.project_id
description = "GCP Project ID where key version was created."
}

output "notification_channel_names" {
value = [for channel in google_monitoring_notification_channel.email_channel : channel.name]
description = "Notification channel names."
}
30 changes: 30 additions & 0 deletions examples/monitoring_alerts/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 "project_id" {
description = "The ID of the project in which to provision resources."
type = string
}

variable "monitor_all_keys_in_the_project" {
type = bool
description = "True for all KMS key versions under the same project to be monitored, false for only the KMS key version created in this example to be monitored. Default: false."
}

variable "email_addresses_to_be_notified" {
type = list(string)
description = "Email addresses used for sending notifications to."
}
23 changes: 23 additions & 0 deletions test/fixtures/monitoring_alerts_on_project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

module "monitoring_alert_on_project" {
source = "../../../examples/monitoring_alerts"

monitor_all_keys_in_the_project = true
project_id = var.project_id
email_addresses_to_be_notified = ["[email protected]", "[email protected]"]
}
35 changes: 35 additions & 0 deletions test/fixtures/monitoring_alerts_on_project/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 "key" {
value = module.monitoring_alert_on_project.key
description = "The version of the created KMS key."
}

output "keyring" {
value = module.monitoring_alert_on_project.keyring
description = "The keyring created."
}

output "project_id" {
value = module.monitoring_alert_on_project.project_id
description = "GCP Project ID where key version was created."
}

output "notification_channel_names" {
value = module.monitoring_alert_on_project.notification_channel_names
description = "Notification channel names."
}
20 changes: 20 additions & 0 deletions test/fixtures/monitoring_alerts_on_project/variables.tf
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.
*/

variable "project_id" {
description = "The ID of the project in which to provision resources."
type = string
}
23 changes: 23 additions & 0 deletions test/fixtures/monitoring_alerts_specific_key/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

module "monitoring_alert_specific_key" {
source = "../../../examples/monitoring_alerts"

monitor_all_keys_in_the_project = false
project_id = var.project_id
email_addresses_to_be_notified = ["[email protected]", "[email protected]"]
}
35 changes: 35 additions & 0 deletions test/fixtures/monitoring_alerts_specific_key/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 "key" {
value = module.monitoring_alert_specific_key.key
description = "The version of the created KMS key."
}

output "keyring" {
value = module.monitoring_alert_specific_key.keyring
description = "The keyring created."
}

output "project_id" {
value = module.monitoring_alert_specific_key.project_id
description = "GCP Project ID where key version was created."
}

output "notification_channel_names" {
value = module.monitoring_alert_specific_key.notification_channel_names
description = "Notification channel names."
}
20 changes: 20 additions & 0 deletions test/fixtures/monitoring_alerts_specific_key/variables.tf
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.
*/

variable "project_id" {
description = "The ID of the project in which to provision resources."
type = string
}
Loading