Microsoft Entra 授权代理

Microsoft Entra 授权代理是反向代理,可用于使用 Microsoft Entra ID 对请求进行身份验证。 此代理可用于对任何支持 Microsoft Entra 身份验证的服务的请求进行身份验证。 使用此代理对 Prometheus 的 Azure Monitor 托管服务的请求进行身份验证。

先决条件

  • Azure Monitor 工作区。 如果没有工作区,请使用本文创建工作区。
  • 已在群集上安装了 Prometheus。

注意

本文中的远程写入示例使用 Prometheus 远程写入将数据写入 Azure Monitor。 将 AKS 群集载入 Prometheus 会自动在群集上安装 Prometheus,并将数据发送到工作区。

部署

可以通过发布映像或 helm 图表使用自定义模板部署代理。 这两个部署都包含相同的可自定义参数。 这些参数将在参数表中进行说明。

有关详细信息,请参阅 Microsoft Entra 身份验证代理项目。

以下示例演示如何部署代理以用于远程写入和从 Azure Monitor 查询数据。

注意

此示例演示如何使用代理对 Prometheus 的 Azure Monitor 托管服务的远程写入请求进行身份验证。 Prometheus 远程写入具有用于远程写入的专用 side car,这是建议用于实现远程写入的方法。

在部署代理之前,请找到托管标识,并为其分配 Azure Monitor 工作区数据收集规则的 Monitoring Metrics Publisher 角色。

  1. 查找 AKS 群集的托管标识的 clientId。 托管标识用于向 Azure Monitor 工作区进行身份验证。 创建 AKS 群集时会创建托管标识。

    # Get the identity client_id
    az aks show -g <AKS-CLUSTER-RESOURCE-GROUP> -n <AKS-CLUSTER-NAME> --query "identityProfile"
    

    输出格式如下:

    {
      "kubeletidentity": {
        "clientId": "abcd1234-1243-abcd-9876-1234abcd5678",
        "objectId": "12345678-abcd-abcd-abcd-1234567890ab",
        "resourceId": "/subscriptions/def0123-1243-abcd-9876-1234abcd5678/resourcegroups/MC_rg-proxytest-01_proxytest-01_chinanorth/providers/Microsoft.ManagedIdentity/userAssignedIdentities/proxytest-01-agentpool"
      }
    
  2. 查找 Azure Monitor 工作区的数据收集规则 (DCR) ID。
    规则名称与工作区名称相同。 数据收集规则的资源组名称采用以下格式:MA_<workspace-name>_<REGION>_managed,例如 MA_amw-proxytest_chinanorth_managed。 使用以下命令查找数据收集规则 ID:

    az monitor data-collection rule show --name <dcr-name> --resource-group <resource-group-name> --query "id"
    
  3. 或者,可以使用 Azure Monitor 工作区“概述”页上的 Azure 门户来查找 DCR ID 和指标引入终结点。

    在工作区“概述”选项卡上选择“数据收集规则”,然后选择“JSON 视图”来查看“资源 ID”。

    屏幕截图显示 Azure Monitor 工作区的概述页面。

  4. Monitoring Metrics Publisher 角色分配给托管标识的 clientId,以便能够写入 Azure Monitor 工作区数据收集规则。

    az role assignment create /
    --assignee <clientid>  /
    --role "Monitoring Metrics Publisher" /
    --scope <workspace-dcr-id>
    

    例如:

    az role assignment create \
    --assignee abcd1234-1243-abcd-9876-1234abcd5678  \
    --role "Monitoring Metrics Publisher" \
    --scope /subscriptions/ef0123-1243-abcd-9876-1234abcd5678/resourceGroups/MA_amw-proxytest_chinanorth_managed/providers/Microsoft.Insights/dataCollectionRules/amw-proxytest
    
  5. 使用以下 YAML 文件部署代理进行远程写入。 修改以下参数:

    • TARGET_HOST - 要将请求转发到的目标主机。 若要将数据发送到 Azure Monitor 工作区,请使用工作区概述页中的 Metrics ingestion endpoint 的主机名部分。 例如: http://amw-proxytest-abcd.chinanorth-1.metrics.ingest.monitor.azure.cn
    • AAD_CLIENT_ID - 使用的托管标识的 clientId,该托管标识分配有 Monitoring Metrics Publisher 角色。
    • AUDIENCE - 为将指标引入到 Azure Monitor 工作区,将 AUDIENCE 设置为 https://monitor.azure.cn/.default
    • 如果不使用 OpenTelemetry,请删除 OTEL_GRPC_ENDPOINTOTEL_SERVICE_NAME

    有关参数的详细信息,请参阅参数表。

    proxy-ingestion.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
        labels:
            app: azuremonitor-ingestion
        name: azuremonitor-ingestion
        namespace: observability
    spec:
        replicas: 1
        selector:
            matchLabels:
                app: azuremonitor-ingestion
        template:
            metadata:
                labels:
                    app: azuremonitor-ingestion
                name: azuremonitor-ingestion
            spec:
                containers:
                - name: aad-auth-proxy
                  image: mcr.microsoft.com/azuremonitor/auth-proxy/prod/aad-auth-proxy/images/aad-auth-proxy:0.1.0-main-05-24-2023-b911fe1c
                  imagePullPolicy: Always
                  ports:
                  - name: auth-port
                    containerPort: 8081
                  env:
                  - name: AUDIENCE
                    value: https://monitor.azure.cn/.default
                  - name: TARGET_HOST
                    value: http://<workspace-endpoint-hostname>
                  - name: LISTENING_PORT
                    value: "8081"
                  - name: IDENTITY_TYPE
                    value: userAssigned
                  - name: AAD_CLIENT_ID
                    value: <clientId>
                  - name: AAD_TOKEN_REFRESH_INTERVAL_IN_PERCENTAGE
                    value: "10"
                  - name: OTEL_GRPC_ENDPOINT
                    value: <YOUR-OTEL-GRPC-ENDPOINT> # "otel-collector.observability.svc.cluster.local:4317"
                  - name: OTEL_SERVICE_NAME
                    value: <YOUE-SERVICE-NAME>
                  livenessProbe:
                    httpGet:
                      path: /health
                      port: auth-port
                    initialDelaySeconds: 5
                    timeoutSeconds: 5
                  readinessProbe:
                    httpGet:
                      path: /ready
                      port: auth-port
                    initialDelaySeconds: 5
                    timeoutSeconds: 5
    ---
    apiVersion: v1
    kind: Service
    metadata:
        name: azuremonitor-ingestion
        namespace: observability
    spec:
        ports:
            - port: 80
              targetPort: 8081
        selector:
            app: azuremonitor-ingestion
    
  6. 使用以下命令部署代理:

    # create the namespace if it doesn't already exist
    kubectl create namespace observability 
    
    kubectl apply -f proxy-ingestion.yaml -n observability
    
  7. 或者,可以使用 helm 部署代理,如下所示:

    helm install aad-auth-proxy oci://mcr.microsoft.com/azuremonitor/auth-proxy/prod/aad-auth-proxy/helmchart/aad-auth-proxy \
    --version 0.1.0-main-05-24-2023-b911fe1c \
    -n observability \
    --set targetHost=https://proxy-test-abc123.chinanorth-1.metrics.ingest.monitor.azure.cn \
    --set identityType=userAssigned \
    --set aadClientId= abcd1234-1243-abcd-9876-1234abcd5678 \
    --set audience=https://monitor.azure.cn/.default
    
  8. 配置远程写入 URL。
    URL 主机名由引入服务名和命名空间组成,其格式如下:<ingestion service name>.<namespace>.svc.cluster.local。 在该示例中,主机为 azuremonitor-ingestion.observability.svc.cluster.local
    使用 Azure Monitor 工作区概述页中的 Metrics ingestion endpoint 路径配置 URL 路径。 例如 dataCollectionRules/dcr-abc123d987e654f3210abc1def234567/streams/Microsoft-PrometheusMetrics/api/v1/write?api-version=2021-11-01-preview

    prometheus:
      prometheusSpec:
        externalLabels:
          cluster: <cluster name to be used in the workspace>
        ## https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
        ##
        remoteWrite:
        - url: "http://azuremonitor-ingestion.observability.svc.cluster.local/dataCollectionRules/dcr-abc123d987e654f3210abc1def234567/streams/Microsoft-PrometheusMetrics/api/v1/write?api-version=2021-11-01-preview" 
    
  9. 应用远程写入配置。

注意

有关最新的代理映像版本,请参阅发行说明

检查代理是否正在引入数据

通过检查 Pod 的日志或查询 Azure Monitor 工作区,检查代理是否成功引入指标。

通过运行以下命令检查 Pod 的日志:

# Get the azuremonitor-ingestion pod ID
 kubectl get pods -A | grep azuremonitor-ingestion
 #Using the returned pod ID, get the logs
 kubectl logs --namespace observability <pod ID> --tail=10

成功引入指标会生成带有 StatusCode=200 的日志,类似于以下示例:

time="2023-05-16T08:47:27Z" level=info msg="Successfully sent request, returning response back." ContentLength=0 Request="https://amw-proxytest-05-t16w.chinanorth-1.metrics.ingest.monitor.azure.cn/dataCollectionRules/dcr-688b6ed1f2244e098a88e32dde18b4f6/streams/Microsoft-PrometheusMetrics/api/v1/write?api-version=2021-11-01-preview" StatusCode=200

若要查询 Azure Monitor 工作区,请执行以下步骤:

  1. 从 Azure Monitor 工作区中选择“工作簿”。

  2. 选择“Prometheus 资源管理器”磁贴。 屏幕截图显示 Azure Monitor 工作区的工作簿库。

  3. 在资源管理器页上,在查询框中输入“up”。

  4. 选择“网格”选项卡以查看结果。

  5. 检查“群集”列以查看显示的内容是否来自于你的群集。 屏幕截图显示 Prometheus 资源浏览器查询页。

参数

映像参数 Helm 图表参数名称 说明 支持的值 必需
TARGET_HOST targetHost 要将请求转发到的目标主机。
若要将数据发送到 Azure Monitor 工作区,请使用工作区概述页中的 Metrics ingestion endpoint
若要从 Azure Monitor 工作区读取数据,请使用工作区概述页中的 Query endpoint
IDENTITY_TYPE identityType 用于对请求进行身份验证的标识类型。 此代理支持三种类型的标识。 systemassigneduserassignedaadapplication
AAD_CLIENT_ID aadClientId 使用的标识的客户端 ID。 这用于 userassignedaadapplication 标识类型。 使用 az aks show -g <AKS-CLUSTER-RESOURCE-GROUP> -n <AKS-CLUSTER-NAME> --query "identityProfile" 检索客户端 ID 适用于 userassignedaadapplication
AAD_TENANT_ID aadTenantId 使用的标识的租户 ID。 租户 ID 用于 aadapplication 标识类型。 适用于 aadapplication
AAD_CLIENT_CERTIFICATE_PATH aadClientCertificatePath 代理可在其中查找 aadapplication 证书的路径。 此路径应可供代理访问,并且应是包含私钥的 pfx 或 pem 证书。 仅适用于 aadapplication 标识类型
AAD_TOKEN_REFRESH_INTERVAL_IN_PERCENTAGE aadTokenRefreshIntervalInMinutes 根据令牌过期前的时间百分比刷新令牌。 默认值为过期前的 10% 的时间。
AUDIENCE audience 令牌的受众
LISTENING_PORT listeningPort 侦听此端口的代理
OTEL_SERVICE_NAME otelServiceName OTEL 跟踪和指标的服务名称。 默认值:aad_auth_proxy
OTEL_GRPC_ENDPOINT otelGrpcEndpoint 代理将 OTEL 遥测推送到此终结点。 默认值:30http://localhost:4317

疑难解答

  • 代理容器未启动。
    运行以下命令以显示代理容器的任何错误。

    kubectl --namespace <Namespace> describe pod <Prometheus-Pod-Name>`
    
  • 代理无法启动 - 配置错误

    代理检查有效标识以在启动期间提取令牌。 如果未能检索令牌,启动将失败。 将记录错误,并可通过运行以下命令查看错误:

    kubectl --namespace <Namespace> logs <Proxy-Pod-Name>
    

    示例输出:

    time="2023-05-15T11:24:06Z" level=info msg="Configuration settings loaded:" AAD_CLIENT_CERTIFICATE_PATH= AAD_CLIENT_ID=abc123de-be75-4141-a1e6-abc123987def AAD_TENANT_ID= AAD_TOKEN_REFRESH_INTERVAL_IN_PERCENTAGE=10 AUDIENCE="https://prometheus.monitor.azure.cn" IDENTITY_TYPE=userassigned LISTENING_PORT=8082 OTEL_GRPC_ENDPOINT= OTEL_SERVICE_NAME=aad_auth_proxy TARGET_HOST=proxytest-01-workspace-orkw.chinanorth.prometheus.monitor.azure.cn
    2023-05-15T11:24:06.414Z [ERROR] TokenCredential creation failed:Failed to get access token: ManagedIdentityCredential authentication failed
    GET http://169.254.169.254/metadata/identity/oauth2/token
    --------------------------------------------------------------------------------
    RESPONSE 400 Bad Request
    --------------------------------------------------------------------------------
    {
      "error": "invalid_request",
      "error_description": "Identity not found"
    }
    --------------------------------------------------------------------------------