Variable encryption for secrets in Apache Airflow - Time & Space Complexity
When encrypting secrets stored as variables in Airflow, it is important to understand how the time to encrypt or decrypt grows as the number of secrets increases.
We want to know how the work Airflow does changes when handling more secrets.
Analyze the time complexity of the following Airflow code snippet.
from airflow.models import Variable
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher = Fernet(key)
secrets = {"db_password": "mypassword", "api_key": "12345"}
for name, value in secrets.items():
encrypted_value = cipher.encrypt(value.encode())
Variable.set(name, encrypted_value.decode())
This code encrypts each secret value and stores it as an Airflow variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each secret to encrypt and store it.
- How many times: Once for each secret in the input dictionary.
As the number of secrets increases, the encryption and storage steps happen for each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 encryptions and 10 variable sets |
| 100 | 100 encryptions and 100 variable sets |
| 1000 | 1000 encryptions and 1000 variable sets |
Pattern observation: The work grows directly with the number of secrets.
Time Complexity: O(n)
This means the time to encrypt and store secrets grows linearly as you add more secrets.
[X] Wrong: "Encrypting multiple secrets happens all at once, so time stays the same no matter how many secrets there are."
[OK] Correct: Each secret must be encrypted and stored separately, so more secrets mean more work and more time.
Understanding how encryption time scales helps you design secure workflows that handle secrets efficiently as they grow.
"What if we batch encrypt all secrets into one string before storing? How would the time complexity change?"
Practice
Solution
Step 1: Understand variable encryption purpose
Encryption protects sensitive information by making it unreadable without a key.Step 2: Relate to Airflow variables
Airflow encrypts variables to keep secrets like passwords safe from unauthorized users.Final Answer:
To keep sensitive data like passwords safe from unauthorized access -> Option AQuick Check:
Encryption = Protect secrets [OK]
- Thinking encryption speeds up DAGs
- Confusing encryption with compression
- Believing encryption makes variables visible
API_KEY with value 12345 in Airflow CLI?Solution
Step 1: Recall Airflow CLI syntax for encrypted variables
The correct syntax places the--encryptflag before the variable name and value.Step 2: Match syntax with options
airflow variables set --encrypt API_KEY 12345 usesairflow variables set --encrypt API_KEY 12345, which is correct.Final Answer:
airflow variables set --encrypt API_KEY 12345 -> Option AQuick Check:
Flag before name and value = airflow variables set --encrypt API_KEY 12345 [OK]
- Placing --encrypt after variable name or value
- Swapping variable name and value order
- Omitting --encrypt flag
from airflow.models import Variable
secret = Variable.get('MY_SECRET')What will
secret contain if MY_SECRET was set with encryption?Solution
Step 1: Understand Variable.get behavior with encrypted variables
Airflow automatically decrypts encrypted variables when accessed via Variable.get.Step 2: Determine what
It holds the original decrypted secret value, not the encrypted string or None.secretholdsFinal Answer:
The decrypted original secret value -> Option BQuick Check:
Variable.get decrypts automatically [OK]
- Expecting encrypted string instead of decrypted
- Assuming Variable.get raises error for encrypted vars
- Thinking encrypted vars are hidden and return None
airflow variables set API_SECRET 98765 --encryptBut the variable is not encrypted. What is the likely mistake?
Solution
Step 1: Check correct placement of
The flag must be placed immediately after--encryptflagset, before variable name and value.Step 2: Analyze the command given
The flag is placed after value, so encryption is not applied.Final Answer:
The--encryptflag should come before the variable name and value -> Option DQuick Check:
Flag position matters for encryption [OK]
- Putting --encrypt after value
- Restarting Airflow unnecessarily
- Encoding value manually before setting
Solution
Step 1: Understand best practice for multiple encrypted variables
Using a script automates setting each secret with encryption flag, ensuring safety.Step 2: Evaluate other options
Uploading plain text or skipping encryption risks exposure; connections are different concept.Final Answer:
Write a script that usesairflow variables set --encryptfor each secret -> Option CQuick Check:
Automate encrypted variable setting for multiple secrets [OK]
- Uploading plain text files
- Ignoring encryption for secrets
- Confusing variables with connections
Ready to prove it?
Pass the test and this pattern moves unknown
