You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.0 KiB

  1. #!/usr/bin/env sh
  2. #export DEPLOY_TENCENT_SSL_SECRET_ID="AKIDz81d2cd22cdcdc2dcd1cc1d1A"
  3. #export DEPLOY_TENCENT_SSL_SECRET_KEY="Gu5t9abcabcaabcbabcbbbcbcbbccbbcb"
  4. tencent_ssl_deploy() {
  5. _cdomain="$1"
  6. _ckey="$2"
  7. _cfullchain="$5"
  8. _debug _cdomain "$_cdomain"
  9. _debug _ckey "$_ckey"
  10. _debug _cfullchain "$_cfullchain"
  11. _getdeployconf DEPLOY_TENCENT_SSL_SECRET_ID
  12. _getdeployconf DEPLOY_TENCENT_SSL_SECRET_KEY
  13. if [ -z "${DEPLOY_TENCENT_SSL_SECRET_ID}" ]; then
  14. _err "Please define DEPLOY_TENCENT_SSL_SECRET_ID."
  15. return 1
  16. fi
  17. if [ -z "${DEPLOY_TENCENT_SSL_SECRET_KEY}" ]; then
  18. _err "Please define DEPLOY_TENCENT_SSL_SECRET_KEY."
  19. return 1
  20. fi
  21. _savedeployconf DEPLOY_TENCENT_SSL_SECRET_ID "$DEPLOY_TENCENT_SSL_SECRET_ID"
  22. _savedeployconf DEPLOY_TENCENT_SSL_SECRET_KEY "$DEPLOY_TENCENT_SSL_SECRET_KEY"
  23. # https://cloud.tencent.com/document/api/400/41665
  24. _payload="{\"CertificatePublicKey\":\"$(_json_encode <"$_cfullchain")\",\"CertificatePrivateKey\":\"$(_json_encode <"$_ckey")\",\"Alias\":\"acme.sh $_cdomain\"}"
  25. if ! cert_id="$(tencent_api_request_ssl "UploadCertificate" "$_payload" "CertificateId")"; then
  26. return 1
  27. fi
  28. _debug cert_id "$cert_id"
  29. _getdeployconf DEPLOY_TENCENT_SSL_CURRENT_CERTIFICATE_ID
  30. old_cert_id="$DEPLOY_TENCENT_SSL_CURRENT_CERTIFICATE_ID"
  31. # https://cloud.tencent.com/document/api/400/91649
  32. # NOTE: no new cert id returned from UpdateCertificateInstance+cert_data
  33. # so it's necessary to upload cert first then UpdateCertificateInstance+new_cert_id
  34. if [ -n "${old_cert_id}" ]; then
  35. _payload="{\"OldCertificateId\":\"$old_cert_id\",\"CertificateId\":\"$cert_id\",\"ResourceTypes\":[\"clb\",\"cdn\",\"waf\",\"live\",\"ddos\",\"teo\",\"apigateway\",\"vod\",\"tke\",\"tcb\",\"tse\"]}"
  36. if ! tencent_api_request_ssl "UpdateCertificateInstance" "$_payload" "RequestId"; then
  37. return 1
  38. fi
  39. _payload="{\"CertificateId\":\"$old_cert_id\"}"
  40. if ! tencent_api_request_ssl "DeleteCertificate" "$_payload" "RequestId"; then
  41. _err "Can not delete old certificate: $old_cert_id"
  42. # NOTE: non-exist old cert id will not break from UpdateCertificateInstance
  43. # break it here
  44. return 1
  45. fi
  46. fi
  47. _savedeployconf DEPLOY_TENCENT_SSL_CURRENT_CERTIFICATE_ID "$cert_id"
  48. return 0
  49. }
  50. tencent_api_request_ssl() {
  51. action=$1
  52. payload=$2
  53. response_field=$3
  54. if ! response="$(tencent_api_request "ssl" "2019-12-05" "$action" "$payload")"; then
  55. _err "Error <$1>"
  56. return 1
  57. fi
  58. err_message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  59. if [ "$err_message" ]; then
  60. _err "$err_message"
  61. return 1
  62. fi
  63. _debug response "$response"
  64. value="$(echo "$response" | _egrep_o "\"$response_field\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  65. if [ -z "$value" ]; then
  66. _err "$response_field not found"
  67. return 1
  68. fi
  69. echo "$value"
  70. }
  71. # shell client for tencent cloud api v3 | @author: rehiy
  72. # copy from dns_tencent.sh
  73. tencent_sha256() {
  74. printf %b "$@" | _digest sha256 hex
  75. }
  76. tencent_hmac_sha256() {
  77. k=$1
  78. shift
  79. hex_key=$(printf %b "$k" | _hex_dump | tr -d ' ')
  80. printf %b "$@" | _hmac sha256 "$hex_key" hex
  81. }
  82. tencent_hmac_sha256_hexkey() {
  83. k=$1
  84. shift
  85. printf %b "$@" | _hmac sha256 "$k" hex
  86. }
  87. tencent_signature_v3() {
  88. service=$1
  89. action=$(echo "$2" | _lower_case)
  90. payload=${3:-'{}'}
  91. timestamp=${4:-$(date +%s)}
  92. domain="$service.tencentcloudapi.com"
  93. secretId="$DEPLOY_TENCENT_SSL_SECRET_ID"
  94. secretKey="$DEPLOY_TENCENT_SSL_SECRET_KEY"
  95. algorithm='TC3-HMAC-SHA256'
  96. date=$(date -u -d "@$timestamp" +%Y-%m-%d 2>/dev/null)
  97. [ -z "$date" ] && date=$(date -u -r "$timestamp" +%Y-%m-%d)
  98. canonicalUri='/'
  99. canonicalQuery=''
  100. canonicalHeaders="content-type:application/json\nhost:$domain\nx-tc-action:$action\n"
  101. _debug2 payload "$payload"
  102. signedHeaders='content-type;host;x-tc-action'
  103. canonicalRequest="POST\n$canonicalUri\n$canonicalQuery\n$canonicalHeaders\n$signedHeaders\n$(printf %s "$payload" | _digest sha256 hex)"
  104. _debug2 canonicalRequest "$canonicalRequest"
  105. credentialScope="$date/$service/tc3_request"
  106. stringToSign="$algorithm\n$timestamp\n$credentialScope\n$(tencent_sha256 "$canonicalRequest")"
  107. _debug2 stringToSign "$stringToSign"
  108. secretDate=$(tencent_hmac_sha256 "TC3$secretKey" "$date")
  109. secretService=$(tencent_hmac_sha256_hexkey "$secretDate" "$service")
  110. secretSigning=$(tencent_hmac_sha256_hexkey "$secretService" 'tc3_request')
  111. signature=$(tencent_hmac_sha256_hexkey "$secretSigning" "$stringToSign")
  112. echo "$algorithm Credential=$secretId/$credentialScope, SignedHeaders=$signedHeaders, Signature=$signature"
  113. }
  114. tencent_api_request() {
  115. service=$1
  116. version=$2
  117. action=$3
  118. payload=${4:-'{}'}
  119. timestamp=${5:-$(date +%s)}
  120. token=$(tencent_signature_v3 "$service" "$action" "$payload" "$timestamp")
  121. _H1="Authorization: $token"
  122. _H2="X-TC-Version: $version"
  123. _H3="X-TC-Timestamp: $timestamp"
  124. _H4="X-TC-Action: $action"
  125. _H5="X-TC-Language: en-US"
  126. _post "$payload" "https://$service.tencentcloudapi.com" "" "POST" "application/json"
  127. }