A platform for community discussion. Free, open, simple.
FIX: Surface S3 errors when listing backups
Jake Goldsborough290b1612e8b7
diff --git a/lib/backup_restore/s3_backup_store.rb b/lib/backup_restore/s3_backup_store.rbindex 65209fd0fec..b342fefbf85 100644--- a/lib/backup_restore/s3_backup_store.rb+++ b/lib/backup_restore/s3_backup_store.rb@@ -115,13 +115,9 @@ module BackupRestoredef unsorted_filesobjects = []- begin- s3_helper.list.each do |obj|- objects << create_file_from_object(obj) if obj.key.match?(file_regex)- end- rescue StandardError- NoMethodError- end #fired when s3_helper.list is nil - wont respond to .nil?+ s3_helper.list.each do |obj|+ objects << create_file_from_object(obj) if obj.key.match?(file_regex)+ endobjectsrescue Aws::Errors::ServiceError => ediff --git a/spec/lib/backup_restore/s3_backup_store_spec.rb b/spec/lib/backup_restore/s3_backup_store_spec.rbindex 6324d8648d1..8253b1b0299 100644--- a/spec/lib/backup_restore/s3_backup_store_spec.rb+++ b/spec/lib/backup_restore/s3_backup_store_spec.rb@@ -114,6 +114,26 @@ RSpec.describe BackupRestore::S3BackupStore doexpect(store.stats[:free_bytes]).to be_nilendend++ describe "#files" do+ # Regression: the listing was wrapped in a bare `rescue StandardError`+ # whose body was the constant `NoMethodError` - it caught everything and+ # did nothing, so an AccessDenied returned an empty list. A site whose+ # IAM policy did not cover its backup prefix showed "no backups", and the+ # restore then failed with "Failed to download archive to tmp directory."+ # instead of naming the permission that was actually missing.+ it "raises StorageError when S3 denies the listing" do+ @s3_client.stub_responses(:list_objects_v2, "AccessDenied")++ expect { store.files }.to raise_error(BackupRestore::BackupStore::StorageError)+ end++ it "still returns an empty list when the bucket genuinely has no backups" do+ remove_backups++ expect(store.files).to eq([])+ end+ endenddef objects_with_prefix(context)diff --git a/spec/lib/backup_restore/shared_examples_for_backup_store.rb b/spec/lib/backup_restore/shared_examples_for_backup_store.rbindex 9599a967c0a..0d620b39d3e 100644--- a/spec/lib/backup_restore/shared_examples_for_backup_store.rb+++ b/spec/lib/backup_restore/shared_examples_for_backup_store.rb@@ -172,9 +172,22 @@ RSpec.shared_examples "backup store" doit "runs if SiteSetting.backup_frequency is configured" dobase_backup_s3_url = "https://s3-backup-bucket.s3.dualstack.us-west-1.amazonaws.com"+ # An empty listing from S3 is a valid ListBucketResult with no+ # <Contents>, not an empty body. Returning an empty body makes the SDK+ # raise "Empty or incomplete response body", which used to be hidden by+ # a bare `rescue StandardError` in S3BackupStore#unsorted_files.stub_request(:get, "#{base_backup_s3_url}/?list-type=2&prefix=default/").to_return(status: 200,- body: "",+ body: <<~XML,+ <?xml version="1.0" encoding="UTF-8"?>+ <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Name>s3-backup-bucket</Name>+ <Prefix>default/</Prefix>+ <KeyCount>0</KeyCount>+ <MaxKeys>1000</MaxKeys>+ <IsTruncated>false</IsTruncated>+ </ListBucketResult>+ XMLheaders: {},)@@ -188,9 +201,22 @@ RSpec.shared_examples "backup store" doit "doesn't run if SiteSetting.backup_frequency is set to 0" dobase_backup_s3_url = "https://s3-backup-bucket.s3.dualstack.us-west-1.amazonaws.com"+ # An empty listing from S3 is a valid ListBucketResult with no+ # <Contents>, not an empty body. Returning an empty body makes the SDK+ # raise "Empty or incomplete response body", which used to be hidden by+ # a bare `rescue StandardError` in S3BackupStore#unsorted_files.stub_request(:get, "#{base_backup_s3_url}/?list-type=2&prefix=default/").to_return(status: 200,- body: "",+ body: <<~XML,+ <?xml version="1.0" encoding="UTF-8"?>+ <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Name>s3-backup-bucket</Name>+ <Prefix>default/</Prefix>+ <KeyCount>0</KeyCount>+ <MaxKeys>1000</MaxKeys>+ <IsTruncated>false</IsTruncated>+ </ListBucketResult>+ XMLheaders: {},)