install-ca.sh
· 4.0 KiB · Bash
Неформатований
#!/usr/bin/env bash
set -euo pipefail
CA_URL="${CA_URL:-https://10.10.40.53}"
CA_FINGERPRINT="${CA_FINGERPRINT:-5fc8c379cab1119c1a9ac7038225f6bcf3a2ffb0e71b257af796b2bd6c71d594}"
FORCE="${FORCE:-0}"
log() {
printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
}
fail() {
echo "ERROR: $*" >&2
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1
}
is_root() {
[ "$(id -u)" -eq 0 ]
}
as_root() {
if is_root; then
"$@"
else
sudo "$@"
fi
}
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
MINGW*|MSYS*|CYGWIN*) echo "windows_bash" ;;
*) echo "unknown" ;;
esac
}
install_step() {
if need_cmd step; then
log "step CLI already installed"
return
fi
case "$(detect_os)" in
darwin)
need_cmd brew || fail "Homebrew not found"
log "Installing step with Homebrew"
brew install step
;;
linux)
if need_cmd apt-get; then
log "Installing step-cli with apt"
as_root apt-get update
as_root apt-get install -y --no-install-recommends curl gpg ca-certificates
as_root mkdir -p /etc/apt/keyrings
curl -fsSL https://packages.smallstep.com/keys/apt/repo-signing-key.gpg | \
as_root tee /etc/apt/keyrings/smallstep.asc >/dev/null
tmpfile="$(mktemp)"
cat > "$tmpfile" <<'EOF'
Types: deb
URIs: https://packages.smallstep.com/stable/debian
Suites: debs
Components: main
Signed-By: /etc/apt/keyrings/smallstep.asc
EOF
as_root cp "$tmpfile" /etc/apt/sources.list.d/smallstep.sources
rm -f "$tmpfile"
as_root apt-get update
as_root apt-get install -y step-cli
elif need_cmd apk; then
log "Installing step-cli with apk"
as_root apk add --no-cache step-cli
elif need_cmd dnf; then
log "Installing step-cli with dnf"
tmpfile="$(mktemp)"
cat > "$tmpfile" <<'EOF'
[smallstep]
name=Smallstep
baseurl=https://packages.smallstep.com/stable/fedora/
enabled=1
repo_gpgcheck=0
gpgcheck=1
gpgkey=https://packages.smallstep.com/keys/smallstep-0x889B19391F774443.gpg
EOF
as_root cp "$tmpfile" /etc/yum.repos.d/smallstep.repo
rm -f "$tmpfile"
as_root dnf makecache
as_root dnf install -y step-cli
elif need_cmd pacman; then
log "Installing step-cli with pacman"
as_root pacman -Sy --noconfirm step-cli
if [ ! -e /usr/local/bin/step ] && [ -x /usr/bin/step-cli ]; then
as_root ln -s /usr/bin/step-cli /usr/local/bin/step
fi
else
fail "No supported package manager found"
fi
;;
*)
fail "Unsupported OS"
;;
esac
need_cmd step || fail "step CLI installation failed"
}
bootstrap_step() {
if [ "$FORCE" = "1" ]; then
rm -rf "$HOME/.step"
fi
log "Bootstrapping against $CA_URL"
step ca bootstrap \
--ca-url "$CA_URL" \
--fingerprint "$CA_FINGERPRINT" \
--install \
--force
}
install_linux_trust() {
local root_cert="$HOME/.step/certs/root_ca.crt"
[ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
if need_cmd update-ca-certificates; then
as_root mkdir -p /usr/local/share/ca-certificates
as_root cp "$root_cert" /usr/local/share/ca-certificates/insmw-root-ca.crt
as_root update-ca-certificates
return
fi
if need_cmd trust; then
as_root trust anchor "$root_cert"
return
fi
fail "Could not determine Linux trust-store tool"
}
install_macos_trust() {
local root_cert="$HOME/.step/certs/root_ca.crt"
[ -f "$root_cert" ] || fail "Root certificate not found at $root_cert"
as_root security add-trusted-cert \
-d \
-r trustRoot \
-k /Library/Keychains/System.keychain \
"$root_cert"
}
install_trust_store() {
case "$(detect_os)" in
linux) install_linux_trust ;;
darwin) install_macos_trust ;;
*) fail "Unsupported OS for trust-store installation" ;;
esac
}
main() {
install_step
bootstrap_step
install_trust_store
log "Done"
}
main "$@"
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | CA_URL="${CA_URL:-https://10.10.40.53}" |
| 5 | CA_FINGERPRINT="${CA_FINGERPRINT:-5fc8c379cab1119c1a9ac7038225f6bcf3a2ffb0e71b257af796b2bd6c71d594}" |
| 6 | FORCE="${FORCE:-0}" |
| 7 | |
| 8 | log() { |
| 9 | printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" |
| 10 | } |
| 11 | |
| 12 | fail() { |
| 13 | echo "ERROR: $*" >&2 |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | need_cmd() { |
| 18 | command -v "$1" >/dev/null 2>&1 |
| 19 | } |
| 20 | |
| 21 | is_root() { |
| 22 | [ "$(id -u)" -eq 0 ] |
| 23 | } |
| 24 | |
| 25 | as_root() { |
| 26 | if is_root; then |
| 27 | "$@" |
| 28 | else |
| 29 | sudo "$@" |
| 30 | fi |
| 31 | } |
| 32 | |
| 33 | detect_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 | |
| 42 | install_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' |
| 66 | Types: deb |
| 67 | URIs: https://packages.smallstep.com/stable/debian |
| 68 | Suites: debs |
| 69 | Components: main |
| 70 | Signed-By: /etc/apt/keyrings/smallstep.asc |
| 71 | EOF |
| 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] |
| 87 | name=Smallstep |
| 88 | baseurl=https://packages.smallstep.com/stable/fedora/ |
| 89 | enabled=1 |
| 90 | repo_gpgcheck=0 |
| 91 | gpgcheck=1 |
| 92 | gpgkey=https://packages.smallstep.com/keys/smallstep-0x889B19391F774443.gpg |
| 93 | EOF |
| 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 | |
| 120 | bootstrap_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 | |
| 133 | install_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 | |
| 152 | install_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 | |
| 163 | install_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 | |
| 171 | main() { |
| 172 | install_step |
| 173 | bootstrap_step |
| 174 | install_trust_store |
| 175 | log "Done" |
| 176 | } |
| 177 | |
| 178 | main "$@" |