cloud_front_signing 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 533dc16e38e6e6070fcc4ce38371ce6621e6cf0b
4
+ data.tar.gz: 86f74303db860f93ce74fd17f4d8fbb19204e835
5
+ SHA512:
6
+ metadata.gz: 767dcaa5ff05c1a01b485226f71d6c3fd753d38fe1b13d469bcb0c329ab881151d13138e044f69e0ef9f8509d2f7be61c24d19b2ccfc0106f981e4cfa0de186b
7
+ data.tar.gz: e90308082eb4fe4cb5ef705a30c87fcf3383b5f5586e67ad0f1882c77996f88339ad5188fad21c6f8bf0391069b5e377f7f928877ae8cbbd1dce75777c042ac5
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloud_front_signing.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ronny Haryanto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # CloudFrontSigning
2
+
3
+ Sign [AWS CloudFront] URLs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cloud_front_signing'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cloud_front_signing
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ # Replace these for your specific AWS account/CloudFront configuration
25
+ private_key_string = IO.read("path/to/pk-123456789012.pem")
26
+ key_pair_id = "123456789012"
27
+
28
+ signer = CloudFrontSigning::Signer.new(private_key_string, key_pair_id)
29
+
30
+ unsigned_url = "https://distribution-id.cloudfront.net/private.zip"
31
+ options = {ending: 1.hour.from_now} # or, e.g. `Time.now + 3600` if you don't have activesupport
32
+ signed_url = signer.sign(unsigned_url, options)
33
+ ```
34
+
35
+ Valid options:
36
+
37
+ - `ending`: required, the signed URL will only be accessible until this time, a `Time` object or a `String` parseable by `Time.parse`
38
+ - `resource`: the base URL including your query strings, if any, but excluding the CloudFront Policy, Signature, and Key-Pair-Id parameters, uses unsigned_url by default
39
+ - `starting`: the signed URL will only be accessible after this time, a `Time` object or a `String` parseable by `Time.parse`
40
+ - `ip_range`: the signed URL will only be accessible by IP addresses in this range, a CIDR string, e.g. `10.1.2.3/32`, `10.2.3.0/24`, and so on.
41
+
42
+ Specifying `starting` and/or `ip_range` will cause the signed URL to include a [custom policy] which is longer.
43
+
44
+ ## Credits
45
+
46
+ Code adapted from https://github.com/dylanvaughn/aws_cf_signer by Dylan Vaughn.
47
+
48
+ Parts of signing code taken from a question on Stack Overflow asked by Ben Wiseley, and answered by Blaz Lipuscek and Manual M:
49
+
50
+ http://stackoverflow.com/questions/2632457/create-signed-urls-for-cloudfront-with-ruby
51
+ http://stackoverflow.com/users/315829/ben-wiseley
52
+ http://stackoverflow.com/users/267804/blaz-lipuscek
53
+ http://stackoverflow.com/users/327914/manuel-m
54
+
55
+ ## References
56
+
57
+ [AWS documentation on private content and signed URLs]
58
+
59
+ [AWS CloudFront]: http://aws.amazon.com/cloudfront/
60
+ [custom policy]: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-creating-signed-url-custom-policy.html
61
+ [AWS documentation on private content and signed URLs]: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls-overview.html
62
+
63
+ ## License
64
+
65
+ MIT. See LICENSE.txt.
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/[my-github-username]/cloud_front_signing/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloud_front_signing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloud_front_signing"
8
+ spec.version = CloudFrontSigning::VERSION
9
+ spec.authors = ["Ronny Haryanto"]
10
+ spec.email = ["ronny@haryan.to"]
11
+ spec.summary = %q{Generate AWS CloudFront signed URLs.}
12
+ spec.description = %q{Generate AWS CloudFront signed URLs.}
13
+ spec.homepage = "https://github.com/ronny/cloud_front_signing"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry", "~> 0.10.1"
24
+ spec.add_development_dependency "rspec", "~> 3.1.0"
25
+
26
+ spec.add_dependency "addressable", '~> 2.3.6'
27
+ spec.add_dependency "yajl-ruby", '~> 1.2.1'
28
+ end
@@ -0,0 +1,76 @@
1
+ require 'time'
2
+ require 'yajl'
3
+
4
+ module CloudFrontSigning
5
+ class Policy
6
+ attr_reader :options
7
+
8
+ def initialize(options)
9
+ @options = options
10
+ end
11
+
12
+ def canned?
13
+ options.keys == [:ending] || options.keys.sort == [:ending, :resource]
14
+ end
15
+
16
+ def object
17
+ @object ||= {
18
+ "Statement" => [
19
+ {
20
+ "Resource" => resource,
21
+ "Condition" => conditions,
22
+ }
23
+ ]
24
+ }
25
+ end
26
+
27
+ def to_s
28
+ Yajl.dump(object, pretty: false, indent: '')
29
+ end
30
+
31
+ private
32
+
33
+ def conditions
34
+ @conditions ||= begin
35
+ result = {
36
+ "DateLessThan" => {"AWS:EpochTime" => ending},
37
+ }
38
+
39
+ if options[:starting]
40
+ result["DateGreaterThan"] = {"AWS:EpochTime" => starting}
41
+ end
42
+
43
+ # CIDR notation, e.g. 127.1.2.0/24
44
+ if options[:ip_range]
45
+ result["IpAddress"] = {"AWS:SourceIp" => options[:ip_range]}
46
+ end
47
+
48
+ result
49
+ end
50
+ end
51
+
52
+ def starting
53
+ @starting ||= epoch_time(options[:starting])
54
+ end
55
+
56
+ def ending
57
+ @ending ||= begin
58
+ raise ArgumentError, 'missing :ending option' unless options[:ending]
59
+ epoch_time(options[:ending])
60
+ end
61
+ end
62
+
63
+ def resource
64
+ @resource ||= options[:resource] or raise ArgumentError, 'missing :resource option'
65
+ end
66
+
67
+ def epoch_time(timelike)
68
+ case timelike
69
+ when String then Time.parse(timelike).to_i
70
+ when Time then timelike.to_i
71
+ else raise ArgumentError.new("Invalid argument #{timelike} - String or Time required - #{timelike.class} passed.")
72
+ end
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,73 @@
1
+ require 'pp'
2
+ require 'addressable/uri'
3
+
4
+ require 'cloud_front_signing/url_safe_encoded_param'
5
+ require 'cloud_front_signing/policy'
6
+
7
+ module CloudFrontSigning
8
+ POLICY_OPTION_KEYS = [:starting, :ending, :resource, :ip_range].freeze
9
+
10
+ class SignedUrl
11
+ attr_reader :unsigned_url, :options
12
+
13
+ def initialize(unsigned_url, options)
14
+ @unsigned_url = unsigned_url
15
+ @options = options
16
+ end
17
+
18
+ def to_s
19
+ uri.to_s
20
+ end
21
+
22
+ private
23
+
24
+ def uri
25
+ @uri ||= Addressable::URI.parse(unsigned_url).tap do |u|
26
+ params = {
27
+ 'Signature' => signature,
28
+ 'Key-Pair-Id' => key_pair_id,
29
+ }
30
+ unless policy.canned?
31
+ # shorter URL if canned (no need to include the encoded policy)
32
+ params['Policy'] = encoded_policy
33
+ end
34
+ u.query_values = (u.query_values || {}).merge(params)
35
+ end
36
+ end
37
+
38
+ def encoded_policy
39
+ @encoded_policy ||= UrlSafeEncodedParam.new(policy.to_s).to_s
40
+ end
41
+
42
+ def policy
43
+ @policy ||= Policy.new(policy_options)
44
+ end
45
+
46
+ def signed_policy
47
+ @signed_policy ||= key.sign(OpenSSL::Digest::SHA1.new, policy.to_s)
48
+ end
49
+
50
+ def signature
51
+ @signature ||= UrlSafeEncodedParam.new(signed_policy).to_s
52
+ end
53
+
54
+ def key_pair_id
55
+ options[:key_pair_id]
56
+ end
57
+
58
+ def key
59
+ options[:key]
60
+ end
61
+
62
+ def policy_options
63
+ @policy_options ||= begin
64
+ initial = {resource: unsigned_url}
65
+ options.reduce(initial) do |hash, (k, v)|
66
+ hash[k] = v if POLICY_OPTION_KEYS.include?(k)
67
+ hash
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,29 @@
1
+ require 'openssl'
2
+
3
+ require "cloud_front_signing/signed_url"
4
+
5
+ module CloudFrontSigning
6
+ class Signer
7
+ attr_reader :private_key_string, :key_pair_id
8
+
9
+ def initialize(private_key_string, key_pair_id)
10
+ @private_key_string = private_key_string
11
+ @key_pair_id = key_pair_id
12
+ end
13
+
14
+ def sign(unsigned_url, options = {})
15
+ url_options = options.merge(
16
+ key_pair_id: key_pair_id,
17
+ key: key,
18
+ )
19
+ SignedUrl.new(unsigned_url, url_options).to_s
20
+ end
21
+
22
+ private
23
+
24
+ def key
25
+ @key ||= OpenSSL::PKey::RSA.new(private_key_string)
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,25 @@
1
+ require 'base64'
2
+
3
+ module CloudFrontSigning
4
+ class UrlSafeEncodedParam
5
+ def self.url_safe(s)
6
+ s.gsub('+','-').gsub('=','_').gsub('/','~').gsub(/[\n ]/,'')
7
+ end
8
+
9
+ attr_reader :input
10
+
11
+ def initialize(input)
12
+ @input = input
13
+ end
14
+
15
+ def to_s
16
+ encoded
17
+ end
18
+
19
+ private
20
+
21
+ def encoded
22
+ @encoded ||= self.class.url_safe(Base64.encode64(input))
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module CloudFrontSigning
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "cloud_front_signing/version"
2
+ require "cloud_front_signing/signer"
3
+
4
+ module CloudFrontSigning
5
+ end
data/script/console ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.require(:default, :development)
5
+
6
+ require 'cloud_front_signing'
7
+ require 'pry'
8
+
9
+ binding.pry
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloud_front_signing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ronny Haryanto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: addressable
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.3.6
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.3.6
83
+ - !ruby/object:Gem::Dependency
84
+ name: yajl-ruby
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.2.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.2.1
97
+ description: Generate AWS CloudFront signed URLs.
98
+ email:
99
+ - ronny@haryan.to
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".ruby-version"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - cloud_front_signing.gemspec
111
+ - lib/cloud_front_signing.rb
112
+ - lib/cloud_front_signing/policy.rb
113
+ - lib/cloud_front_signing/signed_url.rb
114
+ - lib/cloud_front_signing/signer.rb
115
+ - lib/cloud_front_signing/url_safe_encoded_param.rb
116
+ - lib/cloud_front_signing/version.rb
117
+ - script/console
118
+ homepage: https://github.com/ronny/cloud_front_signing
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Generate AWS CloudFront signed URLs.
142
+ test_files: []