Skip to content

[πŸ‘©β€πŸ’» Documentation Request]: Call secrets from serverless functionΒ #3160

Open
@Mathr3e

Description

@Mathr3e

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

[email protected]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions