Utoljára aktív 1 month ago

install-ca.sh Eredeti
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}"
7SERVICE_CERT_DIR="${SERVICE_CERT_DIR:-/etc/insmw/certs}"
8
9log() {
10 printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
11}
12
13fail() {
14 echo "ERROR: $*" >&2
15 exit 1
16}
17
18need_cmd() {
19 command -v "$1" >/dev/null 2>&1
20}
21
22is_root() {
23 [ "$(id -u)" -eq 0 ]
24}
25
26as_root() {
27 if is_root; then
28 "$@"
29 else
30 sudo "$@"
31 fi
32}
33
34real_user() {
35 if [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER}" != "root" ]; then
36 printf '%s\n' "$SUDO_USER"
37 else
38 id -un
39 fi
40}
41
42real_home() {
43 local user
44 user="$(real_user)"
45 eval echo "~$user"
46}
47
48detect_os() {
49 case "$(uname -s)" in
50 Linux*) echo "linux" ;;
51 Darwin*) echo "darwin" ;;
52 MINGW*|MSYS*|CYGWIN*) echo "windows_bash" ;;
53 *) echo "unknown" ;;
54 esac
55}
56
57install_step() {
58 if need_cmd step; then
59 log "step CLI already installed"
60 return
61 fi
62
63 case "$(detect_os)" in
64 darwin)
65 need_cmd brew || fail "Homebrew not found"
66 log "Installing step with Homebrew"
67 brew install step
68 ;;
69
70 linux)
71 if need_cmd apt-get; then
72 log "Installing step-cli with apt"
73 as_root apt-get update
74 as_root apt-get install -y --no-install-recommends curl gpg ca-certificates
75 as_root mkdir -p /etc/apt/keyrings
76 curl -fsSL https://packages.smallstep.com/keys/apt/repo-signing-key.gpg | \
77 as_root tee /etc/apt/keyrings/smallstep.asc >/dev/null
78
79 tmpfile="$(mktemp)"
80 cat > "$tmpfile" <<'EOF'
81Types: deb
82URIs: https://packages.smallstep.com/stable/debian
83Suites: debs
84Components: main
85Signed-By: /etc/apt/keyrings/smallstep.asc
86EOF
87 as_root cp "$tmpfile" /etc/apt/sources.list.d/smallstep.sources
88 rm -f "$tmpfile"
89
90 as_root apt-get update
91 as_root apt-get install -y step-cli
92
93 elif need_cmd apk; then
94 log "Installing step-cli with apk"
95 as_root apk add --no-cache step-cli
96
97 elif need_cmd dnf; then
98 log "Installing step-cli with dnf"
99 tmpfile="$(mktemp)"
100 cat > "$tmpfile" <<'EOF'
101[smallstep]
102name=Smallstep
103baseurl=https://packages.smallstep.com/stable/fedora/
104enabled=1
105repo_gpgcheck=0
106gpgcheck=1
107gpgkey=https://packages.smallstep.com/keys/smallstep-0x889B19391F774443.gpg
108EOF
109 as_root cp "$tmpfile" /etc/yum.repos.d/smallstep.repo
110 rm -f "$tmpfile"
111
112 as_root dnf makecache
113 as_root dnf install -y step-cli
114
115 elif need_cmd pacman; then
116 log "Installing step-cli with pacman"
117 as_root pacman -Sy --noconfirm step-cli
118 if [ ! -e /usr/local/bin/step ] && [ -x /usr/bin/step-cli ]; then
119 as_root ln -s /usr/bin/step-cli /usr/local/bin/step
120 fi
121
122 else
123 fail "No supported package manager found"
124 fi
125 ;;
126
127 *)
128 fail "Unsupported OS"
129 ;;
130 esac
131
132 need_cmd step || fail "step CLI installation failed"
133}
134
135bootstrap_step() {
136 local user
137 local user_home
138
139 user="$(real_user)"
140 user_home="$(real_home)"
141
142 if [ "$FORCE" = "1" ]; then
143 rm -rf "$user_home/.step"
144 fi
145
146 log "Bootstrapping as user $user against $CA_URL"
147
148 if is_root && [ "$user" != "root" ]; then
149 sudo -u "$user" HOME="$user_home" step ca bootstrap \
150 --ca-url "$CA_URL" \
151 --fingerprint "$CA_FINGERPRINT" \
152 --install \
153 --force
154 else
155 HOME="$user_home" step ca bootstrap \
156 --ca-url "$CA_URL" \
157 --fingerprint "$CA_FINGERPRINT" \
158 --install \
159 --force
160 fi
161}
162
163install_linux_trust() {
164 local root_cert
165 root_cert="$(real_home)/.step/certs/root_ca.crt"
166
167 [ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
168
169 if need_cmd update-ca-certificates; then
170 as_root mkdir -p /usr/local/share/ca-certificates
171 as_root cp "$root_cert" /usr/local/share/ca-certificates/insmw-root-ca.crt
172 as_root update-ca-certificates
173 return
174 fi
175
176 if need_cmd trust; then
177 as_root trust anchor "$root_cert"
178 return
179 fi
180
181 fail "Could not determine Linux trust-store tool"
182}
183
184install_macos_trust() {
185 local root_cert
186 root_cert="$(real_home)/.step/certs/root_ca.crt"
187
188 [ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
189
190 as_root security add-trusted-cert \
191 -d \
192 -r trustRoot \
193 -k /Library/Keychains/System.keychain \
194 "$root_cert"
195}
196
197install_trust_store() {
198 case "$(detect_os)" in
199 linux) install_linux_trust ;;
200 darwin) install_macos_trust ;;
201 *) fail "Unsupported OS for trust-store installation" ;;
202 esac
203}
204
205install_service_cert() {
206 local root_cert
207 root_cert="$(real_home)/.step/certs/root_ca.crt"
208
209 [ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
210
211 as_root mkdir -p "$SERVICE_CERT_DIR"
212 as_root cp "$root_cert" "$SERVICE_CERT_DIR/root_ca.crt"
213 as_root chmod 644 "$SERVICE_CERT_DIR/root_ca.crt"
214 as_root chmod 755 "$(dirname "$SERVICE_CERT_DIR")" "$SERVICE_CERT_DIR"
215}
216
217main() {
218 install_step
219 bootstrap_step
220 install_trust_store
221 install_service_cert
222 log "Done"
223}
224
225main "$@"