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.

93 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
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. QINIU_API_BASE="https://api.qiniu.com"
  9. qiniu_deploy() {
  10. _cdomain="$1"
  11. _ckey="$2"
  12. _ccert="$3"
  13. _cca="$4"
  14. _cfullchain="$5"
  15. _cdndomain="${QINIU_CDN_DOMAIN:-$_cdomain}"
  16. _debug _cdomain "$_cdomain"
  17. _debug _ckey "$_ckey"
  18. _debug _ccert "$_ccert"
  19. _debug _cca "$_cca"
  20. _debug _cfullchain "$_cfullchain"
  21. if [ -z "$QINIU_AK" ]; then
  22. if [ -z "$Le_Deploy_Qiniu_AK" ]; then
  23. _err "QINIU_AK is not defined."
  24. return 1
  25. fi
  26. else
  27. Le_Deploy_Qiniu_AK="$QINIU_AK"
  28. _savedomainconf Le_Deploy_Qiniu_AK "$Le_Deploy_Qiniu_AK"
  29. fi
  30. if [ -z "$QINIU_SK" ]; then
  31. if [ -z "$Le_Deploy_Qiniu_SK" ]; then
  32. _err "QINIU_SK is not defined."
  33. return 1
  34. fi
  35. else
  36. Le_Deploy_Qiniu_SK="$QINIU_SK"
  37. _savedomainconf Le_Deploy_Qiniu_SK "$Le_Deploy_Qiniu_SK"
  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\":\"$_cdndomain\",\"ca\":\"$string_fullchain\",\"pri\":\"$string_key\"}"
  44. sslcert_access_token="$(_make_sslcreate_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 "multiline")
  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_path="/domain/$_cdndomain/httpsconf"
  60. update_body="{\"certid\":$_certId,\"forceHttps\":true}"
  61. update_access_token="$(_make_sslcreate_access_token "$update_path")"
  62. _debug update_access_token "$update_access_token"
  63. export _H1="Authorization: QBox $update_access_token"
  64. update_response=$(_post "$update_body" "$QINIU_API_BASE$update_path" 0 "PUT" "application/json" | _dbase64 "multiline")
  65. if _contains "$update_response" "error"; then
  66. _err "Error in updating domain httpsconf:"
  67. _err "$update_response"
  68. return 1
  69. fi
  70. _debug update_response "$update_response"
  71. _info "Certificate successfully deployed"
  72. return 0
  73. }
  74. _make_sslcreate_access_token() {
  75. _token="$(printf "%s\n" "$1" | _hmac "sha1" "$(printf "%s" "$Le_Deploy_Qiniu_SK" | _hex_dump | tr -d " ")" | _base64)"
  76. echo "$Le_Deploy_Qiniu_AK:$_token"
  77. }