update openclaw secret
This commit is contained in:
@@ -2,14 +2,13 @@
|
||||
|
||||
import {
|
||||
args,
|
||||
execCommand,
|
||||
exit,
|
||||
fetchDataWithTimeout,
|
||||
getEnv,
|
||||
getSecretValue,
|
||||
log,
|
||||
stdinToArrayBuffer,
|
||||
} from "https://script.hatter.ink/@50/deno-commons-mod.ts";
|
||||
import { parseArgs } from "jsr:@std/cli/parse-args";
|
||||
} from "https://script.hatter.ink/@54/deno-commons-mod.ts";
|
||||
import {parseArgs} from "jsr:@std/cli/parse-args";
|
||||
|
||||
// specification: https://docs.openclaw.ai/gateway/secrets
|
||||
|
||||
@@ -52,63 +51,6 @@ interface GetSecretResponse {
|
||||
};
|
||||
}
|
||||
|
||||
async function getSecretValueViaAlibabaCloudHttps(
|
||||
key: string,
|
||||
): Promise<string> {
|
||||
const pkcs7Response = await fetchDataWithTimeout(
|
||||
"http://100.100.100.200/latest/dynamic/instance-identity/pkcs7",
|
||||
);
|
||||
if (pkcs7Response.status != 200) {
|
||||
throw new Error("Get PKCS#7 failed: ${pkcs7Response.status}`)");
|
||||
}
|
||||
const pkcs7 = await pkcs7Response.text();
|
||||
const httpSecretResponse = await fetchDataWithTimeout(
|
||||
"https://hatter.ink/secret/get.json?name=" +
|
||||
encodeURIComponent(key),
|
||||
{
|
||||
headers: {
|
||||
"Authorization": `PKCS7 ${pkcs7}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (httpSecretResponse.status != 200) {
|
||||
throw new Error(`Get secret failed: ${httpSecretResponse.status}`);
|
||||
}
|
||||
const secretResponse = await httpSecretResponse
|
||||
.json() as GetSecretResponse;
|
||||
log.debug("secretResponse", secretResponse);
|
||||
if (secretResponse.status != 200) {
|
||||
throw new Error(`Get secret failed: ${secretResponse.status}`);
|
||||
}
|
||||
return secretResponse.data.value;
|
||||
}
|
||||
|
||||
async function getSecretValueViaHatterCli(key: string): Promise<string> {
|
||||
const output = await execCommand("hatter", [
|
||||
"secret",
|
||||
"get",
|
||||
"--name",
|
||||
key,
|
||||
]);
|
||||
const secretResponse = output.getStdoutAsJson() as GetSecretResponse;
|
||||
log.debug("secretResponse", secretResponse);
|
||||
if (secretResponse.status != 200) {
|
||||
throw new Error(`Get secret failed: ${secretResponse.status}`);
|
||||
}
|
||||
return secretResponse.data.value;
|
||||
}
|
||||
|
||||
async function getSecretValue(
|
||||
isOnAlibabaCloud: boolean,
|
||||
key: string,
|
||||
): Promise<string> {
|
||||
if (isOnAlibabaCloud) {
|
||||
return await getSecretValueViaAlibabaCloudHttps(key);
|
||||
} else {
|
||||
return await getSecretValueViaHatterCli(key);
|
||||
}
|
||||
}
|
||||
|
||||
const flags = parseArgs(args(), {
|
||||
boolean: ["help", "direct-output"],
|
||||
collect: ["id"],
|
||||
@@ -132,9 +74,8 @@ if (flags.help) {
|
||||
|
||||
// RUN_ENV values ALIBABA_CLOUD, LOCAL
|
||||
const runEnv = getEnv("RUN_ENV");
|
||||
const isOnAlibabaCloud = runEnv == "ALIBABA_CLOUD";
|
||||
|
||||
log.debug("isOnAlibabaCloud", isOnAlibabaCloud);
|
||||
log.debug("runEnv", runEnv);
|
||||
|
||||
let openClawInput;
|
||||
if (flags.id) {
|
||||
@@ -166,13 +107,13 @@ if (flags["direct-output"]) {
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
console.log(await getSecretValue(isOnAlibabaCloud, openClawInput.ids[0]));
|
||||
console.log(await getSecretValue(openClawInput.ids[0], runEnv));
|
||||
exit(0);
|
||||
}
|
||||
|
||||
for (const id of openClawInput.ids) {
|
||||
try {
|
||||
values[id] = await getSecretValue(isOnAlibabaCloud, id);
|
||||
values[id] = await getSecretValue(id, runEnv);
|
||||
} catch (e) {
|
||||
errors[id] = { message: e.message };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user