Skip to content

Commit 4d400d7

Browse files
authored
feat: added support to configure Secrets Manager with the Event Notifications service using new input variables: enable_event_notification, existing_en_instance_crn, endpoint_type, skip_en_iam_authorization_policy.<br><br>**NOTE:** skip_iam_authorization_policy has been renamed to skip_kms_iam_authorization_policy to keep naming consistent now that module supports creating multiple auth policies. (#26)
1 parent d157f04 commit 4d400d7

File tree

6 files changed

+101
-11
lines changed

6 files changed

+101
-11
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,29 @@ You need the following permissions to run this module.
7676

7777
| Name | Type |
7878
|------|------|
79-
| [ibm_iam_authorization_policy.policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
79+
| [ibm_iam_authorization_policy.en_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
80+
| [ibm_iam_authorization_policy.kms_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
8081
| [ibm_resource_instance.secrets_manager_instance](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/resource_instance) | resource |
82+
| [ibm_sm_en_registration.sm_en_registration](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_en_registration) | resource |
8183
| [time_sleep.wait_for_authorization_policy](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
8284

8385
### Inputs
8486

8587
| Name | Description | Type | Default | Required |
8688
|------|-------------|------|---------|:--------:|
8789
| <a name="input_cbr_rules"></a> [cbr\_rules](#input\_cbr\_rules) | (Optional, list) List of CBR rules to create | <pre>list(object({<br> description = string<br> account_id = string<br> rule_contexts = list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> }))) }))<br> enforcement_mode = string<br> }))</pre> | `[]` | no |
88-
| <a name="input_existing_kms_instance_guid"></a> [existing\_kms\_instance\_guid](#input\_existing\_kms\_instance\_guid) | The GUID of the Hyper Protect Crypto Services or Key Protect instance in which the key specified in `kms_key_crn` is coming from. Required only if `kms_encryption_enabled` is set to true, and `skip_iam_authorization_policy` is set to false. | `string` | `null` | no |
90+
| <a name="input_enable_event_notification"></a> [enable\_event\_notification](#input\_enable\_event\_notification) | Set this to true to enable lifecycle notifications for your Secrets Manager instance by connecting an Event Notifications service. When setting this to true, a value must be passed for `existing_en_instance_crn` variable. | `bool` | `false` | no |
91+
| <a name="input_endpoint_type"></a> [endpoint\_type](#input\_endpoint\_type) | The endpoint type of the Secrets Manager instance required for establishing a connection between an Event Notifications service. Possible values are `public`, `private`. | `string` | `"public"` | no |
92+
| <a name="input_existing_en_instance_crn"></a> [existing\_en\_instance\_crn](#input\_existing\_en\_instance\_crn) | The CRN of the Event Notifications service to enable lifecycle notifications for your Secrets Manager instance. | `string` | `null` | no |
93+
| <a name="input_existing_kms_instance_guid"></a> [existing\_kms\_instance\_guid](#input\_existing\_kms\_instance\_guid) | The GUID of the Hyper Protect Crypto Services or Key Protect instance in which the key specified in `kms_key_crn` is coming from. Required only if `kms_encryption_enabled` is set to true, and `skip_kms_iam_authorization_policy` is set to false. | `string` | `null` | no |
8994
| <a name="input_kms_encryption_enabled"></a> [kms\_encryption\_enabled](#input\_kms\_encryption\_enabled) | Set this to true to control the encryption keys used to encrypt the data that you store in Secrets Manager. If set to false, the data that you store is encrypted at rest by using envelope encryption. For more details, see https://cloud.ibm.com/docs/secrets-manager?topic=secrets-manager-mng-data&interface=ui#about-encryption. | `bool` | `false` | no |
9095
| <a name="input_kms_key_crn"></a> [kms\_key\_crn](#input\_kms\_key\_crn) | The root key CRN of a Key Management Service like Key Protect or Hyper Protect Crypto Services (HPCS) that you want to use for encryption. Only used if `kms_encryption_enabled` is set to true. | `string` | `null` | no |
9196
| <a name="input_region"></a> [region](#input\_region) | The region to provision the Secrets Manager instance to. | `string` | n/a | yes |
9297
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The ID of the resource group to provision the Secrets Manager instance to. | `string` | n/a | yes |
9398
| <a name="input_secrets_manager_name"></a> [secrets\_manager\_name](#input\_secrets\_manager\_name) | The name to give the Secrets Manager instance. | `string` | n/a | yes |
9499
| <a name="input_service_endpoints"></a> [service\_endpoints](#input\_service\_endpoints) | The types of service endpoints to set on the Secrets Manager instance. Possible values are `public`, `private` or `public-and-private`. | `string` | `"public-and-private"` | no |
95-
| <a name="input_skip_iam_authorization_policy"></a> [skip\_iam\_authorization\_policy](#input\_skip\_iam\_authorization\_policy) | Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances in the resource group to read the encryption key from the KMS instance. If set to false, pass in a value for the KMS instance in the `existing_kms_instance_guid` variable. In addition, no policy is created if `kms_encryption_enabled` is set to false. | `bool` | `false` | no |
100+
| <a name="input_skip_en_iam_authorization_policy"></a> [skip\_en\_iam\_authorization\_policy](#input\_skip\_en\_iam\_authorization\_policy) | Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances (scoped to the resource group) an 'Event Source Manager' role to the given Event Notifications instance passed in the `existing_en_instance_crn` input variable. In addition, no policy is created if `enable_event_notification` is set to false. | `bool` | `false` | no |
101+
| <a name="input_skip_kms_iam_authorization_policy"></a> [skip\_kms\_iam\_authorization\_policy](#input\_skip\_kms\_iam\_authorization\_policy) | Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances in the resource group to read the encryption key from the KMS instance. If set to false, pass in a value for the KMS instance in the `existing_kms_instance_guid` variable. In addition, no policy is created if `kms_encryption_enabled` is set to false. | `bool` | `false` | no |
96102
| <a name="input_sm_service_plan"></a> [sm\_service\_plan](#input\_sm\_service\_plan) | The Secrets Manager plan to provision. | `string` | `"standard"` | no |
97103
| <a name="input_sm_tags"></a> [sm\_tags](#input\_sm\_tags) | The list of resource tags that you want to associate with your Secrets Manager instance. | `list(string)` | `[]` | no |
98104

examples/complete/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ module "key_protect" {
1717
}
1818
}
1919

20+
module "event_notification" {
21+
source = "terraform-ibm-modules/event-notifications/ibm"
22+
version = "1.0.4"
23+
resource_group_id = module.resource_group.resource_group_id
24+
name = "${var.prefix}-en"
25+
tags = var.resource_tags
26+
plan = "lite"
27+
service_endpoints = "public"
28+
region = var.en_region
29+
}
30+
2031
module "secrets_manager" {
2132
source = "../.."
2233
resource_group_id = module.resource_group.resource_group_id
@@ -28,4 +39,6 @@ module "secrets_manager" {
2839
kms_encryption_enabled = true
2940
existing_kms_instance_guid = module.key_protect.key_protect_guid
3041
kms_key_crn = module.key_protect.keys["${var.prefix}-sm.${var.prefix}-sm-key"].crn
42+
enable_event_notification = true
43+
existing_en_instance_crn = module.event_notification.crn
3144
}

examples/complete/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ variable "resource_tags" {
3232
description = "Optional list of tags to be added to created resources"
3333
default = []
3434
}
35+
36+
variable "en_region" {
37+
type = string
38+
description = "Region where event notification will be created"
39+
default = "au-syd"
40+
}

main.tf

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ locals {
1212
# tflint-ignore: terraform_unused_declarations
1313
validate_kms_vars = var.kms_encryption_enabled && var.kms_key_crn == null ? tobool("When setting var.kms_encryption_enabled to true, a value must be passed for var.kms_key_crn") : true
1414
# tflint-ignore: terraform_unused_declarations
15-
validate_auth_policy = var.kms_encryption_enabled && var.skip_iam_authorization_policy == false && var.existing_kms_instance_guid == null ? tobool("When var.skip_iam_authorization_policy is set to false, and var.kms_encryption_enabled to true, a value must be passed for var.existing_kms_instance_guid in order to create the auth policy.") : true
16-
15+
validate_auth_policy = var.kms_encryption_enabled && var.skip_kms_iam_authorization_policy == false && var.existing_kms_instance_guid == null ? tobool("When var.skip_kms_iam_authorization_policy is set to false, and var.kms_encryption_enabled to true, a value must be passed for var.existing_kms_instance_guid in order to create the auth policy.") : true
16+
# tflint-ignore: terraform_unused_declarations
17+
validate_event_notification = var.enable_event_notification && var.existing_en_instance_crn == null ? tobool("When setting var.enable_event_notification to true, a value must be passed for var.existing_en_instance_crn") : true
18+
# tflint-ignore: terraform_unused_declarations
19+
validate_endpoint = var.enable_event_notification && (var.endpoint_type == "public" && var.service_endpoints == "private") || (var.endpoint_type == "private" && var.service_endpoints == "public") ? tobool("It is not allowed to have conflicting var.endpoint_type and var.service_endpoints values.") : true
1720
}
1821

1922
# Create Secrets Manager Instance
2023
resource "ibm_resource_instance" "secrets_manager_instance" {
21-
depends_on = [ibm_iam_authorization_policy.policy]
24+
depends_on = [ibm_iam_authorization_policy.kms_policy]
2225
name = var.secrets_manager_name
2326
service = "secrets-manager"
2427
service_endpoints = var.service_endpoints
@@ -46,8 +49,8 @@ locals {
4649
) : null
4750
}
4851

49-
resource "ibm_iam_authorization_policy" "policy" {
50-
count = var.kms_encryption_enabled && !var.skip_iam_authorization_policy ? 1 : 0
52+
resource "ibm_iam_authorization_policy" "kms_policy" {
53+
count = var.kms_encryption_enabled && !var.skip_kms_iam_authorization_policy ? 1 : 0
5154
source_service_name = "secrets-manager"
5255
source_resource_group_id = var.resource_group_id
5356
target_service_name = local.kms_service_name
@@ -58,7 +61,7 @@ resource "ibm_iam_authorization_policy" "policy" {
5861

5962
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
6063
resource "time_sleep" "wait_for_authorization_policy" {
61-
depends_on = [ibm_iam_authorization_policy.policy]
64+
depends_on = [ibm_iam_authorization_policy.kms_policy, ibm_iam_authorization_policy.en_policy]
6265

6366
create_duration = "30s"
6467
}
@@ -103,3 +106,29 @@ module "cbr_rule" {
103106
}]
104107
}]
105108
}
109+
110+
##############################################################################
111+
# Event Notification
112+
##############################################################################
113+
114+
# Create IAM Authorization Policies to allow SM to access event notification
115+
resource "ibm_iam_authorization_policy" "en_policy" {
116+
count = var.enable_event_notification == false || var.skip_en_iam_authorization_policy ? 0 : 1
117+
source_service_name = "secrets-manager"
118+
source_resource_group_id = var.resource_group_id
119+
target_service_name = "event-notifications"
120+
target_resource_instance_id = regex(".*:(.*)::", var.existing_en_instance_crn)[0]
121+
roles = ["Event Source Manager"]
122+
description = "Allow all Secrets Manager instances in the resource group ${var.resource_group_id} 'Event Source Manager' role access on the Event Notification instance GUID ${regex(".*:(.*)::", var.existing_en_instance_crn)[0]}"
123+
}
124+
125+
resource "ibm_sm_en_registration" "sm_en_registration" {
126+
count = var.enable_event_notification ? 1 : 0
127+
depends_on = [time_sleep.wait_for_authorization_policy]
128+
instance_id = local.secrets_manager_guid
129+
region = var.region
130+
event_notifications_instance_crn = var.existing_en_instance_crn
131+
event_notifications_source_description = "Secret Manager"
132+
event_notifications_source_name = var.secrets_manager_name
133+
endpoint_type = var.endpoint_type
134+
}

moved.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
moved {
2+
from = ibm_iam_authorization_policy.policy
3+
to = ibm_iam_authorization_policy.kms_policy
4+
}

variables.tf

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ variable "kms_encryption_enabled" {
4949
default = false
5050
}
5151

52-
variable "skip_iam_authorization_policy" {
52+
variable "skip_kms_iam_authorization_policy" {
5353
type = bool
5454
description = "Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances in the resource group to read the encryption key from the KMS instance. If set to false, pass in a value for the KMS instance in the `existing_kms_instance_guid` variable. In addition, no policy is created if `kms_encryption_enabled` is set to false."
5555
default = false
5656
}
5757

5858
variable "existing_kms_instance_guid" {
5959
type = string
60-
description = "The GUID of the Hyper Protect Crypto Services or Key Protect instance in which the key specified in `kms_key_crn` is coming from. Required only if `kms_encryption_enabled` is set to true, and `skip_iam_authorization_policy` is set to false."
60+
description = "The GUID of the Hyper Protect Crypto Services or Key Protect instance in which the key specified in `kms_key_crn` is coming from. Required only if `kms_encryption_enabled` is set to true, and `skip_kms_iam_authorization_policy` is set to false."
6161
default = null
6262
}
6363

@@ -86,3 +86,35 @@ variable "cbr_rules" {
8686
default = []
8787
# Validation happens in the rule module
8888
}
89+
90+
##############################################################################
91+
# Event Notification
92+
##############################################################################
93+
94+
variable "skip_en_iam_authorization_policy" {
95+
type = bool
96+
description = "Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances (scoped to the resource group) an 'Event Source Manager' role to the given Event Notifications instance passed in the `existing_en_instance_crn` input variable. In addition, no policy is created if `enable_event_notification` is set to false."
97+
default = false
98+
}
99+
100+
variable "enable_event_notification" {
101+
type = bool
102+
default = false
103+
description = "Set this to true to enable lifecycle notifications for your Secrets Manager instance by connecting an Event Notifications service. When setting this to true, a value must be passed for `existing_en_instance_crn` variable."
104+
}
105+
106+
variable "existing_en_instance_crn" {
107+
type = string
108+
default = null
109+
description = "The CRN of the Event Notifications service to enable lifecycle notifications for your Secrets Manager instance."
110+
}
111+
112+
variable "endpoint_type" {
113+
type = string
114+
description = "The endpoint type of the Secrets Manager instance required for establishing a connection between an Event Notifications service. Possible values are `public`, `private`."
115+
default = "public"
116+
validation {
117+
condition = contains(["public", "private"], var.endpoint_type)
118+
error_message = "The specified endpoint_type is not a valid selection!"
119+
}
120+
}

0 commit comments

Comments
 (0)