in solution/solution-compliance-audit-for-data-plane/source/function/ecs-timezone/index.js [43:128]
async function main(eventParams, context) {
const { logger } = context;
const {
invokingEvent: {
configurationItem
},
ruleParameters: {
tagScopes,
timezone
}
} = eventParams;
if (!configurationItem) {
logger.error(`There is no configurationItem in invokingEvent. Params is ${JSON.stringify(eventParams)}`);
return;
}
const { regionId, resourceId, tags, accountId } = configurationItem;
logger.info(`Start evaluating for resource ${resourceId} of account ${accountId} in region ${regionId}`);
//校验资源标签是否在要检测的范围内
if (tagScopes) {
const allowedTags = JSON.parse(tagScopes);
if (!tags) {
logger.info(`Resource ${resourceId} don't need to evaluate`);
return;
}
const resourceTags = JSON.parse(tags);
var needEvaluate = false;
for (let i = 0; i < allowedTags.length; i++) {
if (resourceTags[allowedTags[i].TagKey] != null && resourceTags[allowedTags[i].TagKey].indexOf(allowedTags[i].TagValue) > -1) {
needEvaluate = true;
break;
}
}
//忽略资源不在需要巡检的范围内的资源
if (needEvaluate === false) {
logger.info(`Resource ${resourceId} don't need to evaluate`);
return;
}
}
//构造ECS云助手Client
const client = await getEcsClient(eventParams, context);
//构造请求参数
const params = {
RegionId: regionId,
Type: "RunShellScript",
CommandContent: btoa("timedatectl | grep Time | awk -F ':' '{print $2}'"),
RepeatMode: "Once",
ContentEncoding: "Base64",
InstanceId: [resourceId],
Timeout: 60,
};
//请求云助手
const result = await client.request("RunCommand", params, requestOption);
const { CommandId: commandId, InvokeId: invokeId } = result;
while (true) {
let invocationResult = await getCommandResult(
commandId,
invokeId,
eventParams,
context,
client
);
if (invocationResult !== null) {
invocationResult = invocationResult.trim();
const isCompliant =
invocationResult === `${timezone.trim()}`;
const annotation = isCompliant ? {} : {
desiredValue: timezone.trim(),
configuration: invocationResult,
};
const complianceType = isCompliant ? COMPLIANCE_TYPE_COMPLIANT : COMPLIANCE_TYPE_NON_COMPLIANT;
await putEvaluationResult(complianceType, eventParams, context, annotation);
break;
}
await sleep(2000);
}
}