【Azure Developer】Go语言调用Azure SDK如何登录到中国区Azure环境

简介: 【Azure Developer】Go语言调用Azure SDK如何登录到中国区Azure环境

问题描述

在 “使用 Azure SDK for Go 进行 Azure 身份验证” 文章中的 Go 示例代码进行登录Azure时,默认指向的是Globa Azure。当只修改AAD AZURE_CLIENT_ID , AZURE_TENANT_ID 和 AZURE_CLIENT_SECRET参数值,运行会抛出以下错误:

The resource principal named https://management.core.windows.net/ was not found in the tenant named XXXXXXXX有限公司. This cf the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.

那么如何能够连接到China Azure呢?

 

问题解答

Go代码中,使用 azidentity.NewDefaultAzureCredential(nil) 函数登录Azure AD,并且没有传入参数。所以默认就是登录到Global Azure中。

 

查看NewDefaultAzureCredential的构造函数,可以添加 ClientSecretCredentialOptions 参数,来设置登录的Azure 环境。

// NewClientSecretCredential constructs a ClientSecretCredential. Pass nil for options to accept defaults.
func NewClientSecretCredential(tenantID string, clientID string, clientSecret string, options *ClientSecretCredentialOptions) (*ClientSecretCredential, error) {
    if options == nil {
        options = &ClientSecretCredentialOptions{}
    }
    cred, err := confidential.NewCredFromSecret(clientSecret)
    if err != nil {
        return nil, err
    }
    c, err := getConfidentialClient(clientID, tenantID, cred, &options.ClientOptions)
    if err != nil {
        return nil, err
    }
    return &ClientSecretCredential{client: c}, nil
}
func getConfidentialClient(clientID, tenantID string, cred confidential.Credential, co *azcore.ClientOptions, additionalOpts ...confidential.Option) (confidential.Client, error) {
    if !validTenantID(tenantID) {
        return confidential.Client{}, errors.New(tenantIDValidationErr)
    }
    authorityHost, err := setAuthorityHost(co.Cloud)
    if err != nil {
        return confidential.Client{}, err
    }
    o := []confidential.Option{
        confidential.WithAuthority(runtime.JoinPaths(authorityHost, tenantID)),
        confidential.WithAzureRegion(os.Getenv(azureRegionalAuthorityName)),
        confidential.WithHTTPClient(newPipelineAdapter(co)),
    }
    o = append(o, additionalOpts...)
    return confidential.New(clientID, cred, o...)
}

所以修改代码就是添加环境参数!

opts := azcore.ClientOptions{Cloud: cloud.AzureChina}
    cred, err := azidentity.NewDefaultAzureCredential(
        &azidentity.DefaultAzureCredentialOptions{ClientOptions: opts},
    )

修改前后的代码对比图:

 

附录:修改后的全部代码

package main
// Import key modules.
import (
    "log"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore"
    "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)
// Define key global variables.
var (
    subscriptionId = "<subscription ID>"
)
// Define the function to create a resource group.
func main() {
    opts := azcore.ClientOptions{Cloud: cloud.AzureChina}
    cred, err := azidentity.NewDefaultAzureCredential(
        &azidentity.DefaultAzureCredentialOptions{ClientOptions: opts},
    )
    if err != nil {
        log.Fatalf("Authentication failure: %+v", err)
    }
    // Azure SDK Azure Resource Management clients accept the credential as a parameter
    client := armresources.NewClient(subscriptionId, cred, nil)
    log.Printf("Authenticated to subscription", client)
}

 

参考资料

Go SDK 设置Cloud : https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud#section-sourcefiles

使用 DefaultAzureCredential 对 ResourceClient 进行身份验证 : https://learn.microsoft.com/zh-cn/azure/developer/go/azure-sdk-authentication?tabs=bash

Successfully Authenticate AzureChina with an Azure Public Credential #18508 : https://github.com/Azure/azure-sdk-for-go/issues/18508

相关文章
|
5月前
|
Java Shell Maven
【Azure Container App】构建Java应用镜像时候遇无法编译错误:ERROR [build 10/10] RUN ./mvnw.cmd dependency:go-offline -B -Dproduction package
在部署Java应用到Azure Container App时,构建镜像过程中出现错误:“./mvnw.cmd: No such file or directory”。尽管项目根目录包含mvnw和mvnw.cmd文件,但依然报错。问题出现在Dockerfile构建阶段执行`./mvnw dependency:go-offline`命令时,系统提示找不到可执行文件。经过排查,确认是mvnw文件内容异常所致。最终通过重新生成mvnw文件解决该问题,镜像成功构建。
191 1
|
10月前
|
Shell Go 开发工具
【环境】Rocky8使用gvm配置Go多版本管理的微服务开发环境(go-zero)
通过本文的介绍,我们详细讲解了如何在Rocky8上使用gvm来管理多个Go版本,并配置go-zero框架的开发环境。通过gvm的灵活管理,开发者可以轻松切换不同的Go版本,以适应不同项目的需求。同时,go-zero框架的使用进一步提升了微服务开发的效率和质量。希望本文能帮助开发者构建高效的Go语言开发环境,提高项目开发的灵活性和稳定性。
319 63
|
8月前
|
SQL 监控 Go
新一代 Cron-Job分布式调度平台,v1.0.8版本发布,支持Go执行器SDK!
现代化的Cron-Job分布式任务调度平台,支持Go语言执行器SDK,多项核心优势优于其他调度平台。
176 8
|
JavaScript 前端开发 API
【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)
【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)
117 1
|
10月前
|
API 开发工具 Python
【Azure Developer】编写Python SDK代码实现从China Azure中VM Disk中创建磁盘快照Snapshot
本文介绍如何使用Python SDK为中国区微软云(China Azure)中的虚拟机磁盘创建快照。通过Azure Python SDK的Snapshot Class,指定`location`和`creation_data`参数,使用`Copy`选项从现有磁盘创建快照。代码示例展示了如何配置Default Azure Credential,并设置特定于中国区Azure的`base_url`和`credential_scopes`。参考资料包括官方文档和相关API说明。
196 1
|
JavaScript 前端开发 开发工具
【Azure Developer】使用JavaScript通过SDK进行monitor-query的client认证报错问题
AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Check with your subscription administrator, this may happen if there are no active subscriptions for the tenant.
126 1
|
Kubernetes API 开发工具
【Azure Developer】通过SDK(for python)获取Azure服务生命周期信息
需要通过Python SDK获取Azure服务的一些通知信息,如:K8S版本需要更新到指定的版本,Azure服务的维护通知,服务处于不健康状态时的通知,及相关的操作建议等内容。
188 18
|
Java 开发工具
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
164 1
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
124 0
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
Unix Linux Go
Linux 使用Yum安装Go和配置环境
Linux 使用Yum安装Go和配置环境