s3_master 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/s3_master +7 -0
- data/bin/setup +8 -0
- data/lib/s3_master.rb +8 -0
- data/lib/s3_master/cli.rb +112 -0
- data/lib/s3_master/config.rb +46 -0
- data/lib/s3_master/local_policy.rb +47 -0
- data/lib/s3_master/policy_differ.rb +22 -0
- data/lib/s3_master/remote_policy.rb +126 -0
- data/lib/s3_master/version.rb +3 -0
- data/s3_master.gemspec +33 -0
- metadata +193 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a92a30de3a30a8623d4927d25591d150f1176ee
|
4
|
+
data.tar.gz: 583016ca2200f580f6715027ce7bb246b5967607
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 72877735cf2066a6a94bf5895f0cd40e79f4f4a7528ab67c7d2165baa6e4d1082a9ffaa97df40767ec37349fd5aaabf7d775636575e439bce685286f6946c1b8
|
7
|
+
data.tar.gz: 5fb481997e46cd71c96031e25df5548ed86d3b7c4ef699d752241f1434afc737309080713eb0e4960cfa3255d808a7449d5a76596306d139a647e75cf21ad94c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 dave@steinbergcomputing.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
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Dave Steinberg
|
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,79 @@
|
|
1
|
+
# s3_master - Manage policies on existing S3 buckets
|
2
|
+
|
3
|
+
Inspired by [stack_master](https://github.com/envato/stack_master), `s3_master` aims to manage various policies on S3 buckets created outside of cloudformation.
|
4
|
+
If your buckets were created via cloudformation, then you should use cfn to manage the policies.
|
5
|
+
|
6
|
+
`s3_master` provides a simple diff/push workflow so policy documents can be stored in git and reconciled against AWS easily.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 's3_master'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install s3_master
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
N.B. this is an alpha release!
|
27
|
+
|
28
|
+
* Make a `policies` directory to hold your json policy documents.
|
29
|
+
* Make a `s3_policies.yml` file like:
|
30
|
+
|
31
|
+
```
|
32
|
+
buckets:
|
33
|
+
bucket-a:
|
34
|
+
region: us-east-1
|
35
|
+
lifecycle: bucket-a/lifecycle.json
|
36
|
+
replication: bucket-a/replication.json
|
37
|
+
inventory:
|
38
|
+
all: bucket-a/inventory-all.json
|
39
|
+
bucket-b:
|
40
|
+
lifecycle: bucket-b/lifecycle.json
|
41
|
+
replication: bucket-b/replication.json
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
* Policies can be imported from S3 via the `fetch` subcommand. Policies will be written to the file specified in the `s3_policies.yml`, e.g. for the above, running:
|
46
|
+
|
47
|
+
`s3_master fetch bucket-a lifecycle`
|
48
|
+
|
49
|
+
It would write out the current policy to `policies/bucket-a/lifecycle.json`
|
50
|
+
|
51
|
+
* Policy changes are loaded to S3 via `apply`, e.g:
|
52
|
+
|
53
|
+
`s3_master apply bucket-a lifecycle`
|
54
|
+
|
55
|
+
A diff is shown and confirmation is requested by default.
|
56
|
+
|
57
|
+
## TODO
|
58
|
+
|
59
|
+
* Need some tests with `aruba`
|
60
|
+
* Better warnings / error messages when the bucket requires a regional endpoint, and when an invalid policy-type is specified.
|
61
|
+
* Make an init subcommand
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
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.
|
66
|
+
|
67
|
+
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).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bustle/s3_master. 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.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). [BDG Media](https://bustle.company/) sponsored this work. [Come work with us!](https://jobs.lever.co/bustle)
|
76
|
+
|
77
|
+
## Code of Conduct
|
78
|
+
|
79
|
+
Everyone interacting in the S3Master project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bustle/s3_master/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "s3_master"
|
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/s3_master
ADDED
data/bin/setup
ADDED
data/lib/s3_master.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'active_support/core_ext/hash'
|
5
|
+
require 'aws-sdk-s3'
|
6
|
+
require 'diffy'
|
7
|
+
require 'json'
|
8
|
+
require 'neatjson'
|
9
|
+
require 'thor'
|
10
|
+
require 'yaml'
|
11
|
+
|
12
|
+
require 's3_master'
|
13
|
+
|
14
|
+
Diffy::Diff.default_format = :color
|
15
|
+
|
16
|
+
class S3MasterCli < Thor
|
17
|
+
include Thor::Shell
|
18
|
+
|
19
|
+
class_option "config-file".to_sym, type: :string, aliases: %w(c), default: "s3_policies.yml"
|
20
|
+
class_option "policy-dir".to_sym, type: :string, aliases: %w(d), default: "policies"
|
21
|
+
class_option :debug, type: :boolean
|
22
|
+
class_option :force, type: :boolean
|
23
|
+
|
24
|
+
desc "diff <bucket> <policy-type> [policy-id]", "Show differences between the current and the desired policy."
|
25
|
+
def diff(bucket, policy_type, policy_id=nil)
|
26
|
+
config = S3Master::Config.new(options[:"config-file"])
|
27
|
+
|
28
|
+
remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})
|
29
|
+
local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(id: policy_id).symbolize_keys)
|
30
|
+
|
31
|
+
if options[:debug]
|
32
|
+
bkt = Aws::S3::Bucket.new(bucket)
|
33
|
+
puts "%s: %s" % [bkt.name, bkt.url]
|
34
|
+
puts "=== Remote Policy:\n%s" % [JSON.neat_generate(remote_policy.body, sort: true)]
|
35
|
+
puts "=== Local Policy:\n%s" % [JSON.neat_generate(local_policy.body, sort: true)]
|
36
|
+
end
|
37
|
+
|
38
|
+
policy_diff = S3Master::PolicyDiffer.new(remote_policy.body, local_policy.body)
|
39
|
+
prefix = "#{bucket}/#{policy_type}"
|
40
|
+
prefix += "/#{policy_id}" if policy_id
|
41
|
+
|
42
|
+
if policy_diff.identical?
|
43
|
+
puts "#{prefix}: Local and remote policies match."
|
44
|
+
else
|
45
|
+
puts "#{prefix} diff:\n%s" % [policy_diff.to_s]
|
46
|
+
end
|
47
|
+
policy_diff
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "apply [<bucket>] [<policy-type>] [policy-id]", "Appies the local policy to the bucket."
|
51
|
+
def apply(cli_bucket=nil, cli_policy_type=nil, cli_policy_id=nil)
|
52
|
+
config = S3Master::Config.new(options[:"config-file"])
|
53
|
+
config.each do |bucket, policy_type, policy_id|
|
54
|
+
next if !cli_bucket.nil? && cli_bucket != bucket ||
|
55
|
+
!cli_policy_type.nil? && cli_policy_type != policy_type ||
|
56
|
+
!cli_policy_id.nil? && cli_policy_id != policy_id
|
57
|
+
|
58
|
+
policy_diff = diff(bucket, policy_type, policy_id)
|
59
|
+
|
60
|
+
next if policy_diff.identical? || ! (options[:force] || yes?("Proceed? (y/N)"))
|
61
|
+
|
62
|
+
local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(id: policy_id).symbolize_keys)
|
63
|
+
remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})
|
64
|
+
remote_policy.write(local_policy)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
desc "fetch <bucket> <policy-type> [policy-id]", "Retrieves the specified policy for the bucket and saves it in the config-specified file"
|
69
|
+
def fetch(buckets=nil, policy_types=S3Master::RemotePolicy::POLICY_TYPES, policy_id=nil)
|
70
|
+
config = S3Master::Config.new(options[:"config-file"])
|
71
|
+
buckets ||= config[:buckets].keys
|
72
|
+
|
73
|
+
Array(buckets).each do |bucket|
|
74
|
+
Array(policy_types).each do |policy_type|
|
75
|
+
next if ! S3Master::RemotePolicy.known_policy_type?(policy_type)
|
76
|
+
local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(skip_load: true, id: policy_id).symbolize_keys)
|
77
|
+
remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})
|
78
|
+
|
79
|
+
if !local_policy.basename.nil?
|
80
|
+
local_policy.write(remote_policy)
|
81
|
+
else
|
82
|
+
puts "%s policy:\n%s" % [policy_type, remote_policy.pretty_body]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
desc "status [<bucket>]", "Checks if the policies have differences"
|
89
|
+
def status(user_bucket=nil)
|
90
|
+
config = S3Master::Config.new(options[:"config-file"])
|
91
|
+
|
92
|
+
any_differences = false
|
93
|
+
config.each do |bucket, policy_type, policy_id|
|
94
|
+
next if !user_bucket.nil? && user_bucket != bucket
|
95
|
+
local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(id: policy_id).symbolize_keys)
|
96
|
+
remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})
|
97
|
+
|
98
|
+
policy_diff = S3Master::PolicyDiffer.new(remote_policy.body, local_policy.body)
|
99
|
+
if !policy_diff.identical?
|
100
|
+
any_differences = true
|
101
|
+
|
102
|
+
if policy_id.nil?
|
103
|
+
puts "* %s: %s" % [bucket, policy_type]
|
104
|
+
else
|
105
|
+
puts "* %s: %s %s" % [bucket, policy_type, policy_id]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
puts "No differences detected." if !any_differences
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module S3Master
|
4
|
+
class Config
|
5
|
+
include Enumerable
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegator :@cfg, :[]
|
9
|
+
|
10
|
+
def initialize(cfg_file)
|
11
|
+
@cfg = ActiveSupport::HashWithIndifferentAccess.new(YAML.load_file(cfg_file))
|
12
|
+
end
|
13
|
+
|
14
|
+
def region(bucket) @cfg[:buckets][bucket.to_s]["region"] ; end
|
15
|
+
|
16
|
+
def has_subpolicies?(bucket, policy_type)
|
17
|
+
@cfg[:buckets][bucket.to_s][policy_type.to_s].kind_of?(Hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
def template_relname(bucket, policy_type, policy_id=nil)
|
21
|
+
if self.has_subpolicies?(bucket, policy_type)
|
22
|
+
raise(RuntimeError, "Bucket #{bucket} policy #{policy_type} has subpolicies so an id is required") if policy_id.nil?
|
23
|
+
@cfg[:buckets][bucket][policy_type][policy_id]
|
24
|
+
else
|
25
|
+
@cfg[:buckets][bucket][policy_type]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def policy_ids(bucket, policy_type)
|
30
|
+
subpolicy = @cfg[:buckets][bucket][policy_type]
|
31
|
+
policy_ids = subpolicy.kind_of?(Hash) ? subpolicy.keys : nil
|
32
|
+
policy_ids
|
33
|
+
end
|
34
|
+
|
35
|
+
def each(&block)
|
36
|
+
@cfg[:buckets].each_pair do |bucket, policy_types|
|
37
|
+
policy_types.each_pair do |policy_type, subpolicy|
|
38
|
+
next if ! S3Master::RemotePolicy.known_policy_type?(policy_type)
|
39
|
+
(policy_ids(bucket, policy_type) || [nil]).each do |policy_id|
|
40
|
+
block.call(bucket, policy_type, policy_id)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module S3Master
|
2
|
+
class LocalPolicy
|
3
|
+
attr_reader :body
|
4
|
+
|
5
|
+
def initialize(cfg, bucket_name, policy_type, options={})
|
6
|
+
@config = cfg
|
7
|
+
@bucket_name = bucket_name
|
8
|
+
@policy_type = policy_type.to_sym
|
9
|
+
@options = options
|
10
|
+
@policy_id = options[:id]
|
11
|
+
|
12
|
+
if @config["buckets"][@bucket_name].nil?
|
13
|
+
raise(RuntimeError, "No bucket named '#{@bucket_name}' found in loaded config.")
|
14
|
+
end
|
15
|
+
|
16
|
+
load_policy if !options[:skip_load]
|
17
|
+
end
|
18
|
+
|
19
|
+
def preserve_keys?() S3Master::RemotePolicy::POLICIES[@policy_type][:preserve_keys] ; end
|
20
|
+
def empty?() @body.nil? || @body.empty? ; end
|
21
|
+
def pretty_body() JSON.neat_generate(body, sort: (self.preserve_keys? ? false : true)) ; end
|
22
|
+
|
23
|
+
def basename() @config.template_relname(@bucket_name, @policy_type, @policy_id) ; end
|
24
|
+
def path() File.join(@options[:"policy-dir"], self.basename) ; end
|
25
|
+
|
26
|
+
def load_policy
|
27
|
+
@body = if basename.nil? || basename == false
|
28
|
+
# Empty policy
|
29
|
+
{}
|
30
|
+
else
|
31
|
+
JSON.parse(File.binread(path))
|
32
|
+
end
|
33
|
+
|
34
|
+
if ! self.preserve_keys?
|
35
|
+
@body.deep_transform_keys!{|k| k.underscore.to_sym }
|
36
|
+
end
|
37
|
+
|
38
|
+
@body
|
39
|
+
end
|
40
|
+
|
41
|
+
def write(other_policy)
|
42
|
+
File.open(self.path, "wb") do |fh|
|
43
|
+
fh.puts other_policy.pretty_body
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'diffy'
|
2
|
+
|
3
|
+
module S3Master
|
4
|
+
class PolicyDiffer
|
5
|
+
attr_reader :remote, :local, :diff
|
6
|
+
|
7
|
+
def initialize(remote_policy, local_policy)
|
8
|
+
@remote = remote_policy
|
9
|
+
@local = local_policy
|
10
|
+
compute_diff
|
11
|
+
end
|
12
|
+
|
13
|
+
def compute_diff
|
14
|
+
rj = JSON.neat_generate(@remote, sort: true)
|
15
|
+
lj = JSON.neat_generate(@local, sort: true)
|
16
|
+
@diff = Diffy::Diff.new(rj, lj, context: 7, include_diff_info: true)
|
17
|
+
end
|
18
|
+
|
19
|
+
def identical?() diff.to_s == "\n" ; end
|
20
|
+
def to_s() diff.to_s ; end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'aws-sdk-s3'
|
2
|
+
|
3
|
+
module S3Master
|
4
|
+
class RemotePolicy
|
5
|
+
attr_reader :body
|
6
|
+
|
7
|
+
POLICIES = {
|
8
|
+
lifecycle: {
|
9
|
+
get: :get_bucket_lifecycle_configuration,
|
10
|
+
put: :put_bucket_lifecycle_configuration,
|
11
|
+
delete: :delete_bucket_lifecycle,
|
12
|
+
policy_key: :lifecycle_configuration,
|
13
|
+
},
|
14
|
+
replication: {
|
15
|
+
get: :get_bucket_replication,
|
16
|
+
put: :put_bucket_replication,
|
17
|
+
delete: :delete_bucket_replication,
|
18
|
+
policy_merge: true,
|
19
|
+
ensure_versioning: true,
|
20
|
+
},
|
21
|
+
inventory: {
|
22
|
+
get: :get_bucket_inventory_configuration,
|
23
|
+
put: :put_bucket_inventory_configuration,
|
24
|
+
delete: :delete_bucket_inventory_configuration,
|
25
|
+
policy_merge: true,
|
26
|
+
requires_id: true,
|
27
|
+
},
|
28
|
+
access: {
|
29
|
+
get: :get_bucket_policy,
|
30
|
+
put: :put_bucket_policy,
|
31
|
+
delete: :delete_bucket_policy,
|
32
|
+
policy_key: :policy,
|
33
|
+
preserve_keys: true,
|
34
|
+
},
|
35
|
+
events: {
|
36
|
+
get: :get_bucket_notification_configuration,
|
37
|
+
put: :put_bucket_notification_configuration,
|
38
|
+
policy_key: :notification_configuration,
|
39
|
+
},
|
40
|
+
}
|
41
|
+
POLICY_TYPES = POLICIES.keys.freeze
|
42
|
+
|
43
|
+
NO_POLICY_EXCEPTIONS = [
|
44
|
+
Aws::S3::Errors::NoSuchBucketPolicy,
|
45
|
+
Aws::S3::Errors::NoSuchConfiguration,
|
46
|
+
Aws::S3::Errors::NoSuchLifecycleConfiguration,
|
47
|
+
Aws::S3::Errors::ReplicationConfigurationNotFoundError,
|
48
|
+
]
|
49
|
+
|
50
|
+
def initialize(bucket_name, policy_type, options={})
|
51
|
+
@client = options[:region].nil? ? Aws::S3::Client.new() : Aws::S3::Client.new(region: options[:region])
|
52
|
+
@bucket_name = bucket_name
|
53
|
+
@policy_type = policy_type.to_sym
|
54
|
+
@options = options
|
55
|
+
raise(RuntimeError, "Policy type #{policy_type} not supported") if !POLICIES.has_key?(@policy_type)
|
56
|
+
load_policy
|
57
|
+
end
|
58
|
+
|
59
|
+
def policy_key() POLICIES[@policy_type][:policy_key] ; end
|
60
|
+
def parse_as_string() POLICIES[@policy_type][:parse_as_string] || false ; end
|
61
|
+
|
62
|
+
def inflate(read_policy)
|
63
|
+
if @policy_type == :access_policy
|
64
|
+
JSON.parse(read_policy[policy_key].string)
|
65
|
+
else
|
66
|
+
read_policy
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def deflate(policy_hash)
|
71
|
+
case @policy_type
|
72
|
+
when :access_policy
|
73
|
+
policy_hash[policy_key] = JSON.generate(policy_hash[policy_key])
|
74
|
+
end
|
75
|
+
policy_hash
|
76
|
+
end
|
77
|
+
|
78
|
+
def load_policy
|
79
|
+
begin
|
80
|
+
args = base_args
|
81
|
+
@body = self.inflate(@client.send(POLICIES[@policy_type][:get], args).to_hash)
|
82
|
+
rescue *NO_POLICY_EXCEPTIONS => e
|
83
|
+
# No policy there currently
|
84
|
+
@body = {}
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def pretty_body() JSON.neat_generate(body, sort: true) ; end
|
89
|
+
|
90
|
+
def write(local_policy)
|
91
|
+
args = base_args
|
92
|
+
|
93
|
+
if local_policy.empty? && POLICIES[@policy_type].has_key?(:delete)
|
94
|
+
@client.send(POLICIES[@policy_type][:delete], args)
|
95
|
+
else
|
96
|
+
if POLICIES[@policy_type][:ensure_versioning]
|
97
|
+
self.ensure_versioning!
|
98
|
+
end
|
99
|
+
|
100
|
+
if POLICIES[@policy_type][:policy_merge]
|
101
|
+
args.merge!(local_policy.body)
|
102
|
+
else
|
103
|
+
args[policy_key] = local_policy.body
|
104
|
+
end
|
105
|
+
|
106
|
+
@client.send(POLICIES[@policy_type][:put], self.deflate(args))
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def ensure_versioning!
|
111
|
+
bkt = Aws::S3::Bucket.new(@bucket_name, client: @client)
|
112
|
+
bkt.versioning.status == "Enabled" || bkt.versioning.enable
|
113
|
+
end
|
114
|
+
|
115
|
+
def base_args
|
116
|
+
args = {bucket: @bucket_name}
|
117
|
+
if POLICIES[@policy_type][:requires_id]
|
118
|
+
args[:id] = @options[:id]
|
119
|
+
end
|
120
|
+
args
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.known_policy_type?(policy) POLICIES.has_key?(policy.to_sym) ; end
|
124
|
+
def known_policy_type?(policy) self.class.known_policy_type(policy) ; end
|
125
|
+
end
|
126
|
+
end
|
data/s3_master.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "s3_master/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "s3_master"
|
7
|
+
spec.version = S3Master::VERSION
|
8
|
+
spec.authors = ["Dave Steinberg"]
|
9
|
+
spec.email = ["dave@steinbergcomputing.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Cohesive S3 bucket policy management}
|
12
|
+
spec.description = %q{s3_master aims to provide a git-based workflow for managing policy documents on legacy (e.g. non-cloudformation) S3 buckets.}
|
13
|
+
spec.homepage = "https://github.com/bustle/s3_master"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features|bin/console|bin/setup)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "bin"
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "byebug"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
|
28
|
+
spec.add_dependency "activesupport", ">= 4.0"
|
29
|
+
spec.add_dependency "aws-sdk-s3", "~> 1"
|
30
|
+
spec.add_dependency "diffy"
|
31
|
+
spec.add_dependency "neatjson"
|
32
|
+
spec.add_dependency "thor"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3_master
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Steinberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-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: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
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: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: aws-sdk-s3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: diffy
|
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
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: neatjson
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: thor
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: s3_master aims to provide a git-based workflow for managing policy documents
|
140
|
+
on legacy (e.g. non-cloudformation) S3 buckets.
|
141
|
+
email:
|
142
|
+
- dave@steinbergcomputing.com
|
143
|
+
executables:
|
144
|
+
- console
|
145
|
+
- s3_master
|
146
|
+
- setup
|
147
|
+
extensions: []
|
148
|
+
extra_rdoc_files: []
|
149
|
+
files:
|
150
|
+
- ".gitignore"
|
151
|
+
- ".rspec"
|
152
|
+
- ".travis.yml"
|
153
|
+
- CODE_OF_CONDUCT.md
|
154
|
+
- Gemfile
|
155
|
+
- LICENSE.txt
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- bin/console
|
159
|
+
- bin/s3_master
|
160
|
+
- bin/setup
|
161
|
+
- lib/s3_master.rb
|
162
|
+
- lib/s3_master/cli.rb
|
163
|
+
- lib/s3_master/config.rb
|
164
|
+
- lib/s3_master/local_policy.rb
|
165
|
+
- lib/s3_master/policy_differ.rb
|
166
|
+
- lib/s3_master/remote_policy.rb
|
167
|
+
- lib/s3_master/version.rb
|
168
|
+
- s3_master.gemspec
|
169
|
+
homepage: https://github.com/bustle/s3_master
|
170
|
+
licenses:
|
171
|
+
- MIT
|
172
|
+
metadata: {}
|
173
|
+
post_install_message:
|
174
|
+
rdoc_options: []
|
175
|
+
require_paths:
|
176
|
+
- lib
|
177
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
requirements: []
|
188
|
+
rubyforge_project:
|
189
|
+
rubygems_version: 2.6.11
|
190
|
+
signing_key:
|
191
|
+
specification_version: 4
|
192
|
+
summary: Cohesive S3 bucket policy management
|
193
|
+
test_files: []
|