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
  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=$(awk '{printf "%s\\n", $0}' "$_cfullchain")
  39. string_key=$(awk '{printf "%s\\n", $0}' "$_ckey")
  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. success_response="certID"
  47. if test "${sslcert_response#*$success_response}" == "$sslcert_response"; then
  48. _err "Error in creating certificate:"
  49. _err "$sslcert_response"
  50. return 1
  51. fi
  52. _debug sslcert_response "$sslcert_response"
  53. _info "Certificate successfully uploaded, updating domain $_cdomain"
  54. ## extract certId
  55. _certId=$(printf "%s" "$sslcert_response" | sed -e "s/^.*certID\":\"//" -e "s/\"\}$//")
  56. _debug certId "$_certId"
  57. ## update domain ssl config
  58. update_path="/domain/$_cdomain/httpsconf"
  59. update_body="{\"certid\":\"$_certId\",\"forceHttps\":true}"
  60. update_access_token="$(_make_sslcreate_access_token "$update_path")"
  61. _debug update_access_token "$update_access_token"
  62. export _H1="Authorization: QBox $update_access_token"
  63. update_response=$(_post "$update_body" "$QINIU_API_BASE$update_path" 0 "PUT" "application/json" | _dbase64 "multiline")
  64. err_response="error"
  65. if test "${update_response#*$err_response}" != "$update_response"; 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. }