Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check serverless.yml to correct no-default false positives #269

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/components/resourceTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getStackResources } from "../services/stackResources";
import { padString } from "../utils/padString";
import { lambdaStatisticsModal, lambdaInvokeModal } from "../modals";
import { getLambdaFunctions } from "../services";
import { abbreviateFunction } from "../utils/abbreviateFunction";

const contrib = require("blessed-contrib");
const open = require("open");
Expand Down Expand Up @@ -256,9 +257,9 @@ class ResourceTable {
this.table.data = lambdaFunctionResources.map((lam) => {
const funcName = lam.PhysicalResourceId;
const func = this.lambdaFunctions[funcName];
const shortenedFuncName = lam.PhysicalResourceId.replace(
`${this.program.stackName}-`,
""
const shortenedFuncName = abbreviateFunction(
lam.PhysicalResourceId,
this.program.stackName
);
this.fullFunctionNames[shortenedFuncName] = funcName;
let timeout = "?";
Expand Down
10 changes: 9 additions & 1 deletion src/guardian/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getStackResources,
getLambdaFunctions,
} from "../services";
import Serverless from "../services/serverless";

const infoLog = chalk.greenBright;
const titleLog = chalk.greenBright.underline.bold;
Expand Down Expand Up @@ -55,6 +56,8 @@ class GuardianCI {
if (this.config) {
this.ignoreConfig = this.config.ignore;
}

this.SLS = new Serverless(program.location);
}

async getAllLambdaFunctions() {
Expand Down Expand Up @@ -139,7 +142,12 @@ class GuardianCI {
// eslint-disable-next-line no-restricted-syntax
for (const Check of this.checksToRun) {
console.group();
const check = new Check(this.AWS, this.stackName, this.stackFunctions);
const check = new Check(
this.AWS,
this.stackName,
this.stackFunctions,
this.SLS
);
if (!this.ignoreCheck(check)) {
const filteredStack = this.ignoreArns(check, this.stackFunctions);
check.stackFunctions = filteredStack;
Expand Down
14 changes: 12 additions & 2 deletions src/guardian/rules/best_practices/no-default-memory/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { abbreviateFunction } from "../../../../utils/abbreviateFunction";

class NoDefaultMemory {
constructor(AWS, stackName, stackFunctions) {
constructor(AWS, stackName, stackFunctions, SLS) {
this.name = "no-default-memory";
this.AWS = AWS;
this.stackName = stackName;
this.stackFunctions = stackFunctions;
this.result = false;
this.defaultMemory = 1024;
this.failingResources = [];
this.SLS = SLS;
this.failureMessage =
"The following functions have their memory set as default.";
this.rulePage =
"See (https://theodo-uk.github.io/sls-dev-tools/docs/no-default-memory) for impact and how to to resolve.";
}

hasDefaultMemory(lambdaFunction) {
return lambdaFunction.MemorySize === this.defaultMemory;
const shortenedName = abbreviateFunction(
lambdaFunction.FunctionName,
this.stackName
);
return (
lambdaFunction.MemorySize === this.defaultMemory &&
!this.SLS.getMemorySize(shortenedName)
);
}

async run() {
Expand Down
18 changes: 13 additions & 5 deletions src/guardian/rules/best_practices/no-default-timeout/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { abbreviateFunction } from "../../../../utils/abbreviateFunction";

class NoDefaultTimeout {
constructor(AWS, stackName, stackFunctions) {
constructor(AWS, stackName, stackFunctions, SLS) {
this.name = "no-default-timeout";
this.AWS = AWS;
this.stackName = stackName;
Expand All @@ -8,17 +10,23 @@ class NoDefaultTimeout {
this.defaultTimeoutAWS = 3;
this.defaultTimeoutServerlessFramework = 6;
this.failingResources = [];
this.SLS = SLS;
this.failureMessage =
"The following functions have their timeout set as default.";
this.rulePage =
"See (https://theodo-uk.github.io/sls-dev-tools/docs/no-default-timeout) for impact and how to to resolve.";
}

hasDefaultTimeout(lambdaFunction) {
return [
this.defaultTimeoutAWS,
this.defaultTimeoutServerlessFramework,
].includes(lambdaFunction.Timeout);
const shortenedName = abbreviateFunction(
lambdaFunction.FunctionName,
this.stackName
);
return (
[this.defaultTimeoutAWS, this.defaultTimeoutServerlessFramework].includes(
lambdaFunction.Timeout
) && !this.SLS.getTimeout(shortenedName)
);
}

async run() {
Expand Down
23 changes: 23 additions & 0 deletions src/services/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ class Serverless {
}
}

getFunctionConfig(functionName) {
if (this.config && this.config.functions) {
return this.config.functions[functionName];
}
return undefined;
}

getTimeout(functionName) {
const config = this.getFunctionConfig(functionName);
if (config && config.timeout) {
return config.timeout;
}
return undefined;
}

getMemorySize(functionName) {
const config = this.getFunctionConfig(functionName);
if (config && config.memorySize) {
return config.memorySize;
}
return undefined;
}

getStage() {
if (typeof this.config !== "object") {
return "dev";
Expand Down
7 changes: 7 additions & 0 deletions src/utils/abbreviateFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function abbreviateFunction(fullFuncName, stackName) {
return fullFuncName.replace(`${stackName}-`, "");
}

module.exports = {
abbreviateFunction,
};