sgdoc 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2a488f37d0d4b8921d508b17f273dbf423d527bf
4
+ data.tar.gz: 2a01b6e054105d90ebc8291fe1a6d018d2252598
5
+ SHA512:
6
+ metadata.gz: 48b43aba768af54d2259d23174d2a1d2fe230202bb607077cdf60f0d7218f5321daa9ea6f4ce2013dadae841e57ca38a8e661f791870daa0f1ccc4f4f6313744
7
+ data.tar.gz: f8a314a60fc2a564ea08059faa525d0a513fd064883df7ea45591d49be22d5244fde22df06998e27ea51b5117d469565d92b0c5be949e86a346593421cd0504e
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ tags
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sgdoc.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Yuji Takaesu
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.
@@ -0,0 +1,33 @@
1
+ # Sgdoc
2
+
3
+ AWSのセキュリティグループをいい感じでドキュメント化してテキスト出力する
4
+ デフォルト対応はマークダウンのみ
5
+ ipv6関連は未対応
6
+ SGを参照しているサービスはEC2,RDS,ELBを対象とする(その他は適宜追加)
7
+
8
+ ## 有効な場面
9
+
10
+ - セキュリティグループは結構変えることが多い(それにドキュメントをつい付いしていくのが大変なので自動化できると楽)
11
+
12
+ ## やりたいこと
13
+
14
+ - [ ] aws-sdkで必要な情報を取得してハッシュ化
15
+ - [ ] Erubisに渡してerbテンプレートで展開する
16
+ - ドキュメントにはSecurityGroupの内容
17
+ - 関連付けの情報(EC2, ELB, EC2)
18
+ - [ ] SecurityGroupのIn,Outのポリシーにコメントを入れれるようにしたい(別ファイルでポートのコメント管理。定型文はYamlで管理(0.0.0.0/0など))
19
+
20
+
21
+ ### Credentialは次の順番で有効になる
22
+ - (これは設定しないのでいらないかな) `:access_key_id` , `:secret_access_key` , and `:session_token` options
23
+ - `ENV['AWS_ACCESS_KEY_ID']`, `ENV['AWS_SECRET_ACCESS_KEY']`
24
+ - HOME/.aws/credentials shared credentials file
25
+ - EC2 instance profile credentials See Plugins::RequestSigner for more details.
26
+
27
+ ## Requirements
28
+
29
+ Ruby 2.1 or higher
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,41 @@
1
+ # Sgdoc
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/sgdoc`. 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 'sgdoc'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sgdoc
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]/sgdoc.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sgdoc"
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__)
@@ -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,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sgdoc'
4
+
5
+ Sgdoc::Cli.run(ARGV)
@@ -0,0 +1,11 @@
1
+ require 'aws-sdk'
2
+ require 'erubis'
3
+ require 'delegate'
4
+
5
+ require 'sgdoc/version'
6
+ require 'sgdoc/cli'
7
+ require 'sgdoc/client'
8
+ require 'sgdoc/ec2/instance'
9
+ require 'sgdoc/ec2/security_group'
10
+ require 'sgdoc/ec2/ip_permission'
11
+ require 'sgdoc/ec2/ip_permission_collection'
@@ -0,0 +1,15 @@
1
+ module Sgdoc
2
+ module Cli
3
+ def self.run(argv)
4
+ # VPC Information vpc_id, cidr, name
5
+
6
+ client = Client.new
7
+ template_path = File.expand_path('../templates/security_groups.md.erb', __FILE__)
8
+
9
+ result = Erubis::Eruby.new(File.read(template_path))
10
+ .result(security_groups_by_vpc_id: client.security_groups_by_vpc_id, instances_by_vpc_id: client.instances_by_vpc_id)
11
+
12
+ puts result
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module Sgdoc
2
+ # TODO: VPCから順に構造体にしていく方法にした方がいい
3
+ class Client
4
+ attr_reader :security_groups
5
+
6
+ def initialize(ec2_client: nil, ec2_resource: nil)
7
+ @ec2_client = ec2_client || Aws::EC2::Client.new
8
+ @ec2_resource = ec2_resource || Aws::EC2::Resource.new(client: @ec2_client)
9
+ end
10
+
11
+ def security_groups_by_vpc_id
12
+ describe_security_groups = @ec2_client.describe_security_groups.security_groups
13
+ security_groups ||= describe_security_groups.map { |sg| Sgdoc::EC2::SecurityGroup.new(sg) }
14
+
15
+ # vpcIDのキーをvpcのオブジェクトにしても良い
16
+ security_groups.group_by { |sg| sg.vpc_id }
17
+ end
18
+
19
+ def instances_by_vpc_id
20
+ instances = @ec2_resource.instances.map { |i| Sgdoc::EC2::Instance.new(i) }
21
+ instances.group_by { |i| i.vpc_id }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module Sgdoc
2
+ module EC2
3
+ class Instance < SimpleDelegator
4
+ def initialize(aws_instance)
5
+ super(aws_instance)
6
+ end
7
+
8
+ def name
9
+ tags.detect { |t| t.key == 'Name' }&.value
10
+ end
11
+
12
+ def group_names_to_s
13
+ security_groups.map(&:group_name).join(', ')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ module Sgdoc
2
+ module EC2
3
+ class IpPermission < SimpleDelegator
4
+ ALL_STRING = 'all'
5
+
6
+ def initialize(ip_permission:, ip_range: nil, user_id_group_pair: nil)
7
+ raise StandardError 'Unknown pattern of IpPermission, at least must need ip_range or user_id_group_pair' if ip_range.nil? && user_id_group_pair.nil?
8
+
9
+ @ip_range = ip_range
10
+ @user_id_group_pair = user_id_group_pair
11
+ super(ip_permission)
12
+ end
13
+
14
+ # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Types/IpPermission.html#ip_protocol-instance_method
15
+ def protocol
16
+ ip_protocol == '-1' ? ALL_STRING : ip_protocol
17
+ end
18
+
19
+ # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Types/IpPermission.html#from_port-instance_method
20
+ # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Types/IpPermission.html#to_port-instance_method
21
+ def port
22
+ if protocol == ALL_STRING
23
+ ALL_STRING
24
+ elsif from_port == -1 && to_port == -1
25
+ 'ICMP ALL'
26
+ elsif from_port == -1 || to_port == -1
27
+ 'CUSTOM ICMP'
28
+ elsif from_port == to_port
29
+ from_port
30
+ else
31
+ "#{from_port} - #{to_port}"
32
+ end
33
+ end
34
+
35
+ def target
36
+ @ip_range ? @ip_range.cidr_ip : @user_id_group_pair.group_id
37
+ end
38
+
39
+ def doc_description
40
+ # TODO desriptionを出力する使用を考える(IPベースの設定ファイルでいいかも)
41
+ '-'
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ module Sgdoc
2
+ module EC2
3
+ class IpPermissionCollection
4
+ include Enumerable
5
+
6
+ def initialize(ip_permissions)
7
+ # ip_ranges, user_id_group_pairs(security_group) がそれぞれ Source, Destination として定義される
8
+ # ip_rages, user_id_group_pairsはそれぞれ配列で複数ある場合がある
9
+ @ip_permissions = ip_permissions.each_with_object([]) do |ip_permission, memo|
10
+ ip_permission.ip_ranges.each do |ip_range|
11
+ memo << IpPermission.new(ip_permission: ip_permission, ip_range: ip_range)
12
+ end
13
+
14
+ ip_permission.user_id_group_pairs.each do |user_id_group_pair|
15
+ memo << IpPermission.new(ip_permission: ip_permission, user_id_group_pair: user_id_group_pair)
16
+ end
17
+ end
18
+ end
19
+
20
+ def each
21
+ @ip_permissions.each do |ip_permission|
22
+ yield ip_permission
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module Sgdoc
2
+ module EC2
3
+ class SecurityGroup < SimpleDelegator
4
+ def initialize(aws_security_group)
5
+ super(aws_security_group)
6
+ end
7
+
8
+ def inbound_permissions
9
+ IpPermissionCollection.new(ip_permissions)
10
+ end
11
+
12
+ def outbound_permissions
13
+ IpPermissionCollection.new(ip_permissions_egress)
14
+ end
15
+
16
+ def tags_to_json
17
+ return '-' if tags.empty?
18
+
19
+ tags.each_with_object({}) do |tag, memo|
20
+ memo[tag.key] = tag.value
21
+ end.to_json
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ <% security_groups_by_vpc_id.each do |vpc_id, security_groups| %>
2
+ # Security Groups (<%= vpc_id %>)
3
+
4
+ <% security_groups.each do |sg| %>
5
+ ## <%= sg.group_name %>
6
+
7
+ | Group ID | Description | Tags |
8
+ | ---- | ---- | ---- |
9
+ | <%= sg.group_id %> | <%= sg.description %> | <%= sg.tags_to_json %> |
10
+
11
+ ### Inbound
12
+
13
+ | Protocol | Port | Source | _Description |
14
+ | ---- | ---- | ---- | ---- |
15
+ <% if sg.inbound_permissions.each do |iperm| %>
16
+ | <%= iperm.protocol %> | <%= iperm.port %> | <%= iperm.target %> | <%= iperm.doc_description %> |
17
+ <% end.empty? %>
18
+ | | | | |
19
+ <% end %>
20
+
21
+ ### Outbound
22
+
23
+ | Protocol | Port | Destination | _Description |
24
+ | ---- | ---- | ---- | ---- |
25
+ <% if sg.outbound_permissions.each do |operm| %>
26
+ | <%= operm.protocol %> | <%= operm.port %> | <%= operm.target %> | <%= operm.doc_description %> |
27
+ <% end.empty? %>
28
+ | | | | |
29
+ <% end %>
30
+
31
+ <% end %>
32
+ ## Instances references
33
+
34
+ <% if instances = instances_by_vpc_id[vpc_id] %>
35
+ | Instance Name | Instance ID | Security Groups |
36
+ | ---- | ---- | ---- |
37
+ <% instances.each do |instance| %>
38
+ | <%= instance.name %> | <%= instance.instance_id %> | <%= instance.group_names_to_s %> |
39
+ <% end %>
40
+ <% else %>
41
+ | | | |
42
+ <% end %>
43
+
44
+ <% end %>
@@ -0,0 +1,3 @@
1
+ module Sgdoc
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sgdoc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sgdoc'
8
+ spec.version = Sgdoc::VERSION
9
+ spec.authors = ['Yuji Takaesu']
10
+ spec.email = ['taka.esu.user@gmail.com']
11
+
12
+ spec.summary = 'AWS SecurityGroup docs generator'
13
+ spec.description = 'Abstrat tools for AWS SecurityGroup docs'
14
+ spec.homepage = 'https://github.com/yusabana/sgdoc'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'aws-sdk', '~> 2'
25
+ spec.add_dependency 'erubis'
26
+ spec.add_development_dependency 'bundler', '~> 1.14'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'tapp-awesome_print'
30
+ spec.add_development_dependency 'pry-byebug'
31
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sgdoc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuji Takaesu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-08 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: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: tapp-awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Abstrat tools for AWS SecurityGroup docs
112
+ email:
113
+ - taka.esu.user@gmail.com
114
+ executables:
115
+ - sgdoc
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.ja.md
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - exe/sgdoc
130
+ - lib/sgdoc.rb
131
+ - lib/sgdoc/cli.rb
132
+ - lib/sgdoc/client.rb
133
+ - lib/sgdoc/ec2/instance.rb
134
+ - lib/sgdoc/ec2/ip_permission.rb
135
+ - lib/sgdoc/ec2/ip_permission_collection.rb
136
+ - lib/sgdoc/ec2/security_group.rb
137
+ - lib/sgdoc/templates/security_groups.md.erb
138
+ - lib/sgdoc/version.rb
139
+ - sgdoc.gemspec
140
+ homepage: https://github.com/yusabana/sgdoc
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.6.11
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: AWS SecurityGroup docs generator
164
+ test_files: []