forked from johnmcguin/cloudfront-signer
-
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.
Merge pull request #3 from cliftonmcintosh/cmcintosh/remove-applicati…
…on-order-keys Remove application file, insure key order in signature
- Loading branch information
Showing
20 changed files
with
426 additions
and
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
lib/cloudfront_signer.ex:16: Invalid type specification for function 'Elixir.CloudfrontSigner':sign/4. | ||
The success typing is 'Elixir.CloudfrontSigner':sign(#{'__struct__':='Elixir.CloudfrontSigner.Distribution', 'domain':=binary() | #{'__struct__':='Elixir.URI', 'authority':='Elixir.URI':authority(), 'fragment':='nil' | binary(), 'host':='nil' | binary(), 'path':='nil' | binary(), 'port':='nil' | char(), 'query':='nil' | binary(), 'scheme':='nil' | binary(), 'userinfo':='nil' | binary()}, 'key_pair_id':=_, 'private_key':={{'RSAPrivateKey',_,_,_,_,_,_,_,_,_,_},{'RSASSA-PSS-params',_,_,_,_}} | {'ECPrivateKey',_,_,_,_,_} | {'DSAPrivateKey',_,_,_,_,_,_} | {'RSAPrivateKey',_,_,_,_,_,_,_,_,_,_}, _=>_},binary() | #{'__struct__':='Elixir.URI', 'authority':='Elixir.URI':authority(), 'fragment':='nil' | binary(), 'host':='nil' | binary(), 'path':='nil' | binary(), 'port':='nil' | char(), 'query':='nil' | binary(), 'scheme':='nil' | binary(), 'userinfo':='nil' | binary()},integer() | #{'__struct__':='Elixir.Timex.Duration', 'megaseconds':=integer(), 'microseconds':=integer(), 'seconds':=integer()},binary() | maybe_improper_list() | map()) -> nonempty_binary() | ||
But the spec is 'Elixir.CloudfrontSigner':sign('Elixir.CloudfrontSigner.Distribution':t(),binary() | [any()] | map(),[any()],integer()) -> binary() | ||
They do not overlap in the 3rd and 4th arguments | ||
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 |
---|---|---|
@@ -1 +1 @@ | ||
export PRIVATE_KEY="<openssl genpkey -algorithm RSA>" | ||
export TESTING_PRIVATE_KEY="$(cat test/support/test_private_key.pem)" |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
plugins: [Styler] | ||
] |
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 |
---|---|---|
@@ -1,44 +1,66 @@ | ||
# CloudfrontSigner | ||
|
||
Elixir implementation of Cloudfront's url signature algorithm. Supports expiration policies and | ||
runtime configurable distributions. Fork of https://github.com/Frameio/cloudfront-signer | ||
runtime configurable distributions. | ||
|
||
The main benefits that this library provides are: | ||
|
||
- Runtime configurable distributions | ||
- Caching of PEM decodes | ||
|
||
## Installation | ||
|
||
The patched package can be installed | ||
by adding `cloudfront_signer` to your list of dependencies in `mix.exs` as a git based dependency: | ||
Add `cloudfront_signer` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:cloudfront_signer, | ||
git: "https://github.com/podium/cloudfront-signer.git", | ||
ref: "73b53cf1364d92708f43ca60dc7150a61cfa5191"} | ||
{:cloudfront_signer, "~> 1.0.0"} | ||
] | ||
end | ||
``` | ||
|
||
Consult the [mix documentation for git based dependencies](https://hexdocs.pm/mix/1.16.0/Mix.Tasks.Deps.html) for valid syntax options. | ||
## Configuring a Distribution | ||
|
||
Configure a distribution with: | ||
|
||
```elixir | ||
config :my_app, :my_distribution, | ||
domain: "https://some.cloudfront.domain", | ||
private_key: {:system, "ENV_VAR"}, # or {:file, "/path/to/key"} | ||
key_pair_id: {:system, "OTHER_ENV_VAR"} | ||
private_key: System.get_env("PRIVATE_KEY"), # or {:file, "/path/to/key"} | ||
key_pair_id: System.get_env("KEY_PAIR_ID") | ||
``` | ||
|
||
Then simply do: | ||
## Signing a URL without Caching PEM Decodes | ||
|
||
Caching PEM decodes is a wise choice, but if you don't want to cache them, you can do the following: | ||
|
||
```elixir | ||
CloudfrontSigner.Distribution.from_config(:my_app, :my_distribution) | ||
|> CloudfrontSigner.sign(path, [arg: "value"], expiry_in_seconds) | ||
``` | ||
|
||
If you want to cache pem decodes (which is a wise choice), a registry of decoded distributions is available. Simply do: | ||
## Caching PEM Decodes | ||
|
||
If you want to cache PEM decodes, you can use the distribution registry. | ||
Add `CloudfrontSigner.DistributionRegistry` to your application's supervision tree: | ||
|
||
```elixir | ||
# In your application.ex | ||
def start(_type, _args) do | ||
children = [ | ||
# ... other children ... | ||
CloudfrontSigner.DistributionRegistry | ||
] | ||
|
||
opts = [strategy: :one_for_one, name: YourApp.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
``` | ||
|
||
Then use it like: | ||
|
||
```elixir | ||
CloudfrontSigner.DistributionRegistry.get_distribution(:my_app, :my_distribution) | ||
|> CloudfrontSigner.sign(path, [arg: "value], expiry) | ||
|> CloudfrontSigner.sign(path, [arg: "value"], expiry) | ||
``` |
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 |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import Config | ||
|
||
config :cloudfront_signer, CloudfrontSigner.DistributionRegistryTest, | ||
domain: "https://test.cloudfront.net", | ||
private_key: System.get_env("TESTING_PRIVATE_KEY"), | ||
key_pair_id: "a_key_pair" | ||
|
||
config :cloudfront_signer, CloudfrontSignerTest, | ||
domain: "https://somewhere.cloudfront.com", | ||
key_pair_id: "a_key_pair", | ||
domain: "https://test.cloudfront.net", | ||
private_key: | ||
System.get_env("PRIVATE_KEY") || | ||
System.get_env("TESTING_PRIVATE_KEY") || | ||
raise(""" | ||
environment variable PRIVATE_KEY is missing. | ||
You can generate one by calling: openssl genpkey -algorithm RSA | ||
""") | ||
environment variable TESTING_PRIVATE_KEY is missing. | ||
You can use the test/support/test_private_key.pem file for your tests. | ||
See the .env.sample file for more information. | ||
You can also generate one by calling: openssl genpkey -algorithm RSA | ||
"""), | ||
key_pair_id: "a_key_pair" | ||
|
||
config :cloudfront_signer, | ||
domain: "https://test.cloudfront.net", | ||
private_key: System.get_env("TESTING_PRIVATE_KEY"), | ||
key_pair_id: "a_key_pair" |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.