Skip to content

Commit

Permalink
FIX: error setting tombstone bucket when set to old version
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Nov 13, 2017
1 parent 232311a commit 4f28c71
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/s3_helper.rb
Expand Up @@ -100,12 +100,24 @@ def update_lifecycle(id, days, prefix: nil, tag: nil)
# skip trying to merge
end

# in the past we has a rule that was called purge-tombstone vs purge_tombstone
# just go ahead and normalize for our bucket
rules.delete_if do |r|
r.id == id
r.id.gsub('_', '-') == id.gsub('_', '-')
end

rules << rule

# normalize filter in rules, due to AWS library bug
rules = rules.map do |r|
r = r.to_h
prefix = r.delete(:prefix)
if prefix
r[:filter] = { prefix: prefix }
end
r
end

s3_resource.client.put_bucket_lifecycle_configuration(
bucket: @s3_bucket_name,
lifecycle_configuration: {
Expand Down
52 changes: 50 additions & 2 deletions spec/components/s3_helper_spec.rb
Expand Up @@ -3,6 +3,54 @@

describe "S3Helper" do

# we should test something here, perhaps in a smoke test that uploads a real image
# to s3
it "can correctly set the purge policy" do

stub_request(:get, "http://169.254.169.254/latest/meta-data/iam/security-credentials/").
to_return(status: 404, body: "", headers: {})

lifecycle = <<~XML
<?xml version="1.0" encoding="UTF-8"?>
<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Rule>
<ID>old_rule</ID>
<Prefix>projectdocs/</Prefix>
<Status>Enabled</Status>
<Expiration>
<Days>3650</Days>
</Expiration>
</Rule>
<Rule>
<ID>purge-tombstone</ID>
<Prefix>test/</Prefix>
<Status>Enabled</Status>
<Expiration>
<Days>3650</Days>
</Expiration>
</Rule>
</LifecycleConfiguration>
XML

stub_request(:get, "https://bob.s3.amazonaws.com/?lifecycle").
to_return(status: 200, body: lifecycle, headers: {})

stub_request(:put, "https://bob.s3.amazonaws.com/?lifecycle").
with do |req|

hash = Hash.from_xml(req.body.to_s)
rules = hash["LifecycleConfiguration"]["Rule"]

expect(rules.length).to eq(2)

# fixes the bad filter
expect(rules[0]["Filter"]["Prefix"]).to eq("projectdocs/")
end.to_return(status: 200, body: "", headers: {})

SiteSetting.enable_s3_uploads = true
SiteSetting.s3_access_key_id = "abc"
SiteSetting.s3_secret_access_key = "def"

helper = S3Helper.new('bob', 'tomb')
helper.update_tombstone_lifecycle(100)
end

end

1 comment on commit 4f28c71

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/amazon-s3-job-exception-filter-element-can-only-be-used-in-lifecycle-v2/71702/18

Please sign in to comment.