Skip to content

Commit

Permalink
feat: removed kms_region variable - its now programmtically determi…
Browse files Browse the repository at this point in the history
…ned<br>- `existing_kms_guid` has been renamed `existing_kms_instance_crn`<br>- `existing_resource_group` has been renamed `use_existing_resource_group` (#52)
  • Loading branch information
akocbek authored Apr 11, 2024
1 parent 7423c64 commit 32a3bba
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cra-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ CRA_TARGETS:
PROFILE_ID: "bfacb71d-4b84-41ac-9825-e8a3a3eb7405" # SCC profile ID (currently set to IBM Cloud Framework for Financial Services 1.6.0 profile).
CRA_ENVIRONMENT_VARIABLES:
TF_VAR_resource_group_name: "test"
TF_VAR_existing_kms_guid: "XXXXXXXXXXXXXXXX"
TF_VAR_existing_kms_instance_crn: "crn:v1:bluemix:public:hs-crypto:us-south:a/xxx:xxxx::"
6 changes: 6 additions & 0 deletions ibm_catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
}
]
},
"configuration" : [
{
"key": "existing_scc_cos_kms_key_crn",
"required": true
}
],
"iam_permissions": [
{
"service_name": "compliance",
Expand Down
7 changes: 3 additions & 4 deletions solutions/instances/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ No resources.
| <a name="input_existing_activity_tracker_crn"></a> [existing\_activity\_tracker\_crn](#input\_existing\_activity\_tracker\_crn) | (Optional) The CRN of an existing Activity Tracker instance. Used to send SCC COS bucket log data and all object write events to Activity Tracker. Only used if not supplying an existing COS bucket. | `string` | `null` | no |
| <a name="input_existing_cos_instance_crn"></a> [existing\_cos\_instance\_crn](#input\_existing\_cos\_instance\_crn) | The CRN of an existing Cloud Object Storage instance. If not supplied, a new instance will be created. | `string` | `null` | no |
| <a name="input_existing_en_crn"></a> [existing\_en\_crn](#input\_existing\_en\_crn) | (Optional) The CRN of an existing Event Notification instance. Used to integrate with SCC. | `string` | `null` | no |
| <a name="input_existing_kms_guid"></a> [existing\_kms\_guid](#input\_existing\_kms\_guid) | The GUID of of the KMS instance used for the SCC COS bucket root Key. Only required if not supplying an existing KMS root key and if 'skip\_cos\_kms\_auth\_policy' is true. | `string` | `null` | no |
| <a name="input_existing_kms_instance_crn"></a> [existing\_kms\_instance\_crn](#input\_existing\_kms\_instance\_crn) | The CRN of the existed Hyper Protect Crypto Services or Key Protect instance. Only required if not supplying an existing KMS root key and if 'skip\_cos\_kms\_auth\_policy' is true. | `string` | `null` | no |
| <a name="input_existing_monitoring_crn"></a> [existing\_monitoring\_crn](#input\_existing\_monitoring\_crn) | (Optional) The CRN of an existing IBM Cloud Monitoring instance. Used to send all COS bucket request and usage metrics to, as well as SCC workload protection data. Ignored if using existing COS bucket and not provisioning SCC workload protection. | `string` | `null` | no |
| <a name="input_existing_resource_group"></a> [existing\_resource\_group](#input\_existing\_resource\_group) | Whether to use an existing resource group. | `bool` | `false` | no |
| <a name="input_existing_scc_cos_bucket_name"></a> [existing\_scc\_cos\_bucket\_name](#input\_existing\_scc\_cos\_bucket\_name) | The name of an existing bucket inside the existing Cloud Object Storage instance to use for SCC. If not supplied, a new bucket will be created. | `string` | `null` | no |
| <a name="input_existing_scc_cos_kms_key_crn"></a> [existing\_scc\_cos\_kms\_key\_crn](#input\_existing\_scc\_cos\_kms\_key\_crn) | The CRN of an existing KMS key to be used to encrypt the SCC COS bucket. If not supplied, a new key ring and key will be created in the provided KMS instance. | `string` | `null` | no |
| <a name="input_existing_scc_cos_kms_key_crn"></a> [existing\_scc\_cos\_kms\_key\_crn](#input\_existing\_scc\_cos\_kms\_key\_crn) | The CRN of an existing KMS key to be used to encrypt the SCC COS bucket. If no value is passed, a value must be passed for either 'var.existing\_kms\_instance\_crn' if you want to create a new key ring and key, or 'var.existing\_scc\_cos\_bucket\_name' if you want to use an existing bucket. | `string` | `null` | no |
| <a name="input_ibmcloud_api_key"></a> [ibmcloud\_api\_key](#input\_ibmcloud\_api\_key) | The API Key to use for IBM Cloud. | `string` | n/a | yes |
| <a name="input_kms_endpoint_type"></a> [kms\_endpoint\_type](#input\_kms\_endpoint\_type) | The type of endpoint to be used for commincating with the KMS instance. Allowed values are: 'public' or 'private' (default) | `string` | `"private"` | no |
| <a name="input_kms_region"></a> [kms\_region](#input\_kms\_region) | The region in which KMS instance exists. | `string` | `"us-south"` | no |
| <a name="input_management_endpoint_type_for_bucket"></a> [management\_endpoint\_type\_for\_bucket](#input\_management\_endpoint\_type\_for\_bucket) | The type of endpoint for the IBM terraform provider to use to manage COS buckets. (`public`, `private` or `direct`). Ensure to enable virtual routing and forwarding (VRF) in your account if using `private`, and that the terraform runtime has access to the the IBM Cloud private network. | `string` | `"private"` | no |
| <a name="input_provision_scc_workload_protection"></a> [provision\_scc\_workload\_protection](#input\_provision\_scc\_workload\_protection) | Whether to provision an SCC Workload Protection instance. | `bool` | `true` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The name of a new or an existing resource group in which to provision resources to. | `string` | n/a | yes |
Expand All @@ -74,6 +72,7 @@ No resources.
| <a name="input_scc_wp_service_plan"></a> [scc\_wp\_service\_plan](#input\_scc\_wp\_service\_plan) | SCC Workload Protection instance service pricing plan. Allowed values are: `free-trial` or `graduated-tier`. | `string` | `"graduated-tier"` | no |
| <a name="input_skip_cos_kms_auth_policy"></a> [skip\_cos\_kms\_auth\_policy](#input\_skip\_cos\_kms\_auth\_policy) | Set to true to skip the creation of an IAM authorization policy that permits the COS instance created to read the encryption key from the KMS instance. WARNING: An authorization policy must exist before an encrypted bucket can be created | `bool` | `false` | no |
| <a name="input_skip_scc_cos_auth_policy"></a> [skip\_scc\_cos\_auth\_policy](#input\_skip\_scc\_cos\_auth\_policy) | Set to true to skip the creation of an IAM authorization policy that permits the SCC instance created by this solution write access to the COS instance. Only used if `provision_scc_instance` is set to true. | `bool` | `false` | no |
| <a name="input_use_existing_resource_group"></a> [use\_existing\_resource\_group](#input\_use\_existing\_resource\_group) | Whether to use an existing resource group. | `bool` | `false` | no |

### Outputs

Expand Down
3 changes: 1 addition & 2 deletions solutions/instances/catalogValidationValues.json.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"ibmcloud_api_key": $VALIDATION_APIKEY,
"resource_group_name": $PREFIX,
"existing_kms_guid": $HPCS_US_SOUTH_GUID,
"kms_region": "us-south"
"existing_kms_instance_crn": $HPCS_US_SOUTH_CRN
}
18 changes: 12 additions & 6 deletions solutions/instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

locals {
# tflint-ignore: terraform_unused_declarations
validate_inputs = var.existing_scc_cos_bucket_name == null && var.existing_scc_cos_kms_key_crn == null && var.existing_kms_guid == null ? tobool("A value must be passed for 'existing_kms_guid' if not supplying any value for 'existing_scc_cos_kms_key_crn' or 'existing_scc_cos_bucket_name'.") : true
validate_inputs = var.existing_scc_cos_bucket_name == null && var.existing_scc_cos_kms_key_crn == null && var.existing_kms_instance_crn == null ? tobool("A value must be passed for 'existing_kms_instance_crn' if not supplying any value for 'existing_scc_cos_kms_key_crn' or 'existing_scc_cos_bucket_name'.") : true
# tflint-ignore: terraform_unused_declarations
validate_cos_inputs = var.existing_scc_cos_bucket_name != null && var.existing_scc_cos_kms_key_crn != null ? tobool("A value should not be passed for 'existing_scc_cos_kms_key_crn' when passing a value for 'existing_scc_cos_bucket_name'. A key is only needed when creating a new COS bucket.") : true
# tflint-ignore: terraform_unused_declarations
Expand All @@ -18,14 +18,20 @@ locals {
module "resource_group" {
source = "terraform-ibm-modules/resource-group/ibm"
version = "1.1.5"
resource_group_name = var.existing_resource_group == false ? var.resource_group_name : null
existing_resource_group_name = var.existing_resource_group == true ? var.resource_group_name : null
resource_group_name = var.use_existing_resource_group == false ? var.resource_group_name : null
existing_resource_group_name = var.use_existing_resource_group == true ? var.resource_group_name : null
}

#######################################################################################################################
# KMS Key
#######################################################################################################################

locals {
parsed_existing_kms_instance_crn = var.existing_kms_instance_crn != null ? split(":", var.existing_kms_instance_crn) : []
kms_region = length(local.parsed_existing_kms_instance_crn) > 0 ? local.parsed_existing_kms_instance_crn[5] : null
existing_kms_guid = length(local.parsed_existing_kms_instance_crn) > 0 ? local.parsed_existing_kms_instance_crn[7] : null
}

# KMS root key for SCC COS bucket
module "kms" {
providers = {
Expand All @@ -35,8 +41,8 @@ module "kms" {
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
version = "4.8.5"
create_key_protect_instance = false
region = var.kms_region
existing_kms_instance_guid = var.existing_kms_guid
region = local.kms_region
existing_kms_instance_guid = local.existing_kms_guid
key_ring_endpoint_type = var.kms_endpoint_type
key_endpoint_type = var.kms_endpoint_type
keys = [
Expand Down Expand Up @@ -99,7 +105,7 @@ module "cos" {
add_bucket_name_suffix = var.add_bucket_name_suffix
bucket_name = var.scc_cos_bucket_name
kms_encryption_enabled = true
kms_guid = var.existing_kms_guid
kms_guid = local.existing_kms_guid
kms_key_crn = local.scc_cos_kms_key_crn
skip_iam_authorization_policy = var.skip_cos_kms_auth_policy
management_endpoint_type = var.management_endpoint_type_for_bucket
Expand Down
2 changes: 1 addition & 1 deletion solutions/instances/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ provider "ibm" {
provider "ibm" {
alias = "kms"
ibmcloud_api_key = var.ibmcloud_api_key
region = var.kms_region
region = local.kms_region
}

provider "ibm" {
Expand Down
14 changes: 4 additions & 10 deletions solutions/instances/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ variable "ibmcloud_api_key" {
sensitive = true
}

variable "existing_resource_group" {
variable "use_existing_resource_group" {
type = bool
description = "Whether to use an existing resource group."
default = false
Expand All @@ -30,22 +30,16 @@ variable "existing_monitoring_crn" {
# KMS variables
########################################################################################################################

variable "kms_region" {
type = string
default = "us-south"
description = "The region in which KMS instance exists."
}

variable "existing_kms_guid" {
variable "existing_kms_instance_crn" {
type = string
default = null
description = "The GUID of of the KMS instance used for the SCC COS bucket root Key. Only required if not supplying an existing KMS root key and if 'skip_cos_kms_auth_policy' is true."
description = "The CRN of the existed Hyper Protect Crypto Services or Key Protect instance. Only required if not supplying an existing KMS root key and if 'skip_cos_kms_auth_policy' is true."
}

variable "existing_scc_cos_kms_key_crn" {
type = string
default = null
description = "The CRN of an existing KMS key to be used to encrypt the SCC COS bucket. If not supplied, a new key ring and key will be created in the provided KMS instance."
description = "The CRN of an existing KMS key to be used to encrypt the SCC COS bucket. If no value is passed, a value must be passed for either 'var.existing_kms_instance_crn' if you want to create a new key ring and key, or 'var.existing_scc_cos_bucket_name' if you want to use an existing bucket."
}

variable "kms_endpoint_type" {
Expand Down
13 changes: 5 additions & 8 deletions tests/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func TestInstancesInSchematics(t *testing.T) {
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
{Name: "resource_group_name", Value: options.Prefix, DataType: "string"},
{Name: "existing_kms_guid", Value: permanentResources["hpcs_south"], DataType: "string"},
{Name: "kms_region", Value: "us-south", DataType: "string"}, // KMS instance is in us-south
{Name: "existing_kms_instance_crn", Value: permanentResources["hpcs_south_crn"], DataType: "string"},
{Name: "scc_region", Value: region, DataType: "string"},
{Name: "cos_region", Value: region, DataType: "string"},
{Name: "cos_instance_tags", Value: options.Tags, DataType: "list(string)"},
Expand All @@ -97,9 +96,8 @@ func TestRunUpgradeInstances(t *testing.T) {

options.TerraformVars = map[string]interface{}{
"resource_group_name": options.Prefix,
"existing_kms_guid": permanentResources["hpcs_south"],
"existing_kms_instance_crn": permanentResources["hpcs_south_crn"],
"kms_endpoint_type": "public",
"kms_region": "us-south",
"management_endpoint_type_for_bucket": "public",
}

Expand Down Expand Up @@ -163,7 +161,7 @@ func TestRunExistingResourcesInstances(t *testing.T) {
"cos_region": region,
"scc_region": region,
"resource_group_name": terraform.Output(t, existingTerraformOptions, "resource_group_name"),
"existing_resource_group": true,
"use_existing_resource_group": true,
"existing_monitoring_crn": terraform.Output(t, existingTerraformOptions, "monitoring_crn"),
"existing_scc_cos_bucket_name": terraform.Output(t, existingTerraformOptions, "bucket_name"),
"existing_cos_instance_crn": terraform.Output(t, existingTerraformOptions, "cos_crn"),
Expand All @@ -189,10 +187,9 @@ func TestRunExistingResourcesInstances(t *testing.T) {
"cos_region": region,
"scc_region": region,
"resource_group_name": terraform.Output(t, existingTerraformOptions, "resource_group_name"),
"existing_resource_group": true,
"use_existing_resource_group": true,
"existing_monitoring_crn": terraform.Output(t, existingTerraformOptions, "monitoring_crn"),
"existing_kms_guid": permanentResources["hpcs_south"],
"kms_region": "us-south",
"existing_kms_instance_crn": permanentResources["hpcs_south_crn"],
"kms_endpoint_type": "public",
"existing_cos_instance_crn": terraform.Output(t, existingTerraformOptions, "cos_crn"),
"management_endpoint_type_for_bucket": "public",
Expand Down

0 comments on commit 32a3bba

Please sign in to comment.