Skip to end of banner
Go to start of banner

Preparing Python Scripts for DataSelf Cloud

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

For security we do not run Python scripts containing embedded credentials or other confidential information on DataSelf cloud servers. Nor do we want to send or receive scripts with credentials via email. How to prepare Python scripts to read credentials and other secure information from Windows environment variables.

Please remove embedded credentials from Python scripts before emailing them to DataSelf.

  1. Use Python’s os.getenv method to replace constants and literal values of credentials and other secure values.

  2. Email the Python script to DataSelf.

  3. Sent credentials outside of email (e.g., text).

os.getenv Method

Python’s os.getenv method returns the value of the environment variable key, if it exists.

(For testing on your computer see Create Windows Environment Variables with PowerShell)

os.getenv Usage

import os

# Retrieve the value of the PASSWORD environment variable
password_credential = os.getenv('PASSWORD')
print(f"Password credential: {password_credential}")

# Optional Default if environment variable does not exist
# Attempt to retrieve a non-existent environment variable with a default value
custom_variable = os.getenv('CUSTOM_VARIABLE', 'default_value')
print(f"Custom Variable: {custom_variable}")

Example Script

Code snippets taken from a working Python script

import os

body={
 "name" : "gshu",
 "password" : os.getenv('CONNECTED_ACU_API_PASSWORD'), #Modify this to get password
 "company" : "Connected",
 "branch" : "2J",
 "locale" : "en-US"
}

def get_metrc_encoding():
    vendor_key = os.getenv('Acme_ACU_API_VENDOR_KEY')  # Modify this to get vendor key
    user_key = os.getenv('Acme_ACU_API_USER_KEY')  # Modify this to get user key
    encoding_str = f"{vendor_key}:{user_key}"
    encoding = base64.b64encode(encoding_str.encode())
    encoding = encoding.decode('ASCII')
    return encoding

Naming Convention

DataSelf’s naming convention for environment variables used in scripts is:

<CLIENTNAME>_<SCRIPT_NAME>_<VARIABLENAM>.

For example: Acme_ACU_API_PASSWORD, Acme_ACU_API_VENDOR_KEY, Acme_ACU_API_USER_KEY

Related Pages

  • No labels