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.

91 lines
2.8 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
  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. QINIU_API_BASE="https://api.qiniu.com"
  8. qiniu_deploy() {
  9. _cdomain="$1"
  10. _ckey="$2"
  11. _ccert="$3"
  12. _cca="$4"
  13. _cfullchain="$5"
  14. _debug _cdomain "$_cdomain"
  15. _debug _ckey "$_ckey"
  16. _debug _ccert "$_ccert"
  17. _debug _cca "$_cca"
  18. _debug _cfullchain "$_cfullchain"
  19. if [ -z "$QINIU_AK" ]; then
  20. if [ -z "$Le_Deploy_Qiniu_AK" ]; then
  21. _err "QINIU_AK is not defined."
  22. return 1
  23. fi
  24. else
  25. Le_Deploy_Qiniu_AK="$QINIU_AK"
  26. _savedomainconf Le_Deploy_Qiniu_AK "$Le_Deploy_Qiniu_AK"
  27. fi
  28. if [ -z "$QINIU_SK" ]; then
  29. if [ -z "$Le_Deploy_Qiniu_SK" ]; then
  30. _err "QINIU_SK is not defined."
  31. return 1
  32. fi
  33. else
  34. Le_Deploy_Qiniu_SK="$QINIU_SK"
  35. _savedomainconf Le_Deploy_Qiniu_SK "$Le_Deploy_Qiniu_SK"
  36. fi
  37. ## upload certificate
  38. string_fullchain=$(sed 's/$/\\n/' "$_cfullchain" | tr -d '\n')
  39. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  40. sslcert_path="/sslcert"
  41. sslcerl_body="{\"name\":\"$_cdomain\",\"common_name\":\"$_cdomain\",\"ca\":\"$string_fullchain\",\"pri\":\"$string_key\"}"
  42. sslcert_access_token="$(_make_sslcreate_access_token "$sslcert_path")"
  43. _debug sslcert_access_token "$sslcert_access_token"
  44. export _H1="Authorization: QBox $sslcert_access_token"
  45. sslcert_response=$(_post "$sslcerl_body" "$QINIU_API_BASE$sslcert_path" 0 "POST" "application/json" | _dbase64 "multiline")
  46. if ! _contains "$sslcert_response" "certID"; then
  47. _err "Error in creating certificate:"
  48. _err "$sslcert_response"
  49. return 1
  50. fi
  51. _debug sslcert_response "$sslcert_response"
  52. _info "Certificate successfully uploaded, updating domain $_cdomain"
  53. ## extract certId
  54. _certId="$(printf "%s" "$sslcert_response" | _normalizeJson | _egrep_o "certID\": *\"[^\"]*\"" | cut -d : -f 2)"
  55. _debug certId "$_certId"
  56. ## update domain ssl config
  57. update_path="/domain/$_cdomain/httpsconf"
  58. update_body="{\"certid\":$_certId,\"forceHttps\":true}"
  59. update_access_token="$(_make_sslcreate_access_token "$update_path")"
  60. _debug update_access_token "$update_access_token"
  61. export _H1="Authorization: QBox $update_access_token"
  62. update_response=$(_post "$update_body" "$QINIU_API_BASE$update_path" 0 "PUT" "application/json" | _dbase64 "multiline")
  63. if _contains "$update_response" "error"; then
  64. _err "Error in updating domain httpsconf:"
  65. _err "$update_response"
  66. return 1
  67. fi
  68. _debug update_response "$update_response"
  69. _info "Certificate successfully deployed"
  70. return 0
  71. }
  72. _make_sslcreate_access_token() {
  73. _token="$(printf "%s\n" "$1" | _hmac "sha1" "$(printf "%s" "$Le_Deploy_Qiniu_SK" | _hex_dump | tr -d " ")" | _base64)"
  74. echo "$Le_Deploy_Qiniu_AK:$_token"
  75. }