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

feat: sign record's public url #358

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

danpoletaev
Copy link

This PR is part of Issue #19363, which updates KeyValueStore.getPublicUrl(recordKey) to generate signed links using HMAC.

This PR:

  • creates signature and appends it to the public URL of the record in KV store

P.S. Before merging, we need to wait for the release of Crawlee, so we can update version of Crawlee.

Previous PR that adds storageObject to Crawlee is here

@danpoletaev danpoletaev requested review from B4nan and barjin February 17, 2025 14:10
@github-actions github-actions bot added this to the 109th sprint - Platform team milestone Feb 17, 2025
@github-actions github-actions bot added the t-platform Issues with this label are in the ownership of the platform team. label Feb 17, 2025
return `https://api.apify.com/v2/key-value-stores/${this.id}/records/${key}`;
}

return `https://api.apify.com/v2/key-value-stores/${this.id}/records/${key}?signature=${createHmacSignature(this.storageObject.urlSigningSecretKea as string, key)}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we pls compose the URL with URL.searchParams and similar? Maybe concatting the path with path.posix.join() too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Aside from ensuring the correct URL syntax, this procedural approach might also allow us to only specify the pathname once (and just conditionally add the query parameter if the secret exists).

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, makes sense. Refactored it 🙃

@@ -19,7 +20,11 @@ export class KeyValueStore extends CoreKeyValueStore {
return getPublicUrl.call(this, key);
}

return `https://api.apify.com/v2/key-value-stores/${this.id}/records/${key}`;
if (!this.storageObject?.urlSigningSecretKea) {
Copy link
Contributor

Choose a reason for hiding this comment

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

urlSigningSecretKea instead of ...key might be a typo?

@danpoletaev danpoletaev requested a review from barjin February 17, 2025 14:28
Copy link
Contributor

@barjin barjin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

(as long as we wait for the new stable Crawlee version, but I see you mentioned that in the PR description already 👍🏽 )

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

looking good, except the red CI which is not related to your changes

@github-actions github-actions bot added the tested Temporary label used only programatically for some analytics. label Mar 5, 2025
vdusek pushed a commit to apify/apify-sdk-python that referenced this pull request Mar 7, 2025
This PR is part of [Issue
#19363](apify/apify-core#19363), which updates
`KeyValueStore.getPublicUrl(recordKey)` to generate signed links using
HMAC.

**This PR**:
- creates signature and appends it to the public URL of the record in KV
store

P.S. Before merging, we need to wait for the release of Crawlee, so we
can update version of Crawlee.

P.P.S It's hard to test the changes, since master branch of SDK and
master branch of Crawlee are out of sync. Crawlee has some breaking
changes in version 6.0, which are not yet addressed in master branch of
SDK.

Previous PR that adds storageObject to Crawlee Python is
[here](apify/crawlee-python#993)

Same PR in SDK JS is
[here](apify/apify-sdk-js#358)
@danpoletaev danpoletaev requested a review from tobice March 10, 2025 09:36
@@ -19,7 +20,13 @@ export class KeyValueStore extends CoreKeyValueStore {
return getPublicUrl.call(this, key);
}

return `https://api.apify.com/v2/key-value-stores/${this.id}/records/${key}`;
const publicUrl = new URL(`https://api.apify.com/v2/key-value-stores/${this.id}/records/${key}`);
Copy link

Choose a reason for hiding this comment

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

(nit) Isn't the API base URL available in the env variables?

That way the Actor will create valid urls in all environments. You could leverage that to create an actual e2e test that not only creates & runs an Actor that produces the signed URL, but actually tests the URL against Apify API.

(The current test below basically just copies the implementation... you are calling the same code twice and checking that it produces the same value 😅 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t-platform Issues with this label are in the ownership of the platform team. tested Temporary label used only programatically for some analytics.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants