s3_sign 0.1.0

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: 447a0d533f6b48c8ba047c28404ed883505f2433
4
+ data.tar.gz: 459ba0f3170e825a39555cfb43bac368c329ee44
5
+ SHA512:
6
+ metadata.gz: c3cc28ad515aa2542735e63e49e5843aa0ca4ccdd7a15374a073c2b0c2cbee06c28293ff62dcbde7f2ba02cc5c24c90f96104b93e088436dacf9f8f39ac569e8
7
+ data.tar.gz: 7672f0e87dc8023452fc00342ec8a5013599cb7f642f7d954e19fb5032dde26371bd9807e505693857d7f8ec6caeb427afa243224b83c51ba7e8f2f30be2a3e5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3_sign.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kajabi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # S3Sign
2
+
3
+ S3Sign allows easy signing of already formatted s3 urls.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 's3_sign'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install s3_sign
21
+
22
+ ## Usage
23
+
24
+ Initialize the `bucket_name` for for `S3Sign`. In rails for instance,
25
+ you might add an initializer for this.
26
+
27
+ ### Setup
28
+ ```ruby
29
+ S3Sign.bucket_name = "super_s3_bucket"
30
+ ```
31
+
32
+ The gem assumes you already have AWS (via aws-sdk) configured globally,
33
+ something like this:
34
+
35
+ ```ruby
36
+ AWS.config(
37
+ :access_key_id => AppConfig.aws.access_key,
38
+ :secret_access_key => AppConfig.aws.secret_key
39
+ )
40
+ ```
41
+
42
+ ### API
43
+
44
+ The `S3Sign` module itself provides `.url`:
45
+
46
+ ```ruby
47
+ # Pass a full s3 url and 1 hour expiration
48
+ S3Sign.url "http://s3.amazonaws.com/bucket/foo.png", 3600
49
+ # => "https://bucket.s3.amazonaws.com/foo.png?AWSAccessKeyId=access_key_id&Expires=1427243780&Signature=a3RzDgElxDpSZLgxurZLiw1a6Ny%3D"
50
+
51
+ # Pass a 'key' portion found under the bucket with default expiration
52
+ S3Sign.url "images/foo.png"
53
+ ```
54
+
55
+ The gem also provides a `S3Sign::Helper` module, useful to mixin to rails
56
+ controllers. It will add two helper methods to your controllers and views.
57
+
58
+ `s3_signed_url_for_key` - Takes a key/url and optional expires
59
+
60
+ `stable_s3_signed_url` - Takes a url and optional reference time. Used for
61
+ generting signatures that expire in the far future year 2036.
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/[my-github-username]/s3_sign/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "s3_sign"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ module S3Sign
2
+ module Helper
3
+ def self.included(base)
4
+ if base.respond_to?(:helper_method)
5
+ base.helper_method :stable_s3_signed_url
6
+ base.helper_method :s3_signed_url_for_key
7
+ end
8
+
9
+ if base.respond_to?(:hide_action)
10
+ base.hide_action :stable_s3_signed_url
11
+ base.hide_action :s3_signed_url_for_key
12
+ end
13
+ end
14
+
15
+ # A pure "time from now" signed s3 asset url. Good for non-visual elements
16
+ # like attachments, and not thumbnails, that don't have the same caching concerns
17
+ def s3_signed_url_for_key(key, expires_in = 86_400)
18
+ S3Sign.url key, expires_in
19
+ end
20
+
21
+ # Uses a far-future date so that the expires and signature on
22
+ # the url doesn't change for every request. This lets us still
23
+ # protect assets but the browser can cache them because the url
24
+ # isn't constantly changing.
25
+ #
26
+ # Use for assets we want browser cached that don't need higher
27
+ # level protection, such as thumbnails. Other more central assets
28
+ # like downloads for a course or videos should have short expiration
29
+ # because they generally don't benefit as much from caching and need
30
+ # better time-based protection.
31
+ #
32
+ # If a reference_time is given, that will use the time given but in the
33
+ # far future stable year. This is useful to cache-bust the url from
34
+ # an updated_at timestamp where some browsers/proxies may try to use
35
+ # the cached asset even though it was updated.
36
+ def stable_s3_signed_url(url, reference_time = nil)
37
+ @stable_s3_expire_at ||= Time.parse("2036-1-1 00:00:00 UTC")
38
+
39
+ if reference_time
40
+ # The time given but in the year 2036
41
+ year = @stable_s3_expire_at.year
42
+ expires_at = Time.parse(reference_time.utc.strftime("#{year}-%m-%d %T UTC"))
43
+ else
44
+ expires_at = @stable_s3_expire_at
45
+ end
46
+
47
+ s3_signed_url_for_key(url, expires_at)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module S3Sign
2
+ VERSION = "0.1.0"
3
+ end
data/lib/s3_sign.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "s3_sign/version"
2
+ require "s3_sign/helper"
3
+ require "aws"
4
+
5
+ module S3Sign
6
+ SEVEN_DAYS = 60 * 60 * 24 * 7
7
+
8
+ class << self
9
+ attr_writer :bucket_name
10
+ def bucket_name
11
+ @bucket_name or raise "No S3Sign.bucket_name is set"
12
+ end
13
+ end
14
+
15
+ def self.url(s3_url, expires = SEVEN_DAYS)
16
+ s3 = AWS::S3.new
17
+ bucket = s3.buckets[bucket_name]
18
+
19
+ path = path_from_s3_url(s3_url)
20
+
21
+ AWS::S3::S3Object.new(bucket, path).url_for(:read, expires: expires).to_s
22
+ end
23
+
24
+ protected
25
+
26
+ def self.path_from_s3_url(s3_url)
27
+ s3_url.sub(%r{^.+?/#{bucket_name}/}, '')
28
+ end
29
+ end
data/s3_sign.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3_sign/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3_sign"
8
+ spec.version = S3Sign::VERSION
9
+ spec.authors = ["Brendon Murphy"]
10
+ spec.email = ["xternal1+github@gmail.com"]
11
+
12
+ spec.summary = %q{A quick a dirty way to s3 sign string s3 urls using the aws-sdk}
13
+ spec.description = %q{A quick a dirty way to s3 sign string s3 urls using the aws-sdk}
14
+ spec.homepage = "https://github.com/Kajabi/s3_sign"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "aws-sdk", "~> 1.36"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.2.0"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_sign
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brendon Murphy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.36'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.36'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
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.2.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.2.0
69
+ description: A quick a dirty way to s3 sign string s3 urls using the aws-sdk
70
+ email:
71
+ - xternal1+github@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/s3_sign.rb
84
+ - lib/s3_sign/helper.rb
85
+ - lib/s3_sign/version.rb
86
+ - s3_sign.gemspec
87
+ homepage: https://github.com/Kajabi/s3_sign
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.5
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A quick a dirty way to s3 sign string s3 urls using the aws-sdk
111
+ test_files: []