# 安全
> 貢獻者:[@ImPerat0R\_](https://github.com/tssujt) [@zhongjiajie](https://github.com/zhongjiajie)
默認情況下,所有門都是打開的。限制對 Web 應用程序的訪問的一種簡單方法是在網絡級別執行此操作,比如使用 SSH 隧道。
但也可以通過使用其中一個已提供的認證后端或創建自己的認證后端來打開身份驗證。
請務必查看[Experimental Rest API](zh/api.md)以保護 API。
## Web 身份驗證
### 密碼
最簡單的身份驗證機制之一是要求用戶在登錄前輸入密碼。密碼身份驗證需要在 requirements 文件中使用`password`子擴展包。它在存儲密碼之前使用了 bcrypt 擴展包來對密碼進行哈希。
```
[webserver]
authenticate = True
auth_backend = airflow.contrib.auth.backends.password_auth
```
啟用密碼身份驗證后,需要先創建初始用戶憑據,然后才能登錄其他人。未在此身份驗證后端的遷移中創建初始用戶,以防止默認 Airflow 安裝受到攻擊。必須通過安裝 Airflow 的同一臺機器上的 Python REPL 來創建新用戶。
```sh
# 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 文檔中](https://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。這意味著 Airflow 可以為自己更新 kerberos 票證并將其存儲在票證緩存中。鉤子和 dags 可以使用票證來驗證 kerberized 服務。
### 限制
請注意,此時并未調整所有鉤子以使用此功能。此外,它沒有將 kerberos 集成到 Web 界面中,您現在必須依賴網絡級安全性來確保您的服務保持安全。
Celery 集成尚未經過試用和測試。 但是,如果您為每個主機生成一個 key tab,并在每個 Worker 旁邊啟動一個 ticket renewer,那么它很可能會起作用。
### 啟用 kerberos
#### Airflow
要啟用 kerberos,您需要生成(服務)key tab。
```py
# 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
```
現在將此文件存儲在 airflow 用戶可以讀取的位置(chmod 600)。然后將以下內容添加到`airflow.cfg`
```
[core]
security = kerberos
[kerberos]
keytab = /etc/airflow/airflow.keytab
reinit_frequency = 3600
principal = airflow
```
啟動 ticket renewer
```sh
# 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 身份驗證
已更新 Hive 鉤子以利用 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
```
為了使用 kerberos 認證,請務必在安裝 Airflow 時添加 kerberos 子擴展包:
```
pip install airflow[kerberos]
```
## 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. 在 airflow.cfg 設置`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)。
```py
# 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`參數,該參數采用用戶的名稱。
**注意:**
要模擬工作,必須使用`sudo`運行 Airflow,因為使用`sudo -u`運行子任務并更改文件的權限。此外,unix 用戶需要存在于 worker 中。假設 airflow 是`airflow`用戶運行,一個簡單的 sudoers 文件可能看起來像這樣。請注意,這意味著必須以與 root 用戶相同的方式信任和處理 airflow 用戶。
```
airflow ALL=(ALL) NOPASSWD: ALL
```
帶模擬的子任務仍將記錄到同一文件夾,但他們登錄的文件將更改權限,只有 unix 用戶才能寫入。
### 默認模擬
要防止不使用模擬的任務以`sudo`權限運行,如果未設置`run_as_user`,可以在`core:default_impersonation`配置中來設置默認的模擬用戶。
```
[core]
default_impersonation = airflow
```
## Flower 認證
Celery Flower 的基礎認證是支持的。
可以在 Flower 進程啟動命令中指定可選參數,或者在`airflow.cfg`指定配置項。對于這兩種情況,請提供用逗號分隔的 user:password 對。
```sh
airflow flower --basic_auth=user1:password1,user2:password2
```
```
[celery]
flower_basic_auth = user1:password1,user2:password2
```