简体   繁体   English

Azure SQL 使用 PowerShell 和 ServicePrincipal 为 AD 用户授予访问权限

[英]Azure SQL Grant Access for AD User using PowerShell and ServicePrincipal

I try to create a User for an Azure SQL Server.我尝试为 Azure SQL 服务器创建一个用户。 I want to use the AzureAD users.我想使用 AzureAD 用户。

This is the code:这是代码:

$accessToken = $(az account get-access-token --resource https://database.windows.net/ --query accessToken -o tsv --subscription ${data.azurerm_client_config.current.subscription_id})
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Data Source = ${azurerm_sql_server.server.fully_qualified_domain_name}; Initial Catalog = ${azurerm_sql_database.sqldb.name}"
$sqlConnection.AccessToken = $accessToken
$sqlConnection.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.Connection = $sqlConnection
$sqlCmd.CommandText = "CREATE USER [someone.name_domain.com#EXT#@rootdomain.onmicrosoft.com] FROM EXTERNAL PROVIDER"
$sqlCmd.ExecuteNonQuery()

The azure commandline which is used to generate the token is authenticated using a service principal.用于生成令牌的 azure 命令行使用服务主体进行身份验证。 The service principal is defined as the azurerm_sql_active_directory_administrator using an AAD-Group.服务主体定义为使用 AAD 组的azurerm_sql_active_directory_administrator I get the following Error我收到以下错误

MethodInvocationException: 
Line |
  11 |  $sqlCmd.ExecuteNonQuery()
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "ExecuteNonQuery" with "0" argument(s): "Principal 'someone.name_domain.com#EXT#@rootdomain.onmicrosoft.com' could not be resolved. Error message: ''"

If I change the SqlCommand to: SELECT * FROM sys.database_principals .如果我将 SqlCommand 更改为: SELECT * FROM sys.database_principals I can query the results.我可以查询结果。 Therefore I assume the Authentication itself is not the Problem.因此,我认为身份验证本身不是问题。 On the other hand, when logged into the azure cli with my active directory user (which is part of the same AAD-Group) instead of the service principal, the CREATE USER... Command works and the user is created.另一方面,当使用我的活动目录用户(属于同一 AAD 组的一部分)而不是服务主体登录 azure cli 时, CREATE USER...命令有效并创建了用户。

I want to run the code in automation therefore I need it to work with my service principal.我想以自动化方式运行代码,因此我需要它与我的服务主体一起使用。 Is there anything that can be done to make this work?有什么可以做的吗?

Azure SQL database doesn't allow service principal to create user for now. Azure SQL 数据库暂时不允许服务主体创建用户。

But others have found some workaround to solve the issue:但其他人找到了一些解决方法来解决这个问题: 在此处输入图像描述

Microsoft MSFT confirm that it's correct and the official tutorial will come soon:微软MSFT确认无误,官方教程即将发布:

  1. Assign a server identity to the Azure SQL logical server将服务器身份分配给 Azure SQL 逻辑服务器

    Set-AzSqlServer -ResourceGroupName -ServerName <Server name> -AssignIdentity Set-AzSqlServer -ResourceGroupName -ServerName <服务器名称> -AssignIdentity

  2. Grant the Directory Readers permissions to the server identity.授予Directory Readers对服务器身份的权限。

Please reference this blogs:请参考此博客:

  1. CREATE USER FROM EXTERNAL PROVIDER by Service Principal由服务主体从外部提供者创建用户
  2. https://github.com/MicrosoftDocs/sql-docs/issues/4959 https://github.com/MicrosoftDocs/sql-docs/issues/4959

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM