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

Add Smart CDN Signature Auth helper #71

Merged
merged 35 commits into from
Nov 28, 2024
Merged

Add Smart CDN Signature Auth helper #71

merged 35 commits into from
Nov 28, 2024

Conversation

kvz
Copy link
Member

@kvz kvz commented Nov 25, 2024

Allows you to generate signed URLs for Transloadit's Smart CDN.

@rmehner
Copy link
Collaborator

rmehner commented Nov 25, 2024

@kvz let me know if there's anything to review.

@kvz kvz marked this pull request as ready for review November 25, 2024 20:07
@kvz kvz requested review from rmehner and Acconut November 25, 2024 20:07
@kvz kvz self-assigned this Nov 25, 2024
@kvz
Copy link
Member Author

kvz commented Nov 26, 2024

Appreciate the offer! This would be ready for you now @rmehner!

it "must call the create! method with the same parameters" do
VCR.use_cassette "submit_assembly" do
file = open("lib/transloadit/version.rb")
mocker = Minitest::Mock.new
mocker.expect :call, nil, [file]
@assembly.stub :create!, mocker do
@assembly.submit!(file)
@assembly.create!(file)
Copy link
Member

Choose a reason for hiding this comment

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

IIRC this test ensured that the deprecated submit! method calls create!. By replacing the submit! call with a create! call, the test is now useless. Either we keep the test as is or we remove it entirely.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay i see.

I thought the output was smelly, reverted this 👌

end

it "requires input" do
assert_raises ArgumentError, "input is required" do
Copy link
Member

Choose a reason for hiding this comment

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

Internally we have discusses use cases where customers might leave ${fields.input} unset because they don't need it at all or prefer using named query parameters. When a customer wants to sign such a URL, should we require them to pass nil and an explicit empty string to the signature method? I prefer an empty string, which would also work with this implementation.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's right, I now changed it to allow empty string inputs as well as empty string e.g. width, which will show up in the URL

end

it "allows overriding credentials" do
url = @transloadit.signed_smart_cdn_url(
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what the use case here is. AFAICT signed_smart_cdn_url is not a static method but an instance method and thus a Transloadit instance is always needed to call it. When would someone want to pass in other credentials to method instead of creating another Transloadit instance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed this now with the exception of the reference implementation

next if value.nil?
if value.is_a?(Array)
value.each do |val|
next if val.nil? || val.to_s.empty?
Copy link
Member

Choose a reason for hiding this comment

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

Does this filter out empty strings? While I don't have a use case, I also think we should not place unnecessary restrictions on the URL parameters. If a customer wants to have an empty parameter value, they can have it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I now changed it to allow empty string e.g. width, which will show up in the URL


url = @transloadit.signed_smart_cdn_url(**params)
node_url = run_node_script(params)
assert_equal node_url, url
Copy link
Member

Choose a reason for hiding this comment

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

These test cases only check if the Ruby implementation matches the Node.js script in this repository. We should also compare the generated URLs against static strings here to ensure they are what we expect them to be:

Suggested change
assert_equal node_url, url
assert_equal node_url, url
assert_equal "https://....", url

This makes the tests easier to understand since you directly see what the expected output is. In addition, we test the Node.js script to ensure it doesn't produce total garbage :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed it to have just one test file, that tests both against node as a hardcoded string

@kvz kvz requested a review from Acconut November 26, 2024 12:51
Copy link
Collaborator

@rmehner rmehner left a comment

Choose a reason for hiding this comment

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

Don't have enough time today, but @Acconut already did some good reviews there :)

Comment on lines 151 to 153
raise ArgumentError, "workspace is required" if workspace.nil?
raise ArgumentError, "template is required" if template.nil?
raise ArgumentError, "input is required" if input.nil?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you want to guard explicitly against nil being passed? Or is this to make sure that the argument is passed at all? If so, that's already taken care of by Ruby itself, since you don't define a default, making the keywords required.

Copy link
Member Author

Choose a reason for hiding this comment

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

We want to allow empty strings, but not omitting or other falsey values

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we want to allow empty strings for workspace and template. They must always be set. Only input could be an empty string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented and rolled tests for this now too 👌

lib/transloadit.rb Outdated Show resolved Hide resolved
query_params["exp"] = [expire_at.to_s]

# Sort parameters to ensure consistent ordering
sorted_params = query_params.sort.map do |key, values|
Copy link
Collaborator

Choose a reason for hiding this comment

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

Save some CPU cycles, use flat_map :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Perfect 🙏

kvz and others added 3 commits November 26, 2024 14:57
Co-authored-by: Robin Mehner <robin@coding-robin.de>
Co-Authored-By: Robin Mehner <robin@coding-robin.de>
Co-Authored-By: Robin Mehner <robin@coding-robin.de>
@kvz kvz requested a review from rmehner November 26, 2024 14:02
@kvz kvz merged commit a3d8049 into main Nov 28, 2024
6 checks passed
@kvz kvz deleted the cdn-sigauth branch November 28, 2024 10:10
@kvz
Copy link
Member Author

kvz commented Nov 28, 2024

I think I did all I could here, but I don't have access to cut a release on Rubygems, can you add me there @rmehner ?

@rmehner
Copy link
Collaborator

rmehner commented Nov 28, 2024

I think I did all I could here, but I don't have access to cut a release on Rubygems, can you add me there @rmehner ?

What's your rubygems.org username?

@rmehner
Copy link
Collaborator

rmehner commented Nov 28, 2024

@kvz also could you please add yourself here: https://github.com/transloadit/ruby-sdk/blob/main/transloadit.gemspec#L10-L11?

@kvz
Copy link
Member Author

kvz commented Nov 28, 2024

@kvz also could you please add yourself here: https://github.com/transloadit/ruby-sdk/blob/main/transloadit.gemspec#L10-L11?

✅ done!

What's your rubygems.org username?

Just signed up as kvz

@kvz
Copy link
Member Author

kvz commented Nov 28, 2024

Thank you!

Successfully registered gem: transloadit (3.1.0)

If anything looks out the order to you please let me know :)

@rmehner
Copy link
Collaborator

rmehner commented Nov 28, 2024

You could draft a release here: https://github.com/transloadit/ruby-sdk/releases, so it shows up in the releases tab of this repo.

@kvz
Copy link
Member Author

kvz commented Nov 28, 2024

Check, just did it.

Would you say these steps are accurate? https://github.com/transloadit/ruby-sdk?tab=readme-ov-file#releasing-on-rubygems

@rmehner
Copy link
Collaborator

rmehner commented Nov 28, 2024

Yes, it's accurate. We could ease it a little bit with the help of gem-release, but since we don't do it so often and the steps are pretty well documented, I don't think it's worth the effort here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants