Skip to content

Commit 86baecd

Browse files
authored
Merge pull request #10 from zama-ai/darwin/feat/copy-eth-pk
feat: copy eth private key between secrets manager in two aws accounts
2 parents 4555e3c + 0d13d98 commit 86baecd

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

scripts/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ chmod +x create-age-keypair.sh create-and-backup-eth-kms-key.sh create-and-uploa
4545
## Usage
4646

4747
To generate an Age keypair and upload it to 1Password:
48-
4948
```
5049
export OP_SERVICE_ACCOUNT_TOKEN= # 1Password Service Account token
5150
export OP_VAULT= # Name of the 1Password Vault where Age private key will be saved
@@ -66,3 +65,33 @@ export SECRET_NAME= # Name of the AWS Secrets Manager secret to store t
6665
export AGE_PUBLIC_KEY= # Age public key to encrypt the Ethereum private key at rest
6766
./create-and-backup-eth-kms-key.sh
6867
```
68+
69+
To copy an Ethereum private key from AWS secret manager in a first AWS account to a second AWS account:
70+
```
71+
export SOURCE_SECRET_NAME= # Name of the AWS secret manager secret in the first AWS account
72+
export DEST_SECRET_NAME= # Name of the AWS secret manager secret in the second AWS account
73+
export DEST_REGION= # Region in the second AWS account where the secret should be stored
74+
75+
# AWS IAM credentials for the second AWS account. These should be tightly scoped and least permissive.
76+
# The following is all that is required:
77+
# {
78+
# "Version": "2012-10-17",
79+
# "Statement": [
80+
# {
81+
# "Sid": "AllowSecretsManagerWrite",
82+
# "Effect": "Allow",
83+
# "Action": [
84+
# "secretsmanager:CreateSecret",
85+
# "secretsmanager:PutSecretValue",
86+
# "secretsmanager:TagResource"
87+
# ],
88+
# "Resource": "*"
89+
# }
90+
# ]
91+
# }
92+
export ENV_AWS_ACCESS_KEY_ID=
93+
export ENV_AWS_SECRET_ACCESS_KEY=
94+
export ENV_AWS_SESSION_TOKEN=
95+
96+
./copy-eth-private-key.sh
97+
```

scripts/copy-eth-private-key.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
set -e # Exit on error
4+
set -u # Exit on undefined variable
5+
6+
# Colors for output
7+
RED='\033[0;31m'
8+
GREEN='\033[0;32m'
9+
YELLOW='\033[1;33m'
10+
NC='\033[0m' # No Color
11+
12+
# Function to print colored output
13+
print_status() {
14+
echo -e "${GREEN}[INFO]${NC} $1"
15+
}
16+
17+
print_error() {
18+
echo -e "${RED}[ERROR]${NC} $1"
19+
}
20+
21+
# Configuration
22+
SOURCE_SECRET_NAME="${SOURCE_SECRET_NAME}"
23+
DEST_SECRET_NAME="${DEST_SECRET_NAME}"
24+
DEST_REGION="${DEST_REGION}"
25+
26+
print_status "Copying secret: $SOURCE_SECRET_NAME -> $DEST_SECRET_NAME"
27+
28+
# Step 1: Pull secret from first account
29+
print_status "Step 1: Pulling secret from source (e.g., current) AWS account..."
30+
SOURCE_SECRET_VALUE=$(aws secretsmanager get-secret-value \
31+
--secret-id "$SOURCE_SECRET_NAME" \
32+
--query 'SecretString' \
33+
--output text)
34+
35+
if [ -z "$SOURCE_SECRET_VALUE" ]; then
36+
print_error "Failed to retrieve source secret"
37+
exit 1
38+
fi
39+
40+
print_status "Secret retrieved successfully"
41+
42+
# Step 2: Switch to second account credentials
43+
print_status "Step 2: Switching to destination account credentials..."
44+
45+
if [ -z "${ENV_AWS_ACCESS_KEY_ID:-}" ] || [ -z "${ENV_AWS_SECRET_ACCESS_KEY:-}" ]; then
46+
print_error "ENV_AWS_ACCESS_KEY_ID and ENV_AWS_SECRET_ACCESS_KEY must be set"
47+
exit 1
48+
fi
49+
50+
export AWS_ACCESS_KEY_ID=$ENV_AWS_ACCESS_KEY_ID
51+
export AWS_SECRET_ACCESS_KEY=$ENV_AWS_SECRET_ACCESS_KEY
52+
export AWS_SESSION_TOKEN=${ENV_AWS_SESSION_TOKEN:-}
53+
54+
print_status "Credentials switched"
55+
56+
# Step 3: Create secret in second account
57+
print_status "Step 3: Creating secret in destination AWS account..."
58+
59+
aws secretsmanager create-secret \
60+
--name "$DEST_SECRET_NAME" \
61+
--secret-string "$SOURCE_SECRET_VALUE" \
62+
--region "$DEST_REGION"
63+
64+
print_status "Secret created successfully in $DEST_REGION"
65+
print_status "Done!"

0 commit comments

Comments
 (0)