Skip to content

Commit 757febf

Browse files
runcomclaude
andcommitted
Consolidate duplicate code in delegate tests
Move common delegate test functions and constants to test/utils/certs.sh: - generate_delegate_cert() function - OID_PERMIT_* constants (REDIRECT, ONBOARD_NEW_CRED, ONBOARD_REUSE_CRED) Remove duplications from individual test files: - test-delegate-onboarding.sh: Remove local generate_delegate_cert and OID - test-delegate-invalid-chain.sh: Remove duplicate OID constant - test-delegate-permissions.sh: Remove duplicate OID constant All tests now source certs.sh (directly or transitively) for these utilities. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 277fa43 commit 757febf

4 files changed

Lines changed: 48 additions & 51 deletions

File tree

test/ci/test-delegate-invalid-chain.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ set -euo pipefail
88

99
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/test-delegate-onboarding.sh"
1010

11-
# FDO delegate permission OIDs (re-declare for clarity)
12-
OID_PERMIT_ONBOARD_NEW_CRED="1.3.6.1.4.1.45724.3.1.2"
13-
1411
# Paths for the "wrong" key that will sign the delegate cert
1512
wrong_signer_key="${certs_dir}/wrong_signer.key"
1613
wrong_signer_crt="${certs_dir}/wrong_signer.crt"

test/ci/test-delegate-onboarding.sh

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,16 @@ set -euo pipefail
77

88
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/utils.sh"
99
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/../utils/mgmt-api-v1.sh"
10-
11-
# FDO delegate permission OIDs
12-
OID_PERMIT_ONBOARD_NEW_CRED="1.3.6.1.4.1.45724.3.1.2"
10+
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/../utils/certs.sh"
1311

1412
# Delegate and config paths
1513
delegate_key="${certs_dir}/delegate.key"
1614
delegate_crt="${certs_dir}/delegate.crt"
17-
delegate_cnf="${certs_dir}/delegate.cnf"
18-
delegate_csr="${certs_dir}/delegate.csr"
1915
configs_dir="${base_dir}/configs"
2016
owner_config_file="${configs_dir}/owner.yaml"
2117

2218
directories+=("${configs_dir}")
2319

24-
generate_delegate_cert() {
25-
local owner_key_path=$1
26-
local owner_crt_path=$2
27-
local delegate_key_path=$3
28-
local delegate_crt_path=$4
29-
local permissions_oid=$5
30-
31-
local delegate_cnf_path="${delegate_key_path%.key}.cnf"
32-
local delegate_csr_path="${delegate_key_path%.key}.csr"
33-
34-
log_info "Generating delegate EC key in DER format (same as other service keys)"
35-
openssl ecparam -name prime256v1 -genkey -outform der -out "${delegate_key_path}" 2>/dev/null
36-
37-
log_info "Creating delegate openssl config with FDO permission OID ${permissions_oid}"
38-
cat >"${delegate_cnf_path}" <<EOF
39-
[ext]
40-
basicConstraints = critical, CA:FALSE
41-
keyUsage = critical, digitalSignature
42-
extendedKeyUsage = ${permissions_oid}
43-
EOF
44-
45-
log_info "Generating delegate CSR"
46-
openssl req -new -key "${delegate_key_path}" -keyform der -out "${delegate_csr_path}" -subj "/CN=FDO Delegate" 2>/dev/null
47-
48-
log_info "Signing delegate cert with owner key"
49-
openssl x509 -req \
50-
-in "${delegate_csr_path}" \
51-
-CA "${owner_crt_path}" \
52-
-CAkey "${owner_key_path}" \
53-
-CAkeyform der \
54-
-CAcreateserial \
55-
-out "${delegate_crt_path}" \
56-
-days 30 \
57-
-extfile "${delegate_cnf_path}" \
58-
-extensions ext 2>/dev/null
59-
60-
log_info "Delegate certificate generated:"
61-
openssl x509 -in "${delegate_crt_path}" -noout -subject -issuer -dates >&2
62-
}
63-
6420
start_service_owner() {
6521
log_info "Writing owner config with delegate settings"
6622
cat >"${owner_config_file}" <<EOF

test/ci/test-delegate-permissions.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ set -euo pipefail
88

99
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/test-delegate-onboarding.sh"
1010

11-
# OIDPermitRedirect only (no onboard permission)
12-
OID_PERMIT_REDIRECT="1.3.6.1.4.1.45724.3.1.1"
13-
1411
run_test() {
1512

1613
log_info "Setting the error trap handler"

test/utils/certs.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#! /usr/bin/env bash
22

3+
# FDO delegate permission OIDs
4+
readonly OID_PERMIT_REDIRECT="1.3.6.1.4.1.45724.3.1.1"
5+
readonly OID_PERMIT_ONBOARD_NEW_CRED="1.3.6.1.4.1.45724.3.1.2"
6+
readonly OID_PERMIT_ONBOARD_REUSE_CRED="1.3.6.1.4.1.45724.3.1.3"
7+
38
generate_cert() {
49
local key=$1
510
local crt=$2
@@ -21,3 +26,45 @@ extract_pubkey_from_cert() {
2126
openssl x509 -in "${crt}" -pubkey -noout -out "${pub}"
2227
fi
2328
}
29+
30+
# Generate a delegate certificate signed by the owner key
31+
# Usage: generate_delegate_cert owner_key owner_crt delegate_key delegate_crt permissions_oid
32+
generate_delegate_cert() {
33+
local owner_key_path=$1
34+
local owner_crt_path=$2
35+
local delegate_key_path=$3
36+
local delegate_crt_path=$4
37+
local permissions_oid=$5
38+
39+
local delegate_cnf_path="${delegate_key_path%.key}.cnf"
40+
local delegate_csr_path="${delegate_key_path%.key}.csr"
41+
42+
log_info "Generating delegate EC key in DER format (same as other service keys)"
43+
openssl ecparam -name prime256v1 -genkey -outform der -out "${delegate_key_path}" 2>/dev/null
44+
45+
log_info "Creating delegate openssl config with FDO permission OID ${permissions_oid}"
46+
cat >"${delegate_cnf_path}" <<EOF
47+
[ext]
48+
basicConstraints = critical, CA:FALSE
49+
keyUsage = critical, digitalSignature
50+
extendedKeyUsage = ${permissions_oid}
51+
EOF
52+
53+
log_info "Generating delegate CSR"
54+
openssl req -new -key "${delegate_key_path}" -keyform der -out "${delegate_csr_path}" -subj "/CN=FDO Delegate" 2>/dev/null
55+
56+
log_info "Signing delegate cert with owner key"
57+
openssl x509 -req \
58+
-in "${delegate_csr_path}" \
59+
-CA "${owner_crt_path}" \
60+
-CAkey "${owner_key_path}" \
61+
-CAkeyform der \
62+
-CAcreateserial \
63+
-out "${delegate_crt_path}" \
64+
-days 30 \
65+
-extfile "${delegate_cnf_path}" \
66+
-extensions ext 2>/dev/null
67+
68+
log_info "Delegate certificate generated:"
69+
openssl x509 -in "${delegate_crt_path}" -noout -subject -issuer -dates >&2
70+
}

0 commit comments

Comments
 (0)