qiniu.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. _debug _cdomain "$_cdomain"
  16. _debug _ckey "$_ckey"
  17. _debug _ccert "$_ccert"
  18. _debug _cca "$_cca"
  19. _debug _cfullchain "$_cfullchain"
  20. if [ -z "$QINIU_AK" ]; then
  21. _err "QINIU_AK is not defined."
  22. return 1
  23. else
  24. _savedomainconf QINIU_AK "$QINIU_AK"
  25. fi
  26. if [ -z "$QINIU_SK" ]; then
  27. _err "QINIU_SK is not defined."
  28. return 1
  29. else
  30. _savedomainconf QINIU_SK "$QINIU_SK"
  31. fi
  32. if [ "$QINIU_CDN_DOMAIN" ]; then
  33. _savedomainconf QINIU_CDN_DOMAIN "$QINIU_CDN_DOMAIN"
  34. else
  35. QINIU_CDN_DOMAIN="$_cdomain"
  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\":\"$QINIU_CDN_DOMAIN\",\"ca\":\"$string_fullchain\",\"pri\":\"$string_key\"}"
  42. sslcert_access_token="$(_make_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/$QINIU_CDN_DOMAIN/httpsconf"
  58. update_body="{\"certid\":$_certId,\"forceHttps\":false}"
  59. update_access_token="$(_make_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_access_token() {
  73. _token="$(printf "%s\n" "$1" | _hmac "sha1" "$(printf "%s" "$QINIU_SK" | _hex_dump | tr -d " ")" | _base64 | tr -- '+/' '-_')"
  74. echo "$QINIU_AK:$_token"
  75. }