@infrascan/aws-s3-scanner
Version:
Infrascan scanner definition for AWS S3
520 lines (513 loc) • 15.8 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default,
getClient: () => getClient
});
module.exports = __toCommonJS(index_exports);
var import_client_s33 = require("@aws-sdk/client-s3");
// src/generated/getters.ts
var import_core = require("@infrascan/core");
var import_client_s3 = require("@aws-sdk/client-s3");
var import_debug = __toESM(require("debug"));
async function ListBuckets(client, stateConnector, context) {
const getterDebug = (0, import_debug.default)("s3:ListBuckets");
const state = [];
getterDebug("ListBuckets");
const preparedParams = {};
try {
const cmd = new import_client_s3.ListBucketsCommand(preparedParams);
const result = await client.send(cmd);
state.push({
_metadata: {
account: context.account,
region: context.region,
timestamp: Date.now()
},
_parameters: preparedParams,
_result: result
});
} catch (err) {
if (err instanceof import_client_s3.S3ServiceException) {
if (err?.$retryable) {
console.log("Encountered retryable error", err);
} else {
console.log("Encountered unretryable error", err);
}
} else {
console.log("Encountered unexpected error", err);
}
}
getterDebug("Recording state");
await stateConnector.onServiceScanCompleteCallback(
context.account,
context.region,
"S3",
"ListBuckets",
state
);
}
async function GetBucketTagging(client, stateConnector, context) {
const getterDebug = (0, import_debug.default)("s3:GetBucketTagging");
const state = [];
getterDebug("Fetching state");
const resolvers = [
{ Key: "Bucket", Selector: "S3|ListBuckets|[]._result.Buckets[].Name" }
];
const parameterQueue = await (0, import_core.resolveFunctionCallParameters)(
context.account,
context.region,
resolvers,
stateConnector
);
for (const parameters of parameterQueue) {
const preparedParams = parameters;
try {
const cmd = new import_client_s3.GetBucketTaggingCommand(preparedParams);
const result = await client.send(cmd);
state.push({
_metadata: {
account: context.account,
region: context.region,
timestamp: Date.now()
},
_parameters: preparedParams,
_result: result
});
} catch (err) {
if (err instanceof import_client_s3.S3ServiceException) {
if (err?.$retryable) {
console.log("Encountered retryable error", err);
} else {
console.log("Encountered unretryable error", err);
}
} else {
console.log("Encountered unexpected error", err);
}
}
}
getterDebug("Recording state");
await stateConnector.onServiceScanCompleteCallback(
context.account,
context.region,
"S3",
"GetBucketTagging",
state
);
}
async function GetBucketNotificationConfiguration(client, stateConnector, context) {
const getterDebug = (0, import_debug.default)("s3:GetBucketNotificationConfiguration");
const state = [];
getterDebug("Fetching state");
const resolvers = [
{ Key: "Bucket", Selector: "S3|ListBuckets|[]._result.Buckets[].Name" }
];
const parameterQueue = await (0, import_core.resolveFunctionCallParameters)(
context.account,
context.region,
resolvers,
stateConnector
);
for (const parameters of parameterQueue) {
const preparedParams = parameters;
try {
const cmd = new import_client_s3.GetBucketNotificationConfigurationCommand(preparedParams);
const result = await client.send(cmd);
state.push({
_metadata: {
account: context.account,
region: context.region,
timestamp: Date.now()
},
_parameters: preparedParams,
_result: result
});
} catch (err) {
if (err instanceof import_client_s3.S3ServiceException) {
if (err?.$retryable) {
console.log("Encountered retryable error", err);
} else {
console.log("Encountered unretryable error", err);
}
} else {
console.log("Encountered unexpected error", err);
}
}
}
getterDebug("Recording state");
await stateConnector.onServiceScanCompleteCallback(
context.account,
context.region,
"S3",
"GetBucketNotificationConfiguration",
state
);
}
async function GetBucketWebsite(client, stateConnector, context) {
const getterDebug = (0, import_debug.default)("s3:GetBucketWebsite");
const state = [];
getterDebug("Fetching state");
const resolvers = [
{ Key: "Bucket", Selector: "S3|ListBuckets|[]._result.Buckets[].Name" }
];
const parameterQueue = await (0, import_core.resolveFunctionCallParameters)(
context.account,
context.region,
resolvers,
stateConnector
);
for (const parameters of parameterQueue) {
const preparedParams = parameters;
try {
const cmd = new import_client_s3.GetBucketWebsiteCommand(preparedParams);
const result = await client.send(cmd);
state.push({
_metadata: {
account: context.account,
region: context.region,
timestamp: Date.now()
},
_parameters: preparedParams,
_result: result
});
} catch (err) {
if (err instanceof import_client_s3.S3ServiceException) {
if (err?.$retryable) {
console.log("Encountered retryable error", err);
} else {
console.log("Encountered unretryable error", err);
}
} else {
console.log("Encountered unexpected error", err);
}
}
}
getterDebug("Recording state");
await stateConnector.onServiceScanCompleteCallback(
context.account,
context.region,
"S3",
"GetBucketWebsite",
state
);
}
async function GetBucketAcl(client, stateConnector, context) {
const getterDebug = (0, import_debug.default)("s3:GetBucketAcl");
const state = [];
getterDebug("Fetching state");
const resolvers = [
{ Key: "Bucket", Selector: "S3|ListBuckets|[]._result.Buckets[].Name" }
];
const parameterQueue = await (0, import_core.resolveFunctionCallParameters)(
context.account,
context.region,
resolvers,
stateConnector
);
for (const parameters of parameterQueue) {
const preparedParams = parameters;
try {
const cmd = new import_client_s3.GetBucketAclCommand(preparedParams);
const result = await client.send(cmd);
state.push({
_metadata: {
account: context.account,
region: context.region,
timestamp: Date.now()
},
_parameters: preparedParams,
_result: result
});
} catch (err) {
if (err instanceof import_client_s3.S3ServiceException) {
if (err?.$retryable) {
console.log("Encountered retryable error", err);
} else {
console.log("Encountered unretryable error", err);
}
} else {
console.log("Encountered unexpected error", err);
}
}
}
getterDebug("Recording state");
await stateConnector.onServiceScanCompleteCallback(
context.account,
context.region,
"S3",
"GetBucketAcl",
state
);
}
// src/generated/graph.ts
var import_core2 = require("@infrascan/core");
var import_debug2 = __toESM(require("debug"));
var edgesDebug = (0, import_debug2.default)("s3:edges");
async function getEdges(stateConnector) {
edgesDebug("Fetching edges");
const edges = [];
edgesDebug("Evaluating S3|GetBucketNotificationConfiguration|[]");
const GetBucketNotificationConfigurationState1 = await (0, import_core2.evaluateSelectorGlobally)(
"S3|GetBucketNotificationConfiguration|[]",
stateConnector
);
const GetBucketNotificationConfigurationEdges1 = GetBucketNotificationConfigurationState1.flatMap((state) => {
const source = (0, import_core2.filterState)(
state,
"_parameters.Bucket | [`arn:aws:s3:::`,@] | join('',@)"
);
const target = (0, import_core2.filterState)(
state,
"_result.TopicConfigurations | [].{target:TopicArn,name:Id}"
);
if (!target || !source) {
return [];
}
if (Array.isArray(target)) {
return target.map((edgeTarget) => (0, import_core2.formatEdge)(source, edgeTarget));
}
return (0, import_core2.formatEdge)(source, target);
});
edgesDebug(
`Evaluated S3|GetBucketNotificationConfiguration|[]: ${GetBucketNotificationConfigurationEdges1.length} Edges found`
);
edges.push(...GetBucketNotificationConfigurationEdges1);
edgesDebug("Evaluating S3|GetBucketNotificationConfiguration|[]");
const GetBucketNotificationConfigurationState2 = await (0, import_core2.evaluateSelectorGlobally)(
"S3|GetBucketNotificationConfiguration|[]",
stateConnector
);
const GetBucketNotificationConfigurationEdges2 = GetBucketNotificationConfigurationState2.flatMap((state) => {
const source = (0, import_core2.filterState)(
state,
"_parameters.Bucket | [`arn:aws:s3:::`,@] | join('',@)"
);
const target = (0, import_core2.filterState)(
state,
"_result.QueueConfigurations | [].{target:QueueArn,name:Id}"
);
if (!target || !source) {
return [];
}
if (Array.isArray(target)) {
return target.map((edgeTarget) => (0, import_core2.formatEdge)(source, edgeTarget));
}
return (0, import_core2.formatEdge)(source, target);
});
edgesDebug(
`Evaluated S3|GetBucketNotificationConfiguration|[]: ${GetBucketNotificationConfigurationEdges2.length} Edges found`
);
edges.push(...GetBucketNotificationConfigurationEdges2);
edgesDebug("Evaluating S3|GetBucketNotificationConfiguration|[]");
const GetBucketNotificationConfigurationState3 = await (0, import_core2.evaluateSelectorGlobally)(
"S3|GetBucketNotificationConfiguration|[]",
stateConnector
);
const GetBucketNotificationConfigurationEdges3 = GetBucketNotificationConfigurationState3.flatMap((state) => {
const source = (0, import_core2.filterState)(
state,
"_parameters.Bucket | [`arn:aws:s3:::`,@] | join('',@)"
);
const target = (0, import_core2.filterState)(
state,
"_result.LambdaFunctionConfigurations | [].{target:LambdaFunctionArn,name:Id}"
);
if (!target || !source) {
return [];
}
if (Array.isArray(target)) {
return target.map((edgeTarget) => (0, import_core2.formatEdge)(source, edgeTarget));
}
return (0, import_core2.formatEdge)(source, target);
});
edgesDebug(
`Evaluated S3|GetBucketNotificationConfiguration|[]: ${GetBucketNotificationConfigurationEdges3.length} Edges found`
);
edges.push(...GetBucketNotificationConfigurationEdges3);
return edges;
}
// src/middleware.ts
var import_client_s32 = require("@aws-sdk/client-s3");
function isRichError(err) {
return err.Code != null;
}
function mapNotFoundErrorToEmptyResponse(context, err) {
const isRichS3Err = err instanceof import_client_s32.S3ServiceException && isRichError(err);
if (!isRichS3Err) {
return void 0;
}
if (context.commandName === "GetBucketTaggingCommand" && err.Code === "NoSuchTagSet") {
return {
output: {
$metadata: {},
TagSet: []
},
response: err
};
}
if (context.commandName === "GetBucketWebsiteCommand" && err.Code === "NoSuchWebsiteConfiguration") {
return {
output: {
$metadata: {}
},
response: err
};
}
return void 0;
}
function ignoreS3ConfigNotFoundMiddleware(next, context) {
return async function mapNotFoundErrors(args) {
try {
return await next(args);
} catch (err) {
const mappedResponse = mapNotFoundErrorToEmptyResponse(context, err);
if (mappedResponse != null) {
return mappedResponse;
}
throw err;
}
};
}
function registerMiddleware(client) {
client.middlewareStack.add(ignoreS3ConfigNotFoundMiddleware, {
step: "finalizeRequest",
name: "ignoreS3ConfigNotFoundErrors",
tags: ["ErrorHandling"],
priority: "high"
});
}
// src/graph.ts
var import_core3 = require("@infrascan/core");
var S3Entity = {
version: "0.1.1",
debugLabel: "s3-bucket",
provider: "aws",
command: "ListBuckets",
category: "s3",
subcategory: "bucket",
nodeType: "s3-bucket",
selector: "S3|ListBuckets|[]",
getState(state, context) {
return (0, import_core3.evaluateSelector)(
context.account,
context.region,
S3Entity.selector,
state
);
},
translate(val) {
return (val._result.Buckets ?? []).map((bucket) => ({
...bucket,
$metadata: val._metadata,
$parameters: val._parameters
}));
},
components: {
$metadata(val) {
return {
version: S3Entity.version,
timestamp: val.$metadata.timestamp
};
},
$graph(val) {
return {
id: `arn:aws:s3:::${val.Name}`,
label: val.Name,
nodeClass: "visual",
nodeType: S3Entity.nodeType,
parent: val.$metadata.account
};
},
$source(val) {
return {
command: S3Entity.command,
parameters: val.$parameters
};
},
tenant(val) {
return {
tenantId: val.$metadata.account,
provider: S3Entity.provider,
partition: val.$metadata.partition
};
},
location(val) {
return {
code: val.$metadata.region
};
},
resource(val) {
return {
id: `arn:aws:s3:::${val.Name}`,
name: val.Name,
category: S3Entity.category,
subcategory: S3Entity.subcategory
};
},
audit(val) {
return {
createdAt: val.CreationDate
};
}
}
};
// src/index.ts
function getClient(credentials, context, retryStrategy) {
const s3Client = new import_client_s33.S3Client({
credentials,
region: context.region,
followRegionRedirects: true,
retryStrategy
});
registerMiddleware(s3Client);
return s3Client;
}
var S3Scanner = {
provider: "aws",
service: "s3",
key: "S3",
getClient,
callPerRegion: false,
getters: [
ListBuckets,
GetBucketTagging,
GetBucketNotificationConfiguration,
GetBucketWebsite,
GetBucketAcl
],
getEdges,
entities: [S3Entity]
};
var index_default = S3Scanner;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getClient
});