Dernière activité 7 hours ago

Révision 3e5e63dd478e49e6c35017cbd19d1909873cf03a

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