install-ca.sh
· 3.7 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
}
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"
sudo apt-get update
sudo apt-get install -y --no-install-recommends curl gpg ca-certificates
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://packages.smallstep.com/keys/apt/repo-signing-key.gpg | \
sudo tee /etc/apt/keyrings/smallstep.asc >/dev/null
cat <<'EOF' | sudo tee /etc/apt/sources.list.d/smallstep.sources >/dev/null
Types: deb
URIs: https://packages.smallstep.com/stable/debian
Suites: debs
Components: main
Signed-By: /etc/apt/keyrings/smallstep.asc
EOF
sudo apt-get update
sudo apt-get install -y step-cli
elif need_cmd apk; then
log "Installing step-cli with apk"
sudo apk add --no-cache step-cli
elif need_cmd dnf; then
log "Installing step-cli with dnf"
cat <<'EOF' | sudo tee /etc/yum.repos.d/smallstep.repo >/dev/null
[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
sudo dnf makecache
sudo dnf install -y step-cli
elif need_cmd pacman; then
log "Installing step-cli with pacman"
sudo pacman -Sy --noconfirm step-cli
if [ ! -e /usr/local/bin/step ] && [ -x /usr/bin/step-cli ]; then
sudo 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
sudo mkdir -p /usr/local/share/ca-certificates
sudo cp "$root_cert" /usr/local/share/ca-certificates/insmw-root-ca.crt
sudo update-ca-certificates
return
fi
if need_cmd trust; then
sudo 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"
sudo 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 | detect_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 | |
| 30 | install_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 |
| 51 | Types: deb |
| 52 | URIs: https://packages.smallstep.com/stable/debian |
| 53 | Suites: debs |
| 54 | Components: main |
| 55 | Signed-By: /etc/apt/keyrings/smallstep.asc |
| 56 | EOF |
| 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] |
| 66 | name=Smallstep |
| 67 | baseurl=https://packages.smallstep.com/stable/fedora/ |
| 68 | enabled=1 |
| 69 | repo_gpgcheck=0 |
| 70 | gpgcheck=1 |
| 71 | gpgkey=https://packages.smallstep.com/keys/smallstep-0x889B19391F774443.gpg |
| 72 | EOF |
| 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 | |
| 93 | bootstrap_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 | |
| 106 | install_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 | |
| 125 | install_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 | |
| 136 | install_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 | |
| 144 | main() { |
| 145 | install_step |
| 146 | bootstrap_step |
| 147 | install_trust_store |
| 148 | log "Done" |
| 149 | } |
| 150 | |
| 151 | main "$@" |