Skip to content

Commit

Permalink
Check serverless.yml to correct no-default false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
mansurpasha committed May 18, 2020
1 parent 3353faa commit acb4412
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
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.getFunctionConfig(shortenedName).memorySize
);
}

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.getFunctionConfig(shortenedName).timeout
);
}

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

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

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,
};

0 comments on commit acb4412

Please sign in to comment.