Configuration and Secrets
Deployment Configuration and Twelve-Factor Config
BOTIE
Separating storage does not validate the values or make them secret. A setting still needs a defined representation and a suitable access policy. The examples model this boundary with supplied dictionaries, so they never inspect the reader's actual environment.
Sources for this history
A Timeout Became Text in the Calculation
Botie moves a timeout out of the source so installations can choose different values. The process receives text, but its worker expects a positive number of seconds. A missing setting and an empty setting also need different decisions.
We need one place to read and validate settings before they are used. That boundary also needs to decide which values may appear in a report or an error message.
The blocks use supplied settings and a small whole-number parser. They do not read environment variables or secrets. The Python task implements its stated configuration rules.
Step 1 of 1
Absence and invalid text have different meanings
The default applies only to a missing key in this example.
| Input | Meaning | Decision |
|---|---|---|
| Missing key | Documented default | 10 seconds |
| "5" | Explicit integer text | 5 seconds |
| "" or "fast" | Invalid supplied text | Reject |
Missing key
- Meaning
- Documented default
- Decision
- 10 seconds
"5"
- Meaning
- Explicit integer text
- Decision
- 5 seconds
"" or "fast"
- Meaning
- Invalid supplied text
- Decision
- Reject
Read configuration at a boundary
Checking the text and checking the allowed range catch different mistakes. Returning rejected rows identifies the environment and value that need repair.
Find invalid report settings
The report receives its timeout as text in each environment. The documented setting must be a whole number within the supplied range. Reading "0" as a number succeeds, but zero is not an allowed timeout when the minimum is 1.
BOTIE
Environment rows with invalid timeout settings
[]
Follow the operations
1. Timeout settings by environment
- environment
- local
- timeout_seconds
- "12"
- environment
- production
- timeout_seconds
- "30"
environment timeout_seconds local "12" production "30" 2. Read a field from every record
- 0"12"
- 1"30"
3. Read whole-number settings
- 012
- 130
4. Smallest allowed timeout in seconds
1
5. Largest allowed timeout in seconds
60
6. Check the allowed range
- 0true
- 1true
7. Reverse the keep decisions
- 0false
- 1false
8. Keep flagged entries
[]
9. Result
[]
The blocks use supplied settings and a small whole-number parser. They do not read environment variables or secrets. The Python task implements its stated configuration rules.
1. Read configuration at a boundary
An environment variable is text supplied to a process. It does not become an integer because the rest of the application expects one. A loader should read the representation, convert it deliberately and check the allowed range before dependent work begins.
Pass validated settings to the code that needs them. Looking up a global setting during every calculation hides the dependency and can make a request use inconsistent settings.
2. Default absence without hiding invalid input
A default is a deliberate choice when a value was not supplied. It should not silently replace malformed text that was supplied. For this timeout, absence means ten seconds and an explicit value must be ASCII decimal digit text from one through sixty.
The chosen representation is a contract. Another application could accept fractions or surrounding whitespace, but it needs to document and test that choice.
Validate an explicit dictionary
No real environment variables are read.
def timeout_from(settings):
raw = settings.get("TIMEOUT_SECONDS", "10")
if not isinstance(raw, str) or not raw.isascii() or not raw.isdecimal():
raise ValueError("Supply integer text")
value = int(raw)
if not 1 <= value <= 60:
raise ValueError("Timeout outside allowed range")
return value
print(timeout_from({}))
print(timeout_from({"TIMEOUT_SECONDS":"5"}))
try:
timeout_from({"TIMEOUT_SECONDS":""})
except ValueError:
print("Rejected empty setting")10
5
Rejected empty settingThe error describes the requirement without printing the whole settings object.
Check yourself
Questions on Parts 1 and 2
Question 1 of 2
What follows the contract?
Question data
Timeout must be ASCII decimal text from 1 through 60. Input: "0".3. Parse Boolean text explicitly
bool("false") is True because any nonempty string is truthy. A Boolean setting needs an explicit list of accepted representations, such as the exact strings "true" and "false". Decide separately whether capitalization or whitespace is accepted.
A Pydantic settings boundary can perform conversion and constraints too. Configure and test its accepted representations rather than assuming its defaults match the application's policy.
Truthiness is not configuration parsing
The table explicitly defines the two accepted strings.
def parse_debug(raw):
choices = {"true": True, "false": False}
if raw not in choices:
raise ValueError("Use true or false")
return choices[raw]
print(bool("false"))
print(parse_debug("false"))True
FalseThe same text has different outcomes because these are different operations.
4. Choose which settings may be reported
A token, password or private connection string may be configuration, but that does not authorize displaying it. Environment variables are not automatically a secure secret store. Use the deployment's secret-management mechanism and limit which process can read private values.
Build routine output from reviewed public fields instead of printing an entire object and removing a few familiar secret keys afterward. A new private field could otherwise leak without changing the logging code. Allowed field values still need review; a harmless-looking key does not guarantee harmless content.
Produce an allowlisted status record
The token is a fixed teaching placeholder, not a real credential.
settings = {"mode":"preview", "timeout_seconds":10,
"token":"teaching-placeholder"}
public = {name: settings[name] for name in ("mode","timeout_seconds")}
print(public){'mode': 'preview', 'timeout_seconds': 10}Adding another private key does not automatically add it to the public record.
5. Validate before creating dependent work
Load the settings before constructing a worker that needs them. A validation failure should identify the setting and requirement without echoing private raw values. Then keep the settings' lifetime clear instead of partly reloading configuration during a request.
Dictionary-based tests can check missing, valid and invalid cases without using the host environment. Real deployment checks must establish service reachability and authorization separately. A correctly formatted endpoint does not establish a working connection.
Check yourself
Questions on Parts 1 to 5
Question 1 of 4
What is the result?
Question data
DEBUG is the string "false". Python evaluates bool(DEBUG).Try it with Botie
Build with Botie
Write and run Python