scaleway / docs-content

Scaleway Documentation contents
https://www.scaleway.com/en/docs/
78 stars 204 forks source link

[👩‍💻 Documentation Request]: Call secrets from serverless function #3160

Open Mathr3e opened 1 month ago

Mathr3e commented 1 month ago

Summary

I was initially wondering what the difference was between: a) the secrets availables from the advanced options at creation time of a serverless function b) secrets created from the Secret Manager menu

Why is it needed?

It was quite difficult to find documentation on calling secrets from a serverless function. I finally was able to figure out the difference between secret types. I post my code here. It will probably be useful to someone else

Want to write this documentation yourself?

Yes

Related PR(s)

from urllib import request, parse, error
import os, base64, json

# Passed as local ENV variable
region = os.getenv('REGION')            #Available Regions: fr-par/nl-ams/pl-waw

# Passed as local SECRET variable
authtk = os.getenv('SCW_SECRET_KEY')    #API key
secret = os.getenv('SECRET_ID')         #Secret ID from secret manager

# Variable in SECRET Manager 
url = (
    "https://api.scaleway.com/secret-manager/v1beta1/regions/"
    + region
    + "/secrets/"
    + secret
    + "/versions/latest/access"
)

def handle(event, context):

    req = request.Request(url, method='GET')
    req.add_header('X-Auth-Token', authtk)
    req.add_header('Content-Type', 'application/json')

    try:
        res = json.loads(request.urlopen(req).read().decode())
        key = base64.b64decode(res['data']).decode('ascii')

    except error.HTTPError as e:
        res = 'exception:' + e.read().decode()
        key = ''

    return {
        "body": {
            "message": str(res),
            "key": str(key),
        },
        "statusCode": 200,
    }

(Optional) Scaleway Organization ID

b7190efa-fa4d-4717-b653-d351d5094caf

Email address

mathr3e@gmail.com

nerda-codes commented 1 month ago

Hello @Mathr3e, thank you for raising this documentation need! You can go ahead and open a pull request whenever you are ready to start working on the content. You can refer to our contribution guidelines for more information on how to write it. Feel free to contact us here or on the #documentation channel of our Community Slack if you have any questions.

Best,

Néda

SamyOubouaziz commented 1 month ago

Hello @Mathr3e, thank you for sharing your code! The Secrets Manager and Serverless Functions integration is currently under works as it has already been requested by several users. The feature should be live in the next months, and will supersede this documentation.

I would suggest not writing a dedicated documentation and keeping this issue open in the meantime so users can use your code until the feature goes live.

Thanks again for raising this issue!

Samy

(cc @nerda-codes)