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.

226 lines
6.7 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #AWS_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #AWS_SECRET_ACCESS_KEY="xxxxxxx"
  6. #
  7. #AWS_SES_REGION="us-east-1"
  8. #
  9. #AWS_SES_TO="xxxx@xxx.com"
  10. #
  11. #AWS_SES_FROM="xxxx@cccc.com"
  12. #
  13. #AWS_SES_FROM_NAME="Something something"
  14. #This is the Amazon SES api wrapper for acme.sh
  15. AWS_WIKI="https://docs.aws.amazon.com/ses/latest/dg/send-email-api.html"
  16. aws_ses_send() {
  17. _subject="$1"
  18. _content="$2"
  19. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  20. _debug "_statusCode" "$_statusCode"
  21. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  22. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  23. AWS_SES_REGION="${AWS_SES_REGION:-$(_readaccountconf_mutable AWS_SES_REGION)}"
  24. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  25. _use_container_role || _use_instance_role
  26. fi
  27. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  28. AWS_ACCESS_KEY_ID=""
  29. AWS_SECRET_ACCESS_KEY=""
  30. _err "You haven't specified the aws SES api key id and api key secret yet."
  31. _err "Please create your key and try again. see $(__green $AWS_WIKI)"
  32. return 1
  33. fi
  34. if [ -z "$AWS_SES_REGION" ]; then
  35. AWS_SES_REGION=""
  36. _err "You haven't specified the aws SES api region yet."
  37. _err "Please specify your region and try again. see https://docs.aws.amazon.com/general/latest/gr/ses.html"
  38. return 1
  39. fi
  40. _saveaccountconf_mutable AWS_SES_REGION "$AWS_SES_REGION"
  41. #save for future use, unless using a role which will be fetched as needed
  42. if [ -z "$_using_role" ]; then
  43. _saveaccountconf_mutable AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
  44. _saveaccountconf_mutable AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
  45. fi
  46. AWS_SES_TO="${AWS_SES_TO:-$(_readaccountconf_mutable AWS_SES_TO)}"
  47. if [ -z "$AWS_SES_TO" ]; then
  48. AWS_SES_TO=""
  49. _err "You didn't specify an email to AWS_SES_TO receive messages."
  50. return 1
  51. fi
  52. _saveaccountconf_mutable AWS_SES_TO "$AWS_SES_TO"
  53. AWS_SES_FROM="${AWS_SES_FROM:-$(_readaccountconf_mutable AWS_SES_FROM)}"
  54. if [ -z "$AWS_SES_FROM" ]; then
  55. AWS_SES_FROM=""
  56. _err "You didn't specify an email to AWS_SES_FROM receive messages."
  57. return 1
  58. fi
  59. _saveaccountconf_mutable AWS_SES_FROM "$AWS_SES_FROM"
  60. AWS_SES_FROM_NAME="${AWS_SES_FROM_NAME:-$(_readaccountconf_mutable AWS_SES_FROM_NAME)}"
  61. _saveaccountconf_mutable AWS_SES_FROM_NAME "$AWS_SES_FROM_NAME"
  62. AWS_SES_SENDFROM="$AWS_SES_FROM_NAME <$AWS_SES_FROM>"
  63. AWS_SES_ACTION="Action=SendEmail"
  64. AWS_SES_SOURCE="Source=$AWS_SES_SENDFROM"
  65. AWS_SES_TO="Destination.ToAddresses.member.1=$AWS_SES_TO"
  66. AWS_SES_SUBJECT="Message.Subject.Data=$_subject"
  67. AWS_SES_MESSAGE="Message.Body.Text.Data=$_content"
  68. _data="${AWS_SES_ACTION}&${AWS_SES_SOURCE}&${AWS_SES_TO}&${AWS_SES_SUBJECT}&${AWS_SES_MESSAGE}"
  69. response="$(aws_rest POST "" "" "$_data")"
  70. }
  71. _use_metadata() {
  72. _aws_creds="$(
  73. _get "$1" "" 1 |
  74. _normalizeJson |
  75. tr '{,}' '\n' |
  76. while read -r _line; do
  77. _key="$(echo "${_line%%:*}" | tr -d '"')"
  78. _value="${_line#*:}"
  79. _debug3 "_key" "$_key"
  80. _secure_debug3 "_value" "$_value"
  81. case "$_key" in
  82. AccessKeyId) echo "AWS_ACCESS_KEY_ID=$_value" ;;
  83. SecretAccessKey) echo "AWS_SECRET_ACCESS_KEY=$_value" ;;
  84. Token) echo "AWS_SESSION_TOKEN=$_value" ;;
  85. esac
  86. done |
  87. paste -sd' ' -
  88. )"
  89. _secure_debug "_aws_creds" "$_aws_creds"
  90. if [ -z "$_aws_creds" ]; then
  91. return 1
  92. fi
  93. eval "$_aws_creds"
  94. _using_role=true
  95. }
  96. #method uri qstr data
  97. aws_rest() {
  98. mtd="$1"
  99. ep="$2"
  100. qsr="$3"
  101. data="$4"
  102. _debug mtd "$mtd"
  103. _debug ep "$ep"
  104. _debug qsr "$qsr"
  105. _debug data "$data"
  106. CanonicalURI="/$ep"
  107. _debug2 CanonicalURI "$CanonicalURI"
  108. CanonicalQueryString="$qsr"
  109. _debug2 CanonicalQueryString "$CanonicalQueryString"
  110. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  111. _debug2 RequestDate "$RequestDate"
  112. #RequestDate="20161120T141056Z" ##############
  113. export _H1="x-amz-date: $RequestDate"
  114. aws_host="email.$AWS_SES_REGION.amazonaws.com"
  115. CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
  116. SignedHeaders="host;x-amz-date"
  117. if [ -n "$AWS_SESSION_TOKEN" ]; then
  118. export _H3="x-amz-security-token: $AWS_SESSION_TOKEN"
  119. CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
  120. SignedHeaders="${SignedHeaders};x-amz-security-token"
  121. fi
  122. _debug2 CanonicalHeaders "$CanonicalHeaders"
  123. _debug2 SignedHeaders "$SignedHeaders"
  124. RequestPayload="$data"
  125. _debug2 RequestPayload "$RequestPayload"
  126. Hash="sha256"
  127. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
  128. _debug2 CanonicalRequest "$CanonicalRequest"
  129. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  130. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  131. Algorithm="AWS4-HMAC-SHA256"
  132. _debug2 Algorithm "$Algorithm"
  133. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  134. _debug2 RequestDateOnly "$RequestDateOnly"
  135. Region="$AWS_SES_REGION"
  136. Service="ses"
  137. CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
  138. _debug2 CredentialScope "$CredentialScope"
  139. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  140. _debug2 StringToSign "$StringToSign"
  141. kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
  142. #kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
  143. _secure_debug2 kSecret "$kSecret"
  144. kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
  145. _secure_debug2 kSecretH "$kSecretH"
  146. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  147. _debug2 kDateH "$kDateH"
  148. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  149. _debug2 kRegionH "$kRegionH"
  150. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  151. _debug2 kServiceH "$kServiceH"
  152. kSigningH="$(printf "%s" "aws4_request" | _hmac "$Hash" "$kServiceH" hex)"
  153. _debug2 kSigningH "$kSigningH"
  154. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  155. _debug2 signature "$signature"
  156. Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  157. _debug2 Authorization "$Authorization"
  158. _H2="Authorization: $Authorization"
  159. _debug _H2 "$_H2"
  160. url="https://$aws_host/$ep"
  161. if [ "$qsr" ]; then
  162. url="https://$aws_host/$ep?$qsr"
  163. fi
  164. if [ "$mtd" = "GET" ]; then
  165. response="$(_get "$url")"
  166. else
  167. response="$(_post "$data" "$url")"
  168. fi
  169. _ret="$?"
  170. _debug2 response "$response"
  171. if [ "$_ret" = "0" ]; then
  172. if _contains "$response" "<ErrorResponse"; then
  173. _err "Response error:$response"
  174. return 1
  175. fi
  176. fi
  177. }