0
0
Apache Airflowdevops~10 mins

Secrets management in Apache Airflow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to retrieve a secret value using Airflow's Variable API.

Apache Airflow
from airflow.models import Variable

api_key = Variable.get([1])
Drag options to blanks, or click blank then click option'
A'API_KEY'
BAPI_KEY
C"api_key"
Dapi_key
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the secret name
Using variable names without quotes
2fill in blank
medium

Complete the code to set a default value when retrieving a secret that might not exist.

Apache Airflow
from airflow.models import Variable

password = Variable.get('DB_PASSWORD', [1]='default_pass')
Drag options to blanks, or click blank then click option'
Adefault
Bdefault_var=True
Cdefault_var='default_pass'
Ddefault_var
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' instead of 'default_var'
Not assigning the default value as a string
3fill in blank
hard

Fix the error in the code to correctly fetch a secret from Airflow's connection backend.

Apache Airflow
from airflow.hooks.base import BaseHook

conn = BaseHook.get_connection([1])
print(conn.password)
Drag options to blanks, or click blank then click option'
Amy_conn_id
B'my_conn_id'
Cmy_conn_id()
D"my_conn_id"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the connection ID without quotes
Calling the connection ID as a function
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores secret names and their values from Airflow Variables.

Apache Airflow
secrets = { [1]: Variable.get([2]) for [1] in secret_names }
Drag options to blanks, or click blank then click option'
Aname
B'name'
Csecret
Dsecret_names
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the key in the dictionary
Using different variable names in key and get()
5fill in blank
hard

Fill all three blanks to filter Airflow Variables whose values are not empty strings.

Apache Airflow
filtered_vars = { [1]: Variable.get([2]) for [1] in Variable.get_all() if Variable.get([3]) != '' }
Drag options to blanks, or click blank then click option'
Akey
B'key'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the variable name in get()
Using different variable names in the comprehension