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.

225 lines
6.7 KiB

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