Skip to content

Commit 4dd98e9

Browse files
authored
Merge pull request #71 from zama-ai/darwin/feat/cross-account-kms-key
feat(mpc-party): add support for usage of cross-account aws kms key
2 parents e46b460 + c9e52c9 commit 4dd98e9

3 files changed

Lines changed: 65 additions & 33 deletions

File tree

modules/mpc-party/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,14 @@ The module can optionally create:
365365
| <a name="input_kms_backup_external_role_arn"></a> [kms\_backup\_external\_role\_arn](#input\_kms\_backup\_external\_role\_arn) | ARN of the backup vault for the KMS key | `string` | `null` | no |
366366
| <a name="input_kms_backup_vault_customer_master_key_spec"></a> [kms\_backup\_vault\_customer\_master\_key\_spec](#input\_kms\_backup\_vault\_customer\_master\_key\_spec) | Key spec for the backup vault | `string` | `"ASYMMETRIC_DEFAULT"` | no |
367367
| <a name="input_kms_backup_vault_key_usage"></a> [kms\_backup\_vault\_key\_usage](#input\_kms\_backup\_vault\_key\_usage) | Key usage for the backup vault | `string` | `"ENCRYPT_DECRYPT"` | no |
368+
| <a name="input_kms_cross_account_kms_key_id"></a> [kms\_cross\_account\_kms\_key\_id](#input\_kms\_cross\_account\_kms\_key\_id) | KMS key ID of KMS key created in a different AWS account | `string` | `""` | no |
368369
| <a name="input_kms_customer_master_key_spec"></a> [kms\_customer\_master\_key\_spec](#input\_kms\_customer\_master\_key\_spec) | Specification for the KMS customer master key (e.g., SYMMETRIC\_DEFAULT, RSA\_2048) | `string` | `"SYMMETRIC_DEFAULT"` | no |
369370
| <a name="input_kms_deletion_window_in_days"></a> [kms\_deletion\_window\_in\_days](#input\_kms\_deletion\_window\_in\_days) | Deletion window in days for KMS key | `number` | `30` | no |
370371
| <a name="input_kms_enable_backup_vault"></a> [kms\_enable\_backup\_vault](#input\_kms\_enable\_backup\_vault) | Whether to enable the backup vault for the KMS key | `bool` | `false` | no |
371372
| <a name="input_kms_enabled_nitro_enclaves"></a> [kms\_enabled\_nitro\_enclaves](#input\_kms\_enabled\_nitro\_enclaves) | Whether to enable KMS for Nitro Enclaves | `bool` | n/a | yes |
372373
| <a name="input_kms_image_attestation_sha"></a> [kms\_image\_attestation\_sha](#input\_kms\_image\_attestation\_sha) | Attestation SHA for KMS image | `string` | n/a | yes |
373374
| <a name="input_kms_key_usage"></a> [kms\_key\_usage](#input\_kms\_key\_usage) | Key usage for KMS | `string` | `"ENCRYPT_DECRYPT"` | no |
375+
| <a name="input_kms_use_cross_account_kms_key"></a> [kms\_use\_cross\_account\_kms\_key](#input\_kms\_use\_cross\_account\_kms\_key) | Whether a KMS key has been created in a different AWS account | `bool` | `false` | no |
374376
| <a name="input_namespace_annotations"></a> [namespace\_annotations](#input\_namespace\_annotations) | Additional annotations to apply to the namespace | `map(string)` | `{}` | no |
375377
| <a name="input_namespace_labels"></a> [namespace\_labels](#input\_namespace\_labels) | Additional labels to apply to the namespace | `map(string)` | `{}` | no |
376378
| <a name="input_network_environment"></a> [network\_environment](#input\_network\_environment) | MPC network environment that determines region constraints | `string` | `"testnet"` | no |

modules/mpc-party/main.tf

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# ***************************************
44
data "aws_caller_identity" "current" {}
55

6-
76
data "aws_eks_cluster" "cluster" {
87
name = var.cluster_name
98
lifecycle {
@@ -226,6 +225,7 @@ module "iam_assumable_role_mpc_party" {
226225

227226
resource "kubernetes_service_account" "mpc_party_service_account" {
228227
count = var.create_service_account ? 1 : 0
228+
229229
metadata {
230230
name = var.k8s_service_account_name
231231
namespace = var.k8s_namespace
@@ -248,16 +248,23 @@ resource "kubernetes_service_account" "mpc_party_service_account" {
248248
}
249249

250250
# ***************************************
251-
# aws kms key for mpc party
251+
# AWS KMS Key for MPC Party
252252
# ***************************************
253+
locals {
254+
create_mpc_party_key = var.kms_enabled_nitro_enclaves && !var.kms_use_cross_account_kms_key
255+
create_mpc_party_key_backup = var.kms_enabled_nitro_enclaves && var.kms_enable_backup_vault && !var.kms_use_cross_account_kms_key
256+
}
257+
253258
resource "aws_kms_key" "mpc_party" {
254-
count = var.kms_enabled_nitro_enclaves ? 1 : 0
259+
count = local.create_mpc_party_key ? 1 : 0
260+
255261
description = "KMS key for MPC Party"
256262
key_usage = var.kms_key_usage
257263
customer_master_key_spec = var.kms_customer_master_key_spec
258264
enable_key_rotation = false
259265
deletion_window_in_days = var.kms_deletion_window_in_days
260266
tags = var.tags
267+
261268
policy = jsonencode({
262269
Version = "2012-10-17"
263270
Statement = [
@@ -315,7 +322,8 @@ resource "aws_kms_key" "mpc_party" {
315322
}
316323

317324
resource "aws_kms_alias" "mpc_party" {
318-
count = var.kms_enabled_nitro_enclaves ? 1 : 0
325+
count = local.create_mpc_party_key ? 1 : 0
326+
319327
name = "alias/mpc-${var.party_name}"
320328
target_key_id = aws_kms_key.mpc_party[0].key_id
321329
}
@@ -324,12 +332,15 @@ resource "aws_kms_alias" "mpc_party" {
324332
# ASYMMETRIC KMS Key Backup for MPC Party
325333
# ***************************************
326334
resource "aws_kms_key" "mpc_party_backup" {
327-
count = var.kms_enabled_nitro_enclaves && var.kms_enable_backup_vault ? 1 : 0
335+
count = local.create_mpc_party_key_backup ? 1 : 0
336+
328337
description = "Asymmetric KMS key backup for MPC Party"
329338
key_usage = var.kms_backup_vault_key_usage
330339
customer_master_key_spec = var.kms_backup_vault_customer_master_key_spec
331340
enable_key_rotation = false
332341
deletion_window_in_days = var.kms_deletion_window_in_days
342+
tags = var.tags
343+
333344
policy = jsonencode({
334345
Version = "2012-10-17"
335346
Statement = [
@@ -382,24 +393,29 @@ resource "aws_kms_key" "mpc_party_backup" {
382393
],
383394
Resource = "*"
384395
}
385-
386396
]
387397
})
388-
tags = var.tags
389398
}
390399

391400
# ***************************************
392401
# KMS Key Alias for MPC Party Backup
393402
# ***************************************
394403
resource "aws_kms_alias" "mpc_party_backup" {
395-
count = var.kms_enabled_nitro_enclaves && var.kms_enable_backup_vault ? 1 : 0
404+
count = local.create_mpc_party_key_backup ? 1 : 0
405+
396406
name = "alias/mpc-${var.party_name}-backup"
397407
target_key_id = aws_kms_key.mpc_party_backup[0].key_id
398408
}
399409

400410
# ***************************************
401411
# ConfigMap for MPC Party
402412
# ***************************************
413+
locals {
414+
kms_key_id = var.kms_enabled_nitro_enclaves ? (
415+
var.kms_use_cross_account_kms_key ? var.kms_cross_account_kms_key_id : aws_kms_key.mpc_party[0].key_id
416+
) : null
417+
}
418+
403419
resource "kubernetes_config_map" "mpc_party_config" {
404420
count = var.create_config_map ? 1 : 0
405421

@@ -428,7 +444,7 @@ resource "kubernetes_config_map" "mpc_party_config" {
428444
"KMS_CORE__PRIVATE_VAULT__STORAGE__S3__PREFIX" = ""
429445
"KMS_CORE__PUBLIC_VAULT__STORAGE__S3__BUCKET" = aws_s3_bucket.vault_public_bucket.id
430446
"KMS_CORE__PUBLIC_VAULT__STORAGE__S3__PREFIX" = ""
431-
"KMS_CORE__PRIVATE_VAULT__KEYCHAIN__AWS_KMS__ROOT_KEY_ID" = var.kms_enabled_nitro_enclaves ? aws_kms_key.mpc_party[0].key_id : null
447+
"KMS_CORE__PRIVATE_VAULT__KEYCHAIN__AWS_KMS__ROOT_KEY_ID" = local.kms_key_id
432448
"KMS_CORE__PRIVATE_VAULT__KEYCHAIN__AWS_KMS__ROOT_KEY_SPEC" = var.kms_enabled_nitro_enclaves ? "symm" : null
433449
}
434450

@@ -439,18 +455,19 @@ resource "kubernetes_config_map" "mpc_party_config" {
439455
# EKS Managed Node Group
440456
# ***************************************
441457
data "aws_ec2_instance_type" "this" {
458+
count = var.nodegroup_enable_nitro_enclaves ? 1 : 0
459+
442460
instance_type = var.nodegroup_instance_types[0]
443-
count = var.nodegroup_enable_nitro_enclaves ? 1 : 0
444461
}
445462

446463
locals {
447464
cluster_security_group_id = var.nodegroup_auto_assign_security_group ? try(tolist(data.aws_eks_cluster.cluster.vpc_config[0].security_group_ids)[0], null) : null
448465
}
449466

450-
451467
# Get all rule IDs on the cluster SG (ingress and egress)
452468
data "aws_vpc_security_group_rules" "cluster_rules" {
453469
count = var.nodegroup_auto_assign_security_group && local.cluster_security_group_id != null ? 1 : 0
470+
454471
filter {
455472
name = "group-id"
456473
values = [local.cluster_security_group_id]
@@ -459,7 +476,8 @@ data "aws_vpc_security_group_rules" "cluster_rules" {
459476

460477
# Read each rule to find which ones reference another SG
461478
data "aws_vpc_security_group_rule" "cluster_sg_rules_by_id" {
462-
for_each = toset(try(data.aws_vpc_security_group_rules.cluster_rules[0].ids, []))
479+
for_each = toset(try(data.aws_vpc_security_group_rules.cluster_rules[0].ids, []))
480+
463481
security_group_rule_id = each.value
464482
}
465483

@@ -475,14 +493,16 @@ locals {
475493

476494
data "aws_vpc_security_group_rules" "node_group_sg_rules" {
477495
count = var.nodegroup_auto_assign_security_group && local.auto_resolved_node_sg != null ? 1 : 0
496+
478497
filter {
479498
name = "group-id"
480499
values = [local.auto_resolved_node_sg]
481500
}
482501
}
483502

484503
data "aws_vpc_security_group_rule" "node_group_sg_rules_by_id" {
485-
for_each = toset(try(data.aws_vpc_security_group_rules.node_group_sg_rules[0].ids, []))
504+
for_each = toset(try(data.aws_vpc_security_group_rules.node_group_sg_rules[0].ids, []))
505+
486506
security_group_rule_id = each.value
487507
}
488508

@@ -496,6 +516,7 @@ locals {
496516

497517
resource "null_resource" "validate_auto_resolved_node_sg" {
498518
count = var.nodegroup_auto_assign_security_group ? 1 : 0
519+
499520
lifecycle {
500521
precondition {
501522
# Ensure the auto resolved node group SG is valid
@@ -528,7 +549,8 @@ locals {
528549
}
529550

530551
module "eks_managed_node_group" {
531-
count = var.create_nodegroup ? 1 : 0
552+
count = var.create_nodegroup ? 1 : 0
553+
532554
source = "terraform-aws-modules/eks/aws//modules/eks-managed-node-group"
533555
version = "21.0.6"
534556

@@ -764,7 +786,8 @@ locals {
764786
}
765787

766788
module "rds_instance" {
767-
count = var.enable_rds ? 1 : 0
789+
count = var.enable_rds ? 1 : 0
790+
768791
source = "terraform-aws-modules/rds/aws"
769792
version = "~> 6.10"
770793

@@ -808,7 +831,8 @@ module "rds_instance" {
808831
}
809832

810833
module "rds_security_group" {
811-
count = var.enable_rds ? 1 : 0
834+
count = var.enable_rds ? 1 : 0
835+
812836
source = "terraform-aws-modules/security-group/aws"
813837
version = "~> 5.3.0"
814838

modules/mpc-party/variables.tf

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ variable "network_environment" {
88
}
99
}
1010

11-
12-
13-
1411
variable "bucket_prefix" {
1512
type = string
1613
description = "The prefix for the S3 bucket names"
1714
default = "mpc-vault"
1815
}
1916

20-
2117
# MPC Party Configuration
2218
variable "party_id" {
2319
description = "Party ID for the MPC service"
@@ -109,7 +105,6 @@ variable "config_map_name" {
109105
default = "mpc-party"
110106
}
111107

112-
113108
# Deprecated Tagging
114109
variable "common_tags" {
115110
type = map(string)
@@ -130,7 +125,9 @@ variable "tags" {
130125
}
131126
}
132127

128+
# ******************************************************
133129
# EKS Node Group Core Configuration
130+
# ******************************************************
134131
variable "create_nodegroup" {
135132
type = bool
136133
description = "Whether to create an EKS managed node group"
@@ -142,14 +139,12 @@ variable "nodegroup_name" {
142139
description = "Name of the EKS managed node group"
143140
}
144141

145-
146142
variable "nodegroup_use_latest_ami_release_version" {
147143
type = bool
148144
description = "Whether to use the latest AMI release version"
149145
default = false
150146
}
151147

152-
153148
variable "nodegroup_ami_release_version" {
154149
type = string
155150
description = "AMI release version for the node group"
@@ -210,7 +205,6 @@ variable "nodegroup_disk_size" {
210205
default = 20
211206
}
212207

213-
214208
# Remote Access Configuration
215209
variable "nodegroup_enable_remote_access" {
216210
type = bool
@@ -230,7 +224,6 @@ variable "nodegroup_source_security_group_ids" {
230224
default = []
231225
}
232226

233-
234227
variable "nodegroup_additional_security_group_ids" {
235228
type = list(string)
236229
description = "List of additional security group IDs to associate with the node group"
@@ -322,13 +315,31 @@ variable "nodegroup_update_config" {
322315
}
323316
}
324317

325-
326-
# kms configuration
318+
# ******************************************************
319+
# KMS Configuration
320+
# ******************************************************
327321
variable "kms_enabled_nitro_enclaves" {
328322
type = bool
329323
description = "Whether to enable KMS for Nitro Enclaves"
330324
}
331325

326+
variable "kms_use_cross_account_kms_key" {
327+
type = bool
328+
description = "Whether a KMS key has been created in a different AWS account"
329+
default = false
330+
}
331+
332+
variable "kms_cross_account_kms_key_id" {
333+
type = string
334+
description = "KMS key ID of KMS key created in a different AWS account"
335+
default = ""
336+
337+
validation {
338+
condition = !var.kms_use_cross_account_kms_key || (var.kms_use_cross_account_kms_key && var.kms_cross_account_kms_key_id != "")
339+
error_message = "kms_cross_account_kms_key_id must be provided when kms_use_cross_account_kms_key is true."
340+
}
341+
}
342+
332343
variable "kms_key_usage" {
333344
type = string
334345
description = "Key usage for KMS"
@@ -364,8 +375,6 @@ variable "kms_backup_external_role_arn" {
364375
default = null
365376
}
366377

367-
368-
369378
variable "kms_backup_vault_key_usage" {
370379
type = string
371380
description = "Key usage for the backup vault"
@@ -378,16 +387,14 @@ variable "kms_backup_vault_customer_master_key_spec" {
378387
default = "ASYMMETRIC_DEFAULT"
379388
}
380389

381-
382390
variable "nodegroup_enable_ssm_managed_instance" {
383391
type = bool
384392
description = "Whether to enable SSM managed instance"
385393
default = false
386394
}
387395

388-
389396
# ******************************************************
390-
# variables for the RDS instance
397+
# RDS instance
391398
# ******************************************************
392399
variable "enable_rds" {
393400
type = bool
@@ -514,7 +521,6 @@ variable "rds_parameters" {
514521
}]
515522
}
516523

517-
518524
variable "rds_create_externalname_service" {
519525
description = "Whether to create a Kubernetes ExternalName service for RDS database access"
520526
type = bool

0 commit comments

Comments
 (0)