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.

96 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env sh
  2. # Script to create certificate to qiniu.com
  3. #
  4. # This deployment required following variables
  5. # export QINIU_AK="QINIUACCESSKEY"
  6. # export QINIU_SK="QINIUSECRETKEY"
  7. # export QINIU_CDN_DOMAIN="cdn.example.com"
  8. # If you have more than one domain, just
  9. # export QINIU_CDN_DOMAIN="cdn1.example.com cdn2.example.com"
  10. QINIU_API_BASE="https://api.qiniu.com"
  11. qiniu_deploy() {
  12. _cdomain="$1"
  13. _ckey="$2"
  14. _ccert="$3"
  15. _cca="$4"
  16. _cfullchain="$5"
  17. _debug _cdomain "$_cdomain"
  18. _debug _ckey "$_ckey"
  19. _debug _ccert "$_ccert"
  20. _debug _cca "$_cca"
  21. _debug _cfullchain "$_cfullchain"
  22. if [ -z "$QINIU_AK" ]; then
  23. _err "QINIU_AK is not defined."
  24. return 1
  25. else
  26. _savedomainconf QINIU_AK "$QINIU_AK"
  27. fi
  28. if [ -z "$QINIU_SK" ]; then
  29. _err "QINIU_SK is not defined."
  30. return 1
  31. else
  32. _savedomainconf QINIU_SK "$QINIU_SK"
  33. fi
  34. if [ "$QINIU_CDN_DOMAIN" ]; then
  35. _savedomainconf QINIU_CDN_DOMAIN "$QINIU_CDN_DOMAIN"
  36. else
  37. QINIU_CDN_DOMAIN="$_cdomain"
  38. fi
  39. ## upload certificate
  40. string_fullchain=$(sed 's/$/\\n/' "$_cfullchain" | tr -d '\n')
  41. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  42. sslcert_path="/sslcert"
  43. sslcerl_body="{\"name\":\"$_cdomain\",\"common_name\":\"$QINIU_CDN_DOMAIN\",\"ca\":\"$string_fullchain\",\"pri\":\"$string_key\"}"
  44. sslcert_access_token="$(_make_access_token "$sslcert_path")"
  45. _debug sslcert_access_token "$sslcert_access_token"
  46. export _H1="Authorization: QBox $sslcert_access_token"
  47. sslcert_response=$(_post "$sslcerl_body" "$QINIU_API_BASE$sslcert_path" 0 "POST" "application/json" | _dbase64)
  48. if ! _contains "$sslcert_response" "certID"; then
  49. _err "Error in creating certificate:"
  50. _err "$sslcert_response"
  51. return 1
  52. fi
  53. _debug sslcert_response "$sslcert_response"
  54. _info "Certificate successfully uploaded, updating domain $_cdomain"
  55. ## extract certId
  56. _certId="$(printf "%s" "$sslcert_response" | _normalizeJson | _egrep_o "certID\": *\"[^\"]*\"" | cut -d : -f 2)"
  57. _debug certId "$_certId"
  58. ## update domain ssl config
  59. update_body="{\"certid\":$_certId,\"forceHttps\":false}"
  60. for domain in $QINIU_CDN_DOMAIN; do
  61. update_path="/domain/$domain/httpsconf"
  62. update_access_token="$(_make_access_token "$update_path")"
  63. _debug update_access_token "$update_access_token"
  64. export _H1="Authorization: QBox $update_access_token"
  65. update_response=$(_post "$update_body" "$QINIU_API_BASE$update_path" 0 "PUT" "application/json" | _dbase64)
  66. if _contains "$update_response" "error"; then
  67. _err "Error in updating domain $domain httpsconf:"
  68. _err "$update_response"
  69. return 1
  70. fi
  71. _debug update_response "$update_response"
  72. _info "Domain $domain certificate has been deployed successfully"
  73. done
  74. return 0
  75. }
  76. _make_access_token() {
  77. _token="$(printf "%s\n" "$1" | _hmac "sha1" "$(printf "%s" "$QINIU_SK" | _hex_dump | tr -d " ")" | _base64 | tr -- '+/' '-_')"
  78. echo "$QINIU_AK:$_token"
  79. }