Preparing Python Scripts for DataSelf Cloud

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 with calls to Window’s environment variables.

  2. Email the Python script to DataSelf.

  3. Send 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.

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" : "abcd", "password" : os.getenv('ACME_SCRIPT_NAME_PASSWORD'), "company" : "ACME", "branch" : "1", "locale" : "en-US" } def get_metrc_encoding(): vendor_key = os.getenv('ACME_SCRIPT_NAME_VENDOR_KEY') user_key = os.getenv('ACME_SCRIPT_NAME_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>_<VARIABLENAME>.

For example: ACME_SCRIPT_NAME_PASSWORD, ACME_SCRIPT_NAME_VENDOR_KEY, ACME_SCRIPT_NAME_USER_KEY

Â