示例工程 53-code-signing-notarization

53-code-signing-notarization/app/shared/plans.ts

typescript · 5.8 KB

export type Step = {
  id: string;
  title: string;
  command: string;
  note: string;
  executes: false;
};

export type Plan = {
  id: "macos" | "windows" | "linux";
  title: string;
  summary: string;
  secrets: string[];
  tools: string[];
  steps: Step[];
  limits: string[];
};

const SIGN = "codesign";
const NOTARY = "notarytool";
const STAPLE = "stapler";
const WINSIGN = "signtool";
const GPG = "gpg";

export function buildPlans(): Plan[] {
  return [
    {
      id: "macos",
      title: "macOS · Developer ID + 公证",
      summary:
        "用 Developer ID Application 证书签名,开启 hardened runtime,再提交 Apple 公证并装订票据。",
      secrets: ["APPLE_ID", "APPLE_APP_SPECIFIC_PASSWORD", "APPLE_TEAM_ID"],
      tools: [SIGN, NOTARY, STAPLE, "spctl"],
      steps: [
        {
          id: "identity",
          title: "确认证书与 Team ID",
          command: `security find-identity -v -p codesigning`,
          note: "只读列出本机身份;确认存在 Developer ID Application,不要导出私钥。",
          executes: false,
        },
        {
          id: "sign",
          title: "签名 .app(hardened runtime + 时间戳)",
          command: `${SIGN} --deep --force --options runtime --timestamp --sign "Developer ID Application: <TEAM>" "MyApp.app"`,
          note: "三件套缺一不可:Developer ID、runtime、timestamp;--deep 只用于演示,真实项目应逐层签名。",
          executes: false,
        },
        {
          id: "verify",
          title: "本地校验签名",
          command: `${SIGN} --verify --deep --strict --verbose=2 "MyApp.app"`,
          note: "校验失败时不要继续打包,先解决证书链或权限问题。",
          executes: false,
        },
        {
          id: "notarize",
          title: "提交公证并等待结果",
          command: `xcrun ${NOTARY} submit "MyApp.zip" --apple-id "<APPLE_ID>" --team-id "<TEAM>" --password "<APP_SPECIFIC_PASSWORD>" --wait`,
          note: "密码用 app-specific password,不要用 Apple ID 主密码;--wait 让 CI 阻塞到结果返回。",
          executes: false,
        },
        {
          id: "staple",
          title: "装订公证票据",
          command: `xcrun ${STAPLE} staple "MyApp.app"`,
          note: "装订后离线也能通过 Gatekeeper;dmg 要对镜像本身再装订一次。",
          executes: false,
        },
        {
          id: "assess",
          title: "Gatekeeper 评估",
          command: `spctl --assess --type execute --verbose=4 "MyApp.app"`,
          note: "评估通过只代表本机认可,不代表所有系统版本都认可。",
          executes: false,
        },
      ],
      limits: [
        "证书与私钥不进入仓库,不写进 CI 日志。",
        "公证需要 Apple Developer 账号,教程不申请、不执行。",
        "签名后修改任何文件都会破坏签名,必须重新签。",
      ],
    },
    {
      id: "windows",
      title: "Windows · Authenticode",
      summary:
        "用代码签名证书对 exe/msi 做 Authenticode 签名,带 RFC3161 时间戳,再用 Windows 签名工具校验。",
      secrets: ["WIN_CSC_LINK", "WIN_CSC_KEY_PASSWORD"],
      tools: [WINSIGN],
      steps: [
        {
          id: "cert",
          title: "准备证书",
          command: `certutil -store My`,
          note: "证书放当前用户或机器存储;PFX 只从 secret 注入,不落盘到工作区。",
          executes: false,
        },
        {
          id: "sign",
          title: "签名安装包",
          command: `${WINSIGN} sign /fd SHA256 /tr "https://timestamp.example" /td SHA256 /f cert.pfx /p "<PASSWORD>" "MyApp-Setup.exe"`,
          note: "/tr 与 /td 同时指定,时间戳服务器不可用时签名不应静默降级。",
          executes: false,
        },
        {
          id: "verify",
          title: "校验签名",
          command: `${WINSIGN} verify /pa /v "MyApp-Setup.exe"`,
          note: "校验失败通常意味着证书链缺失或时间戳无效。",
          executes: false,
        },
      ],
      limits: [
        "EV 证书可能需要硬件令牌,CI 上要单独设计。",
        "签名后不要再次修改安装包。",
        "教程不购买证书、不执行签名。",
      ],
    },
    {
      id: "linux",
      title: "Linux · 仓库与包签名",
      summary:
        "Linux 没有统一的系统级代码签名;常见做法是签名仓库元数据、AppImage 或发布校验和。",
      secrets: ["GPG_PRIVATE_KEY", "GPG_PASSPHRASE"],
      tools: [GPG],
      steps: [
        {
          id: "detach",
          title: "生成分离签名",
          command: `${GPG} --armor --detach-sign --output MyApp.AppImage.asc MyApp.AppImage`,
          note: "签名文件与产物一起发布,用户用公钥验证。",
          executes: false,
        },
        {
          id: "checksum",
          title: "生成校验和",
          command: `shasum -a 256 MyApp.AppImage > MyApp.AppImage.sha256`,
          note: "校验和防损坏,签名防替换,两者都要有。",
          executes: false,
        },
        {
          id: "verify",
          title: "验证签名",
          command: `${GPG} --verify MyApp.AppImage.asc MyApp.AppImage`,
          note: "公钥要通过可信渠道分发,不能和签名放在同一个下载页。",
          executes: false,
        },
      ],
      limits: [
        "不同发行版的包管理签名机制不同。",
        "AppImage 没有系统级签名验证,用户需手动校验。",
        "教程不生成真实密钥。",
      ],
    },
  ];
}

export type EnvCheck = {
  key: string;
  present: boolean;
};

export type Readiness = {
  platform: string;
  isPackaged: boolean;
  tools: Array<{ name: string; present: boolean }>;
  env: EnvCheck[];
  score: number;
};