repol 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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/repol +169 -0
- data/lib/repol.rb +23 -0
- data/lib/repol/client.rb +87 -0
- data/lib/repol/driver.rb +42 -0
- data/lib/repol/dsl.rb +11 -0
- data/lib/repol/dsl/context.rb +55 -0
- data/lib/repol/dsl/converter.rb +42 -0
- data/lib/repol/dsl/template_helper.rb +18 -0
- data/lib/repol/exporter.rb +51 -0
- data/lib/repol/ext/hash_ext.rb +33 -0
- data/lib/repol/ext/string_ext.rb +28 -0
- data/lib/repol/logger.rb +28 -0
- data/lib/repol/utils.rb +23 -0
- data/lib/repol/version.rb +3 -0
- data/repol.gemspec +31 -0
- metadata +181 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f3855243ab8a0aefe0ac92d7da11405448d39084
|
4
|
+
data.tar.gz: 94c3d8993f2aa644e1126eef07615e7cacd9fd5f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c83dceedaf415f0d2e60bd2c0c811f70b5195e4dbb7558fc89bc373aa1320cc2f7e9416d666bdc7bb52f1092f61325a8964f60ef7ef0581fa6e5542b3d148b9
|
7
|
+
data.tar.gz: 5830c74674af2d0d263f22b3311df5518c5836c6404533d03c5024330432906be495c6057a6eaca66aa72f45dd96dfef8059f6d2c558a30c23842c60f7b273f7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Genki Sugawara
|
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,71 @@
|
|
1
|
+
# Repol
|
2
|
+
|
3
|
+
Repol is a tool to manage [ECR Repository Policy](http://docs.aws.amazon.com/AmazonECR/latest/userguide/RepositoryPolicies.html).
|
4
|
+
|
5
|
+
It defines the state of ECR Repository Policy using DSL, and updates ECR Repository Policy according to DSL.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'repol'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install repol
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```sh
|
26
|
+
export AWS_ACCESS_KEY_ID='...'
|
27
|
+
export AWS_SECRET_ACCESS_KEY='...'
|
28
|
+
repol -e -o RepolFile # export Repository Policy
|
29
|
+
vi RepolFile
|
30
|
+
repol -a --dry-run
|
31
|
+
repol -a # apply `RepolFile`
|
32
|
+
```
|
33
|
+
|
34
|
+
## Help
|
35
|
+
|
36
|
+
```
|
37
|
+
Usage: repol [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
|
+
## RepolFile example
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
require 'other/RepolFile'
|
59
|
+
|
60
|
+
repository "my_ecr_repo" do
|
61
|
+
{"Version"=>"2008-10-17",
|
62
|
+
"Statement"=>
|
63
|
+
[{"Sid"=>"PullOnly",
|
64
|
+
"Effect"=>"Allow",
|
65
|
+
"Principal"=>{"AWS"=>"arn:aws:iam::123456789012:root"},
|
66
|
+
"Action"=>
|
67
|
+
["ecr:BatchCheckLayerAvailability",
|
68
|
+
"ecr:BatchGetImage",
|
69
|
+
"ecr:GetDownloadUrlForLayer"]}]}
|
70
|
+
end
|
71
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "repol"
|
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
|
data/bin/setup
ADDED
data/exe/repol
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$: << File.expand_path('../../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'repol'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
Version = Repol::VERSION
|
8
|
+
|
9
|
+
DEFAULT_FILENAME = 'RepolFile'
|
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: {region: 'us-east-1'}, # TODO: Fix region
|
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
|
+
Repol::Logger.instance.set_debug(options[:debug])
|
72
|
+
|
73
|
+
Aws.config.update(
|
74
|
+
:http_wire_trace => true,
|
75
|
+
:logger => Repol::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 = Repol::Client.new(options)
|
88
|
+
logger = Repol::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 Repository Policy')
|
97
|
+
|
98
|
+
output = DEFAULT_FILENAME if output == '-'
|
99
|
+
dir = File.dirname(output)
|
100
|
+
FileUtils.mkdir_p(dir)
|
101
|
+
requires = []
|
102
|
+
|
103
|
+
exported.each do |repository_name, policy|
|
104
|
+
next unless policy
|
105
|
+
|
106
|
+
filename = "#{repository_name}.repol"
|
107
|
+
requires << filename
|
108
|
+
repol_file = File.join(dir, filename)
|
109
|
+
|
110
|
+
logger.info(" write `#{repol_file}`")
|
111
|
+
|
112
|
+
dsl = Repol::DSL.convert({repository_name => policy}, options)
|
113
|
+
|
114
|
+
open(repol_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 |repol_file|
|
126
|
+
f.puts "require '#{repol_file}'"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
else
|
130
|
+
dsl = Repol::DSL.convert(exported, options)
|
131
|
+
|
132
|
+
if output == '-'
|
133
|
+
logger.info('# Export Repository Policy')
|
134
|
+
puts dsl
|
135
|
+
else
|
136
|
+
logger.info("Export Repository Policy 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 RepolFile found (looking for: #{file})"
|
148
|
+
end
|
149
|
+
|
150
|
+
message = "Apply `#{file}` to Repository Policy"
|
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)
|
data/lib/repol.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
require 'diffy'
|
3
|
+
require 'hashie'
|
4
|
+
require 'json'
|
5
|
+
require 'logger'
|
6
|
+
require 'parallel'
|
7
|
+
require 'pp'
|
8
|
+
require 'singleton'
|
9
|
+
require 'term/ansicolor'
|
10
|
+
|
11
|
+
require 'repol/version'
|
12
|
+
require 'repol/ext/hash_ext'
|
13
|
+
require 'repol/ext/string_ext'
|
14
|
+
require 'repol/logger'
|
15
|
+
require 'repol/utils'
|
16
|
+
|
17
|
+
require 'repol/client'
|
18
|
+
require 'repol/driver'
|
19
|
+
require 'repol/dsl'
|
20
|
+
require 'repol/dsl/template_helper'
|
21
|
+
require 'repol/dsl/context'
|
22
|
+
require 'repol/dsl/converter'
|
23
|
+
require 'repol/exporter'
|
data/lib/repol/client.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
class Repol::Client
|
2
|
+
include Repol::Utils::Helper
|
3
|
+
include Repol::Logger::Helper
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
@options = options
|
7
|
+
@client = @options[:client] || Aws::ECR::Client.new
|
8
|
+
@driver = Repol::Driver.new(@client, @options)
|
9
|
+
@exporter = Repol::Exporter.new(@client, @options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def export
|
13
|
+
@exporter.export
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply(file)
|
17
|
+
walk(file)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def walk(file)
|
23
|
+
expected = load_file(file)
|
24
|
+
actual = @exporter.export
|
25
|
+
|
26
|
+
updated = walk_repositories(expected, actual)
|
27
|
+
|
28
|
+
if @options[:dry_run]
|
29
|
+
false
|
30
|
+
else
|
31
|
+
updated
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def walk_repositories(expected, actual)
|
36
|
+
updated = false
|
37
|
+
|
38
|
+
expected.each do |repository_name, expected_policy|
|
39
|
+
next unless matched?(repository_name)
|
40
|
+
|
41
|
+
actual_policy = actual[repository_name]
|
42
|
+
|
43
|
+
if actual_policy
|
44
|
+
updated = walk_policy(repository_name, expected_policy, actual_policy) || updated
|
45
|
+
elsif actual.has_key?(repository_name)
|
46
|
+
actual.delete(repository_name)
|
47
|
+
|
48
|
+
if expected_policy
|
49
|
+
@driver.create_policy(repository_name, expected_policy)
|
50
|
+
updated = true
|
51
|
+
end
|
52
|
+
else
|
53
|
+
log(:warn, "No such repository: #{repository_name}")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
updated
|
58
|
+
end
|
59
|
+
|
60
|
+
def walk_policy(repository_name, expected_policy, actual_policy)
|
61
|
+
updated = false
|
62
|
+
|
63
|
+
if expected_policy
|
64
|
+
if expected_policy != actual_policy
|
65
|
+
@driver.update_policy(repository_name, expected_policy, actual_policy)
|
66
|
+
updated = true
|
67
|
+
end
|
68
|
+
else
|
69
|
+
@driver.delete_policy(repository_name)
|
70
|
+
updated = true
|
71
|
+
end
|
72
|
+
|
73
|
+
updated
|
74
|
+
end
|
75
|
+
|
76
|
+
def load_file(file)
|
77
|
+
if file.kind_of?(String)
|
78
|
+
open(file) do |f|
|
79
|
+
Repol::DSL.parse(f.read, file)
|
80
|
+
end
|
81
|
+
elsif file.respond_to?(:read)
|
82
|
+
Repol::DSL.parse(file.read, file.path)
|
83
|
+
else
|
84
|
+
raise TypeError, "can't convert #{file} into File"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/repol/driver.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Repol::Driver
|
2
|
+
include Repol::Utils::Helper
|
3
|
+
include Repol::Logger::Helper
|
4
|
+
|
5
|
+
def initialize(client, options = {})
|
6
|
+
@client = client
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_policy(repository_name, policy)
|
11
|
+
log(:info, "Create Repository `#{repository_name}` Policy", color: :cyan)
|
12
|
+
|
13
|
+
unless @options[:dry_run]
|
14
|
+
@client.set_repository_policy(
|
15
|
+
repository_name: repository_name,
|
16
|
+
policy_text: JSON.dump(policy)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete_policy(repository_name)
|
22
|
+
log(:info, "Delete Repository `#{repository_name}` Policy", color: :red)
|
23
|
+
|
24
|
+
unless @options[:dry_run]
|
25
|
+
@client.delete_repository_policy(
|
26
|
+
repository_name: repository_name
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def update_policy(repository_name, policy, old_policy)
|
32
|
+
log(:info, "Update Repository `#{repository_name}` Policy", color: :green)
|
33
|
+
log(:info, diff(old_policy, policy, color: @options[:color]), color: false)
|
34
|
+
|
35
|
+
unless @options[:dry_run]
|
36
|
+
@client.set_repository_policy(
|
37
|
+
repository_name: repository_name,
|
38
|
+
policy_text: JSON.dump(policy)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/repol/dsl.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class Repol::DSL
|
2
|
+
class << self
|
3
|
+
def convert(exported, options = {})
|
4
|
+
Repol::DSL::Converter.convert(exported, options)
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse(dsl, path, options = {})
|
8
|
+
Repol::DSL::Context.eval(dsl, path, options).result
|
9
|
+
end
|
10
|
+
end # of class methods
|
11
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class Repol::DSL::Context
|
2
|
+
include Repol::DSL::TemplateHelper
|
3
|
+
|
4
|
+
def self.eval(dsl, path, options = {})
|
5
|
+
self.new(path, options) {
|
6
|
+
eval(dsl, binding, path)
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def result
|
11
|
+
@result.sort_array!
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(path, options = {}, &block)
|
15
|
+
@path = path
|
16
|
+
@options = options
|
17
|
+
@result = {}
|
18
|
+
|
19
|
+
@context = Hashie::Mash.new(
|
20
|
+
:path => path,
|
21
|
+
:options => options,
|
22
|
+
:templates => {}
|
23
|
+
)
|
24
|
+
|
25
|
+
instance_eval(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def template(name, &block)
|
29
|
+
@context.templates[name.to_s] = block
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def require(file)
|
35
|
+
repolfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))
|
36
|
+
|
37
|
+
if File.exist?(repolfile)
|
38
|
+
instance_eval(File.read(repolfile), repolfile)
|
39
|
+
elsif File.exist?(repolfile + '.rb')
|
40
|
+
instance_eval(File.read(repolfile + '.rb'), repolfile + '.rb')
|
41
|
+
else
|
42
|
+
Kernel.require(file)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def repository(name)
|
47
|
+
name = name.to_s
|
48
|
+
|
49
|
+
if @result[name]
|
50
|
+
raise "Repository `#{name}` is already defined"
|
51
|
+
end
|
52
|
+
|
53
|
+
@result[name] = yield
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Repol::DSL::Converter
|
2
|
+
include Repol::Utils::Helper
|
3
|
+
|
4
|
+
def self.convert(exported, options = {})
|
5
|
+
self.new(exported, options).convert
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(exported, options = {})
|
9
|
+
@exported = exported
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def convert
|
14
|
+
output_repositories(@exported)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def output_repositories(policy_by_repository)
|
20
|
+
repositories = []
|
21
|
+
|
22
|
+
policy_by_repository.sort_by(&:first).each do |repository_name, policy|
|
23
|
+
if not policy or not matched?(repository_name)
|
24
|
+
next
|
25
|
+
end
|
26
|
+
|
27
|
+
repositories << output_repository(repository_name, policy)
|
28
|
+
end
|
29
|
+
|
30
|
+
repositories.join("\n")
|
31
|
+
end
|
32
|
+
|
33
|
+
def output_repository(repository_name, policy)
|
34
|
+
policy = policy.pretty_inspect.gsub(/^/, ' ').strip
|
35
|
+
|
36
|
+
<<-EOS
|
37
|
+
repository #{repository_name.inspect} do
|
38
|
+
#{policy}
|
39
|
+
end
|
40
|
+
EOS
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Repol::DSL::TemplateHelper
|
2
|
+
def include_template(template_name, context = {})
|
3
|
+
tmplt = @context.templates[template_name.to_s]
|
4
|
+
|
5
|
+
unless tmplt
|
6
|
+
raise "Template `#{template_name}` is not defined"
|
7
|
+
end
|
8
|
+
|
9
|
+
context_orig = @context
|
10
|
+
@context = @context.merge(context)
|
11
|
+
instance_eval(&tmplt)
|
12
|
+
@context = context_orig
|
13
|
+
end
|
14
|
+
|
15
|
+
def context
|
16
|
+
@context
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class Repol::Exporter
|
2
|
+
include Repol::Utils::Helper
|
3
|
+
|
4
|
+
def initialize(client, options = {})
|
5
|
+
@client = client
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def export
|
10
|
+
export_repositories.sort_array!
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def export_repositories
|
16
|
+
result = {}
|
17
|
+
repositories = get_repositories
|
18
|
+
concurrency = @options[:request_concurrency]
|
19
|
+
|
20
|
+
Parallel.each(repositories, in_threads: concurrency) do |repository|
|
21
|
+
name = repository.repository_name
|
22
|
+
next unless matched?(name)
|
23
|
+
policy = export_repository_policy(repository)
|
24
|
+
result[name] = policy
|
25
|
+
end
|
26
|
+
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def export_repository_policy(repository)
|
31
|
+
name = repository.repository_name
|
32
|
+
resp = @client.get_repository_policy(repository_name: name)
|
33
|
+
JSON.parse(resp.policy_text)
|
34
|
+
rescue Aws::ECR::Errors::RepositoryPolicyNotFoundException
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_repositories
|
39
|
+
repositories = []
|
40
|
+
next_token = nil
|
41
|
+
|
42
|
+
loop do
|
43
|
+
resp = @client.describe_repositories(next_token: next_token)
|
44
|
+
repositories.concat(resp.repositories)
|
45
|
+
next_token = resp.next_token
|
46
|
+
break unless next_token
|
47
|
+
end
|
48
|
+
|
49
|
+
repositories
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Repol::Ext
|
2
|
+
module HashExt
|
3
|
+
def sort_array!
|
4
|
+
keys.each do |key|
|
5
|
+
value = self[key]
|
6
|
+
self[key] = sort_array0(value)
|
7
|
+
end
|
8
|
+
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def sort_array0(value)
|
15
|
+
case value
|
16
|
+
when Hash
|
17
|
+
new_value = {}
|
18
|
+
|
19
|
+
value.each do |k, v|
|
20
|
+
new_value[k] = sort_array0(v)
|
21
|
+
end
|
22
|
+
|
23
|
+
new_value
|
24
|
+
when Array
|
25
|
+
value.map {|v| sort_array0(v) }.sort_by(&:to_s)
|
26
|
+
else
|
27
|
+
value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Hash.include(Repol::Ext::HashExt)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Repol::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(Repol::Ext::StringExt)
|
28
|
+
String.extend(Repol::Ext::StringExt::ClassMethods)
|
data/lib/repol/logger.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
class Repol::Logger < ::Logger
|
2
|
+
include Singleton
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
super($stdout)
|
6
|
+
|
7
|
+
self.formatter = proc do |severity, datetime, progname, msg|
|
8
|
+
"#{msg}\n"
|
9
|
+
end
|
10
|
+
|
11
|
+
self.level = Logger::INFO
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_debug(value)
|
15
|
+
self.level = value ? Logger::DEBUG : Logger::INFO
|
16
|
+
end
|
17
|
+
|
18
|
+
module Helper
|
19
|
+
def log(level, message, log_options = {})
|
20
|
+
global_option = @options || {}
|
21
|
+
message = "[#{level.to_s.upcase}] #{message}" unless level == :info
|
22
|
+
message << ' (dry-run)' if global_option[:dry_run]
|
23
|
+
message = message.send(log_options[:color]) if log_options[:color]
|
24
|
+
logger = global_option[:logger] || Repol::Logger.instance
|
25
|
+
logger.send(level, message)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/repol/utils.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class Repol::Utils
|
2
|
+
module Helper
|
3
|
+
def matched?(name)
|
4
|
+
if @options[:target]
|
5
|
+
@options[:target] =~ name
|
6
|
+
else
|
7
|
+
true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def diff(obj1, obj2, options = {})
|
12
|
+
diffy = Diffy::Diff.new(
|
13
|
+
obj1.pretty_inspect,
|
14
|
+
obj2.pretty_inspect,
|
15
|
+
:diff => '-u'
|
16
|
+
)
|
17
|
+
|
18
|
+
out = diffy.to_s(options[:color] ? :color : :text).gsub(/\s+\z/m, '')
|
19
|
+
out.gsub!(/^/, options[:indent]) if options[:indent]
|
20
|
+
out
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/repol.gemspec
ADDED
@@ -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 'repol/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'repol'
|
8
|
+
spec.version = Repol::VERSION
|
9
|
+
spec.authors = ['Genki Sugawara']
|
10
|
+
spec.email = ['sugawara@cookpad.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Repol is a tool to manage ECR Repository Policy.}
|
13
|
+
spec.description = %q{Repol is a tool to manage ECR Repository Policy.}
|
14
|
+
spec.homepage = 'https://github.com/winebarrel/repol'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'aws-sdk', '~> 2.3.0'
|
23
|
+
spec.add_dependency 'diffy'
|
24
|
+
spec.add_dependency 'hashie'
|
25
|
+
spec.add_dependency 'parallel'
|
26
|
+
spec.add_dependency 'term-ansicolor'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler'
|
29
|
+
spec.add_development_dependency 'rake'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: repol
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-21 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.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: diffy
|
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: hashie
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
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: parallel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: term-ansicolor
|
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: bundler
|
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: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.0'
|
125
|
+
description: Repol is a tool to manage ECR Repository Policy.
|
126
|
+
email:
|
127
|
+
- sugawara@cookpad.com
|
128
|
+
executables:
|
129
|
+
- repol
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- exe/repol
|
143
|
+
- lib/repol.rb
|
144
|
+
- lib/repol/client.rb
|
145
|
+
- lib/repol/driver.rb
|
146
|
+
- lib/repol/dsl.rb
|
147
|
+
- lib/repol/dsl/context.rb
|
148
|
+
- lib/repol/dsl/converter.rb
|
149
|
+
- lib/repol/dsl/template_helper.rb
|
150
|
+
- lib/repol/exporter.rb
|
151
|
+
- lib/repol/ext/hash_ext.rb
|
152
|
+
- lib/repol/ext/string_ext.rb
|
153
|
+
- lib/repol/logger.rb
|
154
|
+
- lib/repol/utils.rb
|
155
|
+
- lib/repol/version.rb
|
156
|
+
- repol.gemspec
|
157
|
+
homepage: https://github.com/winebarrel/repol
|
158
|
+
licenses:
|
159
|
+
- MIT
|
160
|
+
metadata: {}
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 2.4.5.1
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: Repol is a tool to manage ECR Repository Policy.
|
181
|
+
test_files: []
|