Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

node.js - how to make faasd secrets available to function using node12 template?

While writing this question I found the answer, but still it might be helpful to someone else that start using node12 template model for making functions on openfaas and needs accessing secrets defined using faas-cli.

By creating a function for build and deploy with openfaas (or faasd in my case), we need a function.yml file as follow:

provider:
  name: openfaas
  gateway: https://faasd.mygateway.com
functions:
  my_function:
    lang: node12
    handler: ./my_function
    image: my_org/my_function:latest

Until here the function deployed will work, but the directory "/var/openfaas/secrets/" will not be available or visible to the function, as it needs to be stated explicity in the yaml file as follows:

provider:
  name: openfaas
  gateway: https://faasd.mygateway.com
functions:
  my_function:
    lang: node12
    handler: ./my_function
    image: my_org/my_function:latest
    secrets:
        - mysecret
        - second-secret
        - third-secret

Now, after a new build/push and deploy the openfaas engine will make a link to each one of the secrets. Note that, the function only can acces those secrets stated on the yaml file, even if there are more secrets on the gatweay machine.

here is a good example: https://www.openfaas.com/blog/faasd-tls-terraform/

From here, a secret is easyly accessed using fs.readFile as follows:

async function getsecret(name) {
    try {
        var secretval = ""
        secretval = await fs.readFile("/var/openfaas/secrets/" + name, "utf8")
        return secretval;
    } catch (error) {
        return "ERROR: on getting secret " + name
    }
}

note that the function needs to be async and the fs must use promises:

const fs = require('fs').promises;

so it can be used as:

   try {
        let mysecret = await getsecret("mysecret"); 

//... other bussiness logic
        return wathever;
    } catch (error) {
        return "ERROR" 
    }

All this came up because I followed an outdated example, and by thinking that secrets are available to functions by default, which is not crearly started on the docs. But somehow, it made me realize how well structured and secure the process is implemented in openfaas, Thanks Alex Ellis and the openfaas team and community!.

Hope this helps someone.

Regards, Enrique

note this is my first write in StackOverflow.. hope is readable and into to the standards.

question from:https://stackoverflow.com/questions/65602942/how-to-make-faasd-secrets-available-to-function-using-node12-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...