mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-09 04:40:57 +00:00
e1ac5e0fc8
* Document multi-tenant workspace architecture * Add OSS and commercial workspace boundaries * docs: redesign multi-tenant workspace architecture * feat(tenancy): implement workspace isolation * docs(tenancy): record verification evidence * docs(tenancy): revise single-instance SaaS topology * docs(tenancy): refine architecture options * docs: finalize cloud v2 multi-tenant decisions * feat(tenancy): establish cloud isolation foundations * feat(tenancy): harden shared cloud runtime boundaries * docs(tenancy): record final isolation verification * fix(tenancy): close isolation and permission gaps * docs(tenancy): record final isolation verification * feat(tenancy): connect cloud workspace control plane * fix(build): install git for pinned SDK * docs(cloud): update control plane verification * chore: update multi-tenant SDK pin * fix(cloud): skip legacy model sync during startup * test(cloud): preserve minimal model manager fixtures * fix(cloud): preserve authenticated account context * fix(cloud): reuse authenticated account for user info * feat(cloud): complete Workspace settings navigation * test(web): cover Workspace dropdown menu * feat(web): place workspace controls in sidebar * refactor(web): streamline workspace controls * style(web): format workspace layout test * fix(cloud): surface runtime and workspace plan status * fix(plugin): keep runtime identity stable across restarts * fix(ui): widen and center workspace switcher * fix(ui): hide roles from workspace switcher * fix(ui): align workspace switcher with sidebar entries * feat(workspace): add in-product collaboration and direct Cloud launch * style: format collaboration changes * fix(workspace): bind collaboration APIs to tenant UoW * fix(cloud): preserve Core-owned collaboration state * test(cloud): require Space identity for invite registration * feat(cloud): complete secure invitation experience * style(web): format invitation flows * fix(cloud): recover box runtime without unscoped skill reload * feat(oss): enforce invitation account and owner billing flows * style: format OSS account service * test(oss): cover invitation logout handoff * fix(oss): resolve workspace owner in scoped session * feat(cloud): harden multi-tenant runtime resources * fix(cloud): bound runtime restart storms * fix(cloud): eliminate periodic runtime CPU spikes * fix(cloud): enforce instance capacity ceilings * fix(cloud): scope public login capability discovery * fix(cloud): bound tenant maintenance and monitoring work * fix(runtime): bound tenant resource amplification * fix(deps): pin green multi-tenant plugin SDK * fix(cloud): handle unavailable skill capability * fix(security): require authentication for image file endpoint (H-2) - Changed /api/v1/files/image from AuthType.NONE to USER_TOKEN_OR_API_KEY - Added Permission.RESOURCE_VIEW requirement - Prevents unauthenticated cross-tenant file access via leaked keys - Fixes HIGH severity finding from multi-tenant security review docs: add comprehensive database migration guide - Complete migration steps for OSS → multi-tenant - Backup, execution, verification procedures - Rollback scenarios and recovery plans - Performance tuning recommendations * test: add comprehensive cross-tenant isolation tests Added 7 critical test scenarios for multi-tenant boundaries: - Cross-tenant bot access prevention - Viewer role read-only enforcement - Removed member immediate access revocation - Model provider credential isolation - WebSocket message isolation - Invitation token workspace scoping - Multi-workspace context validation These tests address P0-2 coverage gaps for: - workspaces.py (membership & invitation flows) - user.py (authentication & authorization) - websocket_chat.py (real-time isolation) - plugins.py (resource access control) docs: finalize database migration guide * fix(security): resolve M-1, M-2, M-3 security findings M-1: WebSocket authorization TOCTOU race (FIXED) - Changed _revalidate_websocket_authorization to return RequestContext - Ensures validated context is used immediately without race window - Prevents removed members from sending messages during revalidation gap M-2: Model Manager cache workspace isolation (VERIFIED) - Confirmed _CacheKey already uses 4-tuple: (instance, workspace, generation, resource) - Cache is properly scoped per workspace, no cross-tenant leakage possible - No code change needed, documented as working correctly M-3: Invitation lock workspace scoping (FIXED) - Changed lock key from token_digest to workspace_uuid:token_digest - Prevents DoS where attacker locks token in Workspace A to block Workspace B - Locks now isolated per workspace All MEDIUM severity findings from security review now resolved. * fix(cloud): unblock tenant CI and enforce knowledge quotas * fix(tenancy): scope rerank model sync --------- Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
214 lines
7.0 KiB
Python
214 lines
7.0 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
import boto3
|
|
from botocore.exceptions import ClientError
|
|
|
|
from ...core import app
|
|
from ...utils import bounded_executor
|
|
from .. import provider
|
|
|
|
|
|
class S3StorageProvider(provider.StorageProvider):
|
|
"""S3 object storage provider"""
|
|
|
|
def __init__(self, ap: app.Application):
|
|
super().__init__(ap)
|
|
self.s3_client = None
|
|
self.bucket_name = None
|
|
self._io_semaphore = asyncio.Semaphore(16)
|
|
|
|
async def initialize(self):
|
|
"""Initialize S3 client with configuration from config.yaml"""
|
|
storage_config = self.ap.instance_config.data.get('storage', {})
|
|
s3_config = storage_config.get('s3', {})
|
|
|
|
# Get S3 configuration
|
|
endpoint_url = s3_config.get('endpoint_url', '')
|
|
access_key_id = s3_config.get('access_key_id', '')
|
|
secret_access_key = s3_config.get('secret_access_key', '')
|
|
region_name = s3_config.get('region', 'us-east-1')
|
|
self.bucket_name = s3_config.get('bucket', 'langbot-storage')
|
|
try:
|
|
max_concurrency = int(s3_config.get('max_concurrency', 16))
|
|
except (TypeError, ValueError):
|
|
max_concurrency = 16
|
|
self._io_semaphore = asyncio.Semaphore(max(1, min(max_concurrency, 128)))
|
|
|
|
# Initialize S3 client
|
|
session = boto3.session.Session()
|
|
self.s3_client = session.client(
|
|
service_name='s3',
|
|
region_name=region_name,
|
|
endpoint_url=endpoint_url if endpoint_url else None,
|
|
aws_access_key_id=access_key_id,
|
|
aws_secret_access_key=secret_access_key,
|
|
)
|
|
|
|
await self._run_io(self._ensure_bucket)
|
|
|
|
async def shutdown(self) -> None:
|
|
"""Close the botocore HTTP connection pool without blocking the loop."""
|
|
|
|
client = self.s3_client
|
|
self.s3_client = None
|
|
if client is not None:
|
|
await bounded_executor.run_blocking_cleanup(client.close)
|
|
|
|
async def _run_io(self, operation, /, *args, **kwargs):
|
|
"""Run one blocking boto3 operation behind a bounded concurrency gate."""
|
|
|
|
async with self._io_semaphore:
|
|
return await asyncio.to_thread(operation, *args, **kwargs)
|
|
|
|
def _ensure_bucket(self) -> None:
|
|
"""Probe/create the bucket without blocking the application event loop."""
|
|
|
|
try:
|
|
self.s3_client.head_bucket(Bucket=self.bucket_name)
|
|
except ClientError as e:
|
|
error_code = e.response['Error']['Code']
|
|
if error_code == '404':
|
|
# Bucket doesn't exist, create it
|
|
try:
|
|
self.s3_client.create_bucket(Bucket=self.bucket_name)
|
|
self.ap.logger.info(f'Created S3 bucket: {self.bucket_name}')
|
|
except Exception as create_error:
|
|
self.ap.logger.error(f'Failed to create S3 bucket: {create_error}')
|
|
raise
|
|
else:
|
|
self.ap.logger.error(f'Failed to access S3 bucket: {e}')
|
|
raise
|
|
|
|
async def save(
|
|
self,
|
|
key: str,
|
|
value: bytes,
|
|
):
|
|
"""Save bytes to S3"""
|
|
try:
|
|
await self._run_io(
|
|
self.s3_client.put_object,
|
|
Bucket=self.bucket_name,
|
|
Key=key,
|
|
Body=value,
|
|
)
|
|
except Exception as e:
|
|
self.ap.logger.error(f'Failed to save to S3: {e}')
|
|
raise
|
|
|
|
async def load(
|
|
self,
|
|
key: str,
|
|
) -> bytes:
|
|
return await self.load_bounded(key, max_bytes=provider.HARD_MAX_STORAGE_OBJECT_BYTES)
|
|
|
|
async def load_bounded(
|
|
self,
|
|
key: str,
|
|
*,
|
|
max_bytes: int,
|
|
) -> bytes:
|
|
"""Load bytes from S3"""
|
|
max_bytes = provider.normalize_read_limit(max_bytes)
|
|
try:
|
|
return await self._run_io(self._load_sync, key, max_bytes)
|
|
except Exception as e:
|
|
self.ap.logger.error(f'Failed to load from S3: {e}')
|
|
raise
|
|
|
|
def _load_sync(self, key: str, max_bytes: int) -> bytes:
|
|
response = self.s3_client.get_object(
|
|
Bucket=self.bucket_name,
|
|
Key=key,
|
|
)
|
|
body = response['Body']
|
|
try:
|
|
declared_size = response.get('ContentLength')
|
|
if declared_size is not None and declared_size > max_bytes:
|
|
raise ValueError(f'Storage object exceeds the {max_bytes}-byte read limit')
|
|
value = body.read(max_bytes + 1)
|
|
if len(value) > max_bytes:
|
|
raise ValueError(f'Storage object exceeds the {max_bytes}-byte read limit')
|
|
return value
|
|
finally:
|
|
body.close()
|
|
|
|
async def exists(
|
|
self,
|
|
key: str,
|
|
) -> bool:
|
|
"""Check if object exists in S3"""
|
|
try:
|
|
await self._run_io(
|
|
self.s3_client.head_object,
|
|
Bucket=self.bucket_name,
|
|
Key=key,
|
|
)
|
|
return True
|
|
except ClientError as e:
|
|
if e.response['Error']['Code'] == '404':
|
|
return False
|
|
else:
|
|
self.ap.logger.error(f'Failed to check existence in S3: {e}')
|
|
raise
|
|
|
|
async def delete(
|
|
self,
|
|
key: str,
|
|
):
|
|
"""Delete object from S3"""
|
|
try:
|
|
await self._run_io(
|
|
self.s3_client.delete_object,
|
|
Bucket=self.bucket_name,
|
|
Key=key,
|
|
)
|
|
except Exception as e:
|
|
self.ap.logger.error(f'Failed to delete from S3: {e}')
|
|
raise
|
|
|
|
async def size(
|
|
self,
|
|
key: str,
|
|
) -> int:
|
|
"""Get object size from S3 without downloading it"""
|
|
try:
|
|
response = await self._run_io(
|
|
self.s3_client.head_object,
|
|
Bucket=self.bucket_name,
|
|
Key=key,
|
|
)
|
|
return response['ContentLength']
|
|
except Exception as e:
|
|
self.ap.logger.error(f'Failed to get size from S3: {e}')
|
|
raise
|
|
|
|
async def delete_dir_recursive(
|
|
self,
|
|
dir_path: str,
|
|
):
|
|
"""Delete all objects with the given prefix (directory)"""
|
|
try:
|
|
await self._run_io(self._delete_dir_recursive_sync, dir_path)
|
|
except Exception as e:
|
|
self.ap.logger.error(f'Failed to delete directory from S3: {e}')
|
|
raise
|
|
|
|
def _delete_dir_recursive_sync(self, dir_path: str) -> None:
|
|
if not dir_path.endswith('/'):
|
|
dir_path = dir_path + '/'
|
|
|
|
paginator = self.s3_client.get_paginator('list_objects_v2')
|
|
pages = paginator.paginate(Bucket=self.bucket_name, Prefix=dir_path)
|
|
for page in pages:
|
|
if 'Contents' not in page:
|
|
continue
|
|
objects_to_delete = [{'Key': obj['Key']} for obj in page['Contents']]
|
|
if objects_to_delete:
|
|
self.s3_client.delete_objects(
|
|
Bucket=self.bucket_name,
|
|
Delete={'Objects': objects_to_delete},
|
|
)
|