mol*_*lvg 5 azure-sql-database azure-devops azure-pipelines
我正在尝试设置用于数据库创建的自动化管道,并且需要为某些 AD 组的所有用户开放访问权限。最后一部分是通过CREATE USER [Group Name] FROM EXTERNAL PROVIDER完成的;
为了执行此命令,需要使用 AAD 登录,并且用于执行 SQL 脚本的唯一 Azure DevOps 任务 ( SqlAzureDacpacDeployment@1 ) 具有使用 AD 签名的有限选项。目前支持AD用户名/密码登录和AD集成登录。由于我们使用两因素身份验证,因此无法使用用户/密码选项。后者需要我们没有的自托管代理管道。
此外,还有一个看起来很有前途的登录选项(服务主体:使用来自 Azure 订阅的身份验证数据),但在尝试后却惨败并出现错误:
##[错误]无法创建主体“web-API”。只有与 Active Directory 帐户建立的连接才能创建其他 Active Directory 用户。
我们还可以使用其他选项在 Azure SQL 数据库中创建 AD 用户吗?任何帮助,将不胜感激。
我终于能够解决这个挑战,解决方案就在这个 Mircrosoft 文档中。最大的障碍是 Azure SQL Server 所需的权限。根据文章,服务器必须具有目录读取器权限,以便在将 AD 用户和组添加到 sql 数据库时获取它们。这或多或少是一个手动步骤,除非您的管道具有管理员权限(不建议使用 ofc)。因此,为了尽可能地实现自动化,我们需要创建一个具有目录读取器权限的 AD 组,然后将我们的 SQL Server(我们有树、开发/测试和产品)添加到其中,这样服务器将继承此权限。
以下是我们如何解决此问题的分步教程:
resource sqlServer 'Microsoft.Sql/servers@2022-05-01-preview' = {
name: sqlServerName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
minimalTlsVersion: '1.2'
publicNetworkAccess: 'Enabled'
administratorLogin: '...'
administratorLoginPassword: '...'
}
}
output SqlServerIdentityId string = reference(sqlServer.id, '2022-11-01-preview', 'Full').identity.principalId
Run Code Online (Sandbox Code Playgroud)
- task: AzureCLI@2
displayName: "Give SqlServer DirectoryReader permission"
inputs:
azureSubscription: "${{variables.YourServiceConnection}}"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
sqlServerIdentityId=$(echo $IAC_OUTPUTS | jq -r '.sqlServerIdentityId.value')
expectedErrorMessage="ERROR: One or more added object references already exist for the following modified properties: 'members'."
actualErrorMessage=$(az ad group member add --group "$DirectoryReaderRoleGroupId" --member-id "$sqlServerIdentityId" 2>&1)
if [ $? -eq 0 ] || [ "$expectedErrorMessage" != "$actualErrorMessage" ]; then
echo $actualErrorMessage
echo "##[warning]Could not give SqlServer DirectoryReader permission, would not be able to ad Azure AD users to SqlServer"
exit 1;
fi
env:
DirectoryReaderRoleGroupId: a7xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxb0 # ObjectId of your Group
IAC_OUTPUTS: $(iacOutputs)
Run Code Online (Sandbox Code Playgroud)
iacOutputs是使用以下代码从 AzureResourceManagerTemplateDeployment 任务捕获的输出:
- task: AzureResourceManagerTemplateDeployment@3
inputs:
...
deploymentOutputs: 'iacOutputs'
Run Code Online (Sandbox Code Playgroud)
- task: SqlAzureDacpacDeployment@1
inputs:
azureSubscription: "${{variables.YourServiceConnection}}"
AuthenticationType: 'servicePrincipal'
ServerName: '...'
DatabaseName: '...'
IpDetectionMethod: 'AutoDetect'
deployType: 'InlineSqlTask'
SqlInline: |
IF DATABASE_PRINCIPAL_ID('Azure AD Group name') IS NULL
CREATE USER [Azure AD Group name] FROM EXTERNAL PROVIDER;
Run Code Online (Sandbox Code Playgroud)
甚至使用 AD 身份验证运行 EF Core 迁移,代码中没有 SQL 用户名或密码
- task: AzureCLI@2
displayName: EF database update
inputs:
azureSubscription: "${{variables.YourServiceConnection}}"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
dotnet new tool-manifest
dotnet tool install dotnet-ef
dotnet ef database update --connection "Server=tcp:$(serverUrl),1433;Database=$(dbName);Encrypt=true;Connection Timeout=30;Authentication=\"Active Directory Default\"" --no-build
Run Code Online (Sandbox Code Playgroud)
希望这有帮助:)
| 归档时间: |
|
| 查看次数: |
1158 次 |
| 最近记录: |