Kerberos¶
Airflow 對 Kerberos 有初步支援。這意味著 Airflow 可以為其自身續訂 Kerberos 票據並將其儲存在票據快取中。hook 和 dag 可以利用票據對啟用 Kerberos 的服務進行認證。
限制¶
請注意,目前並非所有 hook 都已調整以利用此功能。此外,它尚未將 Kerberos 整合到 Web 介面中,您目前必須依靠網路級別的安全措施來確保您的服務保持安全。
Celery 整合尚未嘗試和測試。但是,如果您為每個主機生成一個 keytab 並在每個 worker 旁邊啟動一個票據續訂器,則很可能會奏效。
啟用 Kerberos¶
Airflow¶
要啟用 Kerberos,您需要生成一個(服務)keytab。
# 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
如果您在基於 Docker 容器的環境中使用 Airflow,您可以在 Dockerfile 中設定以下環境變數,而無需修改 airflow.cfg。
ENV AIRFLOW__CORE__SECURITY kerberos
ENV AIRFLOW__KERBEROS__KEYTAB /etc/airflow/airflow.keytab
ENV AIRFLOW__KERBEROS__INCLUDE_IP False
如果您需要更精細的 Kerberos 票據選項,可以使用以下選項及其預設值
[kerberos]
# Location of your ccache file once kinit has been performed
ccache = /tmp/airflow_krb5_ccache
# principal gets augmented with fqdn
principal = airflow
reinit_frequency = 3600
kinit_path = kinit
keytab = airflow.keytab
# Allow kerberos token to be flag forwardable or not
forwardable = True
# Allow to include or remove local IP from kerberos token.
# This is particularly useful if you use Airflow inside a VM NATted behind host system IP.
include_ip = True
請記住,Kerberos 票據是透過 kinit 生成的,並且預設會使用您的本地 krb5.conf 檔案。
透過以下方式啟動票據續訂器
# run ticket renewer
airflow kerberos
為了支援更高階的 Kerberos 標準或一次性使用部署模型,您可以在執行 airflow kerberos 命令時使用 --one-time 標誌來指定模式。
a) 標準 (standard): airflow kerberos 命令將持續執行。票據續訂程序每隔幾秒鐘持續執行,並在票據過期時重新整理它。 b) 一次性 (one-time): airflow kerberos 將執行一次然後退出。如果失敗,主任務將不會啟動。
預設模式是標準。
示例用法
對於標準模式
airflow kerberos
對於一次性模式
airflow kerberos --one-time
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 hook 已更新以利用 Kerberos 認證。為了讓您的 dag 使用它,只需更新連線詳細資訊,例如
{ "use_beeline": true, "principal": "hive/_HOST@EXAMPLE.COM"}
根據您的設定調整 principal。 _HOST 部分將被伺服器的完全限定域名替換。
您可以指定是希望使用 DAG 所有者作為連線使用者,還是使用連線的登入部分中指定的使用者。對於登入使用者,請在 extra 中指定以下內容
{ "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 認證,您必須安裝帶有 kerberos 額外軟體包組的 Airflow
pip install 'apache-airflow[kerberos]'
您可以在 Kerberos 認證 worker 中閱讀有關 Kerberos 部署的一些生產方面的內容。