Skip to content

Commit

Permalink
add an agent to store distributino state (and avoid multiple pem deco…
Browse files Browse the repository at this point in the history
…des when runtime configured)
  • Loading branch information
Michael Guarino committed Jun 4, 2018
1 parent 1d61692 commit 726fbb9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
12 changes: 12 additions & 0 deletions lib/cloudfront_signer/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule CloudfrontSigner.Application do
use Application
import Supervisor.Spec

def start(_type, _args) do
children = [
worker(CloudfrontSigner.DistributionRegistry, [])
]
opts = [strategy: :one_for_one, name: CloudfrontSigner.Application.Supervisor]
Supervisor.start_link(children, opts)
end
end
20 changes: 20 additions & 0 deletions lib/cloudfront_signer/distribution_registry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule CloudfrontSigner.DistributionRegistry do
@moduledoc """
Agent to store and fetch cloudfront distributions, to avoid expensive runtime pem decodes
"""
use Agent
alias CloudfrontSigner.Distribution

def start_link() do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end

def get_distribution(scope, key) do
Agent.get_and_update(__MODULE__, &Map.get_and_update(&1, {scope, key}, fn
nil ->
dist = Distribution.from_config(scope, key)
{dist, dist}
dist -> {dist, dist}
end))
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule CloudfrontSigner.MixProject do

def application do
[
extra_applications: [:logger]
extra_applications: [:logger],
mod: {CloudfrontSigner.Application, []}
]
end

Expand Down
11 changes: 11 additions & 0 deletions test/cloudfront_signer/distribution_registry_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule CloudfrontSigner.DistributionRegistryTest do
use ExUnit.Case, async: true

describe "#get_distribution/2" do
test "It will get a registry and save it internally" do
distribution = CloudfrontSigner.DistributionRegistry.get_distribution(:cloudfront_signer, CloudfrontSignerTest)

assert distribution.domain == "https://somewhere.cloudfront.com"
end
end
end
2 changes: 1 addition & 1 deletion test/cloudfront_signer/signature_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule CloudfrontSigner.SignatureTest do
use ExUnit.Case
use ExUnit.Case, async: true

@correct_signature """
ToshDrR-FhIhiStqrp8kAEyQ3YGsz-P5Nh9~~lu0m5l4V-qg3K9Pp~pjYCIYR4yC
Expand Down
2 changes: 1 addition & 1 deletion test/cloudfront_signer_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule CloudfrontSignerTest do
use ExUnit.Case
use ExUnit.Case, async: true
doctest CloudfrontSigner

describe "#sign/3" do
Expand Down

0 comments on commit 726fbb9

Please sign in to comment.