Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add s3_inventory_bucket site setting #27013

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tgxworld
Copy link
Contributor

This commit adds a new s3_inventory_bucket site setting which allows
admins to configure the S3 bucket in which S3 Inventory files should be stored at.
The default for s3_inventory_bucket is blank and when it is blank, the
S3 Inventory files will be stored in the S3 bucket given by
s3_upload_bucket.

@github-actions github-actions bot added the i18n PRs which update English locale files or i18n related code label May 14, 2024
This commit adds a new `s3_inventory_bucket` site setting which allows
admins to configure the S3 bucket in which S3 Inventory files should be stored at.
The default for `s3_inventory_bucket` is blank and when it is blank, the
S3 Inventory files will be stored in the S3 bucket given by
`s3_upload_bucket`.
@tgxworld tgxworld force-pushed the support_alternate_bucket_s3_inventory branch from 5821614 to f2633c9 Compare May 14, 2024 03:08

CSV_KEY_INDEX ||= 1
CSV_ETAG_INDEX ||= 2
INVENTORY_PREFIX ||= "inventory"
INVENTORY_VERSION ||= "1"
INVENTORY_LAG ||= 2.days

def initialize(s3_helper, type, preloaded_inventory_file: nil, preloaded_inventory_date: nil)
Copy link
Contributor Author

@tgxworld tgxworld May 14, 2024

Choose a reason for hiding this comment

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

I removed s3_helper from the arguments accepted by the constructor because the only valid instances of S3Helper are those generated by this constructor. In other words, only Discourse.store.s3_helper or S3Helper.new(SiteSetting.s3_inventory_bucket) are valid.

@@ -9,8 +9,7 @@
SiteSetting.enable_s3_inventory = true

store = FileStore::S3Store.new
@client = Aws::S3::Client.new(stub_responses: true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was no need to set stub_responses: true here. I removed it and the tests still pass.

@@ -5,9 +5,7 @@
require "file_store/s3_store"

RSpec.describe "S3Inventory", type: :multisite do
let(:client) { Aws::S3::Client.new(stub_responses: true) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is redundant too.

Comment on lines -195 to -214
describe "s3 inventory configuration" do
let(:bucket_name) { "s3-upload-bucket" }
let(:subfolder_path) { "subfolder" }
before { SiteSetting.s3_upload_bucket = "#{bucket_name}/#{subfolder_path}" }

it "is formatted correctly for subfolders" do
s3_helper = S3Helper.new(SiteSetting.Upload.s3_upload_bucket.downcase, "", client: client)
config = S3Inventory.new(s3_helper, :upload).send(:inventory_configuration)

expect(config[:destination][:s3_bucket_destination][:bucket]).to eq(
"arn:aws:s3:::#{bucket_name}",
)
expect(config[:destination][:s3_bucket_destination][:prefix]).to eq(
"#{subfolder_path}/inventory/1",
)
expect(config[:id]).to eq("#{subfolder_path}-original")
expect(config[:schedule][:frequency]).to eq("Daily")
expect(config[:included_object_versions]).to eq("Current")
expect(config[:optional_fields]).to eq(["ETag"])
expect(config[:filter][:prefix]).to eq(subfolder_path)
Copy link
Contributor Author

@tgxworld tgxworld May 14, 2024

Choose a reason for hiding this comment

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

Previous test was testing a private method which I didn't like so I rewrote the tests to stub the outgoing request instead.

Comment on lines +287 to 290
def stub_client_responses!
raise "This method is only allowed to be used in the testing environment" if !Rails.env.test?
@s3_client = init_aws_s3_client(stub_responses: true)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I think there is no need to do this here, you can do something like this in tests instead

client = Aws::S3::Client.new(stub_responses: true)
client.stub_responses(xyz)
Aws::S3::Client.stubs(:new).returns(client)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is that this unnecessarily exposes the internals of S3Helper and when a person is initializing Aws::S3::Client manually, they are not initializing it with the necessary options which S3Helper will initialize the client with.

Copy link
Contributor

@nattsw nattsw left a comment

Choose a reason for hiding this comment

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

I reviewed and while I don't see anything bad, is there a possibility that the inventory config gets lost with this new setting?

What would happen to the old inventory config if this value is filled? Is it a manual cleanup?

@tgxworld
Copy link
Contributor Author

Ah I see what you mean here. If we are changing the bucket, we need to remove the inventory config from the old bucket. I'll have to think about this to see what is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i18n PRs which update English locale files or i18n related code
2 participants