← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Scripts/signing-keychain.sh (3480B)

1#!/bin/bash
2# Shared helper for scripts that need signing identities available to a headless
3# build. Source this file, set TMP_KEYCHAIN, then:
4#
5#     cleanup() { restore_signing_keychain; }
6#     trap cleanup EXIT
7#     signing_keychain_begin
8#     signing_keychain_import "$DEV_P12" "$DEV_P12_PASS"
9#     signing_keychain_activate
10#
11# Install the trap BEFORE calling signing_keychain_begin: any step below can die
12# under `set -e`, and cleanup is written to tolerate a partially built setup.
13#
14# Requires TMP_KEYCHAIN_PASS from .asc/secrets.sh.
15
16SIGNING_KEYCHAIN_ORIGINAL_LIST=()
17SIGNING_KEYCHAIN_ORIGINAL_DEFAULT=""
18SIGNING_KEYCHAIN_ACTIVE=false
19
20# `security` prints each keychain path quoted and indented; strip both.
21_signing_keychain_unquote() {
22    local line="$1"
23    line="${line#"${line%%[![:space:]]*}"}"
24    line="${line%\"}"
25    line="${line#\"}"
26    printf '%s' "$line"
27}
28
29signing_keychain_begin() {
30    : "${TMP_KEYCHAIN:?TMP_KEYCHAIN must be set before signing_keychain_begin}"
31    : "${TMP_KEYCHAIN_PASS:?TMP_KEYCHAIN_PASS must be set (see .asc/secrets.sh)}"
32
33    # Capture the caller's exact keychain configuration before touching anything,
34    # so cleanup restores their real prior state rather than assuming login-only.
35    while IFS= read -r line; do
36        [[ -n "$line" ]] && SIGNING_KEYCHAIN_ORIGINAL_LIST+=("$(_signing_keychain_unquote "$line")")
37    done < <(security list-keychains -d user)
38
39    SIGNING_KEYCHAIN_ORIGINAL_DEFAULT="$(_signing_keychain_unquote \
40        "$(security default-keychain -d user 2>/dev/null || true)")"
41
42    if [[ ${#SIGNING_KEYCHAIN_ORIGINAL_LIST[@]} -eq 0 ]]; then
43        echo "Error: could not read the current keychain search list; refusing to modify it." >&2
44        return 1
45    fi
46
47    # Mark active before creating anything, so a failure part-way through setup
48    # still gets cleaned up.
49    SIGNING_KEYCHAIN_ACTIVE=true
50
51    echo "==> Setting up temporary keychain..."
52    security delete-keychain "$TMP_KEYCHAIN" 2>/dev/null || true
53    security create-keychain -p "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
54    security unlock-keychain -p "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
55    security set-keychain-settings -lut 21600 "$TMP_KEYCHAIN"
56}
57
58signing_keychain_import() {
59    local p12="$1"
60    local pass="$2"
61    if [[ ! -f "$p12" ]]; then
62        echo "Error: signing identity not found at $p12" >&2
63        return 1
64    fi
65    security import "$p12" -k "$TMP_KEYCHAIN" -P "$pass" \
66        -T /usr/bin/codesign -T /usr/bin/security -T /usr/bin/productbuild
67}
68
69signing_keychain_activate() {
70    security set-key-partition-list -S apple-tool:,apple:,codesign:,productbuild: \
71        -s -k "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN" >/dev/null
72    # Prepend rather than replace: resetting the list to a hardcoded
73    # login.keychain-db silently discards any other keychains the user had.
74    security list-keychains -d user -s "$TMP_KEYCHAIN" "${SIGNING_KEYCHAIN_ORIGINAL_LIST[@]}"
75}
76
77restore_signing_keychain() {
78    $SIGNING_KEYCHAIN_ACTIVE || return 0
79    echo "==> Restoring keychain search list..."
80    if [[ ${#SIGNING_KEYCHAIN_ORIGINAL_LIST[@]} -gt 0 ]]; then
81        security list-keychains -d user -s "${SIGNING_KEYCHAIN_ORIGINAL_LIST[@]}" || true
82    fi
83    if [[ -n "$SIGNING_KEYCHAIN_ORIGINAL_DEFAULT" ]]; then
84        security default-keychain -d user -s "$SIGNING_KEYCHAIN_ORIGINAL_DEFAULT" || true
85    fi
86    security delete-keychain "$TMP_KEYCHAIN" 2>/dev/null || true
87    SIGNING_KEYCHAIN_ACTIVE=false
88}