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
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ async def async_save(self, installation: Installation):
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-{installation.user_id or ''}"
if key in self.cached_installations:
self.cached_installations.pop(key)
key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-"
for key in list(self.cached_installations.keys()):
if key.startswith(key_prefix):
self.cached_installations.pop(key)
return await self.underlying.async_save(installation)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion (non-blocking): Not new in this PR, but a lookup that runs while this await is in flight can cache the old installation again with nothing left to clear it. Evicting after the save too would close that gap, happy for it to be a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point but lets make this a follow up 🙏


async def async_save_bot(self, bot: Bot):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def save(self, installation: Installation):
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-{installation.user_id or ''}"
if key in self.cached_installations:
self.cached_installations.pop(key)
key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-"
for key in list(self.cached_installations.keys()):
if key.startswith(key_prefix):
self.cached_installations.pop(key)

return self.underlying.save(installation)

Expand Down
35 changes: 35 additions & 0 deletions tests/slack_sdk/oauth/installation_store/test_simple_cache.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import os
import tempfile
import unittest

from slack_sdk.oauth.installation_store import Installation
from slack_sdk.oauth.installation_store.cacheable_installation_store import (
CacheableInstallationStore,
)
from slack_sdk.oauth.installation_store.file import FileInstallationStore
from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore


def build_installation(*, user_id: str, bot_token: str) -> Installation:
return Installation(
app_id="A111",
enterprise_id="E111",
team_id="T111",
user_id=user_id,
bot_id="B111",
bot_token=bot_token,
bot_scopes=["chat:write"],
bot_user_id="UB111",
)


class TestCacheable(unittest.TestCase):
def test_save_and_find(self):
sqlite3_store = SQLite3InstallationStore(database="logs/cacheable.db", client_id="111.222")
Expand Down Expand Up @@ -135,3 +150,23 @@ def test_save_and_find_token_rotation(self):
self.assertIsNone(i)
bot = store.find_bot(enterprise_id="E111", team_id="T222")
self.assertIsNone(bot)

def test_save_evicts_stale_installations(self):
base_dir = tempfile.TemporaryDirectory()
self.addCleanup(base_dir.cleanup)
file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222")
store = CacheableInstallationStore(file_store)

store.save(build_installation(user_id="U111", bot_token="xoxb-initial"))
store.find_installation(enterprise_id="E111", team_id="T111")
store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")

store.save(build_installation(user_id="U111", bot_token="xoxb-refreshed"))
i = store.find_installation(enterprise_id="E111", team_id="T111")
self.assertEqual(i.bot_token, "xoxb-refreshed")
i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-refreshed")

store.save(build_installation(user_id="U222", bot_token="xoxb-u222"))
i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-u222")
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import os
import tempfile
import unittest

from slack_sdk.oauth.installation_store import Installation
from slack_sdk.oauth.installation_store.async_cacheable_installation_store import (
AsyncCacheableInstallationStore,
)
from slack_sdk.oauth.installation_store.file import FileInstallationStore
from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore
from tests.helpers import async_test


def build_installation(*, user_id: str, bot_token: str) -> Installation:
return Installation(
app_id="A111",
enterprise_id="E111",
team_id="T111",
user_id=user_id,
bot_id="B111",
bot_token=bot_token,
bot_scopes=["chat:write"],
bot_user_id="UB111",
)


class TestCacheable(unittest.TestCase):
@async_test
async def test_save_and_find(self):
Expand Down Expand Up @@ -86,3 +101,24 @@ async def test_save_and_find_token_rotation(self):
self.assertIsNone(bot)
bot = await store.async_find_bot(enterprise_id="E111", team_id="T111")
self.assertIsNotNone(bot)

@async_test
async def test_save_evicts_stale_installations(self):
base_dir = tempfile.TemporaryDirectory()
self.addCleanup(base_dir.cleanup)
file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222")
store = AsyncCacheableInstallationStore(file_store)

await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial"))
await store.async_find_installation(enterprise_id="E111", team_id="T111")
await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")

await store.async_save(build_installation(user_id="U111", bot_token="xoxb-refreshed"))
i = await store.async_find_installation(enterprise_id="E111", team_id="T111")
self.assertEqual(i.bot_token, "xoxb-refreshed")
i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-refreshed")

await store.async_save(build_installation(user_id="U222", bot_token="xoxb-u222"))
i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-u222")
Loading