update deno commons mod
This commit is contained in:
@@ -1315,6 +1315,60 @@ export async function fetchAlibabaCloudInstanceIdentityV1(
|
||||
return await pkcs7Response.text();
|
||||
}
|
||||
|
||||
export type InvokeMethod = "GET" | "POST";
|
||||
|
||||
export interface SimpleResponse {
|
||||
status: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export async function invokeViaAlibabaCloudInstanceIdentity(
|
||||
method: InvokeMethod,
|
||||
path: string,
|
||||
params?: string[][],
|
||||
body?: string,
|
||||
mode?: AlibabaCloudInstanceIdentityMode,
|
||||
): Promise<SimpleResponse> {
|
||||
const pkcs7 = await fetchAlibabaCloudInstanceIdentityV1(
|
||||
`hatter.ink:${path}`,
|
||||
mode,
|
||||
);
|
||||
const url = [];
|
||||
url.push(`https://global.hatter.ink${path}`);
|
||||
if (params) {
|
||||
for (let i = 0; i < params.length; i++) {
|
||||
const param = params[i];
|
||||
url.push((i == 0) ? "?" : "&");
|
||||
url.push(encodeURIComponent(param[0]));
|
||||
url.push("=");
|
||||
url.push(encodeURIComponent(param[1]));
|
||||
}
|
||||
}
|
||||
const httpResponse = await fetchDataWithTimeout(
|
||||
url.join(""),
|
||||
{
|
||||
method: method,
|
||||
headers: {
|
||||
"Authorization": `PKCS7 ${pkcs7}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (httpResponse.status != 200) {
|
||||
throw new Error(`Get ${path} failed: ${httpResponse.status}`);
|
||||
}
|
||||
const simpleResponse = await httpResponse
|
||||
.json() as SimpleResponse;
|
||||
log.debug("simpleResponse", simpleResponse);
|
||||
if (simpleResponse.status != 200) {
|
||||
throw new Error(
|
||||
`Get ${path} failed: ${simpleResponse.status}, raw: ${
|
||||
JSON.stringify(simpleResponse)
|
||||
}`,
|
||||
);
|
||||
}
|
||||
return simpleResponse;
|
||||
}
|
||||
|
||||
interface GetSecretResponse {
|
||||
status: number;
|
||||
message: string;
|
||||
@@ -1334,29 +1388,13 @@ export async function getSecretValueViaAlibabaCloudInstanceIdentity(
|
||||
key: string,
|
||||
mode?: AlibabaCloudInstanceIdentityMode,
|
||||
): Promise<string> {
|
||||
const pkcs7 = await fetchAlibabaCloudInstanceIdentityV1(
|
||||
"hatter.ink:/secret/get.json",
|
||||
const secretResponse = await invokeViaAlibabaCloudInstanceIdentity(
|
||||
"GET",
|
||||
"/secret/get.json",
|
||||
[["name", key]],
|
||||
null,
|
||||
mode,
|
||||
);
|
||||
const httpSecretResponse = await fetchDataWithTimeout(
|
||||
`https://global.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}`);
|
||||
}
|
||||
) as GetSecretResponse;
|
||||
return secretResponse.data.value;
|
||||
}
|
||||
|
||||
@@ -1412,39 +1450,19 @@ export async function assumeRoleByKeyViaAlibabaCloudInstanceIdentity(
|
||||
roleArn: string,
|
||||
mode?: AlibabaCloudInstanceIdentityMode,
|
||||
): Promise<StsToken> {
|
||||
const pkcs7 = await fetchAlibabaCloudInstanceIdentityV1(
|
||||
"hatter.ink:/cloud/alibaba_cloud/assume_role_by_key.json",
|
||||
const assumeRoleResponse = await invokeViaAlibabaCloudInstanceIdentity(
|
||||
"GET",
|
||||
"/cloud/alibaba_cloud/assume_role_by_key.json",
|
||||
[["roleArn", roleArn]],
|
||||
null,
|
||||
mode,
|
||||
);
|
||||
const httpAssumeRoleResponse = await fetchDataWithTimeout(
|
||||
`https://global.hatter.ink/cloud/alibaba_cloud/assume_role_by_key.json?roleArn=${
|
||||
encodeURIComponent(roleArn)
|
||||
}`,
|
||||
{
|
||||
headers: {
|
||||
"Authorization": `PKCS7 ${pkcs7}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (httpAssumeRoleResponse.status != 200) {
|
||||
throw new Error(
|
||||
`Assume role by key failed: ${httpAssumeRoleResponse.status}`,
|
||||
);
|
||||
}
|
||||
const assumeRoleResponse = await httpAssumeRoleResponse
|
||||
.json() as AssumeRoleByKeyResponse;
|
||||
log.debug("assumeRoleResponse", assumeRoleResponse);
|
||||
if (assumeRoleResponse.status != 200) {
|
||||
throw new Error(
|
||||
`Assume role by key failed: ${assumeRoleResponse.status}, raw: ${
|
||||
JSON.stringify(assumeRoleResponse)
|
||||
}`,
|
||||
);
|
||||
}
|
||||
) as AssumeRoleByKeyResponse;
|
||||
return assumeRoleResponse.data;
|
||||
}
|
||||
|
||||
export async function assumeRoleByKeyViaHatterCli(roleArn: string): Promise<string> {
|
||||
export async function assumeRoleByKeyViaHatterCli(
|
||||
roleArn: string,
|
||||
): Promise<string> {
|
||||
const output = await execCommand("hatter", [
|
||||
"cloud-key",
|
||||
"assume-role",
|
||||
|
||||
Reference in New Issue
Block a user