Class: S3Secure::Policy::Enforce

Inherits:
Base show all
Defined in:
lib/s3_secure/policy/enforce.rb

Instance Method Summary collapse

Methods inherited from AbstractBase

#buckets

Methods included from Say

#say

Methods included from AwsServices::S3

#check_bucket!, #new_s3_regional_client, #region, #region_map, #s3, #s3_client

Constructor Details

#initialize(options = {}) ⇒ Enforce

Returns a new instance of Enforce.



3
4
5
6
# File 'lib/s3_secure/policy/enforce.rb', line 3

def initialize(options={})
  super
  @sid = options[:sid]
end

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/s3_secure/policy/enforce.rb', line 8

def run
  show = S3Secure::Policy::Show.new(@options)

  bucket_policy = show.policy
  document = Document.new(@bucket, bucket_policy)
  if document.has?(@sid)
    say "Bucket policy for #{@bucket} has ForceSSLOnlyAccess policy statement already:"
    say bucket_policy
  else
    # Set encryption rules
    # Ruby docs: https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#put_bucket_policy-instance_method
    # API docs: https://docs.aws.amazon.com/AmazonS3/latest/API/API_ServerSideEncryptionByDefault.html
    #
    #    put_bucket_policy returns #<struct Aws::EmptyStructure>
    #
    policy_document = document.policy_document(@sid)
    s3.put_bucket_policy(
      bucket: @bucket,
      policy: policy_document,
    )
    say "Add bucket policy to bucket #{@bucket}:"
  end
end