Smallstep CLI 项目教程:零信任多功能工具完全指南

【免费下载链接】cli 🧰 A zero trust swiss army knife for working with X509, OAuth, JWT, OATH OTP, etc. 【免费下载链接】cli 项目地址: https://gitcode.com/gh_mirrors/cli8/cli

概述

Smallstep CLI(简称 step)是一个功能强大的命令行工具,专为构建、操作和自动化公钥基础设施(PKI,Public Key Infrastructure)系统和工作流而设计。它被誉为"零信任多功能工具",支持 X.509、OAuth、JWT、OATH OTP 等多种加密和安全协议。

🔑 核心价值step 让复杂的加密操作变得简单易用,无论是个人开发者还是企业团队,都能轻松管理数字证书和身份验证流程。

安装与配置

系统要求

  • 操作系统:Linux、macOS、Windows
  • 依赖:Go 1.16+(从源码编译时)
  • 架构:x86_64、ARM64

安装方法

# 使用包管理器安装(推荐)
# Ubuntu/Debian
curl -LO https://github.com/smallstep/cli/releases/latest/download/step-cli_linux_amd64.deb
sudo dpkg -i step-cli_linux_amd64.deb

# CentOS/RHEL
curl -LO https://github.com/smallstep/cli/releases/latest/download/step-cli_linux_amd64.rpm
sudo rpm -i step-cli_linux_amd64.rpm

# macOS (Homebrew)
brew install step

# 从源码编译
git clone https://gitcode.com/gh_mirrors/cli8/cli
cd cli
make build
sudo make install

验证安装

step --version
step --help

核心功能模块

1. 证书管理(Certificate Management)

step certificate 命令组提供完整的 X.509 证书生命周期管理:

mermaid

创建根证书颁发机构(CA)
# 创建根CA证书和私钥(默认使用EC P-256曲线)
step certificate create my-root-ca root-ca.crt root-ca.key --profile root-ca

# 使用RSA 4096位密钥
step certificate create my-root-ca root-ca.crt root-ca.key \
  --profile root-ca \
  --kty RSA \
  --size 4096
创建中间CA证书
step certificate create intermediate-ca intermediate.crt intermediate.key \
  --ca root-ca.crt \
  --ca-key root-ca.key \
  --profile intermediate-ca
签发终端证书
step certificate create example.com example.crt example.key \
  --ca intermediate.crt \
  --ca-key intermediate.key \
  --san example.com \
  --san www.example.com
证书检查与验证
# 查看证书详细信息
step certificate inspect example.crt

# 验证证书链
step certificate verify example.crt --roots root-ca.crt

# 检查证书是否需要更新
step certificate needs-renewal example.crt --expires-in 720h

2. 加密工具箱(Crypto Toolkit)

step crypto 提供丰富的加密原语操作:

JWT(JSON Web Token)操作
# 创建JWT
step crypto jwt sign --iss "my-app" --sub "user123" --key key.jwk

# 验证JWT
step crypto jwt verify --jwks jwks.json "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."

# 解析JWT内容
step crypto jwt inspect "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
JWK(JSON Web Key)管理
# 创建不同类型的JWK
step crypto jwk create ec.jwk --kty EC --curve P-256
step crypto jwk create rsa.jwk --kty RSA --size 2048
step crypto jwk create oct.jwk --kty oct --size 256

# 提取公钥
step crypto jwk public ec.jwk ec.pub.jwk

# 管理密钥集
step crypto jwk keyset create my-keyset.json ec.jwk rsa.jwk
OTP(一次性密码)生成
# 生成TOTP令牌
step crypto otp generate --secret "JBSWY3DPEHPK3PXP"

# 验证TOTP令牌
step crypto otp verify --secret "JBSWY3DPEHPK3PXP" 123456

3. OAuth 2.0 集成

step oauth 命令为CLI应用添加OAuth 2.0单点登录流程:

mermaid

# 获取OAuth访问令牌
step oauth --provider google --client-id YOUR_CLIENT_ID --client-secret YOUR_SECRET

# 使用自定义范围
step oauth --provider github --scope "user:email,repo"

4. SSH证书管理

step ssh 提供SSH证书的创建和管理:

# 创建SSH用户证书
step ssh certificate user@example.com user-cert.pub --sign --host example.com

# 创建SSH主机证书
step ssh certificate host.example.com host-cert.pub --sign --host

# 检查SSH证书
step ssh inspect user-cert.pub

实战案例

案例1:搭建内部PKI基础设施

#!/bin/bash
# 创建根CA
step certificate create Internal-Root-CA root-ca.crt root-ca.key --profile root-ca

# 创建中间CA
step certificate create Internal-Intermediate intermediate.crt intermediate.key \
  --ca root-ca.crt --ca-key root-ca.key --profile intermediate-ca

# 为服务创建证书
step certificate create api.internal.com api.crt api.key \
  --ca intermediate.crt --ca-key intermediate.key \
  --san api.internal.com --san "*.internal.com"

# 安装根证书到系统信任库
step certificate install root-ca.crt

案例2:自动化证书轮换

#!/bin/bash
CERT_FILE="app.crt"
KEY_FILE="app.key"
CA_CERT="intermediate.crt"
CA_KEY="intermediate.key"

