[英]Access SQL Server with Integrated security from .NET Core app running on Linux container using AWS ECS
我有一個使用 AWS ECS Fargate 在 Linux 容器上運行的 .NET Core 應用程序。
如何使用集成安全性從該應用程序連接到 SQL 服務器?
第 1 步:確保 SQL 服務器支持 Kerberos 身份驗證 # 使用 SQL 服務器管理工作室 (SSMS),連接到您的數據庫並執行以下語句:
select auth_scheme
from sys.dm_exec_connections
where session_id = @@spid
如果查詢的結果是KERBEROS
,則您已全部設置並繼續執行第 2 步。否則,如果結果是NTLM
,則意味着 Kerberos 身份驗證失敗,SSMS 會自動回退到使用 NTLM 身份驗證。 由於 SQL 服務器和在 Linux 環境中運行的客戶端之間的集成安全完全依賴於 Kerberos 身份驗證,因此必須首先解決此問題。
注意:當連接到 SQL 服務器時,重要的是使用服務器主機名或 FQDN 而不是 IP 地址,否則 Kerberos 身份驗證將不起作用。
檢查 SPN配置
Microsoft 還發布了幾個有助於 SPN 驗證和配置的診斷工具:
最后但同樣重要的是,您可以使用以下setspn
命令來查詢特定的 SPN:
setspn -T CONTOSO.COM -F -Q MSSQLSvc/your_sql_server.contoso.com
上面的查詢確實支持*
通配符(將CONTOSO.COM
替換為您的域)
配置 Kerberos 允許的加密類型
在 Active Directory 中,找到運行 SQL 服務器的帳戶。 在“帳戶”選項卡和“帳戶選項”部分下,確認選擇了適用的 Kerberos 密碼。
例子:
第 2 步:配置 ECS 任務 要使用 Kerberos 身份驗證,ECS 服務中的應用程序任務將由兩個容器組成:
kinit
命令定期(重新)獲取和緩存 Kerberos 票證授予票證 (TGT) 的容器。兩個容器將安裝相同的卷。 第一個容器將緩存/寫入 TGT 票證,第二個容器將從中讀取緩存的 TGT 票證。
TGT獲取容器(sidecar容器)
設置 TGT 采集容器只需要 3 個文件:
# krb5.conf
[libdefaults]
dns_lookup_realm = true
dns_lookup_kdc = true
forwardable = true
default_ccache_name = FILE:/var/kerberos/krbcache # TGT cache location
default_realm = CONTOSO.COM
permitted_enctypes = aes256-cts aes128-cts
[realms]
CONTOSO.COM = {
kdc = CONTOSO.COM
admin_server = CONTOSO.COM
}
[domain_realm]
.contoso.com = SCIF.COM
contoso.com = SCIF.COM
[logging]
default = STDERR
# renew.sh
#!/bin/bash
# Refresh the token periodically.
# Set the length of time that the script will wait to refresh the token.
[[ "$DELAY_SECONDS" == "" ]] && DELAY_SECONDS=3600
# If the AWS region hasn't been set, get it from instance metadata. This will work in an instance as well as in an ECS container.
[[ "$AWS_REGION" == "" ]] && AWS_REGION=$(curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
# Use the ECS container as the source for AWS credentials. This allows the AWS CLI to use the permissions of the task role.
aws configure set credential_source EcsContainer
while true
do
echo "Starting ticket renewal at: " + $(date)
# Get the credentials from Secrets Manager.
CREDENTIALS_SECRET_VALUE=$(aws secretsmanager get-secret-value --secret-id $CREDENTIALS_SECRET_ARN --region $AWS_REGION --query SecretString --output text)
# Use `jq` to parse the credentials into username & password.
CREDENTIALS_USERNAME=$(echo $CREDENTIALS_SECRET_VALUE | jq -r '.username')
CREDENTIALS_PASSWORD=$(echo $CREDENTIALS_SECRET_VALUE | jq -r '.password')
# Use the username & password to authenticate to Kerberos. The resulting token is written to the token cache,
# which is set up in `krb5.conf` to use the task scratch volume, shared by all containers.
echo $CREDENTIALS_PASSWORD | kinit $CREDENTIALS_USERNAME -f -V $OPTIONS
echo "Ticket renewal complete, waiting for $DELAY_SECONDS seconds"
sleep $DELAY_SECONDS &
wait
done
# Dockerfile
FROM amazonlinux:2
COPY renew.sh /
COPY krb5.conf /etc/krb5.conf
# Install the Kerberos tools -- to authenticate;
# `jq` -- to parse the credentials from the AWS Secrets Manager, which returns JSON
# `unzip` -- to install the latest version of the AWS CLI
RUN yum install -y krb5-workstation jq unzip
# Download and install the latest version of the AWS CLI
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install
VOLUME ["/var/kerberos"]
ENTRYPOINT ["/renew.sh"]
根據CREDENTIALS_SECRET_ARN
中指定的值,renew.sh 將定期更新 TGT 並將其緩存/保存在 krb5.conf 文件中指定的位置(例如/var/kerberos/krbcache
)。
要測試容器是否成功獲取給定原則的 TGT,請與容器建立交互 session 並執行klist
命令。 成功后,您應該會看到 TGT 票據的詳細信息,包括委托人姓名、到期日期等。
應用容器
應用程序容器運行您的 .NET Core 應用程序。 要在該容器上啟用 Kerberos,請在 Dockerfile 中添加以下行:
...
RUN apt update
RUN apt install -y krb5-config krb5-user
COPY krb5.conf /etc/krb5.conf
VOLUME ["/var/kerberos"]
...
krb5.conf 文件的內容應該與 TGT 獲取容器中的內容相同,它將指示應用程序將 Kerberos TGT 定位到FILE:/var/kerberos/krbcache
。
您的應用程序 SQL 連接字符串應類似於以下內容:
Server=yourSqlServer.contoso.com;Initial Catalog=YourDB;Integrated Security=true;
要測試容器是否可以訪問緩存的 TGT,請與容器建立交互 session 並執行klist
。 成功后,您應該會看到與另一個容器中相同的 TGT 票證。
如果一切順利,您應該能夠從運行在 Linux 上的 .NET 核心應用程序使用集成安全成功連接到 SQL 服務器。
額外資源
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.