origin
Recently serverless and FAAS articles are a lot, the major cloud vendors are promoting, looking at the hands are too itchily, but business development did not use cloud services, but the use of internal K8S cluster. So I decided to build faAS myself. Easy said, easy done!
In fact, the internal already started, always want to write an article, but the business is too busy, finally take some time to share.
In a word demand
How to implement any function as an interface
Analysis of the
Both functions and interfaces are inherently intuitive, with inputs and outputs. The request of the interface is the input of the function, and the output of the function is the return value of the interface. Perfect!
function
How does this function work?
Nodejs implements its own VM, happily leaving the code to the VM to run. Great!
Hold on! You think that settles the matter?
Please do not use VM to run untrusted code. Can your colleagues’ code be trusted? A question from the big guy.
How is it okay to search the Internet and find this line of code, and then your service dies?
new vm.Script('this.constructor.constructor("return process")().exit()')
Copy the code
Believing in the power of community, HE discovered VM2 as a treasure, just like his official slogan
vm2 is a sandbox that can run untrusted code with whitelisted Node’s built-in modules. Securely!
Big guy ran over and asked, do you believe he is safe is safe? I ran to the issue and tried it. Fortunately, the service was not suspended this time, but there seemed to be no response
const {VM} = require('vm2');
new VM({timeout:1}).run(`
function main() {while(1){}
}
Copy the code
After all, it’s a flag. Why salt? Take it out of the pain.
Sandbox security
After all, sandbox is not safe, even VM2, can not be sure that it is safe, how to break?
Internet Explorer crashes a lot. Chrome is much better. Why? Yes, that’s right, the multi-process model.
For example: a process is like a house, thread is the room, the room catches fire and the house is lost, multi-process is multiple houses, destroy one and still have a place to live.
Our final solution was to isolate user unsafe code in child processes.
vmbox
Vmbox is open source. Check out the next article.
practice
Current internal implementation functions