# 检查证书是否需要更新
if step certificate needs-renewal "$CERT_FILE" --expires-in 168h; then
    echo "证书即将过期,开始更新..."
    
    # 创建新的证书签名请求
    step certificate create app-new app.csr app-new.key --csr
    
    # 签发新证书
    step certificate sign app.csr "$CA_CERT" "$CA_KEY" --out app-new.crt
    
    # 替换旧证书
    mv app-new.crt "$CERT_FILE"
    mv app-new.key "$KEY_FILE"
    
    # 重启服务
    systemctl restart nginx
    echo "证书更新完成"
fi

案例3:JWT-based API认证

#!/bin/bash
# 创建用于API认证的JWK
step crypto jwk create api-auth.jwk --kty EC --curve P-256

# 生成JWT令牌
TOKEN=$(step crypto jwt sign \
  --iss "api-server" \
  --sub "service-account" \
  --aud "https://api.example.com" \
  --exp $(date -d "+1 hour" +%s) \
  --key api-auth.jwk)

# 使用令牌调用API
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/protected

安全最佳实践

密钥管理策略

密钥类型 推荐算法 密钥长度 使用场景
对称密钥 AES 256位 数据加密
RSA密钥 RSA 3072位+ 证书签名
EC密钥 ECDSA P-256/P-384 高效签名
EdDSA Ed25519 256位 现代应用

证书生命周期管理

mermaid

安全配置建议

  1. 密钥保护

    # 设置合适的文件权限
    chmod 600 *.key
    chmod 644 *.crt
    
    # 使用密码保护私钥
    step crypto change-pass private.key
    
  2. 证书策略

    # 设置合理的证书有效期
    step certificate create --not-after 8760h  # 1年
    
    # 启用CRL(证书撤销列表)
    step ca crl generate --ca-config ca.json
    
  3. 审计日志

    # 记录所有证书操作
    step certificate inspect --format json example.crt | jq .
    

故障排除与调试

常见问题解决

问题现象 可能原因 解决方案
证书验证失败 证书链不完整 使用 step certificate bundle 打包完整链
JWT验证错误 算法不匹配 检查JWK的alg参数与JWT头部是否一致
OAuth流程中断 重定向URI配置错误 检查OAuth提供商的重定向URI设置
SSH证书无效 时间不同步 同步系统时间,检查证书有效期

调试技巧

# 启用详细输出
step --verbose certificate inspect example.crt

# 检查证书链
step certificate verify --verbose example.crt

# 调试OAuth流程
step oauth --debug --provider google

性能优化

批量操作处理

# 批量签发证书
for domain in api web db cache; do
    step certificate create "$domain.example.com" \
      "${domain}.crt" "${domain}.key" \
      --ca intermediate.crt --ca-key intermediate.key \
      --san "$domain.example.com" &
done
wait

# 并行验证多个证书
find . -name "*.crt" -print0 | xargs -0 -P 4 -I {} step certificate verify {} --roots root-ca.crt

缓存优化

# 使用内存缓存加速重复操作
step certificate inspect --cache --ttl 300 example.crt

# 预加载JWK集合
step crypto jwk keyset preload jwks.json

扩展与集成

与CI/CD流水线集成

# GitHub Actions示例
name: Certificate Renewal
on:
  schedule:
    - cron: '0 0 * * 0'  # 每周日午夜运行

jobs:
  renew-certs:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup step CLI
      run: |
        wget https://github.com/smallstep/cli/releases/latest/download/step-cli_linux_amd64.deb
        sudo dpkg -i step-cli_linux_amd64.deb
    - name: Renew certificates
      run: |
        step certificate renew --all
      env:
        STEP_CA_URL: ${{ secrets.CA_URL }}
        STEP_CA_TOKEN: ${{ secrets.CA_TOKEN }}

监控与告警

#!/bin/bash
# 证书过期监控脚本
CERT_FILE="/etc/ssl/certs/example.crt"
EXPIRY_DAYS=7

if step certificate needs-renewal "$CERT_FILE" --expires-in "${EXPIRY_DAYS}d"; then
    # 发送告警通知
    echo "警告: 证书 $CERT_FILE 将在 $EXPIRY_DAYS 天内过期" | \
    mail -s "证书过期警告" admin@example.com
fi

总结

Smallstep CLI 是一个功能全面、设计精良的加密工具集,它让复杂的PKI和加密操作变得简单易用。通过本教程,您应该能够:

  • ✅ 掌握 step 工具的安装和基本配置
  • ✅ 理解各功能模块的核心用途和最佳实践
  • ✅ 在实际场景中应用证书管理、加密操作和OAuth集成
  • ✅ 实施安全可靠的密钥和证书生命周期管理
  • ✅ 将 step 集成到自动化流程和监控系统中

无论是开发测试环境还是生产系统,Smallstep CLI 都能为您提供企业级的加密和安全保障。开始使用这个强大的工具,提升您的应用程序安全水平吧!

【免费下载链接】cli 🧰 A zero trust swiss army knife for working with X509, OAuth, JWT, OATH OTP, etc. 【免费下载链接】cli 项目地址: https://gitcode.com/gh_mirrors/cli8/cli

Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