【发布时间】:2022-01-07 11:43:10
【问题描述】:
我需要在 Terraform 中执行 Invoke-SQLCmd - 一切正常,但我需要获取整个构建过程中使用的服务主体 (Azure) 的 Secret。所以我可以用这个:
Import-Module SQLServer
# Note: the sample assumes that you or your DBA configured the server to accept connections using
# that Service Principal and has granted it access to the database (in this example at least
# the SELECT permission).
$clientid = "enter application id that corresponds to the Service Principal" # Do not confuse with its display name
$tenantid = "enter the tenant ID of the Service Principal"
$secret = "enter the secret associated with the Service Principal"
$request = Invoke-RestMethod -Method POST `
-Uri "https://login.microsoftonline.com/$tenantid/oauth2/token"`
-Body @{ resource="https://database.windows.net/"; grant_type="client_credentials"; client_id=$clientid; client_secret=$secret }`
-ContentType "application/x-www-form-urlencoded"
$access_token = $request.access_token
# Now that we have the token, we use it to connect to the database 'mydb' on server 'myserver'
Invoke-Sqlcmd -ServerInstance myserver.database.windows.net -Database mydb -AccessToken $access_token`
-query 'select * from Table1'
我可以很容易地在 PowerShell 中获得 cliendId 和 TenantID,但我无法获得秘密。那我怎么得到它?虽然我在构建过程中使用相同的服务原则。
【问题讨论】:
-
hello @jason_hough ,无法为您的客户端 id 检索已经创建的客户端密码,唯一的方法是生成一个新的并在创建后从 powershell 使用它或使用默认的对其进行硬编码。
标签: azure powershell azure-devops terraform