s3_with_iam 0.0.1.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,19 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ aws-sdk (1.42.0)
5
+ json (~> 1.4)
6
+ nokogiri (>= 1.4.4)
7
+ json (1.8.1)
8
+ mini_portile (0.6.0)
9
+ nokogiri (1.6.2.1)
10
+ mini_portile (= 0.6.0)
11
+ trollop (2.0)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ aws-sdk
18
+ json
19
+ trollop
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrew Rollins
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.
@@ -0,0 +1,38 @@
1
+ # S3 with IAM
2
+
3
+ A tool for interacting with S3 using the AWS IAM roles from the local instance (i.e., this tool is meant to be run on an EC2 instance with IAM roles).
4
+
5
+ Right now it just supports getting a file from S3. In its simpliest form, it will auto-discover the roles on the local instance and try each one until it downloads the file or runs out of roles.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 's3_with_iam'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install s3_with_iam
20
+
21
+ ## Usage
22
+
23
+ Example:
24
+
25
+ s3_with_iam --bucket example --key foo/example.txt --destination local/example.txt
26
+
27
+ * `key` is the path on S3
28
+ * `destination` is where you want to write the file locally
29
+
30
+ See more help with `s3_with_iam -h`
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 's3_with_iam'
4
+ require 'trollop'
5
+
6
+ options = Trollop::options do
7
+ version "s3_with_iam #{S3WithIam::VERSION}"
8
+ opt :bucket, "bucket", :short => '-b', :type => :string
9
+ opt :key, "S3 object key", :short => '-k', :type => :string
10
+ opt :destination, "Local file destination", :short => '-d', :type => :string
11
+ opt :region, "AWS region", :short => '-r', :type => :string, :default => 'us-east-1'
12
+ opt :roles, "Try specific roles, will auto-discover roles by default", :short => '-o', :type => :string
13
+ end
14
+
15
+ Trollop::die :bucket, "must be given" unless options[:bucket]
16
+ Trollop::die :key, "must be given" unless options[:key]
17
+ Trollop::die :destination, "must be given" unless options[:destination]
18
+
19
+ S3WithIam.get(options)
@@ -0,0 +1,86 @@
1
+ require 's3_with_iam/version'
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'aws-sdk'
6
+
7
+ module S3WithIam
8
+
9
+ CRED_ROOT_URL = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/'
10
+
11
+ class <<self
12
+
13
+ def errputs(*args)
14
+ STDERR.puts(*args)
15
+ end
16
+
17
+ def get_uri(uri)
18
+ url = URI.parse(uri)
19
+ req = Net::HTTP::Get.new(url.path)
20
+ res = Net::HTTP.start(url.host, url.port) do |http|
21
+ http.request(req)
22
+ end
23
+ res.body
24
+ end
25
+
26
+ def get_roles
27
+ begin
28
+ get_uri(CRED_ROOT_URL).
29
+ split("\n").
30
+ map{|s| s.strip}.
31
+ select{|s| s != '' && !s.nil?}
32
+ rescue
33
+ errputs "Error retreiving role names from metadata"
34
+ []
35
+ end
36
+
37
+ end
38
+
39
+ def get_creds(roles)
40
+ roles = roles.is_a?(Array) ? roles : roles.to_s.split(',')
41
+ begin
42
+ roles.inject({}) do |hash, role|
43
+ json = get_uri("#{CRED_ROOT_URL}/#{role}/")
44
+ cred =
45
+ begin
46
+ JSON.parse(json)
47
+ rescue
48
+ errputs "Did not find valid role for '#{role}'"
49
+ end
50
+ hash[role] = cred if cred
51
+ hash
52
+ end
53
+ rescue
54
+ errputs "Error retreiving credentials from IAM metadata"
55
+ {}
56
+ end
57
+ end
58
+
59
+ def get(options)
60
+ get_creds(options[:roles] || get_roles).each.find do |role, cred|
61
+ begin
62
+ puts "Trying download using role #{role}"
63
+ s3 = AWS::S3.new(
64
+ :access_key_id => cred['AccessKeyId'],
65
+ :secret_access_key => cred['SecretAccessKey'],
66
+ :session_token => cred['Token'],
67
+ :region => options[:region]
68
+ )
69
+ File.open(options[:destination], 'wb') do |file|
70
+ s3.buckets[options[:bucket]].objects[options[:key]].read do |chunk|
71
+ file.write(chunk)
72
+ end
73
+ end
74
+ puts "File downloaded to #{options[:destination]}"
75
+ true
76
+ rescue AWS::S3::Errors::AccessDenied
77
+ errputs "Access denied on S3 object for role #{role}"
78
+ false
79
+ end
80
+ end
81
+ rescue AWS::S3::Errors::NoSuchBucket
82
+ errputs "No such bucket '#{options[:bucket]}'"
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,3 @@
1
+ module S3WithIam
2
+ VERSION = "0.0.1.beta"
3
+ end
@@ -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_with_iam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ summary = %q{A tool for interacting with S3 using the AWS IAM roles from the local instance}
8
+ spec.name = "s3_with_iam"
9
+ spec.version = S3WithIam::VERSION
10
+ spec.authors = ["Andrew Rollins"]
11
+ spec.email = ["gems@andrewrollins.com"]
12
+ spec.description = summary
13
+ spec.summary = summary
14
+ spec.homepage = "https://github.com/andrew311/s3-with-iam"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+
24
+ spec.add_runtime_dependency "aws-sdk", "~> 1.42.0"
25
+ spec.add_runtime_dependency "json", "~> 1.8.1"
26
+ spec.add_runtime_dependency "trollop", "2.0"
27
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_with_iam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.beta
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Rollins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: aws-sdk
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.42.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.42.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: trollop
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: '2.0'
78
+ description: A tool for interacting with S3 using the AWS IAM roles from the local
79
+ instance
80
+ email:
81
+ - gems@andrewrollins.com
82
+ executables:
83
+ - s3_with_iam
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - bin/s3_with_iam
94
+ - lib/s3_with_iam.rb
95
+ - lib/s3_with_iam/version.rb
96
+ - s3_with_iam.gemspec
97
+ homepage: https://github.com/andrew311/s3-with-iam
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>'
114
+ - !ruby/object:Gem::Version
115
+ version: 1.3.1
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.23
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A tool for interacting with S3 using the AWS IAM roles from the local instance
122
+ test_files: []