Skip to content

Commit 2e428eb

Browse files
committed
feat: add mpc-backup-vault module
1 parent ed463c9 commit 2e428eb

5 files changed

Lines changed: 174 additions & 0 deletions

File tree

modules/mpc-backup-vault/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# MPC Key backup modules\
2+
3+
This module is aim to create :
4+
- bucket for backup vault
5+
6+
The kms keys is handled by kms-stack terraform module in infra repo.

modules/mpc-backup-vault/main.tf

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# ***************************************
2+
# Local variables
3+
# ***************************************
4+
resource "random_id" "mpc_party_suffix" {
5+
byte_length = 4
6+
}
7+
locals {
8+
backup_bucket_name = "${var.bucket_prefix}-${var.party_name}-${random_id.mpc_party_suffix.hex}"
9+
}
10+
11+
# ***************************************
12+
# S3 Buckets for Vault Private Storage
13+
# ***************************************
14+
resource "aws_s3_bucket" "backup_bucket" {
15+
force_destroy = true
16+
bucket = local.backup_bucket_name
17+
tags = merge(var.tags, {
18+
"Name" = local.backup_bucket_name
19+
"Type" = "backup-vault"
20+
"Party" = var.party_name
21+
"Purpose" = "mpc-backup-storage"
22+
})
23+
}
24+
25+
resource "aws_s3_bucket_ownership_controls" "backup_bucket" {
26+
bucket = aws_s3_bucket.backup_bucket.id
27+
rule {
28+
object_ownership = "BucketOwnerEnforced"
29+
}
30+
}
31+
32+
resource "aws_s3_bucket_versioning" "backup_bucket" {
33+
bucket = aws_s3_bucket.backup_bucket.id
34+
versioning_configuration {
35+
status = "Enabled"
36+
}
37+
}
38+
39+
resource "aws_s3_bucket_public_access_block" "backup_bucket" {
40+
bucket = aws_s3_bucket.backup_bucket.id
41+
block_public_acls = true
42+
block_public_policy = true
43+
ignore_public_acls = true
44+
restrict_public_buckets = true
45+
}
46+
47+
# ***************************************
48+
# IAM Role & Policy for MPC Backup Vault
49+
# ***************************************
50+
51+
# Trust policy: Allow trusted principals to assume this role
52+
data "aws_iam_policy_document" "assume_role" {
53+
statement {
54+
actions = ["sts:AssumeRole"]
55+
effect = "Allow"
56+
principals {
57+
type = "AWS"
58+
identifiers = var.trusted_principal_arns
59+
}
60+
}
61+
}
62+
63+
resource "aws_iam_role" "mpc_backup_role" {
64+
name = "mpc-backup-${var.party_name}"
65+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
66+
tags = var.tags
67+
}
68+
69+
# Policy allowing access to the bucket
70+
resource "aws_iam_policy" "mpc_aws" {
71+
name = "mpc-backup-${var.party_name}"
72+
policy = jsonencode({
73+
Version = "2012-10-17"
74+
Statement = [
75+
{
76+
Sid = "AllowObjectActions"
77+
Effect = "Allow"
78+
Action = "s3:*Object"
79+
Resource = [
80+
"arn:aws:s3:::${aws_s3_bucket.backup_bucket.id}/*"
81+
]
82+
},
83+
{
84+
Sid = "AllowListBucket"
85+
Effect = "Allow"
86+
Action = "s3:ListBucket"
87+
Resource = [
88+
"arn:aws:s3:::${aws_s3_bucket.backup_bucket.id}"
89+
]
90+
}
91+
]
92+
})
93+
}
94+
95+
# Attach policy to the role
96+
resource "aws_iam_role_policy_attachment" "mpc_backup_attach" {
97+
role = aws_iam_role.mpc_backup_role.name
98+
policy_arn = aws_iam_policy.mpc_aws.arn
99+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "bucket_name" {
2+
description = "The name of the created S3 bucket"
3+
value = aws_s3_bucket.backup_bucket.id
4+
}
5+
6+
output "bucket_arn" {
7+
description = "The ARN of the created S3 bucket"
8+
value = aws_s3_bucket.backup_bucket.arn
9+
}
10+
11+
output "role_name" {
12+
description = "The name of the IAM role created for accessing the bucket"
13+
value = aws_iam_role.mpc_backup_role.name
14+
}
15+
16+
output "role_arn" {
17+
description = "The ARN of the IAM role created for accessing the bucket"
18+
value = aws_iam_role.mpc_backup_role.arn
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Tagging
2+
variable "tags" {
3+
type = map(string)
4+
description = "A map of tags to assign to the resource"
5+
default = {
6+
"terraform" = "true"
7+
"module" = "mpc-party"
8+
}
9+
}
10+
11+
variable "bucket_prefix" {
12+
type = string
13+
description = "The prefix for the S3 bucket names"
14+
default = "mpc-backup-vault"
15+
}
16+
17+
# MPC Party Configuration
18+
variable "party_name" {
19+
type = string
20+
description = "The name of the MPC party (used for resource naming and tagging)"
21+
22+
validation {
23+
condition = can(regex("^[a-z0-9-]+$", var.party_name))
24+
error_message = "Party name must contain only lowercase letters, numbers, and hyphens."
25+
}
26+
}
27+
28+
variable "trusted_principal_arns" {
29+
type = list(string)
30+
description = "List of ARNs (users, roles, or root accounts) that can assume the backup role."
31+
default = []
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_version = ">= 1.10"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 6.0"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 3.1"
12+
}
13+
null = {
14+
source = "hashicorp/null"
15+
version = ">= 3.0"
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)