Skip to content

Commit a5823a4

Browse files
feat: op-signer: Necessary script adjustments [5/N] (#379)
**Description** In this PR, the script for generating local credentials is adjusted to fit our usecase (unnecessary indirection is removed & file permissions are adjusted). Additionally, [an inline script from the original PR](https://github.com/ethpandaops/optimism-package/pull/207/files#diff-8952ca36c0cbca2168c99c185e468d02357daaab464a93cd7bf58bf8f3b90eb0R136) is moved into a shell script - this script converts a HEX private key to a PEM file. This is necessary since `op-signer` requires private keys (that we have in the deployment output in HEX) to be converted to PEM files. Both of these scripts can be tested locally so if we ever decide to test these, it would be very easy.
1 parent 7bd848e commit a5823a4

2 files changed

Lines changed: 51 additions & 41 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env sh
2+
3+
# Get private key from the script arguments
4+
if [ $# -ne 1 ]; then
5+
echo "Error: Private key argument is required."
6+
exit 1
7+
fi
8+
9+
# We grab the private key from the arguments
10+
PRIVATE_KEY="$1"; shift
11+
12+
# We convert it to ASCII
13+
PRIVATE_KEY_ASCII=$(echo -n "$PRIVATE_KEY" | xxd -r -p)
14+
15+
# And pad it
16+
PRIVATE_KEY_PREFIX="\\x30\\x2e\\x02\\x01\\x01\\x04\\x20"
17+
PRIVATE_KEY_SUFFIX="\\xa0\\x07\\x06\\x05\\x2b\\x81\\x04\\x00\\x0a"
18+
PRIVATE_KEY_WRAPPED="${PRIVATE_KEY_PREFIX}${PRIVATE_KEY_ASCII}${PRIVATE_KEY_SUFFIX}"
19+
20+
# And finally we create the EC encoded private key
21+
printf "%b" "$PRIVATE_KEY_WRAPPED" | openssl ec -inform DER -outform PEM

src/signer/op-signer/scripts/gen-local-creds.sh

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env sh
22

3+
#
4+
# This script is based on https://github.com/ethereum-optimism/infra/blob/main/op-signer/gen-local-creds.sh
5+
# with small adjustments to fit to our use case
6+
#
7+
38
set -euo pipefail
49

5-
if [ -z "$TLS_DIR" ]; then
10+
if [ -z "${TLS_DIR-}" ]; then
611
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
712
TLS_DIR="$SCRIPT_DIR/tls"
813
fi
914

10-
OPENSSL_IMAGE="alpine/openssl:3.3.3"
11-
12-
USER_UID=$(id -u)
13-
USER_GID=$(id -g)
14-
1515
CERT_ORG_NAME="OP-Signer Local Org"
1616
MOD_LENGTH=2048
1717

@@ -24,34 +24,12 @@ CLIENT_TLS_CERT="tls.crt"
2424
CLIENT_PRIVATE_KEY="ec_private.pem"
2525
CLIENT_OPENSSL_CNF="openssl.cnf"
2626

27-
# Check if we should use Docker (default to true if not set)
28-
USE_DOCKER=${OP_SIGNER_GEN_TLS_DOCKER:-true}
29-
30-
# Helper function to run openssl commands
31-
run_openssl() {
32-
if [ "$USE_DOCKER" = "true" ]; then
33-
docker run --rm \
34-
-v "$TLS_DIR:$TLS_DIR" \
35-
-u "$USER_UID:$USER_GID" \
36-
"$OPENSSL_IMAGE" "$@"
37-
else
38-
# Check if openssl is available locally
39-
if ! command -v openssl &> /dev/null; then
40-
echo "Error: OpenSSL is not installed locally. Please install OpenSSL or use Docker by setting OP_SIGNER_GEN_TLS_DOCKER=true"
41-
exit 1
42-
fi
43-
openssl "$@"
44-
fi
45-
}
46-
4727
generate_ca() {
48-
local force="$1"
49-
[ "$force" = "true" ] || [ ! -f "$CA_CERT" ] || return 0
50-
5128
echo
5229
echo "Generating CA..."
5330

54-
run_openssl req -newkey "rsa:$MOD_LENGTH" \
31+
openssl req \
32+
-newkey "rsa:$MOD_LENGTH" \
5533
-new -nodes -x509 \
5634
-days 365 \
5735
-sha256 \
@@ -72,7 +50,13 @@ generate_client_tls() {
7250

7351
# Generate client key
7452
echo "Generating client key..."
75-
run_openssl genrsa -out "$clientDir/$CLIENT_TLS_KEY" "$MOD_LENGTH"
53+
openssl genrsa -out "$clientDir/$CLIENT_TLS_KEY" "$MOD_LENGTH"
54+
55+
# Since we are in a testing environment, we are not so strict about file permissions
56+
#
57+
# Allowing the private key to be readable by all users
58+
# makes the integration with op-signer easier
59+
chmod 644 "$clientDir/$CLIENT_TLS_KEY"
7660

7761
local confFile="$clientDir/$CLIENT_OPENSSL_CNF"
7862

@@ -85,15 +69,19 @@ subjectAltName=DNS:$hostname
8569
EOF
8670

8771
echo "Generating client certificate signing request..."
88-
run_openssl req -new -key "$clientDir/$CLIENT_TLS_KEY" \
72+
openssl req \
73+
-new \
74+
-key "$clientDir/$CLIENT_TLS_KEY" \
8975
-sha256 \
9076
-out "$clientDir/$CLIENT_TLS_CSR" \
9177
-subj "/O=$CERT_ORG_NAME/CN=$hostname" \
9278
-extensions san \
9379
-config "$confFile"
9480

9581
echo "Generating client certificate..."
96-
run_openssl x509 -req -in "$clientDir/$CLIENT_TLS_CSR" \
82+
openssl x509 \
83+
-req \
84+
-in "$clientDir/$CLIENT_TLS_CSR" \
9785
-sha256 \
9886
-CA "$CA_CERT" \
9987
-CAkey "$CA_KEY" \
@@ -106,11 +94,18 @@ EOF
10694

10795
generate_client_signing_key() {
10896
local hostname="$1"
97+
10998
echo
11099
echo "Generating private key for $hostname..."
100+
111101
local clientDir="$TLS_DIR/$hostname"
112102
mkdir -p "$clientDir"
113-
run_openssl ecparam -name secp256k1 -genkey -noout -param_enc explicit \
103+
104+
openssl ecparam \
105+
-name secp256k1 \
106+
-genkey \
107+
-noout \
108+
-param_enc explicit \
114109
-out "$clientDir/$CLIENT_PRIVATE_KEY"
115110
}
116111

@@ -148,12 +143,7 @@ fi
148143
TARGET="$1"; shift
149144

150145
echo "----------------------------------------"
151-
echo "!!!! DO NOT USE IN PRODUCTION !!!!!"
152-
echo "This script is meant for development/testing ONLY."
153-
echo "Generating credentials..."
154-
echo
155-
echo "Target: $TARGET"
156-
echo "Using Docker: $USE_DOCKER"
146+
echo "Generating credentials for $TARGET"
157147
echo "----------------------------------------"
158148

159149
mkdir -p "$TLS_DIR"
@@ -184,6 +174,5 @@ case "$TARGET" in
184174
esac
185175

186176
echo "----------------------------------------"
187-
echo
188177
echo "Credentials generated successfully."
189178
echo "----------------------------------------"

0 commit comments

Comments
 (0)