generated from terraform-ibm-modules/terraform-ibm-module-template
-
Notifications
You must be signed in to change notification settings - Fork 2
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 : scope KMS auth policy to the exact KMS key #530
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0c8a8fa
feat : scope KMS-policy to the exact KMS key
2b1d174
Added Auth policy to backup encryption key
f7d4bd5
Update Branch
dc79d66
added changes main.tf
415495f
latest changes
36b6011
Changes in description
a0cc673
added variable in cra-config.yaml
3536343
added changes related to backup_encryption_key variable
0c467b8
fixed the cr-config.yaml issue
39b60e3
Merge branch 'main' into 11342-scope-kms
arya-girish-k 0610552
latest changes
29d4104
Added Changes in Readme and cra-config.yaml
953ee2f
Merge remote-tracking branch 'origin/11342-scope-kms' into 11342-scop…
509b863
Testing
df12a6d
Added parser and Auth policy resource for Backup encryption key
5fe8a92
Resolved Pre-commit errors
79e09e3
Merge branch 'main' into 11342-scope-kms
arya-girish-k ca26e4d
Updated validation code in main.tf
6cf4cb0
Resolved precommit error
c9702b2
SKIP UPGRADE TEST
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -28,32 +28,133 @@ locals { | |
# Determine if restore, from backup or point in time recovery | ||
recovery_mode = var.backup_crn != null || var.pitr_id != null | ||
|
||
# Determine what KMS service is being used for database encryption | ||
kms_service = var.kms_key_crn != null ? ( | ||
can(regex(".*kms.*", var.kms_key_crn)) ? "kms" : ( | ||
can(regex(".*hs-crypto.*", var.kms_key_crn)) ? "hs-crypto" : null | ||
) | ||
) : null | ||
#validation for creating KMS and backup KMS policy | ||
create_backup_auth_policy = var.use_default_backup_encryption_key != true && var.backup_encryption_key_crn != null ? 1 : 0 | ||
create_kms_auth_policy = var.kms_encryption_enabled == true && !var.skip_iam_authorization_policy ? 1 : 0 | ||
|
||
#KMS Encryption key details | ||
kms_service = var.kms_key_crn != null ? module.kms_crn_parser[0].service_name : null | ||
kms_account_id = var.kms_key_crn != null ? module.kms_crn_parser[0].account_id : null | ||
kms_key_id = var.kms_key_crn != null ? module.kms_crn_parser[0].resource : null | ||
instance = var.kms_key_crn != null ? module.kms_crn_parser[0].service_instance : null | ||
|
||
#Backup encryption key details | ||
backup_kms_service = local.backup_encryption_key_crn != null && length(module.backup_kms_crn_parser) > 0 ? module.backup_kms_crn_parser[0].service_name : null | ||
backup_kms_account_id = local.backup_encryption_key_crn != null && length(module.backup_kms_crn_parser) > 0 ? module.backup_kms_crn_parser[0].account_id : null | ||
backup_kms_key_id = local.backup_encryption_key_crn != null && length(module.backup_kms_crn_parser) > 0 ? module.backup_kms_crn_parser[0].resource : null | ||
backup_instance = local.backup_encryption_key_crn != null && length(module.backup_kms_crn_parser) > 0 ? module.backup_kms_crn_parser[0].service_instance : null | ||
Comment on lines
+42
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use var.backup_encryption_key_crn in conditions instead of local.backup_encryption_key_crn |
||
} | ||
|
||
# Module to parse the KMS CRN and extract kms key details. | ||
# The module is only created if the 'create_kms_auth_policy' local variable is true . | ||
module "kms_crn_parser" { | ||
count = local.create_kms_auth_policy | ||
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser" | ||
version = "1.1.0" | ||
crn = var.kms_key_crn | ||
} | ||
|
||
# Create IAM Authorization Policies to allow PostgreSQL to access KMS for the encryption key | ||
resource "ibm_iam_authorization_policy" "kms_policy" { | ||
count = var.kms_encryption_enabled == false || var.skip_iam_authorization_policy ? 0 : 1 | ||
source_service_name = "databases-for-postgresql" | ||
source_resource_group_id = var.resource_group_id | ||
target_service_name = local.kms_service | ||
target_resource_instance_id = var.existing_kms_instance_guid | ||
roles = ["Reader"] | ||
description = "Allow all ICD Postgres instances in the resource group ${var.resource_group_id} to read from the ${local.kms_service} instance GUID ${var.existing_kms_instance_guid}" | ||
count = local.create_kms_auth_policy | ||
source_service_name = "databases-for-postgresql" | ||
source_resource_group_id = var.resource_group_id | ||
roles = ["Reader"] | ||
description = "Allow all ICD Postgres instances in the resource group ${var.resource_group_id} to read from the ${local.kms_service} instance GUID ${local.instance}." | ||
resource_attributes { | ||
name = "serviceName" | ||
operator = "stringEquals" | ||
value = local.kms_service | ||
} | ||
resource_attributes { | ||
name = "accountId" | ||
operator = "stringEquals" | ||
value = local.kms_account_id | ||
} | ||
resource_attributes { | ||
name = "serviceInstance" | ||
operator = "stringEquals" | ||
value = local.instance | ||
} | ||
resource_attributes { | ||
name = "resourceType" | ||
operator = "stringEquals" | ||
value = "key" | ||
} | ||
resource_attributes { | ||
name = "resource" | ||
operator = "stringEquals" | ||
value = local.kms_key_id | ||
} | ||
# Scope of policy now includes the key, so ensure to create new policy before | ||
# destroying old one to prevent any disruption to every day services. | ||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478 | ||
resource "time_sleep" "wait_for_authorization_policy" { | ||
count = local.create_kms_auth_policy | ||
depends_on = [ibm_iam_authorization_policy.kms_policy] | ||
|
||
create_duration = "30s" | ||
} | ||
|
||
# Module to parse the backup encryption key crn and extract key details. | ||
# The module is only created if the 'create_backup_auth_policy' local variable is true | ||
module "backup_kms_crn_parser" { | ||
count = local.create_backup_auth_policy | ||
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser" | ||
version = "1.1.0" | ||
crn = local.backup_encryption_key_crn | ||
} | ||
|
||
resource "ibm_iam_authorization_policy" "backup_kms_policy" { | ||
count = local.create_backup_auth_policy | ||
source_service_name = "databases-for-postgresql" | ||
source_resource_group_id = var.resource_group_id | ||
roles = ["Reader"] | ||
description = "Allow all ICD Postgres instances in the resource group ${var.resource_group_id} to read from the ${local.backup_kms_service} instance GUID ${local.backup_instance}." | ||
resource_attributes { | ||
name = "serviceName" | ||
operator = "stringEquals" | ||
value = local.backup_kms_service | ||
} | ||
resource_attributes { | ||
name = "accountId" | ||
operator = "stringEquals" | ||
value = local.backup_kms_account_id | ||
} | ||
resource_attributes { | ||
name = "serviceInstance" | ||
operator = "stringEquals" | ||
value = local.backup_instance | ||
} | ||
resource_attributes { | ||
name = "resourceType" | ||
operator = "stringEquals" | ||
value = "key" | ||
} | ||
resource_attributes { | ||
name = "resource" | ||
operator = "stringEquals" | ||
value = local.backup_kms_key_id | ||
} | ||
# Scope of policy now includes the key, so ensure to create new policy before | ||
# destroying old one to prevent any disruption to every day services. | ||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "time_sleep" "wait_for_backup_kms_authorization_policy" { | ||
count = local.create_backup_auth_policy | ||
depends_on = [ibm_iam_authorization_policy.backup_kms_policy] | ||
|
||
create_duration = "30s" | ||
} | ||
|
||
# Create postgresql database | ||
resource "ibm_database" "postgresql_db" { | ||
depends_on = [time_sleep.wait_for_authorization_policy] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should say create_backup_kms_auth_policy