Skip to content

Commit 1b59fea

Browse files
WilliamBergaminClaude
andauthored
fix(oauth): evict every cached installation for a workspace on save (#2051)
Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
1 parent 39775bb commit 1b59fea

4 files changed

Lines changed: 79 additions & 6 deletions

File tree

‎slack_sdk/oauth/installation_store/async_cacheable_installation_store.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ async def async_save(self, installation: Installation):
3131
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}"
3232
if key in self.cached_bots:
3333
self.cached_bots.pop(key)
34-
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-{installation.user_id or ''}"
35-
if key in self.cached_installations:
36-
self.cached_installations.pop(key)
34+
key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-"
35+
for key in list(self.cached_installations.keys()):
36+
if key.startswith(key_prefix):
37+
self.cached_installations.pop(key)
3738
return await self.underlying.async_save(installation)
3839

3940
async def async_save_bot(self, bot: Bot):

‎slack_sdk/oauth/installation_store/cacheable_installation_store.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ def save(self, installation: Installation):
2929
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}"
3030
if key in self.cached_bots:
3131
self.cached_bots.pop(key)
32-
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-{installation.user_id or ''}"
33-
if key in self.cached_installations:
34-
self.cached_installations.pop(key)
32+
key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-"
33+
for key in list(self.cached_installations.keys()):
34+
if key.startswith(key_prefix):
35+
self.cached_installations.pop(key)
3536

3637
return self.underlying.save(installation)
3738

‎tests/slack_sdk/oauth/installation_store/test_simple_cache.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import os
2+
import tempfile
23
import unittest
34

45
from slack_sdk.oauth.installation_store import Installation
56
from slack_sdk.oauth.installation_store.cacheable_installation_store import (
67
CacheableInstallationStore,
78
)
9+
from slack_sdk.oauth.installation_store.file import FileInstallationStore
810
from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore
911

1012

13+
def build_installation(*, user_id: str, bot_token: str) -> Installation:
14+
return Installation(
15+
app_id="A111",
16+
enterprise_id="E111",
17+
team_id="T111",
18+
user_id=user_id,
19+
bot_id="B111",
20+
bot_token=bot_token,
21+
bot_scopes=["chat:write"],
22+
bot_user_id="UB111",
23+
)
24+
25+
1126
class TestCacheable(unittest.TestCase):
1227
def test_save_and_find(self):
1328
sqlite3_store = SQLite3InstallationStore(database="logs/cacheable.db", client_id="111.222")
@@ -135,3 +150,23 @@ def test_save_and_find_token_rotation(self):
135150
self.assertIsNone(i)
136151
bot = store.find_bot(enterprise_id="E111", team_id="T222")
137152
self.assertIsNone(bot)
153+
154+
def test_save_evicts_stale_installations(self):
155+
base_dir = tempfile.TemporaryDirectory()
156+
self.addCleanup(base_dir.cleanup)
157+
file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222")
158+
store = CacheableInstallationStore(file_store)
159+
160+
store.save(build_installation(user_id="U111", bot_token="xoxb-initial"))
161+
store.find_installation(enterprise_id="E111", team_id="T111")
162+
store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
163+
164+
store.save(build_installation(user_id="U111", bot_token="xoxb-refreshed"))
165+
i = store.find_installation(enterprise_id="E111", team_id="T111")
166+
self.assertEqual(i.bot_token, "xoxb-refreshed")
167+
i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
168+
self.assertEqual(i.bot_token, "xoxb-refreshed")
169+
170+
store.save(build_installation(user_id="U222", bot_token="xoxb-u222"))
171+
i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
172+
self.assertEqual(i.bot_token, "xoxb-u222")

‎tests/slack_sdk_async/oauth/installation_store/test_simple_cache.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import os
2+
import tempfile
23
import unittest
34

45
from slack_sdk.oauth.installation_store import Installation
56
from slack_sdk.oauth.installation_store.async_cacheable_installation_store import (
67
AsyncCacheableInstallationStore,
78
)
9+
from slack_sdk.oauth.installation_store.file import FileInstallationStore
810
from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore
911
from tests.helpers import async_test
1012

1113

14+
def build_installation(*, user_id: str, bot_token: str) -> Installation:
15+
return Installation(
16+
app_id="A111",
17+
enterprise_id="E111",
18+
team_id="T111",
19+
user_id=user_id,
20+
bot_id="B111",
21+
bot_token=bot_token,
22+
bot_scopes=["chat:write"],
23+
bot_user_id="UB111",
24+
)
25+
26+
1227
class TestCacheable(unittest.TestCase):
1328
@async_test
1429
async def test_save_and_find(self):
@@ -86,3 +101,24 @@ async def test_save_and_find_token_rotation(self):
86101
self.assertIsNone(bot)
87102
bot = await store.async_find_bot(enterprise_id="E111", team_id="T111")
88103
self.assertIsNotNone(bot)
104+
105+
@async_test
106+
async def test_save_evicts_stale_installations(self):
107+
base_dir = tempfile.TemporaryDirectory()
108+
self.addCleanup(base_dir.cleanup)
109+
file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222")
110+
store = AsyncCacheableInstallationStore(file_store)
111+
112+
await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial"))
113+
await store.async_find_installation(enterprise_id="E111", team_id="T111")
114+
await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
115+
116+
await store.async_save(build_installation(user_id="U111", bot_token="xoxb-refreshed"))
117+
i = await store.async_find_installation(enterprise_id="E111", team_id="T111")
118+
self.assertEqual(i.bot_token, "xoxb-refreshed")
119+
i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
120+
self.assertEqual(i.bot_token, "xoxb-refreshed")
121+
122+
await store.async_save(build_installation(user_id="U222", bot_token="xoxb-u222"))
123+
i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
124+
self.assertEqual(i.bot_token, "xoxb-u222")

0 commit comments

Comments
 (0)