You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/11/11 09:27:51 UTC

[GitHub] [airflow] ddelange commented on issue #5056: [AIRFLOW-4259] Move remaining models out of models.py

ddelange commented on issue #5056: [AIRFLOW-4259] Move remaining models out of models.py
URL: https://github.com/apache/airflow/pull/5056#issuecomment-552362175
 
 
   Hey all, why was `user.py` [removed](https://github.com/apache/airflow/blame/50343040ff4679e32e01f138ead80bc4bcef4b47/airflow/models/__init__.py#L38-L41) in `master`?
   
    
   
   It is still used by multiple auth backends on `master` (there's also no mention of this removal in `UPDATING.md`):
   
   https://github.com/apache/airflow/blob/88989200a66291580088188f06a6db503ac823e2/airflow/contrib/auth/backends/kerberos_auth.py#L48
   https://github.com/apache/airflow/blob/88989200a66291580088188f06a6db503ac823e2/airflow/contrib/auth/backends/github_enterprise_auth.py#L38
   https://github.com/apache/airflow/blob/88989200a66291580088188f06a6db503ac823e2/airflow/contrib/auth/backends/google_auth.py#L38
   https://github.com/apache/airflow/blob/88989200a66291580088188f06a6db503ac823e2/airflow/contrib/auth/backends/ldap_auth.py#L127
   https://github.com/apache/airflow/blob/88989200a66291580088188f06a6db503ac823e2/airflow/contrib/auth/backends/password_auth.py#L54
   
   How would I now on `master` for create an API user for instance?
   
   Below workflow will work on 1.10 but will fail on master:
   
   airflow.cfg
   ```
   [api]
   # https://airflow.apache.org/security.html?highlight=impersonation#password
   auth_backend = airflow.contrib.auth.backends.password_auth
   ```
   
   upon startup I now use models.User to create a PasswordUser:
   ```
   import os
   
   from airflow.contrib.auth.backends.password_auth import PasswordUser
   from airflow import models
   from airflow.settings import Session
   
   
   def delete_users(session, username):
       """Delete all users with username"""
       query = session.query(models.User)
       to_delete = [user.id for user in query.all() if user.username == username]
       for user_id in to_delete:
           session.delete(query.get(user_id))
       if to_delete:
           session.commit()
   
   
   def create_user(session, username, password):
       """Create a new user with password"""
       user = PasswordUser(models.User())
       user.username = username
       user.email = "REST_API"
       user.password = password
       session.add(user)
       session.commit()
   
   
   def set_or_overwrite_api_user():
       """Add REST API user (BasicAuth), or delete if env var is unset."""
       username = os.environ.get("AIRFLOW__API_USERNAME", "api_user")
       password = os.environ.get("AIRFLOW__API_PWD")
       session = Session()  # sqlalchemy.orm.session under the hood
       delete_users(session, username)
       if password:  # empty env var treated as missing env var
           create_user(session, username, password)
       session.close()
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services