最終更新 7 hours ago

修正履歴 541e7c5f8c13564df50c6b531c6fb4e67f529028

install-ca.sh Raw
1#!/usr/bin/env bash
2set -euo pipefail
3
4CA_URL="${CA_URL:-https://10.10.40.53}"
5CA_FINGERPRINT="${CA_FINGERPRINT:-5fc8c379cab1119c1a9ac7038225f6bcf3a2ffb0e71b257af796b2bd6c71d594}"
6FORCE="${FORCE:-0}"
7
8log() {
9 printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
10}
11
12fail() {
13 echo "ERROR: $*" >&2
14 exit 1
15}
16
17need_cmd() {
18 command -v "$1" >/dev/null 2>&1
19}
20
21detect_os() {
22 case "$(uname -s)" in
23 Linux*) echo "linux" ;;
24 Darwin*) echo "darwin" ;;
25 MINGW*|MSYS*|CYGWIN*) echo "windows_bash" ;;
26 *) echo "unknown" ;;
27 esac
28}
29
30install_step() {
31 if need_cmd step; then
32 log "step CLI already installed"
33 return
34 fi
35
36 case "$(detect_os)" in
37 darwin)
38 need_cmd brew || fail "Homebrew not found"
39 log "Installing step with Homebrew"
40 brew install step
41 ;;
42 linux)
43 if need_cmd apt-get; then
44 log "Installing step-cli with apt"
45 sudo apt-get update
46 sudo apt-get install -y --no-install-recommends curl gpg ca-certificates
47 sudo mkdir -p /etc/apt/keyrings
48 curl -fsSL https://packages.smallstep.com/keys/apt/repo-signing-key.gpg | \
49 sudo tee /etc/apt/keyrings/smallstep.asc >/dev/null
50 cat <<'EOF' | sudo tee /etc/apt/sources.list.d/smallstep.sources >/dev/null
51Types: deb
52URIs: https://packages.smallstep.com/stable/debian
53Suites: debs
54Components: main
55Signed-By: /etc/apt/keyrings/smallstep.asc
56EOF
57 sudo apt-get update
58 sudo apt-get install -y step-cli
59 elif need_cmd apk; then
60 log "Installing step-cli with apk"
61 sudo apk add --no-cache step-cli
62 elif need_cmd dnf; then
63 log "Installing step-cli with dnf"
64 cat <<'EOF' | sudo tee /etc/yum.repos.d/smallstep.repo >/dev/null
65[smallstep]
66name=Smallstep
67baseurl=https://packages.smallstep.com/stable/fedora/
68enabled=1
69repo_gpgcheck=0
70gpgcheck=1
71gpgkey=https://packages.smallstep.com/keys/smallstep-0x889B19391F774443.gpg
72EOF
73 sudo dnf makecache
74 sudo dnf install -y step-cli
75 elif need_cmd pacman; then
76 log "Installing step-cli with pacman"
77 sudo pacman -Sy --noconfirm step-cli
78 if [ ! -e /usr/local/bin/step ] && [ -x /usr/bin/step-cli ]; then
79 sudo ln -s /usr/bin/step-cli /usr/local/bin/step
80 fi
81 else
82 fail "No supported package manager found"
83 fi
84 ;;
85 *)
86 fail "Unsupported OS"
87 ;;
88 esac
89
90 need_cmd step || fail "step CLI installation failed"
91}
92
93bootstrap_step() {
94 if [ "$FORCE" = "1" ]; then
95 rm -rf "$HOME/.step"
96 fi
97
98 log "Bootstrapping against $CA_URL"
99 step ca bootstrap \
100 --ca-url "$CA_URL" \
101 --fingerprint "$CA_FINGERPRINT" \
102 --install \
103 --force
104}
105
106install_linux_trust() {
107 local root_cert="$HOME/.step/certs/root_ca.crt"
108 [ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
109
110 if need_cmd update-ca-certificates; then
111 sudo mkdir -p /usr/local/share/ca-certificates
112 sudo cp "$root_cert" /usr/local/share/ca-certificates/insmw-root-ca.crt
113 sudo update-ca-certificates
114 return
115 fi
116
117 if need_cmd trust; then
118 sudo trust anchor "$root_cert"
119 return
120 fi
121
122 fail "Could not determine Linux trust-store tool"
123}
124
125install_macos_trust() {
126 local root_cert="$HOME/.step/certs/root_ca.crt"
127 [ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
128
129 sudo security add-trusted-cert \
130 -d \
131 -r trustRoot \
132 -k /Library/Keychains/System.keychain \
133 "$root_cert"
134}
135
136install_trust_store() {
137 case "$(detect_os)" in
138 linux) install_linux_trust ;;
139 darwin) install_macos_trust ;;
140 *) fail "Unsupported OS for trust-store installation" ;;
141 esac
142}
143
144main() {
145 install_step
146 bootstrap_step
147 install_trust_store
148 log "Done"
149}
150
151main "$@"