通过 az cli 将 RootCA 上传到 apim

Uploading RootCA to apim via az cli

az cli 中是否有可以通过 Az Powershell 模块执行相同操作的函数,如此处所述:https://docs.microsoft.com/en-us/powershell/module/az.apimanagement/new-azapimanagementsystemcertificate?view=azps-5.8.0#related-links

我查看了 API 文档,您似乎只能更新证书与系统证书:https://docs.microsoft.com/en-us/rest/api/apimanagement/2020-06-01-preview/certificate/createorupdate#apimanagementcreatecertificate

本质上,我想自动执行将 CA 证书上传到 API 管理服务的过程 - 希望避免安装 Powershell 来执行此任务。

在 Azure CLI 中没有 New-AzApiManagementSystemCertificate 的内置等价物,实际上它是一个包装器,用 base64 对证书进行编码并与 certificatePassword(可以省略)和 storeName,最后,它会得到一个CertificateConfiguration,然后用New-AzApiManagementSet-AzApiManagement到create/update APIM,他们本质上是调用Api Management Service - Create Or Update休息API.

因此,要在 Azure CLI 中执行此操作,您只需使用 az rest 直接调用 REST API。

首先,使用bash对证书进行base64编码,在我的测试样本中,它是一个.cer证书。

$ cat ./testapim.cer | base64 -w 0

然后将它传递给下面的示例命令,用你的修改值。

az rest --method put --uri https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<group-name>/providers/Microsoft.ApiManagement/service/joyapim?api-version=2019-12-01 --headers '{"Content-Type":"application/json"}' --body '{
  "properties": {
    "certificates": [
      {
        "encodedCertificate": "*******Base64 encoded Certificate******************",
        "certificatePassword": "Password",
        "storeName": "Root"
      }
    ],
    "publisherEmail": "xxxxx",
    "publisherName": "xxxxx"
  },
  "sku": {
    "name": "Developer",
    "capacity": 1
  },
  "location": "Central US"
}'

在门户中查看结果: