acmaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 671733eab8da6884cd0621b3a7331d5c16e9c82b
4
+ data.tar.gz: 76f5acb657c6c994d8a1b8f1ca9d7f290d75344d
5
+ SHA512:
6
+ metadata.gz: 61098882b32b2f9cac71876107e5c433c71ae7911fdbfbc3059f89ad1f660d328f6a51f7d9826c242ca3bd0f00d5d1782f510b38cbaeced377c40a64c9aacc86
7
+ data.tar.gz: 23a2bcab5c3b5007ac78f6de871c9ddb8f0afe2486671e741e0fd9560197418d121bee9893a76de9160c4c8343452f7d004cda99ad835bee376a2b6df7ec03c9
@@ -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
+ spec/aws_config.yml
11
+ Certificatefile
12
+ *.acm
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
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.1
5
+ before_install: gem install bundler
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at horiyutwins@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acmaker.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Yuta Horii
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,179 @@
1
+ # Acmaker
2
+
3
+ Acmaker is a tool to manage [AWS Certificate Manager](https://aws.amazon.com/jp/certificate-manager/)
4
+ It defines the state of Certificate Manager using DSL, and updates Certificate Manager according to DSL.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'acmaker'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install acmaker
21
+
22
+ ## Usage
23
+
24
+ ```sh
25
+ export AWS_ACCESS_KEY_ID='...'
26
+ export AWS_SECRET_ACCESS_KEY='...'
27
+ export AWS_REGION='...'
28
+ acmaker -e -o Certificatefile # export Certificate Manager
29
+ vi Certificatefile
30
+ acmaker -a --dry-run
31
+ acmaker -a # Apply `Certificatefile` to Certificate Manager
32
+ ```
33
+
34
+ ## Help
35
+
36
+ ```
37
+ Usage: acmaker [options]
38
+ -k, --access-key ACCESS_KEY
39
+ -s, --secret-key SECRET_KEY
40
+ -r, --region REGION
41
+ --profile PROFILE
42
+ --credentials-path PATH
43
+ -a, --apply
44
+ -f, --file FILE
45
+ --dry-run
46
+ -e, --export
47
+ -o, --output FILE
48
+ --split
49
+ --target REGEXP
50
+ --no-color
51
+ --debug
52
+ --request-concurrency N
53
+ ```
54
+
55
+ ## Certificatefile example
56
+
57
+ ```ruby
58
+ require 'other/certificatefile'
59
+
60
+ domain "yutadayo.jp" do
61
+ {:certificate_arn=>
62
+ "arn:aws:acm:ap-northeast-1:XXXXXXXXXXXX:certificate/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
63
+ :created_at=>"2017-01-01 12:00:00 +0900",
64
+ :domain_name=>"yutadayo.jp",
65
+ :domain_validation_options=>
66
+ [{:domain_name=>"yutadayo.jp",
67
+ :validation_domain=>"yutadayo.jp",
68
+ :validation_emails=>
69
+ ["administrator@yutadayo.jp",
70
+ "hostmaster@yutadayo.jp",
71
+ "webmaster@yutadayo.jp",
72
+ "info@fablic.co.jp",
73
+ "postmaster@yutadayo.jp",
74
+ "admin@yutadayo.jp"],
75
+ :validation_status=>"SUCCESS"},
76
+ {:domain_name=>"*.yutadayo.jp",
77
+ :validation_domain=>"yutadayo.jp",
78
+ :validation_emails=>
79
+ ["administrator@yutadayo.jp",
80
+ "hostmaster@yutadayo.jp",
81
+ "webmaster@yutadayo.jp",
82
+ "info@fablic.co.jp",
83
+ "postmaster@yutadayo.jp",
84
+ "admin@yutadayo.jp"],
85
+ :validation_status=>"SUCCESS"}],
86
+ :failure_reason=>nil,
87
+ :imported_at=>nil,
88
+ :in_use_by=>
89
+ ["arn:aws:elasticloadbalancing:ap-northeast-1:XXXXXXXXXXXX:loadbalancer/XXXXXXXXXX",
90
+ "arn:aws:elasticloadbalancing:ap-northeast-1:XXXXXXXXXXXX:loadbalancer/app/XXXXXXXXXX/XXXXXXXXXX"],
91
+ :issued_at=>"2017-01-01 13:00:00 +0900",
92
+ :issuer=>"Amazon",
93
+ :key_algorithm=>"RSA-2048",
94
+ :not_after=>"2018-02-01 21:00:00 +0900",
95
+ :not_before=>"2017-01-01 09:00:00 +0900",
96
+ :renewal_summary=>{},
97
+ :revocation_reason=>nil,
98
+ :revoked_at=>nil,
99
+ :serial=>"XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX",
100
+ :signature_algorithm=>"SHA256WITHRSA",
101
+ :status=>"ISSUED",
102
+ :subject=>"CN=yutadayo.jp",
103
+ :subject_alternative_names=>["yutadayo.jp", "*.yutadayo.jp"],
104
+ :type=>"AMAZON_ISSUED"}
105
+ end
106
+ ```
107
+
108
+ ## Create Certificate
109
+
110
+ ```
111
+ $ cat Certificatefile
112
+
113
+ domain "yutadayo.jp" do
114
+ {
115
+ :domain_name => "yutadayo.jp",
116
+ :subject_alternative_names => ["*.yutadayo.jp"],
117
+ :domain_validation_options => [
118
+ {
119
+ :domain_name => "yutadayo.jp",
120
+ :validation_domain => "yutadayo.jp",
121
+ },
122
+ {
123
+ :domain_name => "*.yutadayo.jp",
124
+ :validation_domain => "yutadayo.jp",
125
+ },
126
+ ],
127
+ }
128
+ end
129
+
130
+ $ acmaker -a
131
+ ```
132
+
133
+ ## Delete Certificate
134
+
135
+ ```
136
+ $ cat Certificatefile
137
+
138
+ domain "yutadayo.jp" do
139
+ end
140
+
141
+ $ acmaker -a
142
+ ```
143
+
144
+ ```
145
+ $ cat Certificatefile
146
+
147
+ domain "yutadayo.jp" do
148
+ {}
149
+ end
150
+
151
+ $ acmaker -a
152
+ ```
153
+
154
+ ```
155
+ $ cat Certificatefile
156
+
157
+ # comment out
158
+ #domain "yutadayo.jp" do
159
+ # ...
160
+ #end
161
+
162
+ $ acmaker -a
163
+ ```
164
+
165
+ ## Test
166
+
167
+ set your AWS arn and domain_name for [spec/aws_config.yml.sample](https://github.com/yutadayo/acmaker/blob/master/spec/aws_config.yml.sample) and rename to spec/aws_config.yml.
168
+
169
+ ## Similar tools
170
+
171
+ - [Codenize.tools](https://codenize.tools/)
172
+
173
+ ## Contributing
174
+
175
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yutadayo/acmaker.
176
+
177
+ ## License
178
+
179
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,33 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'acmaker/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'acmaker'
9
+ spec.version = Acmaker::VERSION
10
+ spec.authors = ['Yuta Horii']
11
+ spec.email = ['horiyutwins@gmail.com']
12
+
13
+ spec.summary = 'Acmaker is a tool to manage AWS Certificate Manager (ACM).'
14
+ spec.description = 'Acmaker is a tool to manage AWS Certificate Manager (ACM). It defines the state of ACM using DSL, and updates ACM according to DSL.'
15
+ spec.homepage = 'https://github.com/yutadayo/acmaker'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec'
28
+
29
+ spec.add_dependency 'aws-sdk', '~>2'
30
+ spec.add_dependency 'diffy'
31
+ spec.add_dependency 'parallel'
32
+ spec.add_dependency 'term-ansicolor'
33
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acmaker"
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,169 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'acmaker'
5
+ require 'optparse'
6
+
7
+ Version = Acmaker::VERSION
8
+
9
+ DEFAULT_FILENAME = 'Certificatefile'
10
+
11
+ MAGIC_COMMENT = <<-EOS
12
+ # -*- mode: ruby -*-
13
+ # vi: set ft=ruby :
14
+ EOS
15
+
16
+ def parse_options(argv)
17
+ options = {
18
+ file: DEFAULT_FILENAME,
19
+ output: '-',
20
+ dry_run: false,
21
+ color: true,
22
+ request_concurrency: 8,
23
+ aws: {},
24
+ }
25
+
26
+ opt = OptionParser.new
27
+ opt.on('-k', '--access-key ACCESS_KEY') {|v| options[:aws][:access_key_id] = v }
28
+ opt.on('-s', '--secret-key SECRET_KEY') {|v| options[:aws][:secret_access_key] = v }
29
+ opt.on('-r', '--region REGION') {|v| options[:aws][:region] = v }
30
+
31
+ opt.on('', '--profile PROFILE') do |v|
32
+ options[:aws][:credentials] ||= {}
33
+ options[:aws][:credentials][:profile_name] = v
34
+ end
35
+
36
+ opt.on('', '--credentials-path PATH') do |v|
37
+ options[:aws][:credentials] ||= {}
38
+ options[:aws][:credentials][:path] = v
39
+ end
40
+
41
+ opt.on('-a', '--apply') { options[:mode] = :apply }
42
+ opt.on('-f', '--file FILE') {|v| options[:file] = v }
43
+ opt.on('' , '--dry-run') { options[:dry_run] = true }
44
+ opt.on('-e', '--export') { options[:mode] = :export }
45
+ opt.on('-o', '--output FILE') {|v| options[:output] = v }
46
+ opt.on('' , '--split') { options[:split] = :true }
47
+ opt.on('' , '--target REGEXP') {|v| options[:target] = Regexp.new(v) }
48
+ opt.on('' , '--no-color') { options[:color] = false }
49
+ opt.on('' , '--debug') { options[:debug] = true }
50
+
51
+ opt.on('' , '--request-concurrency N', Integer) do |v|
52
+ options[:request_concurrency] = v
53
+ end
54
+
55
+ opt.parse!(argv)
56
+
57
+ unless options[:mode]
58
+ puts opt.help
59
+ exit 1
60
+ end
61
+
62
+ if options[:aws][:credentials]
63
+ credentials = Aws::SharedCredentials.new(options[:aws][:credentials])
64
+ options[:aws][:credentials] = credentials
65
+ end
66
+
67
+ Aws.config.update(options[:aws])
68
+ String.colorize = options[:color]
69
+
70
+ if options[:debug]
71
+ Acmaker::Logger.instance.debug(options[:debug])
72
+
73
+ Aws.config.update(
74
+ :http_wire_trace => true,
75
+ :logger => Acmaker::Logger.instance
76
+ )
77
+ end
78
+
79
+ options
80
+ rescue => e
81
+ $stderr.puts("[ERROR] #{e.message}")
82
+ exit 1
83
+ end
84
+
85
+ def main(argv)
86
+ options = parse_options(argv)
87
+ client = Acmaker::Client.new(options)
88
+ logger = Acmaker::Logger.instance
89
+
90
+ case options[:mode]
91
+ when :export
92
+ exported = client.export
93
+ output = options[:output]
94
+
95
+ if options[:split]
96
+ logger.info('Export Certificate Manager')
97
+
98
+ output = DEFAULT_FILENAME if output == '-'
99
+ dir = File.dirname(output)
100
+ FileUtils.mkdir_p(dir)
101
+ requires = []
102
+
103
+ exported.each do |domain_name, certificate|
104
+ next unless certificate
105
+
106
+ filename = "#{domain_name}.acm"
107
+ requires << filename
108
+ acm_file = File.join(dir, filename)
109
+
110
+ logger.info(" write `#{acm_file}`")
111
+
112
+ dsl = Acmaker::DSL.convert({domain_name => certificate}, options)
113
+
114
+ open(acm_file, 'wb') do |f|
115
+ f.puts MAGIC_COMMENT
116
+ f.puts dsl
117
+ end
118
+ end
119
+
120
+ logger.info(" write `#{output}`")
121
+
122
+ open(output, 'wb') do |f|
123
+ f.puts MAGIC_COMMENT
124
+
125
+ requires.each do |acm_file|
126
+ f.puts "require '#{acm_file}'"
127
+ end
128
+ end
129
+ else
130
+ dsl = Acmaker::DSL.convert(exported, options)
131
+
132
+ if output == '-'
133
+ logger.info('# Export Certificate Manager')
134
+ puts dsl
135
+ else
136
+ logger.info("Export Certificate Manager to `#{output}`")
137
+ open(output, 'wb') do |f|
138
+ f.puts MAGIC_COMMENT
139
+ f.puts dsl
140
+ end
141
+ end
142
+ end
143
+ when :apply
144
+ file = options[:file]
145
+
146
+ unless File.exist?(file)
147
+ raise "No Certificatefile found (looking for: #{file})"
148
+ end
149
+
150
+ message = "Apply `#{file}` to Certificate Manager"
151
+ message << ' (dry-run)' if options[:dry_run]
152
+ logger.info(message)
153
+
154
+ updated = client.apply(file)
155
+
156
+ logger.info('No change'.intense_blue) unless updated
157
+ else
158
+ raise "Unknown mode: #{options[:mode]}"
159
+ end
160
+ rescue => e
161
+ if options[:debug]
162
+ raise e
163
+ else
164
+ $stderr.puts("[ERROR] #{e.message}".red)
165
+ exit 1
166
+ end
167
+ end
168
+
169
+ main(ARGV)
@@ -0,0 +1,20 @@
1
+ require 'logger'
2
+ require 'singleton'
3
+ require 'aws-sdk'
4
+ require 'diffy'
5
+ require 'parallel'
6
+ require 'pp'
7
+ require 'term/ansicolor'
8
+
9
+ require 'acmaker/logger'
10
+ require 'acmaker/utils'
11
+ require 'acmaker/ext/string_ext'
12
+
13
+ require 'acmaker/version'
14
+
15
+ require 'acmaker/client'
16
+ require 'acmaker/driver'
17
+ require 'acmaker/dsl'
18
+ require 'acmaker/dsl/context'
19
+ require 'acmaker/dsl/converter'
20
+ require 'acmaker/exporter'
@@ -0,0 +1,81 @@
1
+ module Acmaker
2
+ class Client
3
+ include Acmaker::Logger::Helper
4
+ include Acmaker::Utils::Helper
5
+
6
+ def initialize(options = {})
7
+ @options = options
8
+ @client = @options[:client] || Aws::ACM::Client.new
9
+ @driver = Acmaker::Driver.new(@client, @options)
10
+ @exporter = Acmaker::Exporter.new(@client, @options)
11
+ end
12
+
13
+ def export
14
+ @exporter.export
15
+ end
16
+
17
+ def apply(file)
18
+ walk(file)
19
+ end
20
+
21
+ private
22
+
23
+ def walk(file)
24
+ expected = load_file(file)
25
+ actual = @exporter.export
26
+ updated = walk_domains(expected, actual)
27
+
28
+ @options[:dry_run] ? false : updated
29
+ end
30
+
31
+ def walk_domains(expected, actual)
32
+ updated = false
33
+
34
+ expected.each do |domain_name, expected_certificate|
35
+ next unless matched?(domain_name)
36
+
37
+ actual_certificate = actual.delete(domain_name)
38
+
39
+ if actual_certificate
40
+ updated = walk_certificate(domain_name, expected_certificate, actual_certificate) || updated
41
+ elsif expected_certificate
42
+ @driver.create_certificate(domain_name, expected_certificate)
43
+ updated = true
44
+ end
45
+ end
46
+
47
+ actual.each do |domain_name, actual_certificate|
48
+ @driver.delete_certificate(domain_name, actual_certificate[:certificate_arn])
49
+ updated = true
50
+ end
51
+
52
+ updated
53
+ end
54
+
55
+ def walk_certificate(domain_name, expected_certificate, actual_certificate)
56
+ if expected_certificate.nil? || expected_certificate.empty?
57
+ @driver.delete_certificate(domain_name, actual_certificate[:certificate_arn])
58
+ return true
59
+ end
60
+
61
+ if expected_certificate != actual_certificate
62
+ log(:info, diff(actual_certificate, expected_certificate, color: @options[:color]), color: false)
63
+ log(:warn, "Domain `#{domain_name}`: certificate can not be changed", color: :yellow)
64
+ end
65
+
66
+ false
67
+ end
68
+
69
+ def load_file(file)
70
+ if file.is_a?(String)
71
+ open(file) do |f|
72
+ Acmaker::DSL.parse(f.read, file)
73
+ end
74
+ elsif file.respond_to?(:read)
75
+ Acmaker::DSL.parse(file.read, file.path)
76
+ else
77
+ raise TypeError, "can't convert #{file} into File"
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,28 @@
1
+ module Acmaker
2
+ class Driver
3
+ include Acmaker::Logger::Helper
4
+ include Acmaker::Utils::Helper
5
+
6
+ def initialize(client, options = {})
7
+ @client = client
8
+ @options = options
9
+ end
10
+
11
+ def create_certificate(domain_name, expected_certificate)
12
+ log(:info, "Create Domain `#{domain_name}` Certificate", color: :cyan)
13
+
14
+ unless @options[:dry_run]
15
+ resp = @client.request_certificate(expected_certificate)
16
+ log(:info, "Certificate arn `#{resp.certificate_arn}` has been created", color: :cyan)
17
+ end
18
+ end
19
+
20
+ def delete_certificate(domain_name, certificate_arn)
21
+ log(:info, "Delete Domain `#{domain_name}` Certificate", color: :red)
22
+
23
+ unless @options[:dry_run]
24
+ @client.delete_certificate(certificate_arn: certificate_arn)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module Acmaker
2
+ module DSL
3
+ class << self
4
+ def convert(exported, options = {})
5
+ Acmaker::DSL::Converter.convert(exported, options)
6
+ end
7
+
8
+ def parse(dsl, path, options = {})
9
+ Acmaker::DSL::Context.eval(dsl, path, options).result
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,42 @@
1
+ module Acmaker
2
+ module DSL
3
+ class Context
4
+ def self.eval(dsl, path, options = {})
5
+ new(path, options) do
6
+ eval(dsl, binding, path)
7
+ end
8
+ end
9
+
10
+ attr_reader :result
11
+
12
+ def initialize(path, options = {}, &block)
13
+ @path = path
14
+ @options = options
15
+ @result = {}
16
+ instance_eval(&block)
17
+ end
18
+
19
+ private
20
+
21
+ def require(file)
22
+ acmfile = file =~ %r{\A/} ? file : File.expand_path(File.join(File.dirname(@path), file))
23
+
24
+ if File.exist?(acmfile)
25
+ instance_eval(File.read(acmfile), acmfile)
26
+ elsif File.exist?(acmfile + '.rb')
27
+ instance_eval(File.read(acmfile + '.rb'), acmfile + '.rb')
28
+ else
29
+ Kernel.require(file)
30
+ end
31
+ end
32
+
33
+ def domain(name)
34
+ name = name.to_s
35
+
36
+ raise "Domain #{name} is already defined" if @result[name]
37
+
38
+ @result[name] = yield
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ module Acmaker
2
+ module DSL
3
+ class Converter
4
+ include Acmaker::Utils::Helper
5
+
6
+ class << self
7
+ def convert(exported, options = {})
8
+ new(exported, options).convert
9
+ end
10
+ end
11
+
12
+ def initialize(exported, options)
13
+ @exported = exported
14
+ @options = options
15
+ end
16
+
17
+ def convert
18
+ certificates = []
19
+
20
+ @exported.each do |domain_name, certificate|
21
+ next if !certificate || !matched?(domain_name)
22
+ certificates << output_certificate(domain_name, certificate)
23
+ end
24
+
25
+ certificates.join("\n")
26
+ end
27
+
28
+ private
29
+
30
+ def output_certificate(domain_name, certificate)
31
+ certificate = certificate.pretty_inspect.gsub(/^/, ' ').strip
32
+
33
+ <<-EOS
34
+ domain #{domain_name.inspect} do
35
+ #{certificate}
36
+ end
37
+ EOS
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,96 @@
1
+ module Acmaker
2
+ class Exporter
3
+ include Acmaker::Utils::Helper
4
+
5
+ def initialize(client, options = {})
6
+ @client = client
7
+ @options = options
8
+ end
9
+
10
+ def export
11
+ export_certificates
12
+ end
13
+
14
+ private
15
+
16
+ def export_certificates
17
+ result = {}
18
+ certificates = list_certificates
19
+ concurrency = @options[:request_concurrency]
20
+
21
+ Parallel.each(certificates, in_threads: concurrency) do |certificate|
22
+ domain_name = certificate.domain_name
23
+ next unless matched?(domain_name)
24
+ result[domain_name] = export_domain_certificate(certificate)
25
+ end
26
+
27
+ result
28
+ end
29
+
30
+ def export_domain_certificate(certificate)
31
+ arn = certificate.certificate_arn
32
+ resp = @client.describe_certificate(certificate_arn: arn)
33
+
34
+ {
35
+ certificate_arn: resp.certificate.certificate_arn,
36
+ created_at: resp.certificate.created_at&.to_s,
37
+ domain_name: resp.certificate.domain_name,
38
+ domain_validation_options: export_domain_validation_options(resp.certificate.domain_validation_options),
39
+ failure_reason: resp.certificate.failure_reason,
40
+ imported_at: resp.certificate.imported_at&.to_s,
41
+ in_use_by: resp.certificate.in_use_by,
42
+ issued_at: resp.certificate.issued_at&.to_s,
43
+ issuer: resp.certificate.issuer,
44
+ key_algorithm: resp.certificate.key_algorithm,
45
+ not_after: resp.certificate.not_after&.to_s,
46
+ not_before: resp.certificate.not_before&.to_s,
47
+ renewal_summary: export_renewal_summary(resp.certificate.renewal_summary),
48
+ revocation_reason: resp.certificate.revocation_reason,
49
+ revoked_at: resp.certificate.revoked_at,
50
+ serial: resp.certificate.serial,
51
+ signature_algorithm: resp.certificate.signature_algorithm,
52
+ status: resp.certificate.status,
53
+ subject: resp.certificate.subject,
54
+ subject_alternative_names: resp.certificate.subject_alternative_names,
55
+ type: resp.certificate.type,
56
+ }
57
+ rescue Aws::ACM::Errors::ResourceNotFoundException
58
+ nil
59
+ end
60
+
61
+ def export_domain_validation_options(options)
62
+ options.each_with_object([]) do |option, arr|
63
+ arr << {
64
+ domain_name: option.domain_name,
65
+ validation_domain: option.validation_domain,
66
+ validation_emails: option.validation_emails,
67
+ validation_status: option.validation_status,
68
+ }
69
+ end
70
+ end
71
+
72
+ def export_renewal_summary(summary)
73
+ return {} if summary.nil?
74
+ {
75
+ renewal_summary: {
76
+ renewal_status: summary.renewal_status,
77
+ domain_validation_options: export_domain_validation_options(summary.domain_validation_options),
78
+ }
79
+ }
80
+ end
81
+
82
+ def list_certificates
83
+ certificates = []
84
+ next_token = nil
85
+
86
+ loop do
87
+ resp = @client.list_certificates(next_token: next_token)
88
+ certificates.concat(resp.certificate_summary_list)
89
+ next_token = resp.next_token
90
+ break unless next_token
91
+ end
92
+
93
+ certificates
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,28 @@
1
+ module Acmaker::Ext
2
+ module StringExt
3
+ module ClassMethods
4
+ def colorize=(value)
5
+ @colorize = value
6
+ end
7
+
8
+ def colorize
9
+ @colorize
10
+ end
11
+ end # ClassMethods
12
+
13
+ Term::ANSIColor::Attribute.named_attributes.each do |attribute|
14
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
15
+ def #{attribute.name}
16
+ if String.colorize
17
+ Term::ANSIColor.send(#{attribute.name.inspect}, self)
18
+ else
19
+ self
20
+ end
21
+ end
22
+ EOS
23
+ end
24
+ end
25
+ end
26
+
27
+ String.include(Acmaker::Ext::StringExt)
28
+ String.extend(Acmaker::Ext::StringExt::ClassMethods)
@@ -0,0 +1,30 @@
1
+ module Acmaker
2
+ class Logger < ::Logger
3
+ include Singleton
4
+
5
+ def initialize
6
+ super($stdout)
7
+
8
+ self.formatter = proc do |_severity, _datetime, _progname, msg|
9
+ "#{msg}\n"
10
+ end
11
+
12
+ self.level = Logger::INFO
13
+ end
14
+
15
+ def debug(value)
16
+ self.level = value ? Logger::DEBUG : Logger::INFO
17
+ end
18
+
19
+ module Helper
20
+ def log(level, message, log_options = {})
21
+ global_option = @options || {}
22
+ message = "[#{level.to_s.upcase}] #{message}" unless level == :info
23
+ message << ' (dry-run)' if global_option[:dry_run]
24
+ message = message.send(log_options[:color]) if log_options[:color]
25
+ logger = global_option[:logger] || Acmaker::Logger.instance
26
+ logger.send(level, message)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ module Acmaker
2
+ module Utils
3
+ module Helper
4
+ def matched?(name)
5
+ if @options[:target]
6
+ @options[:target] =~ name
7
+ else
8
+ true
9
+ end
10
+ end
11
+
12
+ def diff(obj1, obj2, options = {})
13
+ diffy = Diffy::Diff.new(
14
+ obj1.pretty_inspect,
15
+ obj2.pretty_inspect,
16
+ diff: '-u'
17
+ )
18
+
19
+ out = diffy.to_s(options[:color] ? :color : :text).gsub(/\s+\z/m, '')
20
+ out.gsub!(/^/, options[:indent]) if options[:indent]
21
+ out
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Acmaker
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acmaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuta Horii
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
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: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: diffy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: parallel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
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: term-ansicolor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Acmaker is a tool to manage AWS Certificate Manager (ACM). It defines
112
+ the state of ACM using DSL, and updates ACM according to DSL.
113
+ email:
114
+ - horiyutwins@gmail.com
115
+ executables:
116
+ - acmaker
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - acmaker.gemspec
129
+ - bin/console
130
+ - bin/setup
131
+ - exe/acmaker
132
+ - lib/acmaker.rb
133
+ - lib/acmaker/client.rb
134
+ - lib/acmaker/driver.rb
135
+ - lib/acmaker/dsl.rb
136
+ - lib/acmaker/dsl/context.rb
137
+ - lib/acmaker/dsl/converter.rb
138
+ - lib/acmaker/exporter.rb
139
+ - lib/acmaker/ext/string_ext.rb
140
+ - lib/acmaker/logger.rb
141
+ - lib/acmaker/utils.rb
142
+ - lib/acmaker/version.rb
143
+ homepage: https://github.com/yutadayo/acmaker
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.6.11
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Acmaker is a tool to manage AWS Certificate Manager (ACM).
167
+ test_files: []