aws-spec-generator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Bradley Atkins
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,43 @@
1
+ # Aws::Spec::Generator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/aws/spec/generator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'aws-spec-generator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install aws-spec-generator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/aws-spec-generator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Aws::Spec::Generator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/aws-spec-generator/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "aws/spec/generator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws-spec-generator"
8
+ spec.version = Aws::Spec::Generator::VERSION
9
+ spec.authors = ["Bradley Atkins"]
10
+ spec.email = ["Bradley.Atkins@bjss.com"]
11
+
12
+ spec.summary = 'Wrapper for awspec generate'
13
+ spec.description = 'Wrapper for awspec generate'
14
+ spec.homepage = 'https://github.com/museadmin/aws-spec-generator'
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = 'https://github.com/museadmin/aws-spec-generator'
22
+ spec.metadata["changelog_uri"] = 'https://github.com/museadmin/aws-spec-generator/blob/master/CHANGELOG.md'
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ['lib']
36
+
37
+ spec.add_development_dependency 'aws_su', '>= 0.1.6'
38
+ spec.add_development_dependency 'awsecrets', '~> 1.14.0'
39
+ spec.add_development_dependency 'awspec'
40
+ spec.add_development_dependency 'bundler', '~> 1.17'
41
+ spec.add_development_dependency 'rake', '~> 10.0'
42
+ spec.add_development_dependency 'rspec', '~> 3.0'
43
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aws/spec/generator"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require "aws/spec/generator/version"
2
+
3
+ module Aws
4
+ module Spec
5
+ module Generator
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Aws
2
+ module Spec
3
+ module Generator
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'awspec'
4
+ require 'fileutils'
5
+ require 'json'
6
+ require 'yaml'
7
+
8
+ # Parent class for individual awspec tests generators
9
+ class AwsSpecGenerator
10
+
11
+ def initialize(options = {})
12
+ ENV['AWS_DEFAULT_REGION'] = 'eu-west-2' # TODO remove when upgrading to 0.1.6 of auth
13
+ ENV.delete('AWS_PROFILE') # TODO remove when upgrading to 0.1.6 of auth
14
+ @vpc_list = []
15
+ @bucket_list = []
16
+ @output_directory = options[:output_directory]
17
+ if @output_directory.nil?
18
+ raise(
19
+ 'Output dir expected by AwsSpecGenerator.new(output_directory: dir)'
20
+ )
21
+ end
22
+ FileUtils.mkdir_p @output_directory
23
+ @output_directory += File::SEPARATOR
24
+ query_vpc_ids
25
+ query_bucket_list
26
+ end
27
+
28
+ # Generate tests for all accounts
29
+ def generate_all(account)
30
+ generate_ec2_tests(account)
31
+ generate_sg_tests(account)
32
+ generate_s3_tests(account)
33
+ generate_nacl_tests(account)
34
+ generate_elb_tests(account)
35
+ end
36
+
37
+ # Generate the EC2 tests
38
+ def generate_ec2_tests(account)
39
+ @vpc_list.each do |vpc|
40
+ target_file = File.absolute_path(
41
+ @output_directory + "ec2_on_#{vpc}_tests_spec.rb"
42
+ )
43
+ File.open(target_file, 'w') do |f|
44
+ f.write("require_relative '../spec_helper'\n\ncontext '#{vpc} tests', #{account}: true do\n\n")
45
+ end
46
+
47
+ begin
48
+ stderr = Open3.capture3(
49
+ "awspec generate ec2 #{vpc} >> #{target_file}"
50
+ )
51
+ rescue StandardError
52
+ raise 'Failed to generate ec2 tests (' + stderr + ')'
53
+ end
54
+
55
+ File.open(target_file, 'a+') { |f|f.write("end\n") }
56
+ end
57
+ end
58
+
59
+ # Generate the ELB Tests
60
+ def generate_elb_tests(account)
61
+ @vpc_list.each do |vpc|
62
+ target_file = File.absolute_path(
63
+ @output_directory + "elbs_on_#{vpc}_tests_spec.rb"
64
+ )
65
+ File.open(target_file, 'w') do |f|
66
+ f.write("require_relative '../spec_helper'\n\ncontext 'ELBs on"\
67
+ " #{vpc} tests', #{account}: true do\n\n")
68
+ end
69
+
70
+ begin
71
+ stderr = Open3.capture3(
72
+ "awspec generate elb #{vpc} >> #{target_file}"
73
+ )
74
+ rescue StandardError
75
+ raise 'Failed to generate elb tests (' + stderr + ')'
76
+ end
77
+
78
+ File.open(target_file, 'a+') { |f|f.write("end\n") }
79
+ end
80
+ end
81
+
82
+ # Generate the SG tests
83
+ def generate_sg_tests(account)
84
+ @vpc_list.each do |vpc|
85
+ target_file = File.absolute_path(
86
+ @output_directory + "security_groups_on_#{vpc}_tests_spec.rb"
87
+ )
88
+ File.open(target_file, 'w') do |f|
89
+ f.write("require_relative '../spec_helper'\n\ncontext 'Security Groups on"\
90
+ " #{vpc} tests', #{account}: true do\n\n")
91
+ end
92
+
93
+ begin
94
+ stderr = Open3.capture3(
95
+ "awspec generate security_group #{vpc} >> #{target_file}"
96
+ )
97
+ rescue StandardError
98
+ raise 'Failed to generate security_group tests (' + stderr + ')'
99
+ end
100
+
101
+ system("awspec generate security_group #{vpc} >> #{target_file}")
102
+ File.open(target_file, 'a+') { |f|f.write("end\n") }
103
+ end
104
+ end
105
+
106
+ # Generate S3 Bucket tests
107
+ def generate_s3_tests(account)
108
+
109
+ @bucket_list.each do |bucket|
110
+ target_file = File.absolute_path(@output_directory +
111
+ "s3_buckets_on_#{bucket['Name']}_tests_spec.rb")
112
+
113
+ File.open(target_file, 'w') do |f|
114
+ f.write(
115
+ "require_relative '../spec_helper'\n\ncontext 'S3 buckets on"\
116
+ " #{bucket['Name']} tests', #{account}: true do\n\n"
117
+ )
118
+ end
119
+
120
+ begin
121
+ stderr = Open3.capture3(
122
+ "awspec generate s3 #{bucket['Name']} >> #{target_file}"
123
+ )
124
+ rescue StandardError
125
+ raise 'Failed to generate bucket tests (' + stderr + ')'
126
+ end
127
+
128
+ File.open(target_file, 'a+') { |f|f.write("end\n") }
129
+ end
130
+ end
131
+
132
+ # Generate NACL tests
133
+ def generate_nacl_tests(account)
134
+ @vpc_list.each do |vpc|
135
+ target_file = File.absolute_path(
136
+ @output_directory + "nacl_buckets_on_#{vpc}_tests_spec.rb"
137
+ )
138
+ File.open(target_file, 'w') do |f|
139
+ f.write(
140
+ "require_relative '../spec_helper'\n\ncontext 'NACL "\
141
+ "on #{vpc} tests', #{account}: true do\n\n"
142
+ )
143
+ end
144
+
145
+ begin
146
+ stderr = Open3.capture3(
147
+ "awspec generate network_acl #{vpc} >> #{target_file}"
148
+ )
149
+ rescue StandardError
150
+ raise 'Failed to generate nacl tests (' + stderr + ')'
151
+ end
152
+
153
+ File.open(target_file, 'a+') { |f|f.write("end\n") }
154
+ end
155
+ end
156
+
157
+ # retrieve the VPC names for this account
158
+ def query_vpc_ids
159
+ begin
160
+ stdout, stderr, status = Open3.capture3('aws ec2 describe-vpcs')
161
+ rescue StandardError
162
+ raise('Error: ' + status + 'Failed to recover vpc list: (' + stderr + ')')
163
+ end
164
+
165
+ JSON.parse(stdout)['Vpcs'].each do |vpc|
166
+ @vpc_list.push(vpc['VpcId'])
167
+ end
168
+ @vpc_list.uniq!
169
+ end
170
+
171
+ # Get the list of s3 bucket names
172
+ def query_bucket_list
173
+ begin
174
+ stdout, stderr, status = Open3.capture3('aws s3api list-buckets')
175
+ rescue StandardError
176
+ raise('Error: ' + status + 'Failed to recover vpc list: (' + stderr + ')')
177
+ end
178
+
179
+ JSON.parse(stdout)['Buckets'].each do |bucket|
180
+ @bucket_list.push(bucket)
181
+ end
182
+ @bucket_list.uniq!
183
+ end
184
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-spec-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bradley Atkins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws_su
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.6
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: awsecrets
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.14.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: awspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Wrapper for awspec generate
98
+ email:
99
+ - Bradley.Atkins@bjss.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CHANGELOG.md
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - aws-spec-generator.gemspec
115
+ - bin/console
116
+ - bin/setup
117
+ - lib/aws/spec/generator.rb
118
+ - lib/aws/spec/generator/version.rb
119
+ - lib/aws_spec_generator.rb
120
+ homepage: https://github.com/museadmin/aws-spec-generator
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ homepage_uri: https://github.com/museadmin/aws-spec-generator
125
+ source_code_uri: https://github.com/museadmin/aws-spec-generator
126
+ changelog_uri: https://github.com/museadmin/aws-spec-generator/blob/master/CHANGELOG.md
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.7.8
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Wrapper for awspec generate
147
+ test_files: []