-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds [persistence](https://github.com/hlte-net/persistence/) and email ingest support. Bumps app version to `0.2.0`. API version remains the same as the surface the extension uses has not changed (only `POST /sns` has been added).
- Loading branch information
Showing
13 changed files
with
334 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,3 @@ config :logger, | |
compile_time_purge_matching: [ | ||
[level_lower_than: :info] | ||
] | ||
|
||
config :hlte, | ||
port: 56555 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Config | ||
|
||
config :hlte, | ||
redis_url: System.fetch_env!("HLTE_REDIS_URL"), | ||
sns_whitelist: Jason.decode!(System.fetch_env!("HLTE_SNS_WHITELIST_JSON")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
defmodule HLTE.EmailProcessor do | ||
require Logger | ||
use GenServer | ||
|
||
def start_link(opts) do | ||
Logger.notice( | ||
"Email processor using whitelist: #{Enum.join(Application.fetch_env!(:hlte, :sns_whitelist), ", ")}" | ||
) | ||
|
||
GenServer.start_link(__MODULE__, :ok, opts) | ||
end | ||
|
||
def from_bucket(bucket, key, from, subject) do | ||
GenServer.cast(EmailProcessor, {:process_from_bucket, bucket, key, from, subject}) | ||
end | ||
|
||
@impl true | ||
def init(:ok) do | ||
{:ok, %{}} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:process_from_bucket, bucket, key, from, subject}, state) do | ||
case Enum.find(Application.fetch_env!(:hlte, :sns_whitelist), fn whiteListedAddress -> | ||
from === whiteListedAddress | ||
end) do | ||
^from -> | ||
case URI.parse(subject) |> validate_parsed_subject_uri() do | ||
:error -> Logger.error("Malformed URI as subject!") | ||
host -> stream_and_parse(bucket, key, subject, host) | ||
end | ||
|
||
nil -> | ||
Logger.error("Message from non-whitelisted address <#{from}>!") | ||
end | ||
|
||
if Application.fetch_env!(:hlte, :delete_sns_s3_post_proc) === true do | ||
{:ok, _content} = ExAws.S3.delete_object(bucket, key) |> ExAws.request() | ||
end | ||
|
||
{:noreply, [state]} | ||
end | ||
|
||
def validate_parsed_subject_uri(%URI{:host => host, :scheme => s}) | ||
when host !== nil and s !== nil, | ||
do: host | ||
|
||
def validate_parsed_subject_uri(_bad_uri), do: :error | ||
|
||
def stream_and_parse(bucket, key, uri, host) do | ||
{content_type, parsed_body, part_type} = | ||
ExAws.S3.download_file(bucket, key, :memory) | ||
|> ExAws.stream!() | ||
|> Stream.chunk_while( | ||
"", | ||
fn cur, acc -> | ||
{:cont, cur, acc <> cur} | ||
end, | ||
fn | ||
"" -> {:cont, ""} | ||
acc -> {:cont, acc, ""} | ||
end | ||
) | ||
|> Enum.to_list() | ||
|> Enum.at(0) | ||
|> Mail.Parsers.RFC2822.parse() | ||
|> extract_body() | ||
|
||
Logger.info( | ||
"Parsed #{String.length(parsed_body)} bytes of '#{content_type}' from a #{part_type} message" | ||
) | ||
|
||
{:ok, rxTime, entryID} = | ||
HLTE.DB.persist( | ||
%{ | ||
"uri" => uri, | ||
"data" => parsed_body | ||
}, | ||
HLTE.HTTP.calculate_body_hmac(parsed_body) | ||
) | ||
|
||
Logger.info("Persisted hilite for #{host} at #{floor(rxTime / 1.0e9)}, work ID #{entryID}") | ||
end | ||
|
||
def extract_body(%Mail.Message{:multipart => true, :parts => parts}) do | ||
target_part = | ||
Enum.find(parts, fn p -> | ||
Map.get(p.headers, "content-type") |> Enum.at(0) === "text/plain" | ||
end) || | ||
Enum.at(parts, 0) | ||
|
||
{Map.get(target_part.headers, "content-type") |> Enum.at(0), target_part.body, "multipart"} | ||
end | ||
|
||
def extract_body(%Mail.Message{ | ||
:multipart => false, | ||
:body => body, | ||
:headers => %{"content-type" => content_type} | ||
}) do | ||
{content_type |> Enum.at(0), body, "mono"} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule HLTE.HTTP.Route.SNSIngest do | ||
require Logger | ||
|
||
def init(req, state) when req.method === "POST" do | ||
{:ok, bodyText, req2} = :cowboy_req.read_body(req) | ||
|
||
case Jason.decode(bodyText) |> ingest_post() do | ||
:error -> | ||
Logger.warn("Raw request:\n#{inspect(req)}") | ||
Logger.warn("Raw body:\n#{bodyText}") | ||
|
||
:ok -> | ||
:ok | ||
end | ||
|
||
{:ok, :cowboy_req.set_resp_header("Access-Control-Allow-Origin", "*", req2), state} | ||
end | ||
|
||
def init(req, state) do | ||
{:ok, :cowboy_req.reply(405, req), state} | ||
end | ||
|
||
defp ingest_post( | ||
{:ok, | ||
%{ | ||
"notificationType" => "Received", | ||
"receipt" => %{ | ||
"action" => %{ | ||
"type" => "S3", | ||
"bucketName" => bucket, | ||
"objectKey" => objectKey | ||
} | ||
}, | ||
"mail" => %{ | ||
"source" => source, | ||
"commonHeaders" => %{ | ||
"subject" => subject | ||
} | ||
} | ||
}} | ||
) do | ||
Logger.info("Processing SNS from <#{source}>, subject \"#{subject}\"") | ||
HLTE.EmailProcessor.from_bucket(bucket, objectKey, source, subject) | ||
:ok | ||
end | ||
|
||
defp ingest_post({:ok, malformed}) do | ||
Logger.error("Malformed POST object! #{inspect(malformed)}") | ||
:error | ||
end | ||
|
||
defp ingest_post({:error, %Jason.DecodeError{:data => d, :position => p, :token => t}}) do | ||
Logger.error("Malformed POST JSON!") | ||
Logger.error(" at position #{p}, token '#{t}' in data: #{d}") | ||
:error | ||
end | ||
|
||
defp ingest_post({:error, unkErr}) do | ||
Logger.error("Malformed POST! Unknown error: #{inspect(unkErr)}") | ||
:error | ||
end | ||
end |
Oops, something went wrong.