@@ -178,14 +178,6 @@ For details on enabling/configuring CORS, see
178
178
To be able to meet the requirements of many organizations, Airflow supports many authentication methods,
179
179
and it is even possible to add your own method.
180
180
181
- If you want to check which auth backend is currently set, you can use
182
- ` airflow config get-value api auth_backends ` command as in the example below.
183
-
184
- ``` bash
185
- $ airflow config get-value api auth_backends
186
- airflow.providers.fab.auth_manager.api.auth.backend.basic_auth
187
- ```
188
-
189
181
The default is to deny all requests.
190
182
191
183
For details on configuring the authentication, see
@@ -279,24 +271,62 @@ import airflow_client.client
279
271
280
272
## Getting Started
281
273
274
+ Before attempting the following examples ensure you have an account with API access.
275
+ As an example you can create an account for usage with the API as follows using the Airflow CLI.
276
+
277
+ ``` bash
278
+ airflow users create -u admin-api -e admin-api@example.com -f admin-api -l admin-api -p $PASSWORD -r Admin
279
+ ```
280
+
282
281
Please follow the [ installation procedure] ( #installation--usage ) and then run the following:
283
282
284
283
``` python
285
284
import airflow_client.client
285
+ import requests
286
286
from airflow_client.client.rest import ApiException
287
287
from pprint import pprint
288
+ from pydantic import BaseModel
289
+
290
+
291
+ # What we expect back from auth/token
292
+ class AirflowAccessTokenResponse (BaseModel ):
293
+ access_token: str
294
+
295
+
296
+ # An optional helper function to retrieve an access token
297
+ def get_airflow_client_access_token (
298
+ host : str ,
299
+ username : str ,
300
+ password : str ,
301
+ ) -> str :
302
+ url = f " { host} /auth/token "
303
+ payload = {
304
+ " username" : username,
305
+ " password" : password,
306
+ }
307
+ headers = {" Content-Type" : " application/json" }
308
+ response = requests.post(url, json = payload, headers = headers)
309
+ if response.status_code != 201 :
310
+ raise RuntimeError (f " Failed to get access token: { response.status_code} { response.text} " )
311
+ response_success = AirflowAccessTokenResponse(** response.json())
312
+ return response_success.access_token
313
+
288
314
289
315
# Defining the host is optional and defaults to http://localhost
290
316
# See configuration.py for a list of all supported configuration parameters.
291
- configuration = airflow_client.client.Configuration(host = " http://localhost" )
317
+ host = " http://localhost"
318
+ configuration = airflow_client.client.Configuration(host = host)
292
319
293
320
# The client must configure the authentication and authorization parameters
294
321
# in accordance with the API server security policy.
295
322
# Examples for each auth method are provided below, use the example that
296
323
# satisfies your auth use case.
297
324
298
- configuration.access_token = os.environ[" ACCESS_TOKEN" ]
299
-
325
+ configuration.access_token = get_airflow_client_access_token(
326
+ host = host,
327
+ username = " admin-api" ,
328
+ password = os.environ[" PASSWORD" ],
329
+ )
300
330
301
331
# Enter a context with an instance of the API client
302
332
with airflow_client.client.ApiClient(configuration) as api_client:
@@ -599,7 +629,7 @@ You can also set it by env variable: `export AIRFLOW__CORE__LOAD_EXAMPLES=True`
599
629
600
630
* optionally expose configuration (NOTE! that this is dangerous setting). The script will happily run with
601
631
the default setting, but if you want to see the configuration, you need to expose it.
602
- In the ` [webserver ] ` section of your ` airflow.cfg ` set:
632
+ In the ` [api ] ` section of your ` airflow.cfg ` set:
603
633
604
634
``` ini
605
635
[api]
0 commit comments