gpt4 book ai didi

【AzureCloudService】使用KeyVaultSecret添加.CER证书到CloudServiceExtendedSupport中

转载 作者:撒哈拉 更新时间:2024-10-14 22:18:59 58 4
gpt4 key购买 nike

问题描述

因为Key Vault的证书上传功能中,只支持pfx格式的证书,而中间证书,根证书不能转换为pfx格式,只能是公钥证书格式 cet 或者 crt,能通过文本工具直接查看base64编码内容.

如一个证书链文件中可以看见中间证书,根证书:

  。

当把包含完成证书链的证书PFX上传到Key Vault certificates中后,certificates只会显示服务器证书的指纹,导致无法直接在Cloud Service(Extended Support)的配置文件中修改.

所以,如果中间证书,根证书需要安装到Cloud Service (Extended Support) 中,要先把中间证书,根证书放置在Key Vault Secrets中,然后调用Cloud Service API更新证书和配置Secrets Identifier URL来完成证书配置.

  。

操作步骤

第一步:准备中间证书和根证书的cer 文件

(* 如果已经有中间证书的cer/crt 文件,用记事本查看证书Base64编码内容则可以跳过第一步) 。

查看PFX证书及证书链信息:

mmc certmgr.msc /CERTMGR:FILENAME="C:\Users\... \Downloads\mykey.pfx"

选中中间证书-> Details -> Copy to File 。

在打开的向导窗口中,点击Next,选择 "Base-64 encoded X.509 (.CER)“ --》设置保存路径 --》 导出成功 。

用记事本打开,查看证书Base64编码内容 。

(重复以上操作,把根证书也保存为CER文件) 。

  。

(非常重要)第二步:把证书内容JSON格式化后,通过az cli命令设置到Key Vault Secret中

(这一步不能通过门户完成) 。

把证书的Base64编码内容填入JSON格式的data中 。

{
"data": "Your base64 certificate",
"dataType": "PFX",
"password": ""
 }

然后把JSON内容保存为一个文件,使用az keyvault secret set   --file “” --encoding base64  添加到Key Vault中 。

注意:可以使用证书指纹作为机密名称,以方便更好的关联到证书信息 。

## 设置Key Vault机密 。

##intermediate
az keyvault secret set  --vault-name <key value name> --name <thumbprint>  --file ".\SSL\intermediate.txt" --encoding base64  

##root
az keyvault secret set  --vault-name <key value name> --name <thumbprint>  --file ".\SSL\root.txt" --encoding base64  

执行完成后,从返回结果中获取到 id 值(Secret Identifier URL). 。

完成以上内容后,复制出指纹值和Secret ID URL,就可以通过Cloud Service (Extended Support)的API更新证书.

  。

第三步:获取Cloud Service的信息,调用接口为GET API

参考文档:https://learn.microsoft.com/en-us/rest/api/compute/cloud-services/get?view=rest-compute-2024-07-01&tabs=HTTP 。

注意,在中国区需要修改Host Endpoint为:management.chinacloudapi.cn 。

GET https:// management.chinacloudapi.cn /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/cloudServices/{cloudServiceName}?api-version=2022-04-04 。

需要携带Authorization Token,否则会获得如下错误:

{
  "error": {
    "code": "AuthenticationFailed",
    "message": "Authentication failed. The 'Authorization' header is missing."
  }
}

获取Token的方式可以通过浏览器访问Cloud Service(Extended Support)门户,然后通过开发者工具(F12)查看网络请求,从访问Cloud Service的请求头中获取Authorization内容。或者通过az cli获取token 。

az cloud set --name AzureChinaCloud
az login
az account get-access-token --scope "https://management.core.chinacloudapi.cn/.default" --query accessToken

  。

当成功获取到Cloud Service的信息后,调整 JSON内容:

删除Properties中,除了configuration 和 osProfile 外的全部内容.

整理之后JSON格式如下:

{
  "name": "cloud service extended support name",
  "id": "cloud service (extended) support resource id",
  "type": "Microsoft.Compute/cloudServices",
  "location": "chinanorth3",
  "properties": {
    "configuration": "{ServiceConfiguration}",
    "osProfile": {
      "secrets": [
        {
          "sourceVault": {
            "id": "key vault resource id"
          },
          "vaultCertificates": [
            {
              "certificateUrl": "key vault Secret Identifier"
            },
            {
              "certificateUrl": "key vault Secret Identifier"
            },
            {
              "certificateUrl": "key vault Secret Identifier"
            }
          ]
        }
      ]
    }
  }
}

 需要修改的地方有两处:

1) configuration内容中Certificates指纹,用第二步中的指纹值替换文件中需要修改的内容 。

2)同时,使用第二步中的机密标识URL来替换旧的certificateUrl值 。

准备好以上的内容后,既可以进行第三步,发送PUT请求把新证书更新到Cloud Service(Extended Support) 。

  。

第四步:更新Cloud Service的信息,调用接口为PUT API

参考文档:https://learn.microsoft.com/en-us/rest/api/compute/cloud-services/create-or-update?view=rest-compute-2024-07-01&tabs=HTTP  。

PUT https:// management.chinacloudapi.cn /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/cloudServices/{cloudServiceName}?api-version=2022-04-04 。

 

使用第三步中同样的URL,把请求类型修改为PUT,然后把第三步修改的JSON放入Request Body。点击发送,查看请求的状态.

* 如果遇见证书格式不对错误,需要检查Key Vault Secret中保存的内容是否是正确的JSON格式.

格式不对的错误信息:

{
  "error": {
    "code": "CertificateImproperlyFormatted",
    "message": "The data retrieved from https://XXXXXXXXX.vault.azure.cn/secrets/XXXXX/7eXXXX is not deserializable into JSON."
  }
}

  。

  。

  。

  。

【END】 。

最后此篇关于【AzureCloudService】使用KeyVaultSecret添加.CER证书到CloudServiceExtendedSupport中的文章就讲到这里了,如果你想了解更多关于【AzureCloudService】使用KeyVaultSecret添加.CER证书到CloudServiceExtendedSupport中的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com