barnyard_aws 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ *.idea
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
6
+ 1-BarnyardHarvester::*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p125@barnyard
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_auditor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2012 Jon Gillies
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.
23
+
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # BarnyardAws
2
+
3
+ This gem provides access to the following Amazon AWS objects:
4
+
5
+ * BarnyardAws::AwsElbs
6
+ * BarnyardAws::AwsSecurityGroups
7
+ * BarnyardAws::AwsInstances
8
+ * BarnyardAws::AwsSnapshots
9
+ * BarnyardAws::AwsVolumes
10
+ * BarnyardAws::AwsSubnets
11
+ * BarnyardAws::AwsIamUsers
12
+ * BarnyardAws::AwsIamGroupPolicies
13
+
14
+ This gem requires the "barnyard_havester" gem.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'barnyard_aws'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install barnyard_aws
29
+
30
+ ## Usage
31
+
32
+ Using this gem is trival. It will iterate the AWS objects and send them to the BarnyardHarvester.
33
+
34
+ WARNING! Do not use crop number 1-9 in production, they are reserved for system testing.
35
+
36
+ require "barnyard_aws"
37
+
38
+ my_logger = Logger.new(STDOUT)
39
+ my_logger.level = Logger::INFO
40
+
41
+ redis_settings = {
42
+ :host => "localhost",
43
+ :port => 6379,
44
+ }
45
+
46
+ BarnyardAws::AwsElbs.new(aws_access_key_id: AWS_ACCESS_KEY_ID,
47
+ aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
48
+ region: "us-west-1",
49
+ account_id: "dev",
50
+ crop_number: 1,
51
+ logger: my_logger,
52
+ redis_settings: redis_settings,
53
+ debug: true)
54
+
55
+ A note about primary keys using BarnyardAws. The "account_id" is added befor the AWS ObjectID separated by a dash. It is imparative that you use unique "account_ids" across different aws accounts. The account_id can be any string and is not tied to the AWS API. It is merly for reference.
56
+
57
+ ## Testing
58
+
59
+ RSPEC is used to test this gem. Note that you should run each spec test individually. For some reason they confilict if run together. You will need Redis installed on the localhost:6379 or adjust the test accordingly.
60
+
61
+ This tests all of the AWS supported objects:
62
+
63
+ rspec spec/aws_spec.rb
64
+
65
+ Note that this test requires the AWS credentails be set in the environment:
66
+
67
+ AWS_ACCESS_KEY_ID="****"
68
+ AWS_SECRET_ACCESS_KEY="****"
69
+
70
+ This test will use the crop_number 1 and will iterate all supported AWS objects.
71
+
72
+ ## Questions or Problems?
73
+
74
+ supercoder@gmail.com
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "barnyard_aws/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "barnyard_aws"
7
+ gem.version = BarnyardAws::VERSION
8
+ gem.authors = ["Jon Gillies"]
9
+ gem.email = %w(supercoder@gmail.com)
10
+ gem.description = %q{This gem provides access to the Amazon AWS objects for use with BarnyardHarvester.}
11
+ gem.summary = %q{Please check the README.md for more information.}
12
+ gem.homepage = "https://github.com/jongillies/barnyard/tree/master/barnyard_aws"
13
+
14
+ gem.rubyforge_project = "barnyard_aws"
15
+
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ gem.require_paths = %w(lib)
20
+
21
+ # specify any dependencies here; for example:
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_runtime_dependency "barnyard_harvester"
24
+ gem.add_runtime_dependency "fog"
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ module BarnyardAws
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,252 @@
1
+ require "fog"
2
+ require "barnyard_harvester"
3
+ require "json"
4
+
5
+ require "barnyard_aws/version"
6
+
7
+ module BarnyardAws
8
+
9
+ class AwsObject
10
+
11
+ attr_reader :synchronizer, :began_at, :ended_at
12
+
13
+ def make_primary_key(account, key)
14
+ "#{account}-#{key}"
15
+ end
16
+
17
+ def initialize(args={})
18
+
19
+ @began_at = Time.now
20
+
21
+ @debug = args.fetch(:debug) { false }
22
+ @log = args.fetch(:logger) { Logger.new(STDOUT) }
23
+
24
+ @aws_access_key_id = args.fetch(:aws_access_key_id) { raise "You must provide :aws_access_key_id" }
25
+ @aws_secret_access_key = args.fetch(:aws_secret_access_key) { raise "You must provide :aws_secret_access_key" }
26
+ @region = args.fetch(:region) { raise "You must provide :region" }
27
+ @account_id = args.fetch(:account_id) { raise "You must provide :account_id" }
28
+
29
+ @log.debug JSON.pretty_generate(args)
30
+ @log.debug "region=#{@region}"
31
+
32
+ @synchronizer = BarnyardHarvester::Sync.new(args)
33
+
34
+ @synchronizer.run do
35
+
36
+ case self.class.to_s
37
+ when "BarnyardAws::AwsElbs"
38
+ compute = Fog::AWS::ELB.new({
39
+ :aws_access_key_id => @aws_access_key_id,
40
+ :aws_secret_access_key => @aws_secret_access_key,
41
+ :region => @region
42
+ })
43
+ when "BarnyardAws::AwsIamUsers",
44
+ "BarnyardAws::AwsIamGroupPolicies"
45
+ compute = Fog::AWS::IAM.new({
46
+ :aws_access_key_id => @aws_access_key_id,
47
+ :aws_secret_access_key => @aws_secret_access_key,
48
+ #:region => @region
49
+ })
50
+ else
51
+ compute = Fog::Compute.new({
52
+ :provider => 'AWS',
53
+ :aws_access_key_id => @aws_access_key_id,
54
+ :aws_secret_access_key => @aws_secret_access_key,
55
+ :region => @region
56
+ })
57
+ end
58
+
59
+ case self.class.to_s
60
+ when "BarnyardAws::AwsSecurityGroups"
61
+ compute.security_groups.each do |security_group|
62
+ unless security_group.group_id.nil?
63
+ obj = Crack::JSON.parse security_group.to_json
64
+ obj["account_id"] = @account_id
65
+
66
+ primary_key = make_primary_key(@account_id, security_group.group_id)
67
+ @log.info "#{primary_key}"
68
+
69
+ @synchronizer.process primary_key, obj
70
+
71
+ end
72
+ end
73
+
74
+ when "BarnyardAws::AwsInstances"
75
+ compute.servers.each do |s|
76
+ unless s.id.nil?
77
+ obj = Crack::JSON.parse s.to_json
78
+ obj["account_id"] = @account_id
79
+
80
+ primary_key = make_primary_key(@account_id, s.id)
81
+ @log.info "#{primary_key}"
82
+
83
+ @synchronizer.process primary_key, obj
84
+
85
+ end
86
+ end
87
+
88
+ when "BarnyardAws::AwsSnapshots"
89
+ compute.snapshots.each do |snapshot|
90
+ unless snapshot.id.nil?
91
+ obj = Crack::JSON.parse snapshot.to_json
92
+ obj["account_id"] = @account_id
93
+
94
+ primary_key = make_primary_key(@account_id, snapshot.id)
95
+ @log.info "#{primary_key}"
96
+
97
+ @synchronizer.process primary_key, obj
98
+
99
+ end
100
+ end
101
+
102
+ when "BarnyardAws::AwsVolumes"
103
+ compute.volumes.each do |s|
104
+ unless s.id.nil?
105
+ obj = Crack::JSON.parse s.to_json
106
+ obj["account_id"] = @account_id
107
+
108
+ primary_key = make_primary_key(@account_id, s.id)
109
+ @log.info "#{primary_key}"
110
+
111
+ @synchronizer.process primary_key, obj
112
+
113
+ end
114
+ end
115
+
116
+ when "BarnyardAws::AwsSubnets"
117
+ compute.subnets.each do |s|
118
+ unless s.subnet_id.nil?
119
+ obj = Crack::JSON.parse s.to_json
120
+ obj["account_id"] = @account_id
121
+
122
+ primary_key = make_primary_key(@account_id, s.subnet_id)
123
+ @log.info "#{primary_key}"
124
+
125
+ @synchronizer.process primary_key, obj
126
+
127
+ end
128
+ end
129
+
130
+ when "BarnyardAws::AwsElbs"
131
+ compute.load_balancers.each do |elb|
132
+ unless elb.dns_name.nil?
133
+ obj = Crack::JSON.parse elb.to_json
134
+ obj["account_id"] = @account_id
135
+
136
+ primary_key = make_primary_key(@account_id, elb.dns_name)
137
+ @log.info "#{primary_key}"
138
+
139
+ @synchronizer.process primary_key, obj
140
+
141
+ end
142
+ end
143
+
144
+ when "BarnyardAws::AwsIamUsers"
145
+ compute.list_users.body["Users"].each do |user|
146
+ unless user['UserId'].nil?
147
+ access_keys = compute.list_access_keys({'UserName' => user['UserName']}).body["AccessKeys"]
148
+ obj = Crack::JSON.parse user.to_json
149
+ obj["account_id"] = @account_id
150
+ obj["access_keys"] = access_keys
151
+
152
+ primary_key = make_primary_key(@account_id, user['UserId'])
153
+ @log.info "#{primary_key}"
154
+
155
+ @synchronizer.process primary_key, obj
156
+
157
+ end
158
+ end
159
+
160
+ when "BarnyardAws::AwsIamGroupPolicies"
161
+
162
+ obj = Hash.new
163
+ obj["account_id"] = @account_id
164
+
165
+ compute.list_groups.body["Groups"].each do |group|
166
+ obj = group
167
+ obj["account_id"] = @account_id
168
+ obj["policies"] = Array.new
169
+
170
+ compute.list_group_policies(group["GroupName"]).body["PolicyNames"].each do |policy|
171
+
172
+ begin
173
+ policy_obj = Crack::JSON.parse CGI::unescape(compute.get_group_policy(policy, group["GroupName"]).body["PolicyDocument"])
174
+
175
+ obj["policies"] << policy_obj
176
+
177
+ rescue Exception => e
178
+ @log.warn "Unable to Crack policy >#{compute.get_group_policy(policy, group["GroupName"]).body["PolicyDocument"]}<"
179
+ end
180
+
181
+ end
182
+
183
+ primary_key = make_primary_key(@account_id, group["GroupId"])
184
+ @log.info "#{primary_key}"
185
+
186
+ @synchronizer.process primary_key, obj
187
+
188
+ end
189
+
190
+ end
191
+
192
+
193
+ end
194
+
195
+ @log.info @synchronizer.stats
196
+
197
+ end
198
+
199
+ @ended_at = Time.now
200
+
201
+ end
202
+
203
+ class AwsIamGroupPolicies < AwsObject
204
+ def initialize args
205
+ super args
206
+ end
207
+ end
208
+
209
+ class AwsIamUsers < AwsObject
210
+ def initialize args
211
+ super args
212
+ end
213
+ end
214
+
215
+ class AwsElbs < AwsObject
216
+ def initialize args
217
+ super args
218
+ end
219
+ end
220
+ class AwsSnapshots < AwsObject
221
+ def initialize args
222
+ super args
223
+ end
224
+ end
225
+
226
+ class AwsVolumes < AwsObject
227
+ def initialize args
228
+ super args
229
+ end
230
+ end
231
+
232
+ class AwsSubnets < AwsObject
233
+ def initialize args
234
+ super args
235
+ end
236
+ end
237
+
238
+ class AwsSecurityGroups < AwsObject
239
+ def initialize args
240
+ super args
241
+ end
242
+ end
243
+
244
+ class AwsInstances < AwsObject
245
+ def initialize args
246
+ super args
247
+ end
248
+ end
249
+
250
+
251
+ end
252
+
data/spec/aws_spec.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "barnyard_aws"
2
+ require "logger"
3
+
4
+ describe BarnyardAws do
5
+
6
+ AWS_SECRET_ACCESS_KEY=ENV["AWS_SECRET_ACCESS_KEY"]
7
+ AWS_ACCESS_KEY_ID=ENV["AWS_ACCESS_KEY_ID"]
8
+
9
+ it "all should work" do
10
+
11
+ my_logger = Logger.new(STDOUT)
12
+ my_logger.level = Logger::INFO
13
+
14
+ redis_settings = {
15
+ :host => "localhost",
16
+ :port => 6379,
17
+ }
18
+
19
+ [BarnyardAws::AwsElbs, BarnyardAws::AwsSecurityGroups,
20
+ BarnyardAws::AwsInstances, BarnyardAws::AwsSnapshots,
21
+ BarnyardAws::AwsVolumes, BarnyardAws::AwsSubnets,
22
+ BarnyardAws::AwsIamUsers, BarnyardAws::AwsIamGroupPolicies].each do |o|
23
+
24
+ o.new(aws_access_key_id: AWS_ACCESS_KEY_ID,
25
+ aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
26
+ region: "us-west-1",
27
+ account_id: "dev",
28
+ crop_number: 1,
29
+ logger: my_logger,
30
+ redis_settings: redis_settings,
31
+ debug: true)
32
+
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barnyard_aws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jon Gillies
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70304793803660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70304793803660
25
+ - !ruby/object:Gem::Dependency
26
+ name: barnyard_harvester
27
+ requirement: &70304793802920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70304793802920
36
+ - !ruby/object:Gem::Dependency
37
+ name: fog
38
+ requirement: &70304793802120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70304793802120
47
+ description: This gem provides access to the Amazon AWS objects for use with BarnyardHarvester.
48
+ email:
49
+ - supercoder@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .DS_Store
55
+ - .gitignore
56
+ - .rspec
57
+ - .rvmrc
58
+ - Gemfile
59
+ - LICENSE.txt
60
+ - README.md
61
+ - Rakefile
62
+ - barnyard_aws.gemspec
63
+ - lib/barnyard_aws.rb
64
+ - lib/barnyard_aws/version.rb
65
+ - spec/aws_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: https://github.com/jongillies/barnyard/tree/master/barnyard_aws
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project: barnyard_aws
87
+ rubygems_version: 1.8.15
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Please check the README.md for more information.
91
+ test_files:
92
+ - spec/aws_spec.rb
93
+ - spec/spec_helper.rb