lambda-local
Advanced tools
Comparing version 1.4.7 to 1.4.8
# ChangeLog | ||
## 1.4.8 (2018/05/26) | ||
* Read default AWS config files | ||
* Improve absolute/relative path finding | ||
* Handle syntax error in handlers | ||
## 1.4.7 (2018/04/03) | ||
@@ -4,0 +9,0 @@ * Support async functions (thanks to @hoegertn) |
@@ -10,4 +10,5 @@ 'use strict'; | ||
var logger = require('winston'); | ||
var dotenv = require('dotenv'); | ||
const dotenv = require('dotenv'); | ||
const fs = require('fs'); | ||
const utils = require('./utils.js'); | ||
@@ -77,4 +78,2 @@ | ||
// set environment variables before the require | ||
process.env['AWS_REGION'] = region || process.env['AWS_REGION'] || 'us-east-1'; | ||
process.env['AWS_DEFAULT_REGION'] = region || process.env['AWS_DEFAULT_REGION'] || 'us-east-1'; | ||
process.env['AWS_LAMBDA_FUNCTION_NAME'] = lambdaHandler; | ||
@@ -93,8 +92,8 @@ process.env['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] = 1024; | ||
if (environment != null) { | ||
if (envdestroy == null){ | ||
envdestroy = false; | ||
} | ||
Object.keys(environment).forEach(function(key) { | ||
process.env[key]=environment[key]; | ||
}); | ||
if (envdestroy == null){ | ||
envdestroy = false; | ||
} | ||
Object.keys(environment).forEach(function(key) { | ||
process.env[key]=environment[key]; | ||
}); | ||
} | ||
@@ -104,10 +103,23 @@ | ||
if (envfile != null) { | ||
dotenv.config({ path: envfile }); | ||
dotenv.config({ path: envfile }); | ||
} | ||
//load profile | ||
if (profilePath) { | ||
//load profiles | ||
profilePath = profilePath || process.env['AWS_SHARED_CREDENTIALS_FILE']; | ||
var default_config_file = utils.getAbsolutePath("~/.aws/config"); | ||
var default_credentials_file = utils.getAbsolutePath("~/.aws/credentials"); | ||
if (fs.existsSync(default_config_file)) { //Default config file | ||
utils.loadAWSCredentials(default_config_file, profileName); | ||
} | ||
if (fs.existsSync(default_credentials_file)) { //Default credentials file | ||
utils.loadAWSCredentials(default_credentials_file, profileName); | ||
} | ||
if (profilePath) { //Provided config/credentials file | ||
utils.loadAWSCredentials(profilePath, profileName); | ||
} | ||
//post loading profiles environment variables | ||
process.env['AWS_REGION'] = region || process.env['AWS_REGION'] || 'us-east-1'; | ||
process.env['AWS_DEFAULT_REGION'] = region || process.env['AWS_DEFAULT_REGION'] || 'us-east-1'; | ||
//Logs | ||
@@ -141,21 +153,22 @@ if (typeof verboseLevel == 'undefined'){ | ||
}); | ||
try { | ||
if(callback) context.callback = callback; | ||
// load lambda function | ||
if (!(lambdaFunc)){ | ||
lambdaFunc = require(utils.getAbsolutePath(lambdaPath)); | ||
} | ||
if(callback) context.callback = callback; | ||
// load lambda function | ||
if (!(lambdaFunc)){ | ||
lambdaFunc = require(utils.getAbsolutePath(lambdaPath)); | ||
} | ||
//load event | ||
if (event instanceof Function){ | ||
event = event(); | ||
} | ||
// Handling timeout | ||
context._timeout = setTimeout(function() { | ||
throw new utils.TimeoutError('Task timed out after ' + (timeoutMs / 1000).toFixed(2) + ' seconds'); | ||
}, timeoutMs); | ||
// execute lambda function | ||
try { | ||
//load event | ||
if (event instanceof Function){ | ||
event = event(); | ||
} | ||
// Handling timeout | ||
context._timeout = setTimeout(function() { | ||
throw new utils.TimeoutError('Task timed out after ' + (timeoutMs / 1000).toFixed(2) + ' seconds'); | ||
}, timeoutMs); | ||
// execute lambda function | ||
var result = lambdaFunc[lambdaHandler](event, context, context.done); | ||
@@ -162,0 +175,0 @@ if (result) { |
@@ -7,3 +7,5 @@ 'use strict' | ||
const join = require('path').join; | ||
const fs = require("fs"); | ||
const os = require("os"); | ||
const join = require("path").join; | ||
@@ -25,4 +27,3 @@ /** | ||
var _getAbsolutePath = function(path) { | ||
var res = null, | ||
homeDir = process.env.HOME || process.env.USERPROFILE; | ||
var homeDir = process.env.HOME || process.env.USERPROFILE; | ||
@@ -33,16 +34,15 @@ var windowsRegex = /([A-Z|a-z]:\\[^*|"<>?\n]*)|(\\\\.*?\\.*)/; | ||
//On Windows and linux | ||
res = path; | ||
return path; | ||
} else { | ||
if (path === '~') { | ||
//On linux only | ||
res = homeDir; | ||
} else if (path.slice(0, 2) !== '~/') { | ||
//On Windows and linux | ||
res = join(process.cwd(), path); | ||
return homeDir; | ||
} else if (path.slice(0, 2) === '~/') { | ||
return join(homeDir, path.slice(2)); | ||
} else if (path.slice(0, 2) === './') { | ||
return join(process.cwd(), path.slice(2)); | ||
} else { | ||
//On linux only | ||
res = join(homeDir, path.slice(2)); | ||
return join(process.cwd(), path); | ||
} | ||
} | ||
return res; | ||
return null; | ||
}; | ||
@@ -68,2 +68,15 @@ | ||
var _load_var_from_file = function(varname, envname, data, profileName){ | ||
if(process.env[envname]){ | ||
//If already set, it overwrites config files | ||
return; | ||
} | ||
var regex = new RegExp('\\[' + profileName + | ||
'\\](.|\\n|\\r\\n)*?' + varname + '( ?)+=( ?)+(.*)'), | ||
match; | ||
if ((match = regex.exec(data)) !== null) { | ||
process.env[envname] = match[4]; | ||
} | ||
} | ||
var _loadAWSCredentials = function(path) { | ||
@@ -78,33 +91,14 @@ //default parameter | ||
data = dataRaw.toString(); | ||
_load_var_from_file("aws_secret_access_key", "AWS_SECRET_ACCESS_KEY", data, profileName); | ||
_load_var_from_file("aws_access_key_id", "AWS_ACCESS_KEY_ID", data, profileName); | ||
_load_var_from_file("aws_session_token", "AWS_SESSION_TOKEN", data, profileName); | ||
_load_var_from_file("metadata_service_timeout", "AWS_METADATA_SERVICE_TIMEOUT", data, profileName); | ||
_load_var_from_file("metadata_service_num_attempts", "AWS_METADATA_SERVICE_NUM_ATTEMPTS", data, profileName); | ||
var regex = new RegExp('\\[' + profileName + | ||
'\\](.|\\n|\\r\\n)*?aws_secret_access_key( ?)+=( ?)+(.*)'), | ||
match; | ||
if ((match = regex.exec(data)) !== null) { | ||
process.env['AWS_SECRET_ACCESS_KEY'] = match[4]; | ||
} else { | ||
console.log('warning', 'Couldn\'t find the \'aws_secret_access_key\' field inside the file.'); | ||
} | ||
_load_var_from_file("region", "AWS_REGION", data, profileName); | ||
regex = new RegExp('\\[' + profileName + '\\](.|\\n|\\r\\n)*?aws_access_key_id( ?)+=( ?)+(.*)'); | ||
if ((match = regex.exec(data)) !== null) { | ||
process.env['AWS_ACCESS_KEY_ID'] = match[4]; | ||
} else { | ||
console.log('warning', 'Couldn\'t find the \'aws_access_key_id\' field inside the file.'); | ||
} | ||
regex = new RegExp('\\[' + profileName + '\\](.|\\n|\\r\\n)*?aws_session_token( ?)+=( ?)+(.*)'); | ||
if ((match = regex.exec(data)) !== null) { | ||
process.env['AWS_SESSION_TOKEN'] = match[4]; | ||
} | ||
if (process.env['AWS_SESSION_TOKEN'] && (process.env['AWS_ACCESS_KEY_ID'] || process.env['AWS_SECRET_ACCESS_KEY'])){ | ||
console.log('warning', 'Using both auth systems: aws_access_key/id and secret_access_token !'); | ||
} | ||
regex = new RegExp('\\[' + profileName + | ||
'\\](.|\\n|\\r\\n)*?region( ?)+=( ?)+(.*)'), | ||
match; | ||
if ((match = regex.exec(data)) !== null) { | ||
process.env['AWS_REGION'] = match[4]; | ||
} | ||
}; | ||
@@ -111,0 +105,0 @@ |
{ | ||
"name": "lambda-local", | ||
"version": "1.4.7", | ||
"version": "1.4.8", | ||
"description": "Commandline tool to run Lambda functions on your local machine.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -43,3 +43,3 @@ "use strict"; | ||
it("should return existing path file", function () { | ||
var f_path = utils.getAbsolutePath("test.js"); | ||
var f_path = utils.getAbsolutePath("./test.js"); | ||
assert.doesNotThrow(function(){fs.accessSync(f_path, fs.F_OK)}); | ||
@@ -87,2 +87,5 @@ }); | ||
before(function (cb) { | ||
//For this test: set an environment var which should not be overwritten by lambda-local | ||
process.env["AWS_REGION"] = "unicorn-universe"; | ||
// | ||
var lambdalocal = require("../lib/lambdalocal.js"); | ||
@@ -105,3 +108,3 @@ lambdalocal.setLogger(winston); | ||
"envkey2": {"k":"v"}, | ||
"envkey3": 123, | ||
"envkey3": 123 | ||
}, | ||
@@ -122,3 +125,11 @@ envfile: path.join(__dirname, "./other/env"), | ||
}); | ||
it("should not have overwritten already-existing env vars", function () { | ||
assert.equal(process.env.AWS_REGION, "unicorn-universe"); | ||
}); | ||
after(function (cb){ | ||
delete process.env["AWS_REGION"]; | ||
cb(); | ||
}); | ||
}); | ||
describe("# Environment Variables (destroy)", function () { | ||
@@ -154,3 +165,2 @@ var done, err; | ||
describe("# AWS credentials", function () { | ||
@@ -399,2 +409,16 @@ it("should return correct credentials", function () { | ||
}); | ||
it("should fail: syntax error", function () { | ||
var command = get_shell("node ../bin/lambda-local -l ./functs/test-func-syntax-error.js -e ./events/test-event.js"); | ||
var r = spawnSync(command[0], command[1]); | ||
assert.equal(r.status, 1); | ||
console.log(r.output.toString('utf8')); | ||
}); | ||
it("should fail: require error", function () { | ||
var command = get_shell("node ../bin/lambda-local -l ./functs/test-func-require-error.js -e ./events/test-event.js"); | ||
var r = spawnSync(command[0], command[1]); | ||
assert.equal(r.status, 1); | ||
console.log(r.output.toString('utf8')); | ||
}); | ||
}); | ||
@@ -401,0 +425,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
70419
52
1425
52