Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Mathr3e opened this issue May 14, 2024 · 2 comments

Comments

@Mathr3e
Copy link

Mathr3e commented May 14, 2024

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
Copy link
Contributor

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
Copy link
Collaborator

SamyOubouaziz commented May 15, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants