GitHub user timkpaine edited a comment on the discussion: How does a 
plugin/extension/dag get a local client without authenticating?

Here was a possible solution, which may work on the server with access to the 
airflow config `[api_auth]`:

```python
from airflow.api_fastapi.auth.tokens import JWTGenerator, get_signing_args
from airflow.configuration import conf

# Generate a JWT token for the public REST API
generator = JWTGenerator(
    **get_signing_args(),  # Uses the same jwt_secret or private_key as the API 
server
    valid_for=conf.getint("api_auth", "jwt_expiration_time"),
    audience=conf.get("api", "jwt_audience", fallback="apache-airflow"),
)

# The token needs 'sub' (username) and 'role' claims for SimpleAuthManager 
token = generator.generate(extras={"sub": "internal_service", "role": "ADMIN"})

# Now use with any HTTP client
import httpx

client = httpx.Client(
    base_url=conf.get("api", "base_url", fallback="http://localhost:8080";),
    headers={"Authorization": f"Bearer {token}"}
)

# Update a pool
response = client.patch("/api/v2/pools/my_pool", json={"slots": 10})
```

GitHub link: 
https://github.com/apache/airflow/discussions/61800#discussioncomment-15806613

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to