Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/apify/storage_clients/_apify/_alias_resolving.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

import os
from asyncio import Lock
from functools import cached_property
from logging import getLogger
from typing import TYPE_CHECKING, ClassVar, Literal, overload

from ._utils import hash_api_public_base_url_and_token
from crawlee._utils.crypto import compute_short_hash

from apify._consts import ApifyEnvVars

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -248,10 +251,27 @@ def _storage_key(self) -> str:
[
self._storage_type,
self._alias,
hash_api_public_base_url_and_token(self._configuration),
self._credentials_hash(),
]
)

def _credentials_hash(self) -> str:
"""Hash the identity of the credentials, so the same alias opened by different users maps to different storages.

For the run's own token (`APIFY_TOKEN`), the identity is the user ID. The platform issues a new run token on
migration, reboot, and resurrection, while the user ID stays the same, so the alias keeps resolving to the same
storage. Any other token is a credential the Actor passed explicitly, which does not change, so it is the
identity itself.
"""
configuration = self._configuration
if configuration.api_public_base_url is None or configuration.token is None:
raise ValueError("'Configuration.api_public_base_url' and 'Configuration.token' must be set.")

is_run_token = configuration.token == os.environ.get(ApifyEnvVars.TOKEN)
identity = configuration.user_id if is_run_token and configuration.user_id else configuration.token

return compute_short_hash(f'{configuration.api_public_base_url}{identity}'.encode())

def _get_default_kvs_client(self) -> KeyValueStoreClientAsync:
"""Get a client for the default key-value store.

Expand Down
59 changes: 59 additions & 0 deletions tests/unit/storage_clients/test_alias_resolver.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from __future__ import annotations

import asyncio
from typing import TYPE_CHECKING
from unittest.mock import AsyncMock, MagicMock, patch

from apify_client import ApifyClientAsync

from apify._configuration import Configuration
from apify._consts import ApifyEnvVars
from apify.storage_clients._apify._alias_resolving import AliasResolver

if TYPE_CHECKING:
import pytest


def _api_client() -> ApifyClientAsync:
"""Build a throwaway API client for resolver construction in tests that never issue real requests."""
Expand Down Expand Up @@ -208,3 +213,57 @@ async def store_on_current_loop(alias: str, storage_id: str) -> None:

# The same injected client served both event loops.
assert kvs_client.set_record.await_count == 2


def test_storage_key_for_run_token_is_stable_across_token_rotation(monkeypatch: pytest.MonkeyPatch) -> None:
"""The key for the run's own token stays the same when the platform issues a new run token after a migration."""
monkeypatch.setenv(ApifyEnvVars.TOKEN, 'run-token-1')
config_before = Configuration(token='run-token-1', user_id='user-a')
key_before = AliasResolver(
storage_type='Dataset', alias='my-alias', configuration=config_before, api_client=_api_client()
)._storage_key

monkeypatch.setenv(ApifyEnvVars.TOKEN, 'run-token-2')
config_after = Configuration(token='run-token-2', user_id='user-a')
key_after = AliasResolver(
storage_type='Dataset', alias='my-alias', configuration=config_after, api_client=_api_client()
)._storage_key

assert key_before == key_after


def test_storage_key_differs_for_token_of_another_user(monkeypatch: pytest.MonkeyPatch) -> None:
"""The same alias opened with the run's own token and with an explicitly passed token maps to different keys."""
monkeypatch.setenv(ApifyEnvVars.TOKEN, 'run-token')
run_config = Configuration(token='run-token', user_id='user-a')
other_config = Configuration(token='other-user-token', user_id='user-a')

run_key = AliasResolver(
storage_type='Dataset', alias='my-alias', configuration=run_config, api_client=_api_client()
)._storage_key
other_key = AliasResolver(
storage_type='Dataset', alias='my-alias', configuration=other_config, api_client=_api_client()
)._storage_key

assert run_key != other_key


def test_storage_key_uses_token_without_user_id(monkeypatch: pytest.MonkeyPatch) -> None:
"""Without a user ID, the key for the run's own token is derived from the token."""
monkeypatch.setenv(ApifyEnvVars.TOKEN, 'run-token-1')
key_1 = AliasResolver(
storage_type='Dataset',
alias='my-alias',
configuration=Configuration(token='run-token-1', user_id=None),
api_client=_api_client(),
)._storage_key

monkeypatch.setenv(ApifyEnvVars.TOKEN, 'run-token-2')
key_2 = AliasResolver(
storage_type='Dataset',
alias='my-alias',
configuration=Configuration(token='run-token-2', user_id=None),
api_client=_api_client(),
)._storage_key

assert key_1 != key_2
Loading