Question

Access Encrypted app-level environment variables from python script

This feels like a dumb question, but how should I be calling app-level environment variables from a python script hosted on app platform? Specifically, I can’t seem to pass the original value of the environment variable to my script after I set the variable to “encrypted” in the app’s control panel. That is, it appears that the encrypted value of the environment variable is getting passed to my app, which is strange because the unencrypted value shows up when I look it up on the console. I’m using this code:

import os

PASSKEY = os.environ.get('PASSKEY')

Any idea what’s going on?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
January 15, 2024

Hey!

Not a dumb question at all! In fact, it’s an important aspect of working with environment variables, especially when dealing with sensitive data like encrypted variables in a cloud environment such as DigitalOcean’s App Platform.

When you set an environment variable as “encrypted” in the DigitalOcean App Platform control panel, it’s encrypted at rest for security purposes and is also obscured from all logs. However, when your application runs, DigitalOcean automatically decrypts these variables, and they should be available to your application in their original, unencrypted form. This means that your Python script should be able to access the unencrypted value of PASSKEY directly, without needing to do any decryption manually.

Your current approach to accessing the environment variable in Python is correct:

import os

PASSKEY = os.environ.get('PASSKEY')

Here are a few things to check and consider:

  1. Ensure that your app has been redeployed after setting the encrypted environment variable. Changes to environment variables often require a redeployment for them to take effect.

  2. Double-check the name of the environment variable. It needs to match exactly between what’s set in the DigitalOcean control panel and what your script is trying to access.

  3. Make sure that the environment variable is set at the appropriate scope. If it’s an app-level variable, it should be available to all components of your app. If it’s set at the component level, it will only be available to that specific component.

  4. To debug, you might temporarily print out the environment variable in a safe, controlled environment (ensuring no sensitive data is exposed in logs or output). This can confirm if the variable is being passed correctly to your application.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel