-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add signing of public URL #407
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
Changes from 6 commits
1e2eb11
fb622d2
208a253
9ce1ae3
7f5d373
2451a25
101fe41
b058ca3
de1d874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
from crawlee.storage_clients._base import BaseKeyValueStoreClient | ||
from crawlee.storage_clients.models import KeyValueStoreListKeysPage, KeyValueStoreMetadata, KeyValueStoreRecord | ||
|
||
from apify._crypto import create_hmac_signature | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import AsyncIterator | ||
from contextlib import AbstractAsyncContextManager | ||
|
@@ -90,5 +92,10 @@ async def get_public_url(self, key: str) -> str: | |
key: The key for which the URL should be generated. | ||
""" | ||
public_api_url = self._api_public_base_url | ||
public_url = f'{public_api_url}/v2/key-value-stores/{self._client.resource_id}/records/{key}' | ||
|
||
url_signing_secret_key = getattr(self.storage_object, 'url_signing_secret_key', None) | ||
|
||
if url_signing_secret_key: | ||
public_url += f'?signature={create_hmac_signature(url_signing_secret_key, key)}' | ||
|
||
|
||
return f'{public_api_url}/v2/key-value-stores/{self._client.resource_id}/records/{key}' | ||
return public_url |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
from ._utils import generate_unique_resource_name | ||
from apify import Actor | ||
from apify._crypto import create_hmac_signature | ||
|
||
if TYPE_CHECKING: | ||
import pytest | ||
|
@@ -210,10 +211,16 @@ async def main() -> None: | |
default_store_id = Actor.config.default_key_value_store_id | ||
|
||
store = await Actor.open_key_value_store() | ||
record_url = await cast(KeyValueStoreClient, store._resource_client).get_public_url('dummy') | ||
print(record_url) | ||
|
||
assert record_url == f'{public_api_url}/v2/key-value-stores/{default_store_id}/records/dummy' | ||
record_key = 'dummy' | ||
record_url = await cast(KeyValueStoreClient, store._resource_client).get_public_url(record_key) | ||
|
||
url_signing_secret_key = cast(str, getattr(store.storage_object, 'url_signing_secret_key', None)) | ||
signature = create_hmac_signature(url_signing_secret_key, record_key) | ||
|
||
assert url_signing_secret_key is not None | ||
assert ( | ||
record_url | ||
== f'{public_api_url}/v2/key-value-stores/{default_store_id}/records/{record_key}?signature={signature}' | ||
) | ||
|
||
actor = await make_actor(label='kvs-get-public-url', main_func=main) | ||
run_result = await run_actor(actor) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method alone is perfect for writing unit test for. Could you please add one parametrized test with couple of input values.
https://docs.pytest.org/en/stable/how-to/parametrize.html#pytest-mark-parametrize