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?"