esa-cli
Version:
A CLI for operating Alibaba Cloud ESA Functions and Pages.
127 lines (126 loc) • 4.77 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as $OpenApi from '@alicloud/openapi-client';
// Domestic site endpoint
const DOMESTIC_ENDPOINT = 'esa.cn-hangzhou.aliyuncs.com';
// International site endpoint
const INTERNATIONAL_ENDPOINT = 'esa.ap-southeast-1.aliyuncs.com';
/**
* Validate credentials for a single endpoint
*/
function validateEndpoint(accessKeyId, accessKeySecret, endpoint) {
return __awaiter(this, void 0, void 0, function* () {
try {
const apiConfig = new $OpenApi.Config({
accessKeyId,
accessKeySecret,
endpoint
});
const client = new $OpenApi.default.default(apiConfig);
const params = {
action: 'GetErService',
version: '2024-09-10',
protocol: 'https',
method: 'GET',
authType: 'AK',
bodyType: 'json',
reqBodyType: 'json',
style: 'RPC',
pathname: '/',
toMap: function () {
return this;
}
};
const request = new $OpenApi.OpenApiRequest({
query: {}
});
const runtime = {
toMap: function () {
return this;
}
};
const response = yield client.callApi(params, request, runtime);
if (response.statusCode === 200) {
if (response.body.Status === 'Running') {
return { valid: true };
}
else {
return {
valid: false,
message: 'Functions and Pages is not active'
};
}
}
else if (response.statusCode === 403) {
return {
valid: false,
message: 'Permission denied: Access key or secret is incorrect'
};
}
else {
return {
valid: false,
message: `Error: ${response.statusCode}`
};
}
}
catch (error) {
return {
valid: false,
message: error instanceof Error ? error.message : 'Unknown error'
};
}
});
}
/**
* Validate if AK/SK/endpoint are valid
*
* @param accessKeyId - AccessKey ID
* @param accessKeySecret - AccessKey Secret
* @returns Validation result, including whether valid, site type and endpoint
*/
export function validateCredentials(accessKeyId, accessKeySecret) {
return __awaiter(this, void 0, void 0, function* () {
if (process.env.CUSTOM_ENDPOINT) {
const result = yield validateEndpoint(accessKeyId, accessKeySecret, process.env.CUSTOM_ENDPOINT);
if (result.valid) {
return { valid: true, endpoint: process.env.CUSTOM_ENDPOINT };
}
else {
return { valid: false, message: result.message };
}
}
// First validate domestic site
const domesticResult = yield validateEndpoint(accessKeyId, accessKeySecret, DOMESTIC_ENDPOINT);
if (domesticResult.valid) {
return {
valid: true,
siteType: 'domestic',
endpoint: DOMESTIC_ENDPOINT
};
}
// If domestic site validation fails, validate international site
const internationalResult = yield validateEndpoint(accessKeyId, accessKeySecret, INTERNATIONAL_ENDPOINT);
if (internationalResult.valid) {
return {
valid: true,
siteType: 'international',
endpoint: INTERNATIONAL_ENDPOINT
};
}
// Both failed, return failure result
return {
valid: false,
message: domesticResult.message ||
internationalResult.message ||
'Invalid credentials'
};
});
}