aws lambda environment variable initiate outside handler - environment-variables

I am calling an environment variable outside handler and initializing a variable to be available in a global scope so multiple methods can reuse them.my handler is triggered by s3 event hence environment variable that i set outside handler become useless, can anyone let me know if its possible to have a variable initialized outside handler
Thanks
masterkey = environ['accesstoken']
`print("environment variable: " + environ['accesstoken'])
def lambda_handler(event, context):
........
........

Related

How to secure replacment?

So I need to secure my code.
My code is obfuscated and im using load(tostring(resultServerr))() but someone can do load = print on top and it will print everything from my code..
I have to secure it to avoid replacement but i dont know how.
If you use Lua 5.2 or higher, you can provide your own sandboxed _ENV table in 4th argument of load. If you use Lua 5.1 or 5.0 you can use setfenv, which works almost the same way as new _ENV.
local func, err = load(unsafecode, nil, nil, {})
if not func then print(err) return end
func()
or
local func, err = load(unsafecode)
if not func then print(err) return end
setfenv(func, {})
func()
Here I use empty table {} to protect from using all globals, adding new ones and overwriting existing. If you want to provide some functions, just add them into this table, they won't be removed from _G if they change it inside the sandboxed code.
It depends on how and where are you using lua environment and what normal user is allowed to do with lua?
If only you controls startup process of the server or programm and the user has no access to startup process, it is possible to do sandboxing on init state as suggested Spar, or something like:
init.lua:
function prepare()
local load = _G.load
_G.load_extension = function()
local obfuscated = get_extension()
load(obfuscated)
end
end
Original load is stored as local variable and can't be overwritten for load_extension in following lua scripts/functions.
If normal user can modify init scripts or can load external libraries (.dll/.so), you're out of luck to properly secure your code.
If the user has access to resultServerr content, you're out of luck hiding it, as this variable can be passed to any other process/function, not only print or load.
Overwrite print = nil then it can't print.
You can create your own hidden print function if you still need that capability; just remember what you called it, so you can access it later.
hidden_print = print
print = nil
print('this')
stdin:1: attempt to call a nil value (global 'print')
stack traceback:
stdin:1: in main chunk
[C]: in ?
hidden_print('that')
that

Why does the jenkinsfile use a closure in this way?

It's a common pattern to write Jenkins pipeline code like this:
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
}
I'm not sure how to word this simply, but this closure get implicitly imported and can be called by the file name of the .groovy file it lives in.
I call it like:
MyClosure { myarg = 'sdfsdf' }
I not entirely sure what this is doing. It's doing call(body) and then assigning body as the delegate. so that means the closure I pass it is the delegate, but isn't that just the owner? Wah? me confused.
When this runs, it is creating an empty map (config). Then it is telling the closure (body) to look at the delegate first to find properties by setting its resolveStrategy to be the constant Closure.DELEGATE_FIRST. Then it assigns the config map as the delegate for the body object.
Now when you execute the body() closure, the variables are scoped to the config map, so now config.myarg = 'sdfsdf'.
Now later in the code you can have easy access to the map of values in config.
body is the owner, and by default is the delegate. But when you switch the delegate to be config, and tell it to use the delegate first, you get the variables config's scope.

LuaJIT setfenv not appearing to set further function calls to the given environment

I'm attempting to sandbox some functions using setfenv, and I recieve the following output:
123
nil
Why is testValue when calling sandboxTest() nil, but it's 123 when it's accessed in callSandboxedTest()?
Using LuaJIT 2.1.0-beta2 (Lua 5.1)
function sandboxTest()
print(testValue)
end
local aNumber = 123
function callSandboxedTest()
setfenv(1, {
print = print,
testValue = aNumber,
sandboxTest = sandboxTest
})
print(testValue)
sandboxTest()
end
callSandboxedTest()
Environments aren't part of the call stack. Every function has its own environment. So sandboxTest has an environment, as does callSandboxTest. Changing the environment of one function has no effect on the environment of another.
sandboxTest will continue to use the default environment, so it will access the regular global table to find testValue. And since testValue is never set in the global table, sandboxTest will get nil.
That's why, when maintaining a sandbox, it is very important to carefully choose what functionality to expose to the sandbox. If a function needs to be part of the sandbox, then that function needs to have its environment set.
That's why it's best to sandbox based on compiled Lua chunks rather than individual functions. When creating functions, the functions created will inherit the current environment.
You haven't modified the environment that sandboxTest is using; you only modified the environment of the current function. You can use setfenv to set the environment of a particular function by passing a function name (passing a number modifies the environment of a function in the call stack):
setfenv(sandboxTest, {
print = print,
testValue = aNumber,
sandboxTest = sandboxTest
})
This will print 123 123.

How to run multiple instances of a lua script from C

I have the following lua script :
mydata={}
function update(val)
mydata["x"] = val
if (val == 10)
-- Call C-Api(1)
else
--Register callback with C when free or some event happens
register_callback(callme)
end
function callme()
end
Basically I would like to have two instances of this script running in my C program/process with out having to create a new LUA state per script. And I want to call the function update() with val = 10 from the one instance and call the function update() with val = 20 from another instance. From the second instance it registers a callback function and just waits to be called.
Basically the script file is kind of a RULE that i am trying to achieve.
Several events on the system can trigger this rule or script file. I would like to process this rule as per the event that triggered it. There could be multiple events triggering this script to run at the same time. So I need to have multiple instances of this script running differentiated by the kind of event that triggered it.
To Summarize, I want each caller to have separate individual instance of mydata
I would like to achieve something like this. I read some where that we should be able to run multiple instances of a lua script with out having to create a new lua instance by loading a new environment before loading the script
But I am not able to find the exact details.
Could some body help ?
While I'm still not sure what exactly you are trying to achieve, but if you want to have two instances of the same function that keep the data they use private, you just need to create a closure and return an anonymous function that your C code will use.
Something like this should work:
function createenv(callme)
local mydata={}
return function (val) -- return anonymous function
mydata["x"] = val
if (val == 10)
-- Call C-Api(1)
else
--Register callback with C when free or some event happens
register_callback(callme)
end
end
end
Now in one part of your (C or Lua) code you can do:
local update = createenv(function() --[[do whatever]] end)
update(10)
And then in another part you can do:
local update = createenv(function() --[[do something else]] end)
update(20)
And they should have nothing in common between each other. Note that they still shared the same Lua state, but their instances of mydata will be independent of each other.

How can I change this from a local to instance variable?

I have this block of code:
if %w[ups fedex usps].include?(carrier.slug)
send(carrier.slug).track(number)
end
Which effectively replicates:
ups.track(number)
fedex.track(number)
usps.track(number)
But what I now need is that, with instance variables:
#ups.track(number)
#fedex.track(number)
#usps.track(number)
What's the equivalent?
if %w[ups fedex usps].include?(carrier.slug)
var_name = "##{carrier.slug}"
instance_variable_get(var_name).track(number)
end
By the way, your interpretation is incorrect.
ups.track(number)
fedex.track(number)
usps.track(number)
These are treated as methods, not local variables. If there's no method ups, your code will fail (even if there is a local var with the same name).

Resources