Secure Configuration with Environment Variables in Python
Environment variables are a crucial tool for managing configuration settings in applications, especially when deploying to different environments (development, staging, production). This challenge will guide you in implementing a Python script that reads configuration values from environment variables, providing a secure and flexible way to manage sensitive information like API keys and database credentials without hardcoding them directly into your code.
Problem Description
You are tasked with creating a Python script that retrieves configuration settings from environment variables. The script should define a dictionary of expected environment variables and their corresponding default values. If an environment variable is not set, the script should use the default value. The script should then print the retrieved configuration values in a user-friendly format.
Key Requirements:
- Environment Variable Retrieval: The script must successfully retrieve values from environment variables using
os.environ.get(). - Default Values: If an environment variable is not found, the script must gracefully fall back to a predefined default value.
- Configuration Dictionary: Use a dictionary to store the expected environment variable names and their default values.
- Clear Output: The script should print the retrieved configuration values in a clear and readable format.
- Error Handling (Optional): While not strictly required, consider how you might handle cases where an environment variable is expected but cannot be retrieved (e.g., due to permissions issues).
Expected Behavior:
The script should execute without errors, even if some environment variables are not set. It should print the retrieved configuration values, using default values where necessary.
Edge Cases to Consider:
- Environment variables with empty strings as values.
- Environment variables with special characters.
- The absence of required environment variables (consider how to handle this gracefully).
Examples
Example 1:
Assume the following environment variables are set:
export API_KEY="your_api_key"
export DATABASE_URL="your_database_url"
# (Your Python code here)
Output:
API_KEY: your_api_key
DATABASE_URL: your_database_url
DEBUG: False
Explanation: The script successfully retrieves API_KEY and DATABASE_URL from the environment. DEBUG is not set, so it uses its default value of False.
Example 2:
Assume only the API_KEY environment variable is set:
export API_KEY="another_api_key"
# (Your Python code here)
Output:
API_KEY: another_api_key
DATABASE_URL: default_database_url
DEBUG: False
Explanation: The script retrieves API_KEY from the environment. DATABASE_URL is not set, so it uses its default value of default_database_url. DEBUG is not set, so it uses its default value of False.
Constraints
- The script must be written in Python 3.
- The script should be concise and readable.
- The script should not rely on external libraries beyond the standard Python library (specifically, the
osmodule). - The script should handle the case where an environment variable is not set without crashing.
Notes
- The
os.environ.get()method is your friend! It allows you to retrieve environment variables and specify a default value if the variable is not found. - Think about how you want to structure your configuration dictionary.
- Consider using a more robust configuration management library (like
python-dotenv) for more complex scenarios, but for this challenge, focus on the core concepts of environment variable retrieval. - Remember that environment variables are often used to store sensitive information, so avoid hardcoding them directly into your script.