-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from lyrasis/bucket-policy
Add commands to switch client media ingest bucket to public/private
- Loading branch information
Showing
10 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/collectionspace_migration_tools/build/s3_media_client.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module CollectionspaceMigrationTools | ||
module Build | ||
# Returns AWS S3 client for interacting with media ingest bucket | ||
class S3MediaClient < S3Client | ||
def initialize | ||
@profile = CMT.config.system.aws_media_ingest_profile | ||
@bucket = CMT.config.client.media_bucket | ||
end | ||
end | ||
end | ||
end |
101 changes: 101 additions & 0 deletions
101
lib/collectionspace_migration_tools/s3/bucket_policy_setter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# frozen_string_literal: true | ||
|
||
module CollectionspaceMigrationTools | ||
module S3 | ||
class BucketPolicySetter | ||
include Dry::Monads[:result] | ||
include Dry::Monads::Do.for(:call) | ||
|
||
class << self | ||
def call(...) | ||
self.new(...).call | ||
end | ||
end | ||
|
||
# @param policy [:private, :public] | ||
def initialize(policy:) | ||
@policy = policy | ||
@public_access_block = policy == :private ? true : false | ||
end | ||
|
||
def call | ||
@bucket = yield get_bucket | ||
@client = yield CMT::Build::S3MediaClient.call | ||
_block_set = yield set_public_access_block | ||
_policy_set = yield set_policy | ||
|
||
Success() | ||
end | ||
|
||
def to_monad | ||
Success(self) | ||
end | ||
|
||
private | ||
|
||
attr_reader :bucket, :client, :policy, :public_access_block | ||
|
||
def get_bucket | ||
return Failure(CMT::Failure.new( | ||
context: "#{self.class.name}.#{__callee__}", | ||
message: "No client media_bucket setting specified" | ||
)) unless CMT.config.client.respond_to?(:media_bucket) | ||
|
||
Success(CMT.config.client.media_bucket) | ||
end | ||
|
||
def set_public_access_block | ||
resp = client.put_public_access_block({ | ||
bucket: bucket, | ||
public_access_block_configuration: public_access_block_config | ||
}) | ||
rescue => err | ||
msg = "#{err.message} IN #{err.backtrace[0]}" | ||
Failure( | ||
CMT::Failure.new( | ||
context: "#{self.class.name}.#{__callee__}", | ||
message: msg | ||
) | ||
) | ||
else | ||
Success(resp) | ||
end | ||
|
||
def public_access_block_config | ||
{ | ||
block_public_acls: public_access_block, | ||
ignore_public_acls: public_access_block, | ||
block_public_policy: public_access_block, | ||
restrict_public_buckets: public_access_block, | ||
} | ||
end | ||
|
||
def set_policy | ||
if policy == :public | ||
resp = client.put_bucket_policy({ | ||
bucket: bucket, | ||
policy: public_policy | ||
}) | ||
else | ||
resp = client.delete_bucket_policy({ | ||
bucket: bucket | ||
}) | ||
end | ||
rescue => err | ||
msg = "#{err.message} IN #{err.backtrace[0]}" | ||
Failure( | ||
CMT::Failure.new( | ||
context: "#{self.class.name}.#{__callee__}", | ||
message: msg | ||
) | ||
) | ||
else | ||
Success(resp) | ||
end | ||
|
||
def public_policy | ||
"{\"Version\": \"2008-10-17\", \"Statement\": [ { \"Sid\": \"AllowPublicRead\", \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"*\" }, \"Action\": \"s3:GetObject\", \"Resource\": \"arn:aws:s3:::#{bucket}/*\" }, { \"Sid\": \"Stmt1546414471931\", \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"*\" }, \"Action\": \"s3:ListBucket\", \"Resource\": \"arn:aws:s3:::#{bucket}\" } ]}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module CollectionspaceMigrationTools | ||
VERSION = '2.1.0' | ||
VERSION = '2.2.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters