aws-url 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - jruby-18mode
5
+ - jruby-19mode
6
+ - rbx-18mode
7
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'jruby-openssl', :platform => :jruby
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Hakan Ensari
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,17 @@
1
+ # AWS URL
2
+
3
+ [![travis][status]][travis]
4
+
5
+ A minimum-viable URL builder for [Amazon Web Services (AWS)][aws].
6
+
7
+ [Vacuum][vacuum] and [Peddler][peddler] implement AWS URL. [Fog][fog] doesn't.
8
+
9
+ ![bezos][bezos]
10
+
11
+ [status]: https://secure.travis-ci.org/hakanensari/aws-url.png
12
+ [travis]: http://travis-ci.org/hakanensari/aws-url
13
+ [aws]: http://aws.amazon.com/
14
+ [vacuum]: https://github.com/hakanensari/vacuum
15
+ [peddler]: https://github.com/hakanensari/peddler
16
+ [fog]: https://github.com/fog/fog
17
+ [bezos]: http://www.wired.com/images/article/magazine/1605/mf_amazon_f.jpg
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Run all specs in spec directory'
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ end
9
+
10
+ task :default => [:spec]
data/aws-url.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require File.expand_path('../lib/aws/url/version.rb', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ['Hakan Ensari']
7
+ gem.email = ['hakan.ensari@papercavalier.com']
8
+ gem.description = %q{A minimum-viable URL builder for Amazon Web Services (AWS)}
9
+ gem.summary = %q{Builds an Amazon Web Services URL}
10
+ gem.homepage = 'https://github.com/hakanensari/aws-url'
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = 'aws-url'
16
+ gem.require_paths = ['lib']
17
+ gem.version = AWS::URL::VERSION
18
+
19
+ gem.add_development_dependency 'rake', '~> 0.9'
20
+ gem.add_development_dependency 'rspec', '~> 2.9'
21
+ end
data/lib/aws-url.rb ADDED
@@ -0,0 +1 @@
1
+ require 'aws/url'
data/lib/aws/url.rb ADDED
@@ -0,0 +1,93 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'time'
4
+ require 'uri'
5
+
6
+ module AWS
7
+ # A Signed Amazon Web Services (AWS) URL.
8
+ #
9
+ # Currently supports Signature Version 2.
10
+ #
11
+ # Note to self: Do not touch this code unless you have a compelling reason.
12
+ class URL
13
+ # The SHA256 hash algorithm.
14
+ SHA256 = OpenSSL::Digest::SHA256.new
15
+
16
+ # Initializes a new URL.
17
+ #
18
+ # base_url - The String base URL, including scheme, host and path, of the
19
+ # AWS endpoint.
20
+ # key - The String AWS access key id.
21
+ # secret - The String AWS secret key.
22
+ def initialize(base_url, key, secret)
23
+ @base_url = URI base_url
24
+ @key = key
25
+ @secret = secret
26
+ @params = {}
27
+ end
28
+
29
+ # Builds a signed URL for specified HTTP method.
30
+ #
31
+ # method - A String-like HTTP method.
32
+ #
33
+ # Returns a String URL.
34
+ def build(method)
35
+ # Build an unsigned query string.
36
+ query = params.sort.map { |k, v| "#{k}=#{ percent_encode v }" }.join '&'
37
+
38
+ # Build the signature.
39
+ string_to_sign = [
40
+ method.to_s.upcase,
41
+ @base_url.host,
42
+ @base_url.path,
43
+ query
44
+ ].join "\n"
45
+ signature = sign string_to_sign
46
+
47
+ "#{@base_url}?#{query}&Signature=#{percent_encode signature}"
48
+ end
49
+
50
+ # Returns the Hash AWS parameters.
51
+ def params
52
+ default_params.merge @params
53
+ end
54
+
55
+ # Updates the AWS parameters.
56
+ #
57
+ # hash - A Hash.
58
+ #
59
+ # Returns nothing.
60
+ def update(hash)
61
+ hash.each do |key, val|
62
+ # Syntactic sugar: Camelize symbol keys.
63
+ if key.is_a? Symbol
64
+ key = key.to_s.split('_').map(&:capitalize).join
65
+ end
66
+
67
+ @params[key] = val
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def percent_encode(value)
74
+ value.to_s.gsub(/([^\w.~-]+)/) do
75
+ '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
76
+ end
77
+ end
78
+
79
+ def default_params
80
+ {
81
+ 'AWSAccessKeyId' => @key,
82
+ 'SignatureVersion' => '2',
83
+ 'SignatureMethod' => 'HmacSHA256',
84
+ 'Timestamp' => Time.now.utc.iso8601
85
+ }
86
+ end
87
+
88
+ def sign(message)
89
+ digest = OpenSSL::HMAC.digest SHA256, @secret, message
90
+ Base64.encode64(digest).chomp
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ module AWS
2
+ class URL
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module AWS
4
+ describe URL do
5
+
6
+ before do
7
+ @base_url = 'http://example.com/path'
8
+ @url = URL.new @base_url, 'key', 'secret'
9
+ end
10
+
11
+ describe '#build' do
12
+ subject { @url.build :get }
13
+
14
+ it 'includes the base URL' do
15
+ should { include @base_url }
16
+ end
17
+
18
+ it 'is signed' do
19
+ should { match /Signature=[^&]+$/ }
20
+ end
21
+ end
22
+
23
+ describe '#params' do
24
+ subject { @url.params }
25
+
26
+ it 'includes a key' do
27
+ should { include 'AWSAccessKeyId' }
28
+ end
29
+
30
+ it 'includes a signature version' do
31
+ should { include 'SignatureVersion' }
32
+ end
33
+
34
+ it 'includes a signature method' do
35
+ should { include 'SignatureMethod' }
36
+ end
37
+
38
+ it 'includes a timestamp' do
39
+ should { include 'Timestamp' }
40
+ end
41
+ end
42
+
43
+ describe '#update' do
44
+ it 'updates the parameters' do
45
+ @url.update 'Foo' => 'bar'
46
+ @url.params.should include 'Foo'
47
+ end
48
+
49
+ it 'camelizes symbol keys' do
50
+ @url.update :foo => 'bar'
51
+ @url.params.should include 'Foo'
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,9 @@
1
+ # Test where the risk is.
2
+
3
+ require 'rspec'
4
+ begin
5
+ require 'pry'
6
+ rescue LoadError
7
+ end
8
+
9
+ require 'aws-url'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hakan Ensari
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70355690895500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.9'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70355690895500
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70355690894780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.9'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70355690894780
36
+ description: A minimum-viable URL builder for Amazon Web Services (AWS)
37
+ email:
38
+ - hakan.ensari@papercavalier.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .travis.yml
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - aws-url.gemspec
50
+ - lib/aws-url.rb
51
+ - lib/aws/url.rb
52
+ - lib/aws/url/version.rb
53
+ - spec/aws/url_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: https://github.com/hakanensari/aws-url
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.11
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Builds an Amazon Web Services URL
79
+ test_files:
80
+ - spec/aws/url_spec.rb
81
+ - spec/spec_helper.rb
82
+ has_rdoc: