From 3e28ef2cab04227f48e884f0360e11234d5f1586 Mon Sep 17 00:00:00 2001 From: happybell80 Date: Wed, 3 Sep 2025 18:45:25 +0900 Subject: [PATCH] =?UTF-8?q?docs:=20Slack=20=EB=A9=80=ED=8B=B0=20=EC=9B=8C?= =?UTF-8?q?=ED=81=AC=EC=8A=A4=ED=8E=98=EC=9D=B4=EC=8A=A4=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0=20=EB=B0=A9=EC=95=88=20=ED=99=95=EC=A0=95=20-=20Gatew?= =?UTF-8?q?ay=20=EA=B2=BD=EC=9C=A0=20=EB=B0=A9=EC=8B=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50903_slack_multi_workspace_token_issue.md | 66 +++++++++++++------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/troubleshooting/250903_slack_multi_workspace_token_issue.md b/troubleshooting/250903_slack_multi_workspace_token_issue.md index 12d49e3..3c51ca1 100644 --- a/troubleshooting/250903_slack_multi_workspace_token_issue.md +++ b/troubleshooting/250903_slack_multi_workspace_token_issue.md @@ -1,9 +1,9 @@ # Slack 멀티 워크스페이스 토큰 관리 문제 ## 작성일: 2025-09-03 -## 작성자: 51123 서버 관리자 -## 상태: 🔴 해결 필요 -## 영향: Company-X 워크스페이스 응답 실패 +## 작성자: 51123 서버 관리자 +## 상태: 🟡 해결 방안 확정 (구현 대기) +## 영향: Company-X, GoodGang Labs 워크스페이스 응답 실패 --- @@ -13,10 +13,11 @@ - **에러**: `channel_not_found` - 잘못된 토큰으로 채널 접근 ## 2. 토큰 현황 (2025-09-03 DB 확인) -- **디지털비잉_로빙**: xoxb-9073915808149-... (환경변수 SLACK_BOT_TOKEN) -- **Company-X**: xoxb-9253435471507-... (slack_workspaces.bot_token) -- **GoodGang Labs**: xoxb-3199535658754-... (slack_workspaces.bot_token) -- **문제**: 환경변수 토큰만 사용, DB 토큰 미조회 +- **Robeing (T0925SXPS4D)**: xoxb-9073915808149-9107868204992-... (DB 등록) +- **Company-X (T097FCTDVEX)**: xoxb-9253435471507-... (DB 등록) +- **GoodGang Labs (T035VFRKCN6)**: xoxb-3199535658754-... (DB 등록) +- **문제**: rb8001이 환경변수 토큰만 사용, DB 토큰 미조회 +- **DB 구조**: 모든 팀이 container_port 8001 (rb8001 공유) ## 3. 상세 분석 @@ -35,25 +36,52 @@ ERROR: Failed to send final response: The request to the Slack API failed. The server responded with: {'ok': False, 'error': 'channel_not_found'} ``` -## 4. 해결 방안 +## 4. 해결 방안 (2025-09-03 확정) -### 4.1 단기 해결 -- team_id 기반 토큰 동적 선택 로직 구현 -- slack_workspaces 테이블에서 bot_token 조회 +### 4.1 Gateway 경유 방식 채택 +- Slack Event URL을 Gateway(8100)로 변경 +- Gateway에서 team_id별 토큰 조회 및 주입 +- rb8001은 헤더에서 토큰 읽어 사용 -### 4.2 장기 해결 -- auth-server의 중앙 라우터 활용 -- `/slack/events/router`가 team_id별 컨테이너 라우팅 -- 각 컨테이너는 해당 워크스페이스 토큰만 보유 +### 4.2 구현 상세 +1. **nginx 설정**: `/api/*` → Gateway(8100) 프록시 +2. **Slack App**: Event URL을 `https://ro-being.com/api/slack/events`로 변경 +3. **Gateway**: team_id로 DB 조회, 헤더에 토큰 추가 +4. **rb8001**: 헤더 우선, 환경변수 폴백 ## 5. 구현 필요 코드 (로컬 개발자) + +### 5.1 Gateway 추가 (robeing-gateway) ```python -# rb8001에서 team_id로 토큰 조회 -async def get_bot_token(team_id: str): - # slack_workspaces 테이블에서 토큰 조회 +@app.post("/api/slack/events") +async def handle_slack_event(request: Request): + # Slack 서명 검증 + verify_slack_signature(request) + + # team_id별 토큰 조회 + body = await request.json() + team_id = body.get("team_id") + result = await db.fetch_one( "SELECT bot_token FROM slack_workspaces WHERE team_id = :team_id", {"team_id": team_id} ) - return result["bot_token"] if result else None + + # rb8001로 전달 + headers = { + "X-Slack-Bot-Token": result["bot_token"] if result else None, + "Content-Type": "application/json" + } + + return await forward_to_rb8001("/api/slack/events", body, headers) +``` + +### 5.2 rb8001 수정 (slack_handler.py) +```python +# 기존: 환경변수만 사용 +slack_token = os.environ.get("SLACK_BOT_TOKEN") + +# 수정: 헤더 우선, 환경변수 폴백 +slack_token = request.headers.get("X-Slack-Bot-Token") \ + or os.environ.get("SLACK_BOT_TOKEN") ``` \ No newline at end of file