Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
// Package service - per-tenant sandbox resolution helpers.
|
|
//
|
|
// The sandbox package must not depend on repositories, so the config lookup is
|
|
// adapted here and injected as sandbox.TenantSandboxConfigLoader.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Tencent/WeKnora/internal/application/repository"
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
"github.com/Tencent/WeKnora/internal/sandbox"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
// tenantSandboxConfigLoader reads one named sandbox config for a workspace.
|
|
type tenantSandboxConfigLoader struct {
|
|
repo repository.TenantSandboxConfigRepository
|
|
now func() time.Time
|
|
}
|
|
|
|
// NewTenantSandboxConfigLoader adapts the config repository onto the sandbox
|
|
// package's loader contract.
|
|
func NewTenantSandboxConfigLoader(
|
|
repo repository.TenantSandboxConfigRepository,
|
|
) sandbox.TenantSandboxConfigLoader {
|
|
return &tenantSandboxConfigLoader{repo: repo, now: time.Now}
|
|
}
|
|
|
|
// Load reports whether the config exists and whether it is currently cordoned.
|
|
// A cordon is honoured here - the single choke point every sandbox operation
|
|
// passes through - so no new sandbox can be created on credentials that are
|
|
// about to be replaced.
|
|
func (l *tenantSandboxConfigLoader) Load(
|
|
ctx context.Context,
|
|
tenantID uint64,
|
|
configID string,
|
|
) (sandbox.ResolvedTenantSandboxConfig, error) {
|
|
if l.repo == nil {
|
|
return sandbox.ResolvedTenantSandboxConfig{}, nil
|
|
}
|
|
entity, err := l.repo.GetByID(ctx, tenantID, configID)
|
|
if err != nil {
|
|
return sandbox.ResolvedTenantSandboxConfig{}, err
|
|
}
|
|
if entity == nil {
|
|
return sandbox.ResolvedTenantSandboxConfig{Found: false}, nil
|
|
}
|
|
return sandbox.ResolvedTenantSandboxConfig{
|
|
Config: entity.Config,
|
|
Found: true,
|
|
Cordoned: entity.IsCordoned(l.now(), types.SandboxCordonLease),
|
|
}, nil
|
|
}
|
|
|
|
// WorkspaceSandboxPolicy reports whether sandbox execution is disabled for the
|
|
// entire workspace, including agents bound to any named backend config.
|
|
type WorkspaceSandboxPolicy interface {
|
|
WorkspaceScriptsDisabled(ctx context.Context, tenantID uint64) (bool, error)
|
|
}
|
|
|
|
// resolveTenantSandboxForConfig returns the Manager for an explicit config.
|
|
//
|
|
// Unlike the previous tenant-only helper this does NOT degrade to the default
|
|
// manager on error: with several configs per workspace, a silent substitution
|
|
// would run scripts on a different backend than the one selected - and then
|
|
// artifact collection and sandbox teardown would target the wrong account.
|
|
func resolveTenantSandboxForConfig(
|
|
ctx context.Context,
|
|
resolver sandbox.TenantSandboxResolver,
|
|
_ sandbox.Manager,
|
|
tenantID uint64,
|
|
configID string,
|
|
policy WorkspaceSandboxPolicy,
|
|
) (sandbox.Manager, error) {
|
|
// ① Workspace kill switch — independent of resolver availability.
|
|
if policy != nil && tenantID != 0 {
|
|
disabled, err := policy.WorkspaceScriptsDisabled(ctx, tenantID)
|
|
if err != nil {
|
|
logger.Warnf(ctx,
|
|
"[sandbox] failed to read workspace sandbox policy for %d: %v",
|
|
tenantID, err)
|
|
} else if disabled {
|
|
return sandbox.NewDisabledManager(), nil
|
|
}
|
|
}
|
|
|
|
// ② No named workspace config means sandbox execution is disabled. There is
|
|
// no deployment-level provider fallback anymore.
|
|
if configID == "" || configID == types.SandboxConfigIDGlobalDefault {
|
|
return sandbox.NewDisabledManager(), nil
|
|
}
|
|
|
|
// ③ Named config: must not silently fall back to another backend.
|
|
if tenantID == 0 {
|
|
return nil, fmt.Errorf(
|
|
"sandbox: resolve config %q: missing workspace context", configID)
|
|
}
|
|
if resolver == nil {
|
|
return nil, fmt.Errorf(
|
|
"sandbox: resolve config %q: per-tenant resolver unavailable", configID)
|
|
}
|
|
mgr, err := resolver.Resolve(ctx, tenantID, configID)
|
|
if err != nil {
|
|
logger.Warnf(ctx,
|
|
"[sandbox] failed to resolve config %q for workspace %d: %v",
|
|
configID, tenantID, err)
|
|
return nil, err
|
|
}
|
|
return mgr, nil
|
|
}
|