File size: 16,979 Bytes
9299241 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | #!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { runAndroidSigningCommandSync } from "./lib/android-release-signing-process.mjs";
import { parseFlagArgs, stringFlag } from "./lib/arg-utils.runtime.mjs";
import { isRecord } from "./lib/record-shared.mjs";
import { resolveRepoRoot } from "./lib/repo-root.mjs";
const rootDir = resolveRepoRoot(import.meta.url);
const defaultManifestPath = path.join(rootDir, "apps", "android", "Config", "ReleaseSigning.json");
const requiredPropertyNames = [
"OPENCLAW_ANDROID_STORE_FILE",
"OPENCLAW_ANDROID_STORE_PASSWORD",
"OPENCLAW_ANDROID_KEY_ALIAS",
"OPENCLAW_ANDROID_KEY_PASSWORD",
];
const sourceRequiredPropertyNames = requiredPropertyNames.filter(
(name) => name !== "OPENCLAW_ANDROID_STORE_FILE",
);
function usage() {
process.stdout.write(`Usage:
scripts/android-release-signing.mjs --mode plan
scripts/android-release-signing.mjs --mode check
scripts/android-release-signing.mjs --mode materialize
scripts/android-release-signing.mjs --mode sync-pull
scripts/android-release-signing.mjs --mode sync-push --keystore PATH --properties PATH
Options:
--manifest PATH Defaults to apps/android/Config/ReleaseSigning.json.
--workspace PATH Defaults to <materializedRoot>/apps-signing.
--materialized-dir PATH Defaults to materializedRoot from the manifest.
--keystore PATH Upload keystore source for --mode sync-push.
--properties PATH Signing properties source for --mode sync-push.
sync-pull and sync-push use MATCH_PASSWORD to decrypt/encrypt Android release
signing assets in the shared apps-signing repository.
`);
}
function parseArgs(argv) {
const options = {
mode: "",
manifestPath: defaultManifestPath,
workspace: "",
materializedDir: "",
keystorePath: process.env.OPENCLAW_ANDROID_UPLOAD_KEYSTORE || "",
propertiesPath: process.env.OPENCLAW_ANDROID_SIGNING_PROPERTIES || "",
};
const helpIndex = argv.findIndex((arg) => arg === "-h" || arg === "--help");
parseFlagArgs(
helpIndex === -1 ? argv : argv.slice(0, helpIndex),
options,
[
stringFlag("--mode", "mode", {
allowInline: false,
missingValueMessage: "Missing value for --mode.",
rejectShortOptions: true,
repeatable: true,
}),
...[
["--manifest", "manifestPath"],
["--workspace", "workspace"],
["--materialized-dir", "materializedDir"],
["--keystore", "keystorePath"],
["--properties", "propertiesPath"],
].map(([flag, key]) =>
stringFlag(flag, key, {
allowInline: false,
missingValueMessage: `Missing value for ${flag}.`,
rejectShortOptions: true,
repeatable: true,
transform: path.resolve,
}),
),
],
{
ignoreDoubleDash: false,
onUnhandledArg(arg) {
throw new Error(`Unknown argument: ${arg}`);
},
},
);
if (helpIndex !== -1) {
usage();
process.exit(0);
}
if (!options.mode) {
throw new Error("Missing required --mode.");
}
return options;
}
function requireString(value, key) {
if (typeof value !== "string" || value.trim() === "") {
throw new Error(`Android release signing manifest missing ${key}.`);
}
return value.trim();
}
function requireGradlePropertyNames(value) {
if (
!Array.isArray(value) ||
!value.every((name) => typeof name === "string") ||
value.length !== requiredPropertyNames.length ||
!requiredPropertyNames.every((name) => value.includes(name))
) {
throw new Error(
`Android release signing manifest must list Gradle properties: ${requiredPropertyNames.join(", ")}.`,
);
}
return value;
}
function readManifest(manifestPath) {
const value = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
const parsed = isRecord(value) ? value : {};
const manifest = {
signingRepo: requireString(parsed.signingRepo, "signingRepo"),
signingBranch: requireString(parsed.signingBranch, "signingBranch"),
assetPath: requireString(parsed.assetPath, "assetPath"),
uploadKeystoreEncryptedFile: requireString(
parsed.uploadKeystoreEncryptedFile,
"uploadKeystoreEncryptedFile",
),
gradlePropertiesEncryptedFile: requireString(
parsed.gradlePropertiesEncryptedFile,
"gradlePropertiesEncryptedFile",
),
apkCertificateSha256: requireString(parsed.apkCertificateSha256, "apkCertificateSha256"),
materializedRoot: requireString(parsed.materializedRoot, "materializedRoot"),
gradlePropertyNames: requireGradlePropertyNames(parsed.gradlePropertyNames),
};
if (!/^[a-f0-9]{64}$/u.test(manifest.apkCertificateSha256)) {
throw new Error(
"Android release signing manifest apkCertificateSha256 must be 64 lowercase hex digits.",
);
}
return manifest;
}
function relativePath(filePath) {
const relative = path.relative(rootDir, filePath);
return relative && !relative.startsWith("..") ? relative : filePath;
}
function resolveMaterializedDir(manifest, options) {
return options.materializedDir || path.resolve(rootDir, manifest.materializedRoot);
}
function resolveWorkspace(manifest, options) {
return options.workspace || path.join(resolveMaterializedDir(manifest, options), "apps-signing");
}
function assertWorkspaceInsideMaterialized(workspace, materializedDir) {
const resolvedWorkspace = path.resolve(workspace);
const resolvedMaterializedDir = path.resolve(materializedDir);
const relative = path.relative(resolvedMaterializedDir, resolvedWorkspace);
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
throw new Error(
`Android signing workspace must be inside ${relativePath(resolvedMaterializedDir)}.`,
);
}
}
function assetDir(workspace, manifest) {
return path.join(workspace, manifest.assetPath);
}
function encryptedKeystorePath(workspace, manifest) {
return path.join(assetDir(workspace, manifest), manifest.uploadKeystoreEncryptedFile);
}
function encryptedPropertiesPath(workspace, manifest) {
return path.join(assetDir(workspace, manifest), manifest.gradlePropertiesEncryptedFile);
}
function materializedKeystorePath(materializedDir) {
return path.join(materializedDir, "upload-keystore.jks");
}
function materializedPropertiesPath(materializedDir) {
return path.join(materializedDir, "gradle.properties");
}
function requireMatchPassword() {
if (!process.env.MATCH_PASSWORD || process.env.MATCH_PASSWORD.trim() === "") {
throw new Error("MATCH_PASSWORD is required for Android release signing sync.");
}
}
function run(command, args, options = {}) {
runAndroidSigningCommandSync(command, args, {
cwd: options.cwd,
env: options.env || process.env,
stdio: options.stdio || "pipe",
});
}
function runText(command, args, options = {}) {
return runAndroidSigningCommandSync(command, args, {
cwd: options.cwd,
env: options.env || process.env,
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
});
}
function cloneSigningRepo(manifest, workspace, materializedDir) {
assertWorkspaceInsideMaterialized(workspace, materializedDir);
fs.rmSync(workspace, { recursive: true, force: true });
fs.mkdirSync(path.dirname(workspace), { recursive: true });
run("git", ["clone", "--branch", manifest.signingBranch, manifest.signingRepo, workspace]);
}
function opensslCrypt({ decrypt, inputPath, outputPath }) {
requireMatchPassword();
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
if (decrypt) {
fs.rmSync(outputPath, { force: true });
}
const args = [
"enc",
"-aes-256-cbc",
"-pbkdf2",
"-md",
"sha256",
...(decrypt ? ["-d"] : ["-salt"]),
"-in",
inputPath,
"-out",
outputPath,
"-pass",
"env:MATCH_PASSWORD",
];
const previousUmask = decrypt ? process.umask(0o077) : undefined;
try {
run("openssl", args);
} finally {
if (previousUmask !== undefined) {
process.umask(previousUmask);
}
}
if (decrypt) {
fs.chmodSync(outputPath, 0o600);
}
}
function readProperties(filePath) {
const properties = new Map();
for (const rawLine of fs.readFileSync(filePath, "utf8").split(/\r?\n/u)) {
const line = rawLine.trim();
if (!line || line.startsWith("#")) {
continue;
}
const separator = line.indexOf("=");
if (separator <= 0) {
throw new Error(`Invalid signing properties line in ${relativePath(filePath)}.`);
}
const key = line.slice(0, separator).trim();
const value = line.slice(separator + 1).trim();
if (!key || !value) {
throw new Error(`Invalid empty signing property in ${relativePath(filePath)}.`);
}
properties.set(key, value);
}
return properties;
}
function requireProperties(properties, names, filePath) {
const missing = names.filter((name) => !properties.get(name));
if (missing.length > 0) {
throw new Error(
`${relativePath(filePath)} is missing Android signing properties: ${missing.join(", ")}.`,
);
}
}
function writeMaterializedProperties(materializedDir, sourceProperties) {
const keystorePath = materializedKeystorePath(materializedDir);
const propertiesPath = materializedPropertiesPath(materializedDir);
const tempPath = `${propertiesPath}.${process.pid}.tmp`;
const properties = new Map(sourceProperties);
properties.set("OPENCLAW_ANDROID_STORE_FILE", keystorePath);
requireProperties(properties, requiredPropertyNames, propertiesPath);
const content = [
"# Generated by scripts/android-release-signing.mjs.",
"# Contains decrypted Android release signing values. Do not commit.",
...requiredPropertyNames.map((name) => `${name}=${properties.get(name)}`),
"",
].join("\n");
try {
fs.writeFileSync(tempPath, content, { mode: 0o600 });
fs.chmodSync(tempPath, 0o600);
fs.renameSync(tempPath, propertiesPath);
fs.chmodSync(propertiesPath, 0o600);
} finally {
fs.rmSync(tempPath, { force: true });
}
}
function validateMaterializedSigning(materializedDir) {
const keystorePath = materializedKeystorePath(materializedDir);
const propertiesPath = materializedPropertiesPath(materializedDir);
if (!fs.existsSync(keystorePath) || fs.statSync(keystorePath).size === 0) {
throw new Error(
`Missing materialized Android upload keystore at ${relativePath(keystorePath)}.`,
);
}
if (!fs.existsSync(propertiesPath)) {
throw new Error(
`Missing materialized Android signing properties at ${relativePath(propertiesPath)}.`,
);
}
const properties = readProperties(propertiesPath);
requireProperties(properties, requiredPropertyNames, propertiesPath);
if (properties.get("OPENCLAW_ANDROID_STORE_FILE") !== keystorePath) {
throw new Error(
`${relativePath(propertiesPath)} must point OPENCLAW_ANDROID_STORE_FILE at ${relativePath(keystorePath)}.`,
);
}
}
function writePlan(manifest, options) {
const materializedDir = resolveMaterializedDir(manifest, options);
process.stdout.write(`Android release signing plan
Signing repo: ${manifest.signingRepo}
Signing branch: ${manifest.signingBranch}
Signing assets: ${manifest.assetPath}
Encrypted upload keystore: ${manifest.uploadKeystoreEncryptedFile}
Encrypted Gradle properties: ${manifest.gradlePropertiesEncryptedFile}
Pinned APK certificate SHA-256: ${manifest.apkCertificateSha256}
Materialized output: ${relativePath(materializedDir)}
Gradle bridge: Fastlane exports ORG_GRADLE_PROJECT_* values from the materialized properties file.
`);
}
function writeSigningRepoManifest(workspace, manifest) {
const signingManifestPath = path.join(assetDir(workspace, manifest), "manifest.json");
const signingManifest = {
version: 1,
assetPath: manifest.assetPath,
uploadKeystoreEncryptedFile: manifest.uploadKeystoreEncryptedFile,
gradlePropertiesEncryptedFile: manifest.gradlePropertiesEncryptedFile,
apkCertificateSha256: manifest.apkCertificateSha256,
gradlePropertyNames: requiredPropertyNames,
};
fs.writeFileSync(signingManifestPath, `${JSON.stringify(signingManifest, null, 2)}\n`);
}
function materialize(manifest, options) {
const workspace = resolveWorkspace(manifest, options);
const materializedDir = resolveMaterializedDir(manifest, options);
const tempPropertiesPath = path.join(materializedDir, ".gradle.properties.decrypted.tmp");
assertWorkspaceInsideMaterialized(workspace, materializedDir);
if (!fs.existsSync(encryptedKeystorePath(workspace, manifest))) {
throw new Error(
`Missing encrypted Android upload keystore in signing repo at ${manifest.assetPath}/${manifest.uploadKeystoreEncryptedFile}.`,
);
}
if (!fs.existsSync(encryptedPropertiesPath(workspace, manifest))) {
throw new Error(
`Missing encrypted Android signing properties in signing repo at ${manifest.assetPath}/${manifest.gradlePropertiesEncryptedFile}.`,
);
}
fs.mkdirSync(materializedDir, { recursive: true });
opensslCrypt({
decrypt: true,
inputPath: encryptedKeystorePath(workspace, manifest),
outputPath: materializedKeystorePath(materializedDir),
});
try {
opensslCrypt({
decrypt: true,
inputPath: encryptedPropertiesPath(workspace, manifest),
outputPath: tempPropertiesPath,
});
const properties = readProperties(tempPropertiesPath);
requireProperties(properties, sourceRequiredPropertyNames, tempPropertiesPath);
writeMaterializedProperties(materializedDir, properties);
} finally {
fs.rmSync(tempPropertiesPath, { force: true });
}
validateMaterializedSigning(materializedDir);
process.stdout.write(
`Materialized Android release signing assets in ${relativePath(materializedDir)}.\n`,
);
}
function syncPull(manifest, options) {
const workspace = resolveWorkspace(manifest, options);
const materializedDir = resolveMaterializedDir(manifest, options);
cloneSigningRepo(manifest, workspace, materializedDir);
materialize(manifest, options);
}
function requirePushSources(options) {
if (!options.keystorePath) {
throw new Error(
"Missing Android upload keystore source. Pass --keystore or set OPENCLAW_ANDROID_UPLOAD_KEYSTORE.",
);
}
if (!options.propertiesPath) {
throw new Error(
"Missing Android signing properties source. Pass --properties or set OPENCLAW_ANDROID_SIGNING_PROPERTIES.",
);
}
if (!fs.existsSync(options.keystorePath) || fs.statSync(options.keystorePath).size === 0) {
throw new Error(
`Android upload keystore source is missing or empty: ${relativePath(options.keystorePath)}.`,
);
}
if (!fs.existsSync(options.propertiesPath)) {
throw new Error(
`Android signing properties source is missing: ${relativePath(options.propertiesPath)}.`,
);
}
const properties = readProperties(options.propertiesPath);
requireProperties(properties, sourceRequiredPropertyNames, options.propertiesPath);
}
function syncPush(manifest, options) {
requireMatchPassword();
requirePushSources(options);
const workspace = resolveWorkspace(manifest, options);
cloneSigningRepo(manifest, workspace, resolveMaterializedDir(manifest, options));
fs.mkdirSync(assetDir(workspace, manifest), { recursive: true });
opensslCrypt({
decrypt: false,
inputPath: options.keystorePath,
outputPath: encryptedKeystorePath(workspace, manifest),
});
opensslCrypt({
decrypt: false,
inputPath: options.propertiesPath,
outputPath: encryptedPropertiesPath(workspace, manifest),
});
writeSigningRepoManifest(workspace, manifest);
run("git", ["add", manifest.assetPath], { cwd: workspace });
const status = runText("git", ["status", "--porcelain"], { cwd: workspace }).trim();
if (!status) {
process.stdout.write("Android release signing assets were already up to date.\n");
return;
}
run("git", ["commit", "-m", "Update Android release signing assets"], { cwd: workspace });
run("git", ["push", "origin", manifest.signingBranch], { cwd: workspace });
process.stdout.write("Pushed encrypted Android release signing assets.\n");
}
try {
const options = parseArgs(process.argv.slice(2));
const manifest = readManifest(options.manifestPath);
if (options.mode === "plan") {
writePlan(manifest, options);
} else if (options.mode === "check") {
validateMaterializedSigning(resolveMaterializedDir(manifest, options));
process.stdout.write("Android release signing materialization is valid.\n");
} else if (options.mode === "materialize") {
materialize(manifest, options);
} else if (options.mode === "sync-pull") {
syncPull(manifest, options);
} else if (options.mode === "sync-push") {
syncPush(manifest, options);
} else {
throw new Error(`Unknown mode: ${options.mode}`);
}
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
}
|