# 安全
默認情況下,所有門都打開。 限制對Web應用程序的訪問的一種簡單方法是在網絡級別或使用SSH隧道執行此操作。
但是,可以通過使用其中一個提供的后端或創建自己的后端來打開身份驗證。
請務必查看[Experimental Rest API](api.html)以保護API。
## Web身份驗證
### 密碼
最簡單的身份驗證機制之一是要求用戶在登錄前指定密碼。密碼身份驗證需要在需求文件中使用`password`子包。 密碼哈希在存儲密碼之前使用bcrypt。
```
[ webserver ]
authenticate = True
auth_backend = airflow.contrib.auth.backends.password_auth
```
啟用密碼身份驗證后,需要先創建初始用戶憑據,然后才能登錄任何人。 未在此身份驗證后端的遷移中創建初始用戶,以防止默認Airflow安裝受到攻擊。 必須通過安裝Airflow的同一臺機器上的Python REPL創建新用戶。
```
# navigate to the airflow installation directory
$ cd ~/airflow
$ python
Python 2 .7.9 ( default, Feb 10 2015 , 03 :28:08 )
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import airflow
>>> from airflow import models, settings
>>> from airflow.contrib.auth.backends.password_auth import PasswordUser
>>> user = PasswordUser ( models.User ())
>>> user.username = 'new_user_name'
>>> user.email = 'new_user_email@example.com'
>>> user.password = 'set_the_password'
>>> session = settings.Session ()
>>> session.add ( user )
>>> session.commit ()
>>> session.close ()
>>> exit ()
```
### LDAP
要打開LDAP身份驗證,請按如下方式配置`airflow.cfg` 。 請注意,該示例使用與ldap服務器的加密連接,因為您可能不希望密碼在網絡級別上可讀。 但是,如果您真的想要,可以在不加密的情況下進行配置。
此外,如果您使用的是Active Directory,并且沒有明確指定用戶所在的OU,則需要將`search_scope`更改為“SUBTREE”。
有效的search_scope選項可以在[ldap3文檔中](http://ldap3.readthedocs.org/searches.html%3Fhighlight%3Dsearch_scope)找到
```
[ webserver ]
authenticate = True
auth_backend = airflow.contrib.auth.backends.ldap_auth
[ ldap ]
# set a connection without encryption: uri = ldap://<your.ldap.server>:<port>
uri = ldaps://<your.ldap.server>:<port>
user_filter = objectClass = *
# in case of Active Directory you would use: user_name_attr = sAMAccountName
user_name_attr = uid
# group_member_attr should be set accordingly with *_filter
# eg :
# group_member_attr = groupMembership
# superuser_filter = groupMembership=CN=airflow-super-users...
group_member_attr = memberOf
superuser_filter = memberOf = CN = airflow-super-users,OU = Groups,OU = RWC,OU = US,OU = NORAM,DC = example,DC = com
data_profiler_filter = memberOf = CN = airflow-data-profilers,OU = Groups,OU = RWC,OU = US,OU = NORAM,DC = example,DC = com
bind_user = cn = Manager,dc = example,dc = com
bind_password = insecure
basedn = dc = example,dc = com
cacert = /etc/ca/ldap_ca.crt
# Set search_scope to one of them: BASE, LEVEL , SUBTREE
# Set search_scope to SUBTREE if using Active Directory, and not specifying an Organizational Unit
search_scope = LEVEL
```
superuser_filter和data_profiler_filter是可選的。 如果已定義,則這些配置允許您指定用戶必須屬于的LDAP組,以便擁有超級用戶(admin)和數據分析器權限。 如果未定義,則所有用戶都將成為超級用戶和數據分析器。
### 滾動你自己
Airflow使用`flask_login`并在`airflow.default_login`模塊中公開一組掛鉤。 您可以更改內容并使其成為`PYTHONPATH`一部分,并將其配置為`airflow.cfg`的后端。
```
[ webserver ]
authenticate = True
auth_backend = mypackage.auth
```
## 多租戶
通過在配置中設置`webserver:filter_by_owner` ,可以在啟用身份驗證時按所有者名稱篩選`webserver:filter_by_owner` 。 有了這個,用戶將只看到它所有者的dags,除非它是超級用戶。
```
[ webserver ]
filter_by_owner = True
```
## Kerberos的
Airflow最初支持Kerberos。 這意味著氣流可以為自己更新kerberos票證并將其存儲在票證緩存中。 鉤子和匕首可以使用票證來驗證kerberized服務。
### 限制
請注意,此時并未調整所有掛鉤以使用此功能。 此外,它沒有將kerberos集成到Web界面中,您現在必須依賴網絡級安全性來確保您的服務保持安全。
Celery集成尚未經過試用和測試。 但是,如果您為每個主機生成一個密鑰選項卡,并在每個工作人員旁邊啟動一個故障單續訂器,那么它很可能會起作用。
### 啟用kerberos
#### 空氣流動
要啟用kerberos,您需要生成(服務)密鑰選項卡。
```
# in the kadmin.local or kadmin shell, create the airflow principal
kadmin: addprinc -randkey airflow/fully.qualified.domain.name@YOUR-REALM.COM
# Create the airflow keytab file that will contain the airflow principal
kadmin: xst -norandkey -k airflow.keytab airflow/fully.qualified.domain.name
```
現在將此文件存儲在氣流用戶可以讀取的位置(chmod 600)。 然后將以下內容添加到`airflow.cfg`
```
[ core ]
security = kerberos
[ kerberos ]
keytab = /etc/airflow/airflow.keytab
reinit_frequency = 3600
principal = airflow
```
啟動票證續訂
```
# run ticket renewer
airflow kerberos
```
#### Hadoop的
如果要使用模擬,則需要在hadoop配置的`core-site.xml`中啟用。
```
<property>
<name>hadoop.proxyuser.airflow.groups</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.airflow.users</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.airflow.hosts</name>
<value>*</value>
</property>
```
當然,如果您需要加強安全性,請用更合適的東西替換星號。
### 使用kerberos身份驗證
已更新配置單元掛鉤以利用kerberos身份驗證。 要允許DAG使用它,只需更新連接詳細信息,例如:
```
{ "use_beeline" : true, "principal" : "hive/_HOST@EXAMPLE.COM" }
```
根據您的設置調整主體。 _HOST部分將替換為服務器的完全限定域名。
您可以指定是否要將dag所有者用作連接的用戶或連接的登錄部分中指定的用戶。 對于登錄用戶,請將以下內容指定為額外:
```
{ "use_beeline" : true, "principal" : "hive/_HOST@EXAMPLE.COM" , "proxy_user" : "login" }
```
對于DAG所有者使用:
```
{ "use_beeline" : true, "principal" : "hive/_HOST@EXAMPLE.COM" , "proxy_user" : "owner" }
```
在DAG中,初始化HiveOperator時,請指定:
```
run_as_owner = True
```
## OAuth身份驗證
### GitHub Enterprise(GHE)身份驗證
GitHub Enterprise身份驗證后端可用于對使用OAuth2安裝GitHub Enterprise的用戶進行身份驗證。 您可以選擇指定團隊白名單(由slug cased團隊名稱組成)以限制僅登錄這些團隊的成員。
```
[ webserver ]
authenticate = True
auth_backend = airflow.contrib.auth.backends.github_enterprise_auth
[ github_enterprise ]
host = github.example.com
client_id = oauth_key_from_github_enterprise
client_secret = oauth_secret_from_github_enterprise
oauth_callback_route = /example/ghe_oauth/callback
allowed_teams = 1 , 345 , 23
```
注意
如果您未指定團隊白名單,那么在GHE安裝中擁有有效帳戶的任何人都可以登錄Airflow。
#### 設置GHE身份驗證
必須先在GHE中設置應用程序,然后才能使用GHE身份驗證后端。 要設置應用程序:
1. 導航到您的GHE配置文件
2. 從左側導航欄中選擇“應用程序”
3. 選擇“開發者應用程序”選項卡
4. 點擊“注冊新申請”
5. 填寫所需信息(“授權回調URL”必須完全合格,例如[http://airflow.example.com/example/ghe_oauth/callback](http://airflow.example.com/example/ghe_oauth/callback) )
6. 點擊“注冊申請”
7. 根據上面的示例,將“客戶端ID”,“客戶端密鑰”和回調路由復制到airflow.cfg
#### 在github.com上使用GHE身份驗證
可以在github.com上使用GHE身份驗證:
1. [創建一個Oauth應用程序](https://developer.github.com/apps/building-oauth-apps/creating-an-oauth-app/)
2. 根據上面的示例,將“客戶端ID”,“客戶端密鑰”復制到airflow.cfg
3. 在`host = github.com`設置`host = github.com`和`oauth_callback_route = /oauth/callback`
### Google身份驗證
Google身份驗證后端可用于使用OAuth2對Google用戶進行身份驗證。 您必須指定電子郵件域以限制登錄(以逗號分隔),僅限于這些域的成員。
```
[ webserver ]
authenticate = True
auth_backend = airflow.contrib.auth.backends.google_auth
[ google ]
client_id = google_client_id
client_secret = google_client_secret
oauth_callback_route = /oauth2callback
domain = "example1.com,example2.com"
```
#### 設置Google身份驗證
必須先在Google API控制臺中設置應用程序,然后才能使用Google身份驗證后端。 要設置應用程序:
1. 導航到[https://console.developers.google.com/apis/](https://console.developers.google.com/apis/)
2. 從左側導航欄中選擇“憑據”
3. 點擊“創建憑據”,然后選擇“OAuth客戶端ID”
4. 選擇“Web應用程序”
5. 填寫所需信息('授權重定向URI'必須完全合格,例如[http://airflow.example.com/oauth2callback](http://airflow.example.com/oauth2callback) )
6. 點擊“創建”
7. 根據上面的示例,將“客戶端ID”,“客戶端密鑰”和重定向URI復制到airflow.cfg
## SSL
可以通過提供證書和密鑰來啟用SSL。 啟用后,請務必在瀏覽器中使用“ [https://](https:) ”。
```
[ webserver ]
web_server_ssl_cert = <path to cert>
web_server_ssl_key = <path to key>
```
啟用S??SL不會自動更改Web服務器端口。 如果要使用標準端口443,則還需要配置它。 請注意,偵聽端口443需要超級用戶權限(或Linux上的cap_net_bind_service)。
```
# Optionally, set the server to listen on the standard SSL port.
web_server_port = 443
base_url = http://<hostname or IP>:443
```
使用SSL啟用CeleryExecutor。 確保正確生成客戶端和服務器證書和密鑰。
```
[ celery ]
CELERY_SSL_ACTIVE = True
CELERY_SSL_KEY = <path to key>
CELERY_SSL_CERT = <path to cert>
CELERY_SSL_CACERT = <path to cacert>
```
## 模擬
Airflow能夠在運行任務實例時模擬unix用戶,該任務實例基于任務的`run_as_user`參數,該參數采用用戶的名稱。
**注意:**要模擬工作,必須使用<cite>sudo</cite>運行Airflow,因為使用<cite>sudo -u</cite>運行子任務并更改文件的權限。 此外,unix用戶需要存在于worker上。 這是一個簡單的sudoers文件條目可能看起來像這樣,假設氣流作為<cite>氣流</cite>用戶運行。 請注意,這意味著必須以與root用戶相同的方式信任和處理氣流用戶。
```
airflow ALL=(ALL) NOPASSWD: ALL
```
帶模擬的子任務仍將記錄到同一文件夾,但他們登錄的文件將更改權限,只有unix用戶才能寫入。
### 默認模擬
要防止不使用模擬的任務以<cite>sudo</cite>權限運行,可以設置`core:default_impersonation` config,如果未設置<cite>run_as_user,</cite>則設置默認用戶模擬。
```
[ core ]
default_impersonation = airflow
```