300-910 · Question #79
An engineer is developing a script that must call an API using a static Bearer token. Which solution securely protects the credentials from being retrievable from the source code?
The correct answer is A. ```python import os import requests def call_api(): result = requests.get( "https://example.api.com", headers={"Authorization": "Bearer " + os.getenv("TOKEN")}, ) result.raise_for_status() return result.json() ```. To securely manage sensitive API keys like Bearer tokens in Python, environment variables should be used, preventing them from being hardcoded or committed to version control.
Question
Options
- A
import os import requests def call_api(): result = requests.get( "https://example.api.com", headers={"Authorization": "Bearer " + os.getenv("TOKEN")}, ) result.raise_for_status() return result.json() - B
import requests import base64 def call_api(): result = requests.get( "https://example.api.com", headers={"Authorization": "Bearer " + base64.b64decode(b"TXlZZWN yZXR0ZXN0dG9rZW4==").decode("utf-8") }, ) result.raise_for_status() return result.json() - C
import requests def call_api(): result = requests.get( "https://example.api.com", headers={"Authorization": "Bearer MySecretToken"}, ) result.raise_for_status() return result.json() - D
import requests from .password import token def call_api(): result = requests.get( "https://example.api.com", headers={"Authorization": "Bearer " + token}, ) result.raise_for_status() return result.json()
How the community answered
(35 responses)- A74% (26)
- B3% (1)
- C6% (2)
- D17% (6)
Why each option
To securely manage sensitive API keys like Bearer tokens in Python, environment variables should be used, preventing them from being hardcoded or committed to version control.
This solution retrieves the `TOKEN` from an environment variable using `os.getenv("TOKEN")`. Storing sensitive information like API tokens in environment variables is a standard and secure practice because it keeps credentials out of the source code, prevents them from being committed to version control, and allows them to be managed externally (e.g., via CI/CD secrets, deployment configurations) without modifying the application code itself.
While `base64.b64decode` obfuscates the token, Base64 encoding is not encryption and can be easily decoded, meaning the token is still directly present in the source code in a recoverable form and is therefore not securely protected.
Hardcoding the token (`MySecretToken`) directly into the source code is highly insecure, as it exposes the credential to anyone with access to the codebase and makes rotation difficult.
Importing the token from a local file like `.password.py` might separate it from the main script, but if `.password.py` itself contains the token directly and is part of the same repository, it offers no real security benefit and could still be committed to version control.
Concept tested: Secure credential management (environment variables)
Source: https://learn.microsoft.com/en-us/azure/key-vault/general/best-practices
Topics
Community Discussion
No community discussion yet for this question.