Class: S3Master::RemotePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_master/remote_policy.rb

Constant Summary collapse

POLICIES =
{
  lifecycle: {
    get: :get_bucket_lifecycle_configuration,
    put: :put_bucket_lifecycle_configuration,
    delete: :delete_bucket_lifecycle,
    policy_key: :lifecycle_configuration,
  },
  replication: {
    get: :get_bucket_replication,
    put: :put_bucket_replication,
    delete: :delete_bucket_replication,
    policy_merge: true,
    ensure_versioning: true,
  },
  inventory: {
    get: :get_bucket_inventory_configuration,
    put: :put_bucket_inventory_configuration,
    delete: :delete_bucket_inventory_configuration,
    policy_merge: true,
    requires_id: true,
  },
  access: {
    get: :get_bucket_policy,
    put: :put_bucket_policy,
    delete: :delete_bucket_policy,
    policy_key: :policy,
    preserve_keys: true,
  },
  events: {
    get: :get_bucket_notification_configuration,
    put: :put_bucket_notification_configuration,
    policy_key: :notification_configuration,
  },
}
POLICY_TYPES =
POLICIES.keys.freeze
NO_POLICY_EXCEPTIONS =
[
  Aws::S3::Errors::NoSuchBucketPolicy,
  Aws::S3::Errors::NoSuchConfiguration,
  Aws::S3::Errors::NoSuchLifecycleConfiguration,
  Aws::S3::Errors::ReplicationConfigurationNotFoundError,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name, policy_type, options = {}) ⇒ RemotePolicy

Returns a new instance of RemotePolicy.

Raises:

  • (RuntimeError)


50
51
52
53
54
55
56
57
# File 'lib/s3_master/remote_policy.rb', line 50

def initialize(bucket_name, policy_type, options={})
  @client = options[:region].nil? ? Aws::S3::Client.new() : Aws::S3::Client.new(region: options[:region])
  @bucket_name = bucket_name
  @policy_type = policy_type.to_sym
  @options = options
  raise(RuntimeError, "Policy type #{policy_type} not supported") if !POLICIES.has_key?(@policy_type)
  load_policy
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/s3_master/remote_policy.rb', line 5

def body
  @body
end

Class Method Details

.known_policy_type?(policy) ⇒ Boolean

Returns:

  • (Boolean)


123
# File 'lib/s3_master/remote_policy.rb', line 123

def self.known_policy_type?(policy) POLICIES.has_key?(policy.to_sym) ; end

Instance Method Details

#base_argsObject



115
116
117
118
119
120
121
# File 'lib/s3_master/remote_policy.rb', line 115

def base_args
  args = {bucket: @bucket_name}
  if POLICIES[@policy_type][:requires_id]
    args[:id] = @options[:id]
  end
  args
end

#deflate(policy_hash) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/s3_master/remote_policy.rb', line 70

def deflate(policy_hash)
  case @policy_type
  when :access_policy
    policy_hash[policy_key] = JSON.generate(policy_hash[policy_key])
  end
  policy_hash
end

#ensure_versioning!Object



110
111
112
113
# File 'lib/s3_master/remote_policy.rb', line 110

def ensure_versioning!
  bkt = Aws::S3::Bucket.new(@bucket_name, client: @client)
  bkt.versioning.status == "Enabled" || bkt.versioning.enable
end

#inflate(read_policy) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/s3_master/remote_policy.rb', line 62

def inflate(read_policy)
  if @policy_type == :access_policy
    JSON.parse(read_policy[policy_key].string)
  else
    read_policy
  end
end

#known_policy_type?(policy) ⇒ Boolean

Returns:

  • (Boolean)


124
# File 'lib/s3_master/remote_policy.rb', line 124

def known_policy_type?(policy) self.class.known_policy_type(policy) ; end

#load_policyObject



78
79
80
81
82
83
84
85
86
# File 'lib/s3_master/remote_policy.rb', line 78

def load_policy
  begin
    args = base_args
    @body = self.inflate(@client.send(POLICIES[@policy_type][:get], args).to_hash)
  rescue *NO_POLICY_EXCEPTIONS => e
    # No policy there currently
    @body = {}
  end
end

#parse_as_stringObject



60
# File 'lib/s3_master/remote_policy.rb', line 60

def parse_as_string() POLICIES[@policy_type][:parse_as_string] || false ; end

#policy_keyObject



59
# File 'lib/s3_master/remote_policy.rb', line 59

def policy_key() POLICIES[@policy_type][:policy_key] ; end

#pretty_bodyObject



88
# File 'lib/s3_master/remote_policy.rb', line 88

def pretty_body() JSON.neat_generate(body, sort: true) ; end

#write(local_policy) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/s3_master/remote_policy.rb', line 90

def write(local_policy)
  args = base_args

  if local_policy.empty? && POLICIES[@policy_type].has_key?(:delete)
    @client.send(POLICIES[@policy_type][:delete], args)
  else
    if POLICIES[@policy_type][:ensure_versioning]
      self.ensure_versioning!
    end

    if POLICIES[@policy_type][:policy_merge]
      args.merge!(local_policy.body)
    else
      args[policy_key] = local_policy.body
    end

    @client.send(POLICIES[@policy_type][:put], self.deflate(args))
  end
end