Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add setting to tag s3 objects enabling tag based access control policies (#32988)

Alan Guo Xiang Tanb02bc707dec1
diff --git a/app/jobs/regular/sync_acls_for_uploads.rb b/app/jobs/regular/sync_access_control_for_uploads.rb
similarity index 67%
rename from app/jobs/regular/sync_acls_for_uploads.rb
rename to app/jobs/regular/sync_access_control_for_uploads.rb
index cc7607ae240..fbf180c4c91 100644
--- a/app/jobs/regular/sync_acls_for_uploads.rb
+++ b/app/jobs/regular/sync_access_control_for_uploads.rb
@@ -1,10 +1,9 @@
# frozen_string_literal: true
module Jobs
- # Sometimes we need to update a _lot_ of ACLs on S3 (such as when secure uploads
- # is enabled), and since it takes ~1s per upload to update the ACL, this is
- # best spread out over many jobs instead of having to do the whole thing serially.
- class SyncAclsForUploads < ::Jobs::Base
+ # Sometimes we need to update the access control metadata for a _lot_ of objects on S3 (such as when secure uploads
+ # is enabled), this is best spread out over many jobs instead of having to do the whole thing serially.
+ class SyncAccessControlForUploads < ::Jobs::Base
sidekiq_options queue: "low"
def execute(args)
@@ -17,7 +16,7 @@ module Jobs
.find_in_batches do |uploads|
uploads.each do |upload|
begin
- Discourse.store.update_upload_ACL(upload)
+ Discourse.store.update_upload_access_control(upload)
rescue => err
Discourse.warn_exception(
err,
diff --git a/app/models/upload.rb b/app/models/upload.rb
index be97fdc95de..87bb627cd6a 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -495,7 +495,7 @@ class Upload < ActiveRecord::Base
if secure_status_did_change && SiteSetting.s3_use_acls && Discourse.store.external?
begin
- Discourse.store.update_upload_ACL(self)
+ Discourse.store.update_upload_access_control(self)
rescue Aws::S3::Errors::NotImplemented => err
Discourse.warn_exception(
err,
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index eeb4153f2af..67ce92d1c52 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -357,7 +357,6 @@ en:
s3_backup_requires_s3_settings: "You cannot use S3 as backup location unless you've provided the '%{setting_name}'."
s3_bucket_reused: "You cannot use the same bucket for 's3_upload_bucket' and 's3_backup_bucket'. Choose a different bucket or use a different path for each bucket."
secure_uploads_requirements: "S3 uploads and S3 ACLs must be enabled before enabling secure uploads."
- s3_use_acls_requirements: "You must have S3 ACLs enabled when secure uploads are enabled."
share_quote_facebook_requirements: "You must set a Facebook app id to enable quote sharing for Facebook."
second_factor_cannot_be_enforced_with_disabled_local_login: "You cannot enforce 2FA if local logins are disabled."
second_factor_cannot_be_enforced_with_discourse_connect_enabled: "You cannot enforce 2FA if DiscourseConnect is enabled."
@@ -2029,7 +2028,7 @@ en:
s3_endpoint: "The endpoint can be modified to backup to an S3 compatible service like DigitalOcean Spaces or Minio. WARNING: Leave blank if using AWS S3."
s3_configure_tombstone_policy: "Enable automatic deletion policy for tombstone uploads. IMPORTANT: If disabled, no space will be reclaimed after uploads are deleted."
s3_disable_cleanup: "Prevent removal of old backups from S3 when there are more backups than the maximum allowed."
- s3_use_acls: "AWS recommends not using ACLs on S3 buckets; if you are following this advice, uncheck this option. This must be enabled if you are using secure uploads."
+ s3_use_acls: "AWS recommends not using ACLs on S3 buckets; only uncheck this option if intend to configure permissions policies on the configured `s3_upload_bucket` and `s3_backup_bucket`."
enable_direct_s3_uploads: "Allows for direct multipart uploads to Amazon S3, see https://meta.discourse.org/t/a-new-era-for-file-uploads-in-discourse/210469 for more details."
backup_time_of_day: "Time of day UTC when the backup should occur."
backup_with_uploads: "Include uploads in scheduled backups. Disabling this will only backup the database."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 0f82e565679..9d600d9acea 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2046,6 +2046,24 @@ files:
default: true
s3_use_acls:
default: true
+ s3_enable_access_control_tags:
+ default: false
+ hidden: true
+ s3_access_control_tag_key:
+ default: "discourse:acl"
+ hidden: true
+ min: 1
+ max: 128
+ s3_access_control_tag_public_value:
+ default: "public"
+ hidden: true
+ min: 1
+ max: 256
+ s3_access_control_tag_private_value:
+ default: "private"
+ hidden: true
+ min: 1
+ max: 256
s3_presigned_get_url_expires_after_seconds:
default: 300
hidden: true
diff --git a/lib/backup_restore/s3_backup_store.rb b/lib/backup_restore/s3_backup_store.rb
index 9aadf750086..b793a482a28 100644
--- a/lib/backup_restore/s3_backup_store.rb
+++ b/lib/backup_restore/s3_backup_store.rb
@@ -81,7 +81,13 @@ module BackupRestore
obj = object_from_path(file_name)
raise BackupFileExists.new if obj.exists?
key = temporary_upload_path(file_name)
- s3_helper.create_multipart(key, content_type, metadata: metadata)
+
+ s3_helper.create_multipart(
+ key,
+ content_type,
+ metadata: metadata,
+ **FileStore::S3Store.default_s3_options(secure: true),
+ )
end
def move_existing_stored_upload(
@@ -92,11 +98,11 @@ module BackupRestore
s3_helper.copy(
existing_external_upload_key,
File.join(s3_helper.s3_bucket_folder_path, original_filename),
- options: {
- acl: SiteSetting.s3_use_acls ? "private" : nil,
- apply_metadata_to_destination: true,
- },
+ options: { apply_metadata_to_destination: true }.merge(
+ FileStore::S3Store.default_s3_options(secure: true),
+ ),
)
+
s3_helper.delete_object(existing_external_upload_key)
end
diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb
index 268214390bd..f3ae56f2250 100644
--- a/lib/file_store/s3_store.rb
+++ b/lib/file_store/s3_store.rb
@@ -39,7 +39,7 @@ module FileStore
filename: upload.original_filename,
content_type: content_type,
cache_locally: true,
- private_acl: upload.secure?,
+ private: upload.secure?,
)
url
end
@@ -54,7 +54,7 @@ module FileStore
filename: upload.original_filename,
content_type: content_type,
cache_locally: false,
- private_acl: upload.secure?,
+ private: upload.secure?,
move_existing: true,
existing_external_upload_key: existing_external_upload_key,
)
@@ -65,7 +65,7 @@ module FileStore
optimized_image.url = nil
path = get_path_for_optimized_image(optimized_image)
url, optimized_image.etag =
- store_file(file, path, content_type: content_type, private_acl: secure)
+ store_file(file, path, content_type: content_type, private: secure)
url
end
@@ -80,6 +80,7 @@ module FileStore
# - content_type
# - cache_locally
# - move_existing
+ # - private
# - existing_external_upload_key
def store_file(file, path, opts = {})
path = path.dup
@@ -87,12 +88,12 @@ module FileStore
filename = opts[:filename].presence || File.basename(path)
# cache file locally when needed
cache_file(file, File.basename(path)) if opts[:cache_locally]
+
options = {
- acl: SiteSetting.s3_use_acls ? (opts[:private_acl] ? "private" : "public-read") : nil,
cache_control: "max-age=31556952, public, immutable",
content_type:
opts[:content_type].presence || MiniMime.lookup_by_filename(filename)&.content_type,
- }
+ }.merge(default_s3_options(secure: opts[:private]))
# Only add a "content disposition: attachment" header for svgs
# see https://github.com/discourse/discourse/commit/31e31ef44973dc4daaee2f010d71588ea5873b53.
@@ -130,9 +131,8 @@ module FileStore
s3_helper.remove(path, true)
end
- def copy_file(url, source, destination)
- return unless has_been_uploaded?(url)
- s3_helper.copy(source, destination)
+ def copy_file(source:, destination:, secure:)
+ s3_helper.copy(source, destination, options: default_s3_options(secure:))
end
def has_been_uploaded?(url)
@@ -254,14 +254,12 @@ module FileStore
metadata: {}
)
key = temporary_upload_path(file_name)
+
s3_helper.presigned_request(
key,
method: :put_object,
expires_in: expires_in,
- opts: {
- metadata: metadata,
- acl: SiteSetting.s3_use_acls ? "private" : nil,
- },
+ opts: { metadata: metadata }.merge(default_s3_options(secure: true)),
)
end
@@ -311,21 +309,21 @@ module FileStore
end
end
- def update_upload_ACL(upload)
+ def update_upload_access_control(upload)
key = get_upload_key(upload)
- update_ACL(key, upload.secure?)
+ update_access_control(key, upload.secure?)
upload.optimized_images.each do |optimized_image|
- update_optimized_image_acl(optimized_image, secure: upload.secure)
+ update_optimized_image_access_control(optimized_image, secure: upload.secure)
end
true
end
- def update_optimized_image_acl(optimized_image, secure: false)
+ def update_optimized_image_access_control(optimized_image, secure: false)
optimized_image_key = get_path_for_optimized_image(optimized_image)
optimized_image_key.prepend(File.join(upload_path, "/")) if Rails.configuration.multisite
- update_ACL(optimized_image_key, secure)
+ update_access_control(optimized_image_key, secure)
end
def download_file(upload, destination_path)
@@ -359,7 +357,55 @@ module FileStore
def create_multipart(file_name, content_type, metadata: {})
key = temporary_upload_path(file_name)
- s3_helper.create_multipart(key, content_type, metadata: metadata)
+ s3_helper.create_multipart(key, content_type, metadata:, **default_s3_options(secure: true))
+ end
+
+ # The following are canned ACLs defined by AWS S3 and not some generic value which we decide to use.
+ # https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html
+ CANNED_ACL_PUBLIC_READ = "public-read"
+ CANNED_ACL_PRIVATE = "private"
+
+ def self.acl_option_value(secure:)
+ return if !SiteSetting.s3_use_acls
+ secure ? CANNED_ACL_PRIVATE : CANNED_ACL_PUBLIC_READ
+ end
+
+ def acl_option_value(secure:)
+ self.class.acl_option_value(secure:)
+ end
+
+ def self.visibility_tagging_option_value(secure:, encode_form: true)
+ return if !SiteSetting.s3_enable_access_control_tags
+
+ key = SiteSetting.s3_access_control_tag_key
+ return if key.blank?
+
+ option_value = {
+ key =>
+ (
+ if secure
+ SiteSetting.s3_access_control_tag_private_value
+ else
+ SiteSetting.s3_access_control_tag_public_value
+ end
+ ),
+ }
+
+ encode_form ? URI.encode_www_form(option_value) : option_value
+ end
+
+ def self.default_s3_options(secure:)
+ options = { acl: acl_option_value(secure:) }
+
+ if tagging_option_value = visibility_tagging_option_value(secure:)
+ options[:tagging] = tagging_option_value
+ end
+
+ options
+ end
+
+ def default_s3_options(secure:)
+ self.class.default_s3_options(secure:)
end
private
@@ -391,14 +437,17 @@ module FileStore
end
end
- def update_ACL(key, secure)
- begin
- object_from_path(key).acl.put(
- acl: SiteSetting.s3_use_acls ? (secure ? "private" : "public-read") : nil,
- )
- rescue Aws::S3::Errors::NoSuchKey
- Rails.logger.warn("Could not update ACL on upload with key: '#{key}'. Upload is missing.")
+ def update_access_control(key, secure)
+ object = object_from_path(key).acl.put(acl: acl_option_value(secure:))
+
+ if tagging_option_value =
+ self.class.visibility_tagging_option_value(secure:, encode_form: false)
+ s3_helper.upsert_tag(key, tagging_option_value)
end
+ rescue Aws::S3::Errors::NoSuchKey
+ Rails.logger.warn(
+ "Could not update access control on upload with key: '#{key}'. Upload is missing.",
+ )
end
def list_missing(model, prefix)
diff --git a/lib/file_store/to_s3_migration.rb b/lib/file_store/to_s3_migration.rb
index a6966c389ba..24564630f83 100644
--- a/lib/file_store/to_s3_migration.rb
+++ b/lib/file_store/to_s3_migration.rb
@@ -237,12 +237,11 @@ module FileStore
end
options = {
- acl: SiteSetting.s3_use_acls ? "public-read" : nil,
bucket: bucket,
content_type: MiniMime.lookup_by_filename(name)&.content_type,
content_md5: content_md5,
key: key,
- }
+ }.merge(FileStore::S3Store.default_s3_options(secure: false))
if !FileHelper.is_supported_image?(name)
upload = Upload.find_by(url: "/#{file}")
@@ -254,7 +253,10 @@ module FileStore
)
end
- options[:acl] = "private" if upload&.secure
+ if upload&.secure
+ options[:acl] = FileStore::S3Store.acl_option_value(secure: true)
+ options[:tagging] = FileStore::S3Store.visibility_tagging_option_value(secure: true)
+ end
elsif !FileHelper.is_svg?(name)
upload = Upload.find_by(url: "/#{file}")
options[:content_disposition] = ActionDispatch::Http::ContentDisposition.format(
diff --git a/lib/s3_helper.rb b/lib/s3_helper.rb
index 5cd05c50c6e..f7e6afff244 100644
--- a/lib/s3_helper.rb
+++ b/lib/s3_helper.rb
@@ -244,15 +244,21 @@ class S3Helper
s3_bucket.objects(options)
end
- def tag_file(key, tags)
- tag_array = []
- tags.each { |k, v| tag_array << { key: k.to_s, value: v.to_s } }
+ def upsert_tag(key, tag_key:, tag_value:)
+ tags = s3_resource.client.get_object_tagging(bucket: @s3_bucket_name, key:).tag_set
+ tag_index = tags.find_index { |tag| tag[:key].to_s == tag_key.to_s }
+
+ if tag_index
+ tags[tag_index][:value] = tag_value.to_s
+ else
+ tags << { key: tag_key.to_s, value: tag_value.to_s }
+ end
s3_resource.client.put_object_tagging(
bucket: @s3_bucket_name,
key: key,
tagging: {
- tag_set: tag_array,
+ tag_set: tags,
},
)
end
@@ -300,15 +306,17 @@ class S3Helper
s3_client.abort_multipart_upload(bucket: s3_bucket_name, key: key, upload_id: upload_id)
end
- def create_multipart(key, content_type, metadata: {})
+ def create_multipart(key, content_type, metadata: {}, acl:, tagging: nil)
response =
s3_client.create_multipart_upload(
- acl: SiteSetting.s3_use_acls ? "private" : nil,
+ acl:,
+ tagging:,
bucket: s3_bucket_name,
key: key,
content_type: content_type,
metadata: metadata,
)
+
{ upload_id: response.upload_id, key: key }
end
diff --git a/lib/site_settings/validations.rb b/lib/site_settings/validations.rb
index 4380b1b849a..143b96b012c 100644
--- a/lib/site_settings/validations.rb
+++ b/lib/site_settings/validations.rb
@@ -173,10 +173,6 @@ module SiteSettings::Validations
end
end
- def validate_s3_use_acls(new_val)
- validate_error :s3_use_acls_requirements if new_val == "f" && SiteSetting.secure_uploads
- end
-
def validate_enable_page_publishing(new_val)
validate_error :page_publishing_requirements if new_val == "t" && SiteSetting.secure_uploads?
end
diff --git a/lib/tasks/s3.rake b/lib/tasks/s3.rake
index 9550a714275..5e23853b172 100644
--- a/lib/tasks/s3.rake
+++ b/lib/tasks/s3.rake
@@ -28,8 +28,7 @@ def upload(path, remote_path, content_type, content_encoding = nil, logger:)
options = {
cache_control: "max-age=31556952, public, immutable",
content_type: content_type,
- acl: SiteSetting.s3_use_acls ? "public-read" : nil,
- }
+ }.merge(Discourse.store.default_s3_options(secure: false))
options[:content_encoding] = content_encoding if content_encoding
diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake
index b76fde48d3c..0410617bd0c 100644
--- a/lib/tasks/uploads.rake
+++ b/lib/tasks/uploads.rake
@@ -524,15 +524,16 @@ task "uploads:recover" => :environment do
end
end
-def sync_s3_acls(async: true, concurrency: 10)
+def sync_access_control(async: true, concurrency: 10)
if async
- puts "CAUTION: This task may take a long time to complete! There are #{Upload.count} uploads to sync ACLs for."
+ puts "CAUTION: This task may take a long time to complete! There are #{Upload.count} uploads to sync access control metadata for."
puts ""
puts "-" * 30
- puts "Uploads marked as secure will get a private ACL, and uploads marked as not secure will get a public ACL."
- puts "Upload ACLs will be updated in Sidekiq jobs in batches of 100 at a time, check Sidekiq queues for SyncAclsForUploads for progress."
- Upload.select(:id).find_in_batches(batch_size: 100) { |uploads| adjust_acls(uploads.map(&:id)) }
- puts "", "Upload ACL sync complete!"
+ puts "Syncing access control metadata will be done in Sidekiq jobs in batches of 100 at a time, check Sidekiq queues for `Jobs::SyncAccessControlForUploads` for progress."
+ Upload
+ .select(:id)
+ .find_in_batches(batch_size: 100) { |uploads| adjust_access_controls(uploads.map(&:id)) }
+ puts "", "Syncing access control metadata complete!"
else
executor = Concurrent::ThreadPoolExecutor.new(min_threads: 1, max_threads: concurrency)
errors = []
@@ -544,9 +545,9 @@ def sync_s3_acls(async: true, concurrency: 10)
Concurrent::Future.execute(executor:) do
RailsMultisite::ConnectionManagement.with_connection(current_db) do
begin
- Discourse.store.update_upload_ACL(upload)
+ Discourse.store.update_upload_access_control(upload)
rescue => error
- errors << "Error updating ACL for upload #{upload.url}: #{error.message}"
+ errors << "Error updating access control for upload #{upload.url}: #{error.message}"
end
end
end
@@ -566,13 +567,21 @@ def sync_s3_acls(async: true, concurrency: 10)
end
end
+# This task is defined for backwards compatibility
task "uploads:sync_s3_acls", %i[synchronous parallel] => :environment do |_, args|
+ Rake::Task["uploads:sync_access_control"].invoke(
+ synchronous: args[:synchronous],
+ parallel: args[:parallel],
+ )
+end
+
+task "uploads:sync_access_control", %i[synchronous parallel] => :environment do |_, args|
unless Discourse.store.external?
puts "This task only works for external storage."
exit 1
end
- method = :sync_s3_acls
+ method = :sync_access_control
method_args = { async: !args.key?(:synchronous) || !(args[:synchronous] == "true") }
method_args[:concurrency] = args[:parallel].to_i if args.key?(:parallel)
@@ -628,7 +637,7 @@ task "uploads:disable_secure_uploads" => :environment do
secure_upload_ids,
)
- adjust_acls(secure_upload_ids)
+ adjust_access_controls(secure_upload_ids)
mark_upload_posts_for_rebake(post_ids_to_rebake)
end
@@ -641,7 +650,7 @@ end
##
# Run this task whenever the secure_uploads or login_required
# settings are changed for a Discourse instance to update
-# the upload secure flag and S3 upload ACLs. Any uploads that
+# the upload secure flag and S3 access control metadata. Any uploads that
# have their secure status changed will have all associated posts
# rebaked.
#
@@ -704,25 +713,26 @@ task "uploads:secure_upload_analyse_and_update" => :environment do
mark_upload_posts_for_rebake(post_ids_to_rebake)
# Also do this AFTER upload transaction complete so we don't end up with any
- # errors leaving ACLs in a bad state (the ACL sync task can be run to fix any
- # outliers at any time).
- adjust_acls(all_upload_ids_changed)
+ # errors leaving access control in a bad state
+ adjust_access_controls(all_upload_ids_changed)
end
puts "", "", "Done!"
end
-def adjust_acls(upload_ids_to_adjust_acl_for)
- jobs_to_create = (upload_ids_to_adjust_acl_for.count.to_f / 100.00).ceil
+def adjust_access_controls(upload_ids_to_adjust_access_control_for)
+ jobs_to_create = (upload_ids_to_adjust_access_control_for.count.to_f / 100.00).ceil
if jobs_to_create > 1
- puts "Adjusting ACLs for #{upload_ids_to_adjust_acl_for} uploads. These will be batched across #{jobs_to_create} sync job(s)."
+ puts "Adjusting access control metadata for #{upload_ids_to_adjust_access_control_for} uploads. These will be batched across #{jobs_to_create} sync job(s)."
end
- upload_ids_to_adjust_acl_for.each_slice(100) do |upload_ids|
- Jobs.enqueue(:sync_acls_for_uploads, upload_ids: upload_ids)
+ upload_ids_to_adjust_access_control_for.each_slice(100) do |upload_ids|
+ Jobs.enqueue(:sync_access_control_for_uploads, upload_ids: upload_ids)
end
- puts "ACL batching complete. Keep an eye on the Sidekiq queue for progress." if jobs_to_create > 1
+ if jobs_to_create > 1
+ puts "Adjusting access control metadata batching complete. Keep an eye on the Sidekiq queue for progress."
+ end
end
def mark_all_as_secure_login_required
diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb
index 5ac0e556a2b..34f9ffe6682 100644
--- a/lib/upload_recovery.rb
+++ b/lib/upload_recovery.rb
@@ -149,14 +149,7 @@ class UploadRecovery
if key.include?(tombstone_prefix)
old_key = key
key = key.sub(tombstone_prefix, "")
-
- Discourse.store.s3_helper.copy(
- old_key,
- key,
- options: {
- acl: SiteSetting.s3_use_acls ? "public-read" : nil,
- },
- )
+ Discourse.store.copy_file(source: old_key, destination: key, secure: false)
end
next if upload_exists
diff --git a/spec/jobs/sync_acls_for_uploads_spec.rb b/spec/jobs/sync_access_control_for_uploads_spec.rb
similarity index 76%
rename from spec/jobs/sync_acls_for_uploads_spec.rb
rename to spec/jobs/sync_access_control_for_uploads_spec.rb
index b2bc37bc5c5..953e601ef1f 100644
--- a/spec/jobs/sync_acls_for_uploads_spec.rb
+++ b/spec/jobs/sync_access_control_for_uploads_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-RSpec.describe Jobs::SyncAclsForUploads do
+RSpec.describe Jobs::SyncAccessControlForUploads do
let(:upload1) { Fabricate(:upload) }
let(:upload2) { Fabricate(:upload) }
let(:upload3) { Fabricate(:secure_upload) }
@@ -21,15 +21,15 @@ RSpec.describe Jobs::SyncAclsForUploads do
stub_s3_store
end
- it "runs update_upload_ACL for each upload" do
- Discourse.store.expects(:update_upload_ACL).times(3)
+ it "runs update_upload_access_control for each upload" do
+ Discourse.store.expects(:update_upload_access_control).times(3)
run_job
end
it "handles updates throwing an exception" do
Discourse
.store
- .expects(:update_upload_ACL)
+ .expects(:update_upload_access_control)
.raises(StandardError)
.then
.returns(true, true)
diff --git a/spec/lib/backup_restore/s3_backup_store_spec.rb b/spec/lib/backup_restore/s3_backup_store_spec.rb
index bb3ecd2ca95..15bf71eda39 100644
--- a/spec/lib/backup_restore/s3_backup_store_spec.rb
+++ b/spec/lib/backup_restore/s3_backup_store_spec.rb
@@ -188,4 +188,100 @@ RSpec.describe BackupRestore::S3BackupStore do
def file_prefix(db_name, multisite)
multisite ? "\\/#{db_name}" : ""
end
+
+ describe "#create_multipart" do
+ it "should set the ACL context when `s3_use_acls` site setting is enabled" do
+ SiteSetting.s3_use_acls = true
+ response = store.create_multipart("test_file.tar.gz", "application/gzip", metadata: {})
+
+ create_multipart_upload_request =
+ @s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_upload_request[:context].params[:acl]).to eq(
+ FileStore::S3Store::CANNED_ACL_PRIVATE,
+ )
+ end
+
+ it "should not set the ACL context when `s3_use_acls` site setting is disabled" do
+ SiteSetting.s3_use_acls = false
+ store.create_multipart("test_file.tar.gz", "application/gzip", metadata: {})
+
+ create_multipart_upload_request =
+ @s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_upload_request[:context].params[:acl]).to eq(nil)
+ end
+
+ it "should set the tagging context when `s3_enable_access_control_tags` site setting is enabled" do
+ SiteSetting.s3_enable_access_control_tags = true
+ store.create_multipart("test_file.tar.gz", "application/gzip", metadata: {})
+
+ create_multipart_upload_request =
+ @s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(
+ URI.decode_www_form_component(create_multipart_upload_request[:context].params[:tagging]),
+ ).to eq(
+ "#{SiteSetting.s3_access_control_tag_key}=#{SiteSetting.s3_access_control_tag_private_value}",
+ )
+ end
+ end
+
+ describe "#move_existing_stored_upload" do
+ before { create_backups }
+ after { remove_backups }
+
+ it "should set the ACL context when `s3_use_acls` site setting is enabled" do
+ store.move_existing_stored_upload(
+ existing_external_upload_key: "default/b.tar.gz",
+ original_filename: "b.tar.gz",
+ content_type: "application/gzip",
+ )
+
+ copy_object_request =
+ @s3_client.api_requests.find { |api_request| api_request[:operation_name] == :copy_object }
+
+ expect(copy_object_request[:context].params[:acl]).to eq(
+ FileStore::S3Store::CANNED_ACL_PRIVATE,
+ )
+ end
+
+ it "should not set the ACL context when `s3_use_acls` site setting is disabled" do
+ SiteSetting.s3_use_acls = false
+
+ store.move_existing_stored_upload(
+ existing_external_upload_key: "default/b.tar.gz",
+ original_filename: "b.tar.gz",
+ content_type: "application/gzip",
+ )
+
+ copy_object_request =
+ @s3_client.api_requests.find { |api_request| api_request[:operation_name] == :copy_object }
+
+ expect(copy_object_request[:context].params[:acl]).to eq(nil)
+ end
+
+ it "should set the tagging context when `s3_enable_access_control_tags` site setting is enabled" do
+ SiteSetting.s3_enable_access_control_tags = true
+
+ store.move_existing_stored_upload(
+ existing_external_upload_key: "default/b.tar.gz",
+ original_filename: "b.tar.gz",
+ content_type: "application/gzip",
+ )
+
+ copy_object_request =
+ @s3_client.api_requests.find { |api_request| api_request[:operation_name] == :copy_object }
+
+ expect(URI.decode_www_form_component(copy_object_request[:context].params[:tagging])).to eq(
+ "#{SiteSetting.s3_access_control_tag_key}=#{SiteSetting.s3_access_control_tag_private_value}",
+ )
+ end
+ end
end
diff --git a/spec/lib/file_store/s3_store_spec.rb b/spec/lib/file_store/s3_store_spec.rb
index c01766bfcbc..298fbf03a8f 100644
--- a/spec/lib/file_store/s3_store_spec.rb
+++ b/spec/lib/file_store/s3_store_spec.rb
@@ -28,15 +28,17 @@ RSpec.describe FileStore::S3Store do
describe "#store_upload" do
it "returns an absolute schemaless url" do
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
+
s3_bucket
.expects(:object)
.with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.png}))
.returns(s3_object)
+
s3_object
.expects(:put)
.with(
{
- acl: "public-read",
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
cache_control: "max-age=31556952, public, immutable",
content_type: "image/png",
content_disposition: "inline; filename=\"logo.png\"; filename*=UTF-8''logo.png",
@@ -48,6 +50,7 @@ RSpec.describe FileStore::S3Store do
expect(store.store_upload(uploaded_file, upload)).to match(
%r{//s3-upload-bucket\.s3\.dualstack\.us-west-1\.amazonaws\.com/original/\d+X.*/#{upload.sha1}\.png},
)
+
expect(upload.etag).to eq(etag)
end
@@ -68,6 +71,7 @@ RSpec.describe FileStore::S3Store do
expect(store.store_upload(uploaded_file, upload)).to match(
%r{//s3-upload-bucket\.s3\.dualstack\.us-west-1\.amazonaws\.com/discourse-uploads/original/\d+X.*/#{upload.sha1}\.png},
)
+
expect(upload.etag).to eq(etag)
end
end
@@ -76,19 +80,22 @@ RSpec.describe FileStore::S3Store do
it "saves secure attachment using private ACL" do
SiteSetting.prevent_anons_from_downloading_files = true
SiteSetting.authorized_extensions = "pdf|png|jpg|gif"
+
upload =
Fabricate(:upload, original_filename: "small.pdf", extension: "pdf", secure: true)
s3_helper.expects(:s3_bucket).returns(s3_bucket)
+
s3_bucket
.expects(:object)
.with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.pdf}))
.returns(s3_object)
+
s3_object
.expects(:put)
.with(
{
- acl: "private",
+ acl: FileStore::S3Store::CANNED_ACL_PRIVATE,
cache_control: "max-age=31556952, public, immutable",
content_type: "application/pdf",
content_disposition: "inline; filename=\"small.pdf\"; filename*=UTF-8''small.pdf",
@@ -106,16 +113,18 @@ RSpec.describe FileStore::S3Store do
SiteSetting.prevent_anons_from_downloading_files = true
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
+
s3_bucket
.expects(:object)
.with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.png}))
.returns(s3_object)
.at_least_once
+
s3_object
.expects(:put)
.with(
{
- acl: "public-read",
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
cache_control: "max-age=31556952, public, immutable",
content_type: "image/png",
content_disposition: "inline; filename=\"logo.png\"; filename*=UTF-8''logo.png",
@@ -130,20 +139,54 @@ RSpec.describe FileStore::S3Store do
expect(store.url_for(upload)).to eq(upload.url)
end
- end
- describe "when ACLs are disabled" do
- it "doesn't supply an ACL" do
+ it "supplies the right tag when `s3_enable_access_control_tags` site setting is enabled" do
+ SiteSetting.s3_enable_access_control_tags = true
+ SiteSetting.authorized_extensions = "pdf|png|jpg|gif"
+
+ upload =
+ Fabricate(:upload, original_filename: "small.pdf", extension: "pdf", secure: true)
+
+ s3_helper.expects(:s3_bucket).returns(s3_bucket)
+
+ s3_bucket
+ .expects(:object)
+ .with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.pdf}))
+ .returns(s3_object)
+
+ s3_object
+ .expects(:put)
+ .with(
+ {
+ acl: FileStore::S3Store::CANNED_ACL_PRIVATE,
+ tagging: described_class.visibility_tagging_option_value(secure: true),
+ cache_control: "max-age=31556952, public, immutable",
+ content_type: "application/pdf",
+ content_disposition: "inline; filename=\"small.pdf\"; filename*=UTF-8''small.pdf",
+ body: uploaded_file,
+ },
+ )
+ .returns(Aws::S3::Types::PutObjectOutput.new(etag: "\"#{etag}\""))
+
+ expect(store.store_upload(uploaded_file, upload)).to match(
+ %r{//s3-upload-bucket\.s3\.dualstack\.us-west-1\.amazonaws\.com/original/\d+X.*/#{upload.sha1}\.pdf},
+ )
+ end
+
+ it "doesn't supply an ACL when `s3_use_acls` site setting is disabled" do
SiteSetting.s3_use_acls = false
SiteSetting.authorized_extensions = "pdf|png|jpg|gif"
+
upload =
Fabricate(:upload, original_filename: "small.pdf", extension: "pdf", secure: true)
s3_helper.expects(:s3_bucket).returns(s3_bucket)
+
s3_bucket
.expects(:object)
.with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.pdf}))
.returns(s3_object)
+
s3_object
.expects(:put)
.with(
@@ -209,7 +252,7 @@ RSpec.describe FileStore::S3Store do
let(:store) { FileStore::S3Store.new(s3_helper) }
let(:upload_opts) do
{
- acl: "public-read",
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
cache_control: "max-age=31556952, public, immutable",
content_type: "image/png",
apply_metadata_to_destination: true,
@@ -256,7 +299,43 @@ RSpec.describe FileStore::S3Store do
describe "copying files in S3" do
describe "#copy_file" do
- it "copies the from in S3 with the right paths" do
+ it "copies the file within S3 with the right paths and request params when `s3_enable_access_control_tags` site setting is enabled and `secure` opts is false" do
+ SiteSetting.s3_enable_access_control_tags = true
+
+ upload.update!(
+ url:
+ "//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png",
+ )
+
+ source = "#{upload_path}/#{Discourse.store.get_path_for_upload(upload)}"
+ destination = source.sub(".png", ".jpg")
+ bucket = prepare_fake_s3(source, upload)
+
+ expect(bucket.find_object(source)).to be_present
+ expect(bucket.find_object(destination)).to be_nil
+
+ store.copy_file(source:, destination:, secure: false)
+
+ copy_api_request =
+ bucket.s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :copy_object
+ end
+
+ expect(copy_api_request[:context].params[:acl]).to eq(
+ FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
+ )
+
+ expect(copy_api_request[:context].params[:tagging]).to eq(
+ FileStore::S3Store.visibility_tagging_option_value(secure: false),
+ )
+
+ expect(bucket.find_object(source)).to be_present
+ expect(bucket.find_object(destination)).to be_present
+ end
+
+ it "copies the file within S3 with the right paths and request params when `s3_enable_access_control_tags` site setting is enabled and `secure` opts is true" do
+ SiteSetting.s3_enable_access_control_tags = true
+
upload.update!(
url:
"//s3-upload-bucket.s3.dualstack.us-west-1.amazonaws.com/original/1X/#{upload.sha1}.png",
@@ -269,7 +348,20 @@ RSpec.describe FileStore::S3Store do
expect(bucket.find_object(source)).to be_present
expect(bucket.find_object(destination)).to be_nil
- store.copy_file(upload.url, source, destination)
+ store.copy_file(source:, destination:, secure: true)
+
+ copy_api_request =
+ bucket.s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :copy_object
+ end
+
+ expect(copy_api_request[:context].params[:acl]).to eq(
+ FileStore::S3Store::CANNED_ACL_PRIVATE,
+ )
+
+ expect(copy_api_request[:context].params[:tagging]).to eq(
+ FileStore::S3Store.visibility_tagging_option_value(secure: true),
+ )
expect(bucket.find_object(source)).to be_present
expect(bucket.find_object(destination)).to be_present
@@ -468,45 +560,78 @@ RSpec.describe FileStore::S3Store do
describe "update ACL" do
before { SiteSetting.authorized_extensions = "pdf|png" }
- describe ".update_upload_ACL" do
+ describe ".update_upload_access_control" do
let(:upload) { Fabricate(:upload, original_filename: "small.pdf", extension: "pdf") }
it "sets acl to public by default" do
s3_helper.expects(:s3_bucket).returns(s3_bucket)
- expect_upload_acl_update(upload, "public-read")
+ expect_upload_access_control_update(upload, FileStore::S3Store::CANNED_ACL_PUBLIC_READ)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
end
it "sets acl to private when upload is marked secure" do
upload.update!(secure: true)
s3_helper.expects(:s3_bucket).returns(s3_bucket)
- expect_upload_acl_update(upload, "private")
+ expect_upload_access_control_update(upload, FileStore::S3Store::CANNED_ACL_PRIVATE)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
+ end
+
+ describe "when `s3_enable_access_control_tags` site setting is enabled" do
+ before do
+ SiteSetting.s3_enable_access_control_tags = true
+ s3_helper.stub_client_responses!
+ end
+
+ it "set the right tagging option for a public upload" do
+ s3_helper.expects(:upsert_tag).with(
+ upload.url,
+ FileStore::S3Store.visibility_tagging_option_value(secure: false, encode_form: false),
+ )
+
+ upload.update!(secure: false)
+
+ store.update_upload_access_control(upload)
+ end
+
+ it "sets the right tagging option for a secure upload" do
+ s3_helper.expects(:upsert_tag).with(
+ upload.url,
+ FileStore::S3Store.visibility_tagging_option_value(secure: true, encode_form: false),
+ )
+
+ upload.update!(secure: true)
+
+ store.update_upload_access_control(upload)
+ end
end
describe "optimized images" do
it "sets acl to public by default" do
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
- expect_upload_acl_update(upload, "public-read")
+ expect_upload_access_control_update(upload, FileStore::S3Store::CANNED_ACL_PUBLIC_READ)
optimized_image = Fabricate(:optimized_image, upload: upload)
path = Discourse.store.get_path_for_optimized_image(optimized_image)
stub_optimized_image = stub
s3_bucket.expects(:object).with(path).returns(stub_optimized_image)
stub_optimized_image.expects(:acl).returns(stub_optimized_image)
- stub_optimized_image.expects(:put).with(acl: "public-read").returns(stub_optimized_image)
+ stub_optimized_image
+ .expects(:put)
+ .with(acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ)
+ .returns(stub_optimized_image)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
end
end
- def expect_upload_acl_update(upload, acl)
+ def expect_upload_access_control_update(upload, acl)
s3_bucket
.expects(:object)
.with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.pdf}))
.returns(s3_object)
+
s3_object.expects(:acl).returns(s3_object)
s3_object.expects(:put).with(acl: acl).returns(s3_object)
end
@@ -580,6 +705,49 @@ RSpec.describe FileStore::S3Store do
end
end
+ describe "#create_multipart" do
+ before { store.s3_helper.stub_client_responses! }
+
+ it "should create a multipart upload with the ACL parameter set to private canned ACL when `s3_use_acls` site setting is enabled" do
+ store.create_multipart("test_file.tar.gz", "application/gzip", metadata: {})
+
+ create_multipart_request =
+ store.s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_request[:context].params[:acl]).to eq(
+ FileStore::S3Store::CANNED_ACL_PRIVATE,
+ )
+ end
+
+ it "should create a multipart upload with the ACL parameter set to nil when `s3_use_acls` site setting is disabled" do
+ SiteSetting.s3_use_acls = false
+ store.create_multipart("test_file.tar.gz", "application/gzip", metadata: {})
+
+ create_multipart_request =
+ store.s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_request[:context].params[:acl]).to eq(nil)
+ end
+
+ it "should create a multipart upload with the tagging parameter set to visibility tags when `s3_enable_access_control_tags` site setting is enabled" do
+ SiteSetting.s3_enable_access_control_tags = true
+ store.create_multipart("test_file.tar.gz", "application/gzip", metadata: {})
+
+ create_multipart_request =
+ store.s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_request[:context].params[:tagging]).to eq(
+ FileStore::S3Store.visibility_tagging_option_value(secure: true),
+ )
+ end
+ end
+
def prepare_fake_s3(upload_key, upload)
@fake_s3 = FakeS3.create
@fake_s3_bucket = @fake_s3.bucket(SiteSetting.s3_upload_bucket)
diff --git a/spec/lib/s3_helper_spec.rb b/spec/lib/s3_helper_spec.rb
index 97aa29efb3c..5f9b25c302b 100644
--- a/spec/lib/s3_helper_spec.rb
+++ b/spec/lib/s3_helper_spec.rb
@@ -286,4 +286,98 @@ RSpec.describe "S3Helper" do
end
end
end
+
+ describe "#create_multipart" do
+ it "creates a multipart upload with the right ACL parameters when `acl` kwarg is set" do
+ s3_helper = S3Helper.new("some-bucket", "", client: client)
+
+ s3_helper.create_multipart(
+ "test_file.tar.gz",
+ "application/gzip",
+ metadata: {
+ },
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
+ )
+
+ create_multipart_upload_request =
+ client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_upload_request[:context].params[:acl]).to eq(
+ FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
+ )
+ end
+
+ it "creates a multipart upload without ACL parameters when only the `tagging` kwarg is set" do
+ s3_helper = S3Helper.new("some-bucket", "", client: client)
+
+ s3_helper.create_multipart(
+ "test_file.tar.gz",
+ "application/gzip",
+ metadata: {
+ },
+ acl: nil,
+ tagging: "some:tag",
+ )
+
+ create_multipart_upload_request =
+ client.api_requests.find do |api_request|
+ api_request[:operation_name] == :create_multipart_upload
+ end
+
+ expect(create_multipart_upload_request[:context].params[:acl]).to be_nil
+ expect(create_multipart_upload_request[:context].params[:tagging]).to eq("some:tag")
+ end
+ end
+
+ describe "#upsert_tag" do
+ it "correctly updates an existing tag" do
+ s3_helper = S3Helper.new("some-bucket", "", client: client)
+
+ s3_helper.s3_client.stub_responses(
+ :get_object_tagging,
+ Aws::S3::Types::GetObjectTaggingOutput.new(
+ { tag_set: [{ key: "tag1", value: "value1" }, { key: "tag2", value: "value2" }] },
+ ),
+ )
+
+ s3_helper.upsert_tag("some/key", tag_key: "tag1", tag_value: "newvalue")
+
+ put_object_tagging_request =
+ s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :put_object_tagging
+ end
+
+ expect(put_object_tagging_request[:context].params[:tagging][:tag_set]).to eq(
+ [{ key: "tag1", value: "newvalue" }, { key: "tag2", value: "value2" }],
+ )
+ end
+
+ it "correctly adds a new tag" do
+ s3_helper = S3Helper.new("some-bucket", "", client: client)
+
+ s3_helper.s3_client.stub_responses(
+ :get_object_tagging,
+ Aws::S3::Types::GetObjectTaggingOutput.new(
+ { tag_set: [{ key: "tag1", value: "value1" }, { key: "tag2", value: "value2" }] },
+ ),
+ )
+
+ s3_helper.upsert_tag("some/key", tag_key: "mytag", tag_value: "myvalue")
+
+ put_object_tagging_request =
+ s3_helper.s3_client.api_requests.find do |api_request|
+ api_request[:operation_name] == :put_object_tagging
+ end
+
+ expect(put_object_tagging_request[:context].params[:tagging][:tag_set]).to eq(
+ [
+ { key: "tag1", value: "value1" },
+ { key: "tag2", value: "value2" },
+ { key: "mytag", value: "myvalue" },
+ ],
+ )
+ end
+ end
end
diff --git a/spec/lib/site_settings/validations_spec.rb b/spec/lib/site_settings/validations_spec.rb
index 7706ff327e3..9162e4bd28d 100644
--- a/spec/lib/site_settings/validations_spec.rb
+++ b/spec/lib/site_settings/validations_spec.rb
@@ -247,32 +247,6 @@ RSpec.describe SiteSettings::Validations do
end
end
- describe "#validate_s3_use_acls" do
- context "when the new value is true" do
- it "is ok" do
- expect { validations.validate_s3_use_acls("t") }.not_to raise_error
- end
- end
-
- context "when the new value is false" do
- it "is ok" do
- expect { validations.validate_s3_use_acls("f") }.not_to raise_error
- end
-
- context "if secure uploads is enabled" do
- let(:error_message) { I18n.t("errors.site_settings.s3_use_acls_requirements") }
- before { enable_secure_uploads }
-
- it "is not ok" do
- expect { validations.validate_s3_use_acls("f") }.to raise_error(
- Discourse::InvalidParameters,
- error_message,
- )
- end
- end
- end
- end
-
describe "#validate_secure_uploads" do
let(:error_message) { I18n.t("errors.site_settings.secure_uploads_requirements") }
diff --git a/spec/lib/upload_recovery_spec.rb b/spec/lib/upload_recovery_spec.rb
index ead6eea7dae..ae25d8e7b07 100644
--- a/spec/lib/upload_recovery_spec.rb
+++ b/spec/lib/upload_recovery_spec.rb
@@ -103,17 +103,25 @@ RSpec.describe UploadRecovery do
tombstone_copy.expects(:key).returns(tombstone_key)
Discourse.store.s3_helper.expects(:list).with("original").returns([])
+
Discourse
.store
.s3_helper
.expects(:list)
.with("#{FileStore::S3Store::TOMBSTONE_PREFIX}original")
.returns([tombstone_copy])
+
Discourse
.store
.s3_helper
.expects(:copy)
- .with(tombstone_key, original_key, options: { acl: "public-read" })
+ .with(
+ tombstone_key,
+ original_key,
+ options: {
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
+ },
+ )
FileHelper.expects(:download).returns(file_from_fixtures("smallest.png"))
stub_request(:get, upload.url).to_return(body: file_from_fixtures("smallest.png"))
@@ -133,17 +141,25 @@ RSpec.describe UploadRecovery do
tombstone_copy.expects(:key).returns(tombstone_key)
Discourse.store.s3_helper.expects(:list).with("original").returns([])
+
Discourse
.store
.s3_helper
.expects(:list)
.with("#{FileStore::S3Store::TOMBSTONE_PREFIX}original")
.returns([tombstone_copy])
+
Discourse
.store
.s3_helper
.expects(:copy)
- .with(tombstone_key, original_key, options: { acl: "public-read" })
+ .with(
+ tombstone_key,
+ original_key,
+ options: {
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
+ },
+ )
expect do upload_recovery.recover end.to_not change {
[post.reload.uploads.count, Upload.count]
@@ -172,7 +188,13 @@ RSpec.describe UploadRecovery do
.store
.s3_helper
.expects(:copy)
- .with(tombstone_key, original_key, options: { acl: "public-read" })
+ .with(
+ tombstone_key,
+ original_key,
+ options: {
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
+ },
+ )
expect do upload_recovery.recover end.to_not change {
[post.reload.uploads.count, Upload.count]
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 64a4d24d79e..69bb519f1cf 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -457,7 +457,7 @@ RSpec.describe Upload do
end
it "marks the upload as not secure if its access control post is a public post" do
- FileStore::S3Store.any_instance.expects(:update_upload_ACL).with(upload)
+ FileStore::S3Store.any_instance.expects(:update_upload_access_control).with(upload)
upload.update!(secure: true, access_control_post: Fabricate(:post))
upload.update_secure_status
expect(upload.secure).to eq(false)
@@ -470,7 +470,7 @@ RSpec.describe Upload do
end
it "does not attempt to change the ACL if the secure status has not changed" do
- FileStore::S3Store.any_instance.expects(:update_upload_ACL).with(upload).never
+ FileStore::S3Store.any_instance.expects(:update_upload_access_control).with(upload).never
upload.update!(secure: true, access_control_post: Fabricate(:private_message_post))
upload.update_secure_status
end
@@ -478,7 +478,7 @@ RSpec.describe Upload do
it "does not attempt to change the ACL if s3_use_acls is disabled" do
SiteSetting.secure_uploads = false
SiteSetting.s3_use_acls = false
- FileStore::S3Store.any_instance.expects(:update_upload_ACL).with(upload).never
+ FileStore::S3Store.any_instance.expects(:update_upload_access_control).with(upload).never
upload.update!(secure: true, access_control_post: Fabricate(:post))
upload.update_secure_status
expect(upload.secure).to eq(false)
@@ -522,7 +522,7 @@ RSpec.describe Upload do
it "does not throw an error if the object storage provider does not support ACLs" do
FileStore::S3Store
.any_instance
- .stubs(:update_upload_ACL)
+ .stubs(:update_upload_access_control)
.raises(
Aws::S3::Errors::NotImplemented.new(
"A header you provided implies functionality that is not implemented",
diff --git a/spec/multisite/s3_store_spec.rb b/spec/multisite/s3_store_spec.rb
index 2342ee622a8..aa36527278f 100644
--- a/spec/multisite/s3_store_spec.rb
+++ b/spec/multisite/s3_store_spec.rb
@@ -27,7 +27,7 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
let(:store) { FileStore::S3Store.new(s3_helper) }
let(:upload_opts) do
{
- acl: "public-read",
+ acl: FileStore::S3Store::CANNED_ACL_PUBLIC_READ,
cache_control: "max-age=31556952, public, immutable",
content_type: "image/png",
}
@@ -260,7 +260,7 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
end
end
- describe "#update_upload_ACL" do
+ describe "#update_upload_access_control" do
it "updates correct file for default and second multisite db" do
test_multisite_connection("default") do
upload = build_upload(secure: true)
@@ -269,7 +269,7 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
expect_upload_acl_update(upload, upload_path)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
end
test_multisite_connection("second") do
@@ -280,7 +280,7 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once
expect_upload_acl_update(upload, upload_path)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
end
end
@@ -294,7 +294,7 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
expect_upload_acl_update(upload, upload_path)
expect_optimized_image_acl_update(optimized_image, upload_path)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
end
test_multisite_connection("second") do
@@ -305,7 +305,7 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
expect_upload_acl_update(upload, upload_path)
expect_optimized_image_acl_update(optimized_image, upload_path)
- expect(store.update_upload_ACL(upload)).to be_truthy
+ expect(store.update_upload_access_control(upload)).to be_truthy
end
end
end
@@ -316,14 +316,14 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
.with("#{upload_path}/original/1X/#{upload.sha1}.#{upload.extension}")
.returns(s3_object)
s3_object.expects(:acl).returns(s3_object)
- s3_object.expects(:put).with(acl: "private").returns(s3_object)
+ s3_object.expects(:put).with(acl: FileStore::S3Store::CANNED_ACL_PRIVATE).returns(s3_object)
end
def expect_optimized_image_acl_update(optimized_image, upload_path)
path = Discourse.store.get_path_for_optimized_image(optimized_image)
s3_bucket.expects(:object).with("#{upload_path}/#{path}").returns(s3_object)
s3_object.expects(:acl).returns(s3_object)
- s3_object.expects(:put).with(acl: "private").returns(s3_object)
+ s3_object.expects(:put).with(acl: FileStore::S3Store::CANNED_ACL_PRIVATE).returns(s3_object)
end
end
end
@@ -383,11 +383,42 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
context "for a bucket with no folder path" do
before { SiteSetting.s3_upload_bucket = "s3-upload-bucket" }
- it "returns a presigned url and headers with the correct params and the key for the temporary file" do
+ it "returns a presigned url and headers with the correct ACL param and the key for the temporary file" do
url, signed_headers = store.signed_request_for_temporary_upload("test.png")
key = store.s3_helper.path_from_url(url)
- expect(signed_headers).to eq("x-amz-acl" => "private")
+
+ expect(signed_headers).to eq("x-amz-acl" => FileStore::S3Store::CANNED_ACL_PRIVATE)
+ expect(url).to match(/Amz-Expires/)
+ expect(key).to match(
+ /temp\/uploads\/default\/test_[0-9]\/[a-zA-z0-9]{0,32}\/[a-zA-z0-9]{0,32}.png/,
+ )
+ end
+
+ it "returns a presigned url and headers with the correct ACL param and the key for the temporary file when `s3_use_acls` is disabled" do
+ SiteSetting.s3_use_acls = false
+ url, signed_headers = store.signed_request_for_temporary_upload("test.png")
+ key = store.s3_helper.path_from_url(url)
+
+ expect(signed_headers).to eq({})
+ expect(url).to match(/Amz-Expires/)
+
+ expect(key).to match(
+ /temp\/uploads\/default\/test_[0-9]\/[a-zA-z0-9]{0,32}\/[a-zA-z0-9]{0,32}.png/,
+ )
+ end
+
+ it "returns a presigned url and headers with the correct tagging params when ``s3_enable_access_control_tags` is enabled" do
+ SiteSetting.s3_enable_access_control_tags = true
+ url, signed_headers = store.signed_request_for_temporary_upload("test.png")
+ key = store.s3_helper.path_from_url(url)
+
+ expect(signed_headers).to eq(
+ "x-amz-acl" => FileStore::S3Store::CANNED_ACL_PRIVATE,
+ "x-amz-tagging" => FileStore::S3Store.visibility_tagging_option_value(secure: true),
+ )
+
expect(url).to match(/Amz-Expires/)
+
expect(key).to match(
/temp\/uploads\/default\/test_[0-9]\/[a-zA-z0-9]{0,32}\/[a-zA-z0-9]{0,32}.png/,
)
@@ -401,7 +432,12 @@ RSpec.describe "Multisite s3 uploads", type: :multisite do
"test-meta": "testing",
},
)
- expect(signed_headers).to eq("x-amz-acl" => "private", "x-amz-meta-test-meta" => "testing")
+
+ expect(signed_headers).to eq(
+ "x-amz-acl" => FileStore::S3Store::CANNED_ACL_PRIVATE,
+ "x-amz-meta-test-meta" => "testing",
+ )
+
expect(url).not_to include("&x-amz-meta-test-meta=testing")
end
end
diff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rb
index e4ad6d22ad2..c7c9015e0a7 100644
--- a/spec/requests/uploads_controller_spec.rb
+++ b/spec/requests/uploads_controller_spec.rb
@@ -921,7 +921,7 @@ RSpec.describe UploadsController do
expect(result["url"]).not_to include("&x-amz-meta-sha1-checksum=testing")
expect(result["url"]).not_to include("&x-amz-meta-blah=wontbeincluded")
expect(result["signed_headers"]).to eq(
- "x-amz-acl" => "private",
+ "x-amz-acl" => FileStore::S3Store::CANNED_ACL_PRIVATE,
"x-amz-meta-sha1-checksum" => "testing",
)
end
diff --git a/spec/tasks/uploads_spec.rb b/spec/tasks/uploads_spec.rb
index 02383df4722..b66dd7071b1 100644
--- a/spec/tasks/uploads_spec.rb
+++ b/spec/tasks/uploads_spec.rb
@@ -161,7 +161,11 @@ RSpec.describe "tasks/uploads" do
end
it "does not attempt to update the acl" do
- FileStore::S3Store.any_instance.expects(:update_upload_ACL).with(upload_2).never
+ FileStore::S3Store
+ .any_instance
+ .expects(:update_upload_access_control)
+ .with(upload_2)
+ .never
invoke_task
end
end
@@ -246,9 +250,9 @@ RSpec.describe "tasks/uploads" do
)
end
- it "updates the affected ACLs via the SyncAclsForUploads job" do
+ it "updates the affected ACLs via the SyncAccessControlForUploads job" do
invoke_task
- expect(Jobs::SyncAclsForUploads.jobs.last["args"][0]["upload_ids"]).to match_array(
+ expect(Jobs::SyncAccessControlForUploads.jobs.last["args"][0]["upload_ids"]).to match_array(
[upload_1.id, upload_2.id, upload_3.id, upload_4.id, upload_6.id],
)
end