Skip to content

Commit c7e5e8e

Browse files
feat: added encryption options (#33)
BREAKING CHANGE: cos bucket will be destroyed if enabling encryption during upgrade
1 parent a0af4a6 commit c7e5e8e

File tree

10 files changed

+324
-105
lines changed

10 files changed

+324
-105
lines changed

.secrets.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2022-11-09T13:08:40Z",
6+
"generated_at": "2022-11-11T12:06:12Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ You need the following permissions to run this module.
8181

8282
## Modules
8383

84-
No modules.
84+
| Name | Source | Version |
85+
|------|--------|---------|
86+
| <a name="module_kp_all_inclusive"></a> [kp\_all\_inclusive](#module\_kp\_all\_inclusive) | git::https://github.com/terraform-ibm-modules/terraform-ibm-key-protect-all-inclusive.git | v1.0.0 |
8587

8688
## Resources
8789

@@ -92,7 +94,6 @@ No modules.
9294
| [ibm_iam_authorization_policy.policy](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
9395
| [ibm_resource_instance.cos_instance](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_instance) | resource |
9496
| [ibm_resource_instance.cos_instance](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/resource_instance) | data source |
95-
| [ibm_resource_instance.keyprotect_instance](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/resource_instance) | data source |
9697

9798
## Inputs
9899

@@ -103,14 +104,18 @@ No modules.
103104
| <a name="input_archive_type"></a> [archive\_type](#input\_archive\_type) | Specifies the storage class or archive type to which you want the object to transition. | `string` | `"Glacier"` | no |
104105
| <a name="input_bucket_infix"></a> [bucket\_infix](#input\_bucket\_infix) | Custom infix for use in cos bucket name (Optional) | `string` | `null` | no |
105106
| <a name="input_cos_instance_name"></a> [cos\_instance\_name](#input\_cos\_instance\_name) | Name of the cos instance where the bucket should be created | `string` | `null` | no |
107+
| <a name="input_cos_key_name"></a> [cos\_key\_name](#input\_cos\_key\_name) | List of strings containing the list of desired Key Protect Key names as the values for each Key Ring, this Key Protect Key is used to encrypt the data in the COS Bucket | `list(string)` | <pre>[<br> "cos-key"<br>]</pre> | no |
108+
| <a name="input_cos_key_ring_name"></a> [cos\_key\_ring\_name](#input\_cos\_key\_ring\_name) | A String containing the desired Key Ring Names as the key of the map for the key protect instance, this Key Protect Key is used to encrypt the data in the COS Bucket | `string` | `"cos-key-ring"` | no |
106109
| <a name="input_cos_location"></a> [cos\_location](#input\_cos\_location) | Location of the cloud object storage instance | `string` | `"global"` | no |
107110
| <a name="input_cos_plan"></a> [cos\_plan](#input\_cos\_plan) | Plan to be used for creating cloud object storage instance | `string` | `"standard"` | no |
108111
| <a name="input_create_cos_instance"></a> [create\_cos\_instance](#input\_create\_cos\_instance) | Set as true to create a new Cloud Object Storage instance | `bool` | `true` | no |
112+
| <a name="input_create_key_protect_instance"></a> [create\_key\_protect\_instance](#input\_create\_key\_protect\_instance) | Set as true to create a new Key Protect instance, this instance will store the Key used to encrypt the data in the COS Bucket | `bool` | `true` | no |
113+
| <a name="input_create_key_protect_key"></a> [create\_key\_protect\_key](#input\_create\_key\_protect\_key) | Set as true to create a new Key Protect Key, this Key Protect Key is used to encrypt the COS Bucket | `bool` | `true` | no |
109114
| <a name="input_encryption_enabled"></a> [encryption\_enabled](#input\_encryption\_enabled) | Set as true to use Key Protect encryption to encrypt data in COS bucket | `bool` | `true` | no |
110115
| <a name="input_environment_name"></a> [environment\_name](#input\_environment\_name) | Prefix name for all related resources | `string` | n/a | yes |
111116
| <a name="input_expire_days"></a> [expire\_days](#input\_expire\_days) | Specifies the number of days when the expire rule action takes effect. | `number` | `365` | no |
112-
| <a name="input_key_protect_instance_name"></a> [key\_protect\_instance\_name](#input\_key\_protect\_instance\_name) | Name of an existing Key Protect instance to use, this instance will store the Key used to encrypt the data in the COS Bucket | `string` | `null` | no |
113-
| <a name="input_key_protect_key_crn"></a> [key\_protect\_key\_crn](#input\_key\_protect\_key\_crn) | CRN of the Key Protect Key to use, this Key Protect Key is used to encrypt the data in the COS Bucket | `string` | `null` | no |
117+
| <a name="input_key_protect_instance_name"></a> [key\_protect\_instance\_name](#input\_key\_protect\_instance\_name) | Name to set as the instance name if creating a Key Protect instance, otherwise name of an existing Key Protect instance to use, this instance will store the Key used to encrypt the data in the COS Bucket | `string` | `null` | no |
118+
| <a name="input_key_protect_key_crn"></a> [key\_protect\_key\_crn](#input\_key\_protect\_key\_crn) | CRN of the Key Protect Key to use if not creating a Key in this module, this Key Protect Key is used to encrypt the data in the COS Bucket | `string` | `null` | no |
114119
| <a name="input_object_versioning_enabled"></a> [object\_versioning\_enabled](#input\_object\_versioning\_enabled) | Enable object versioning to keep multiple versions of an object in a bucket. Cannot be used with retention rule. | `bool` | `false` | no |
115120
| <a name="input_region"></a> [region](#input\_region) | Name of the Region to deploy in to | `string` | `"us-south"` | no |
116121
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where the environment will be created | `string` | n/a | yes |
@@ -128,6 +133,7 @@ No modules.
128133
| <a name="output_bucket_id"></a> [bucket\_id](#output\_bucket\_id) | Bucket id |
129134
| <a name="output_bucket_name"></a> [bucket\_name](#output\_bucket\_name) | Bucket Name |
130135
| <a name="output_cos_instance_id"></a> [cos\_instance\_id](#output\_cos\_instance\_id) | The GUID of the Cloud Object Storage Instance where the buckets are created |
136+
| <a name="output_key_protect_instance_id"></a> [key\_protect\_instance\_id](#output\_key\_protect\_instance\_id) | The GUID of the Key Protect Instance where the Key to encrypt the COS Bucket is stored |
131137
| <a name="output_key_protect_key_crn"></a> [key\_protect\_key\_crn](#output\_key\_protect\_key\_crn) | The CRN of the Key Protect Key used to encrypt the COS Bucket |
132138
| <a name="output_resource_group_id"></a> [resource\_group\_id](#output\_resource\_group\_id) | Resource Group ID |
133139
| <a name="output_s3_endpoint_private"></a> [s3\_endpoint\_private](#output\_s3\_endpoint\_private) | S3 private endpoint |

examples/bucket-without-tracking-monitoring/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ module "resource_group" {
77

88
# Create COS bucket with:
99
# - Retention
10+
# - Encryption
1011
# Create COS bucket without:
1112
# - Monitoring
1213
# - Activity Tracking
13-
# - Encryption
14+
1415
module "cos" {
1516
source = "../../"
1617
environment_name = "${var.prefix}-${var.environment_name}-cos"
1718
resource_group_id = module.resource_group.resource_group_id
1819
region = var.region
19-
encryption_enabled = false
20+
encryption_enabled = true
2021
}

examples/bucket-without-tracking-monitoring/providers.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,19 @@
22
provider "ibm" {
33
ibmcloud_api_key = var.ibmcloud_api_key
44
}
5+
6+
# used by the restapi provider to authenticate the API call based on API key
7+
data "ibm_iam_auth_token" "token_data" {
8+
}
9+
10+
provider "restapi" {
11+
uri = "https:"
12+
write_returns_object = false
13+
create_returns_object = false
14+
debug = false # set to true to show detailed logs, but use carefully as it might print sensitive values.
15+
headers = {
16+
Authorization = data.ibm_iam_auth_token.token_data.iam_access_token
17+
Bluemix-Instance = module.cos.key_protect_instance_id
18+
Content-Type = "application/vnd.ibm.kms.policy+json"
19+
}
20+
}

examples/bucket-without-tracking-monitoring/version.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ terraform {
66
source = "ibm-cloud/ibm"
77
version = "1.45.0"
88
}
9+
restapi = {
10+
source = "Mastercard/restapi"
11+
version = "1.17.0"
12+
}
913
}
1014
}

main.tf

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,36 @@ locals {
1919
# tflint-ignore: terraform_unused_declarations
2020
cos_validate_check = regex("^${local.cos_validate_msg}$", (!local.cos_validate_condition ? local.cos_validate_msg : ""))
2121

22+
# only allow create_key_protect_key or key_protect_key_crn to be passed
23+
kp_key_validate_condition = var.encryption_enabled && ((var.create_key_protect_key && var.key_protect_key_crn != null) || (!var.create_key_protect_key && var.key_protect_key_crn == null))
24+
kp_key_validate_msg = "Value for 'create_key_protect_key' cannot be true if 'key_protect_key_crn' is not null"
25+
# tflint-ignore: terraform_unused_declarations
26+
kp_key_validate_check = regex("^${local.kp_key_validate_msg}$", (!local.kp_key_validate_condition ? local.kp_key_validate_msg : ""))
27+
28+
# ensure if kms_key_crn is passed the create_kms_instance is false
29+
kp_key_instance_validate_condition = var.encryption_enabled && (var.key_protect_key_crn != null && var.create_key_protect_instance)
30+
kp_key_instance_validate_msg = "Value for 'key_protect_key_crn' must be null if instance is created by the module"
31+
# tflint-ignore: terraform_unused_declarations
32+
kp_key_instance_validate_check = regex("^${local.kp_key_instance_validate_msg}$", (!local.kp_key_instance_validate_condition ? local.kp_key_instance_validate_msg : ""))
33+
key_map = tomap({
34+
# tflint-ignore: terraform_deprecated_interpolation
35+
"${var.cos_key_ring_name}" : "${var.cos_key_name}"
36+
})
37+
38+
key_crn = (var.encryption_enabled && var.create_key_protect_key) ? module.kp_all_inclusive[0].keys["${var.cos_key_ring_name}.${var.cos_key_name[0]}"].crn : var.key_protect_key_crn
2239
}
2340

24-
locals {
25-
key_crn = var.encryption_enabled ? var.key_protect_key_crn : ""
41+
# Module to create key protect instance or create keys if key protect instance is provided.
42+
# This module will be executed if encryption_enabled is set to true
43+
module "kp_all_inclusive" {
44+
count = (var.encryption_enabled && var.create_key_protect_key) ? 1 : 0
45+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-key-protect-all-inclusive.git?ref=v1.0.0"
46+
resource_group_id = var.resource_group_id
47+
region = var.region
48+
prefix = var.environment_name
49+
key_protect_instance_name = var.key_protect_instance_name == null ? "${var.environment_name}-kp" : var.key_protect_instance_name
50+
create_key_protect_instance = var.create_key_protect_instance
51+
key_map = local.key_map
2652
}
2753

2854
# Resource to create COS instance if create_cos_instance is true
@@ -50,23 +76,21 @@ data "ibm_resource_instance" "cos_instance" {
5076
service = "cloud-object-storage"
5177
}
5278

53-
# Data source to retrieve the key protect guid
54-
data "ibm_resource_instance" "keyprotect_instance" {
55-
count = var.key_protect_instance_name == null ? 0 : 1
56-
name = var.key_protect_instance_name
57-
}
58-
5979
# Create IAM Access Policy to allow Key protect to access COS instance
6080
resource "ibm_iam_authorization_policy" "policy" {
6181
count = local.create_access_policy ? 1 : 0
6282
source_service_name = "cloud-object-storage"
6383
source_resource_instance_id = local.cos_instance_guid
6484
target_service_name = "kms"
65-
target_resource_instance_id = data.ibm_resource_instance.keyprotect_instance[0].guid
85+
target_resource_instance_id = module.kp_all_inclusive[0].key_protect_guid
6686
roles = ["Reader"]
6787
}
6888

69-
# Create COS bucket
89+
# Create COS bucket with:
90+
# - Retention
91+
# - Encryption
92+
# - Monitoring
93+
# - Activity Tracking
7094
resource "ibm_cos_bucket" "cos_bucket" {
7195
count = var.encryption_enabled ? 1 : 0
7296
depends_on = [ibm_iam_authorization_policy.policy]

0 commit comments

Comments
 (0)