Phase 0.8 把 kneron_model_converter 的轉檔功能整合進 visionA Cloud。
visionA backend 當 streaming proxy(upload)+ delegated download token broker(download)+
ownership trust boundary,converter / FAA / MC 三方零修改。
新增 internal/conversion/ 套件(8 個檔,~10,000 行 prod+test,117+ test cases,race -count=3 全綠):
- conversion.go:Service interface 5 method、Job/PromoteResult/InitJobInput types
- errors.go:13+ sentinel errors + ErrorCode/HTTPStatus mapping,對齊 conversion.md §6
- mc_token_client.go:service-to-service token (client_credentials grant) + DCL cache
(exp - 15s 重取,per-scope cache),IssueDelegatedDownload(MC delegated download token)
錯誤分 idp_misconfigured (4xx) / idp_unavailable (5xx) / download_token_failed / mc_token_unavailable
- converter_client.go:對 converter scheduler 4 method(InitJob multipart streaming /
GetJob / Promote / ListInProgressJobs),InitJob 不 retry 5xx(streaming body 無法 replay)
- faa_client.go:對 FAA GET /files/{key} server-to-server pull,Phase A retry(GET 無 body
可 replay)對齊 §9.1 retry 矩陣,streaming io.ReadCloser 透傳避 OOM
- ownership.go:in-memory job_id → user_id map + per-user mutex 防 thundering herd lazy rebuild
(不同 user 平行 fetch,同 user 100 caller 收斂成 1 次),visionA 重啟靠 converter
ListInProgressJobs(user) 重建
- flow.go:Service interface 整合層(5 method 串接 converter/FAA/MC/ownership)
- InitJob 用 io.Pipe + multipart.Reader/Writer 重組 streaming proxy(黑名單 client user_id
+ 灌入 OIDC sub)
- DownloadRedirectURL 自動觸發 promote(spec §1 Stage 3b),用 ensurePromoted helper
- PromoteToModels 冪等(modelStore.FindBySourceJobID 為 source-of-truth)
- OwnershipMismatch → ErrJobNotFound 不 forbidden(§7.2 防枚舉)
- storage / modelStore 失敗包 ErrStorageUnavailable / ErrModelStoreUnavailable
(視為 visionA 自身 500 而非 502 gateway,SRE alarm 才打對 team)
新增 internal/api/conversion.go(5 endpoint handler + main.go wire):
- POST /api/conversion/init(multipart streaming proxy,不呼叫 c.MultipartForm())
- GET /api/conversion/active(lazy rebuild ownership)
- GET /api/conversion/{job_id}(poll status)
- POST /api/conversion/{job_id}/promote-to-models(FAA pull → models 三段式)
- GET /api/conversion/{job_id}/download(server-side HTTP 302 → FAA,token 不過 frontend
JS,仿 FAA TestSite DownloadFileDirect pattern;Cache-Control: no-store)
5 個 endpoint 全部走 OIDC AuthMiddleware;user_id 從 cookie session 灌(trust boundary),
從不接受 client multipart form / JSON / query 的 user_id。
TestAllAPIEndpointsRequire401WithoutCookie 自動覆蓋新 5 endpoint regression 防呆。
新增 cmd/api-server/conversion_e2e_test.go(4 個 e2e 場景):
- TestConversionE2E_StreamingProxy(10MB body + trust boundary regression)
- TestConversionE2E_LazyRebuildAfterRestart(visionA 重啟仍能 /active)
- TestConversionE2E_Download302Redirect(驗 302 + Location header + token 不在 body)
- TestConversionE2E_ActiveJobConflict(409 + active_job 詳情)
修改 internal/config/{config,load}.go:新增 ConversionConfig 5 欄位
(ConverterBaseURL / FAABaseURL / TenantID / ServiceClientID / ServiceClientSecret)+
Enabled() helper(雙非空判定)。
修改 cmd/api-server/main.go:條件 wire(cfg.Conversion.Enabled() 為 true 才建 client + Service;
否則 Deps.Conversion=nil,handler 自動回 501)。
修改 .env.example:新增 Phase 0.8 區塊註解。
新增 cmd/api-server/conversion_adapters.go:narrow interface adapter(接既有
internal/model.Repository / internal/storage.Store → conversion.ModelStore / Storage,避免 import cycle)。
驗證:go test -race -count=3 ./... 17 packages 全綠 / go vet 0 warning / go build 成功。
對齊文件:
- .autoflow/04-architecture/adr/adr-014-conversion-integration.md
- .autoflow/04-architecture/conversion.md (TDD)
- .autoflow/04-architecture/api/api-conversion.md
- .autoflow/02-prd/features/feature-converter-integration.md
- .autoflow/03-design/wireframes/wireframe-conversion.md
- .autoflow/03-design/flows/flow-conversion.md
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
305 lines
13 KiB
Go
305 lines
13 KiB
Go
// Package config 定義 visionA-backend 的組態結構,對齊 TDD §2.10。
|
||
//
|
||
// 雛形遵循 12-Factor App:所有可變設定皆透過環境變數注入,不寫死在程式碼裡。
|
||
// `api-server` 與 `remote-proxy` 共享同一份 Config;各自只消費自己需要的欄位。
|
||
package config
|
||
|
||
import "time"
|
||
|
||
// Config 是整個 visionA-backend 的環境設定。
|
||
//
|
||
// 所有欄位皆由 Load() 從環境變數讀取並套用預設值。
|
||
// 欄位命名對齊 TDD §2.10;新增欄位時請同步更新 `.env.example`(待 B6)。
|
||
type Config struct {
|
||
Server ServerConfig
|
||
Session SessionConfig
|
||
Auth AuthConfig
|
||
OIDC OIDCConfig
|
||
UserSession UserSessionConfig
|
||
Storage StorageConfig
|
||
Model ModelConfig
|
||
Tunnel TunnelConfig
|
||
Logger LoggerConfig
|
||
CORS CORSConfig
|
||
// Conversion 控制 Phase 0.8 轉檔功能整合(converter / FAA / MC service token)。
|
||
// 對齊 .autoflow/04-architecture/conversion.md §5.3。
|
||
Conversion ConversionConfig
|
||
}
|
||
|
||
// ServerConfig 控制 HTTP listener 的位址與埠號。
|
||
//
|
||
// api-server 端使用 Port 提供 REST / WebSocket;
|
||
// remote-proxy 端使用 TunnelPort(面向 local agent)與 InternalPort(面向 api-server)。
|
||
//
|
||
// Port 預設為 3721 — 對齊 local-tool 的 base URL,這樣 local-tool 前端切到雲端版時
|
||
// base URL 可以維持一致,降低前端的 dev 流程切換成本(B4 決定)。
|
||
type ServerConfig struct {
|
||
Host string // VISIONA_HOST,預設 "0.0.0.0"
|
||
Port int // VISIONA_API_PORT,預設 3721(對齊 local-tool)
|
||
TunnelPort int // VISIONA_TUNNEL_PORT,預設 3800
|
||
InternalPort int // VISIONA_PROXY_INTERNAL_PORT,預設 3801
|
||
|
||
// RelayPublicURL 是 agent 連 tunnel 用的對外可達 URL(通常是 wss://.../tunnel/connect
|
||
// 的 origin 部分,例:wss://relay.visionA.cloud)。
|
||
// AB11 新增:/api/pairing/exchange 會把這個值回傳給 agent。
|
||
// 雛形預設為空 — handler 會 fallback 到 placeholder `wss://relay.visionA.cloud`。
|
||
RelayPublicURL string
|
||
|
||
// SeedDemoData 控制 api-server 啟動時是否塞入示範用 device + model + pairing token。
|
||
// 預設 false;本機開發或 demo 時可設 VISIONA_SEED_DEMO_DATA=true 開啟,
|
||
// 方便前端不必跑完整 pairing 流程就能看到資料。
|
||
SeedDemoData bool
|
||
}
|
||
|
||
// SessionConfig 控制 SessionStore 的實作選擇與連線資訊。
|
||
//
|
||
// Backend:
|
||
// - "inmemory" — remote-proxy 端持有 yamux session 的唯一來源
|
||
// - "proxy-client" — api-server 端透過 internal HTTP 查詢 remote-proxy
|
||
type SessionConfig struct {
|
||
Backend string // VISIONA_SESSION_BACKEND,預設 "inmemory"
|
||
ProxyInternalURL string // VISIONA_PROXY_INTERNAL_URL,預設 "http://localhost:3801"
|
||
}
|
||
|
||
// AuthConfig 控制雛形專用的 user fallback 與 pairing token。
|
||
//
|
||
// OB5(2026-04-26)起認證走 OIDC(OIDCConfig);
|
||
// Phase 0.7(2026-05-01)security audit 移除了 api.Deps.StaticUserID handler fallback
|
||
// (見 .autoflow/05-implementation/review/phase-0.7-security-audit.md C1)。
|
||
// 此處的 StaticUserID 欄位**僅供 dev seed(VISIONA_SEED_DEMO_DATA=true)與 unit test
|
||
// fixture 讀取使用**,不再注入 api.Deps、不影響 stage/prod 認證行為。
|
||
type AuthConfig struct {
|
||
// StaticUserID — Deprecated for routing/auth use. 僅供 dev seed / unit test。
|
||
// 見 internal/api/api.go 的 Deps 註解;stage/prod 留空無影響。
|
||
StaticUserID string // VISIONA_STATIC_USER_ID,預設 "demo-user"(dev seed only)
|
||
PairingToken string // VISIONA_PAIRING_TOKEN,格式必須為 vAc_ + 32 hex
|
||
SigningSecret string // VISIONA_STORAGE_SIGNING_SECRET,presigned URL HMAC secret
|
||
}
|
||
|
||
// OIDCConfig 控制 OpenID Connect 登入流程(BFF 模式)。
|
||
//
|
||
// 對齊 oidc-tdd.md §13.1 + ADR-010 + ADR-011 + ADR-013。
|
||
// OB5 起 OIDC 是唯一認證路徑;A1 起支援 public PKCE-only client。
|
||
type OIDCConfig struct {
|
||
// IssuerURL 是 OIDC IdP 的 issuer(不帶結尾斜線),例如
|
||
// dev: http://localhost:5050
|
||
// prod: https://members.innovedus.com
|
||
// 對齊 VISIONA_OIDC_ISSUER_URL。
|
||
IssuerURL string
|
||
|
||
// ClientID 是 visionA 在 IdP 註冊的 OAuth client id(confidential 或 public 皆可)。
|
||
// 對齊 VISIONA_OIDC_CLIENT_ID。
|
||
ClientID string
|
||
|
||
// ClientSecret 為**選填**(A1, 2026-05-01):
|
||
// - 有值 → confidential client mode(client_secret + PKCE 雙保險)
|
||
// - 留空 → PKCE-only public client mode(純依靠 PKCE 防 code interception)
|
||
// 兩種 mode 由 IdP 決定,visionA-backend 都支援(見 ADR-013)。
|
||
// **禁止 commit 進 repo**;對齊 VISIONA_OIDC_CLIENT_SECRET。
|
||
ClientSecret string
|
||
|
||
// RedirectURL 是 visionA-backend 的 callback URL,必須與 IdP 註冊值完全一致。
|
||
// dev: http://localhost:3721/api/auth/callback
|
||
// prod: https://api.visiona.cloud/api/auth/callback
|
||
// 對齊 VISIONA_OIDC_REDIRECT_URL。
|
||
RedirectURL string
|
||
|
||
// PostLoginURL 是 callback 完成後 302 回 frontend 的 base URL。
|
||
// dev: http://localhost:3000
|
||
// prod: https://app.visiona.cloud
|
||
// 對齊 VISIONA_FRONTEND_URL(沿用 oidc-tdd.md §13.1 命名)。
|
||
PostLoginURL string
|
||
|
||
// ServiceClientID 是「visionA-backend 以服務身份呼叫 MC API」用的 client id,
|
||
// 預留給未來 client_credentials grant flow(例如查詢使用者組織、推送通知等)。
|
||
//
|
||
// **A1 階段不啟用**:Validate() 不檢查、main.go 不 wire;只先把 config 鉤子留好,
|
||
// 之後接時不必再改 OIDCConfig schema。對齊 VISIONA_OIDC_SERVICE_CLIENT_ID。
|
||
ServiceClientID string
|
||
|
||
// ServiceClientSecret 是 service client(client_credentials grant)的 secret。
|
||
// 與 ServiceClientID 配對使用;同樣 A1 階段不啟用、Validate() 不檢查。
|
||
// **禁止 commit 進 repo**;對齊 VISIONA_OIDC_SERVICE_CLIENT_SECRET。
|
||
ServiceClientSecret string
|
||
}
|
||
|
||
// UserSessionConfig 控制 OIDC 登入後在 browser 端建立的 cookie session。
|
||
//
|
||
// 注意:與既有 SessionConfig(tunnel session 用)刻意分開,避免命名混淆。
|
||
// 對齊 oidc-tdd.md §5、§13.1。
|
||
type UserSessionConfig struct {
|
||
// Secret 是 cookie HMAC-SHA256 簽章金鑰;應為至少 32 byte 隨機字串。
|
||
// 對齊 VISIONA_SESSION_SECRET。
|
||
Secret string
|
||
|
||
// CookieName 預設 "visiona_session"。
|
||
// 對齊 VISIONA_SESSION_COOKIE_NAME。
|
||
CookieName string
|
||
|
||
// CookieDomain:dev 留空(host-only cookie),prod 設 ".visiona.cloud"。
|
||
// 對齊 VISIONA_SESSION_COOKIE_DOMAIN。
|
||
CookieDomain string
|
||
|
||
// CookieSecure 控制 Secure flag。dev=false(http),prod=true(https)。
|
||
// 對齊 VISIONA_SESSION_COOKIE_SECURE。
|
||
CookieSecure bool
|
||
|
||
// AbsoluteTTL 是 session 的最長存活時間(從 Create 起算)。預設 168h(7 天)。
|
||
// 對齊 VISIONA_SESSION_ABSOLUTE_TTL。
|
||
AbsoluteTTL time.Duration
|
||
|
||
// IdleTTL 是 session 的閒置存活時間(從 LastSeenAt 起算)。預設 24h。
|
||
// 對齊 VISIONA_SESSION_IDLE_TTL。
|
||
IdleTTL time.Duration
|
||
}
|
||
|
||
// StorageConfig 控制儲存層實作(LocalFS / S3)與路徑。
|
||
type StorageConfig struct {
|
||
Backend string // VISIONA_STORAGE_BACKEND,預設 "localfs"
|
||
RootDir string // VISIONA_STORAGE_LOCALFS_ROOT,預設 "./data/storage"
|
||
BaseURL string // VISIONA_STORAGE_LOCALFS_BASE_URL,預設 "http://localhost:3721/storage"(對齊 api-server port)
|
||
}
|
||
|
||
// ModelConfig 針對模型資源的驗證限制(大小等)。
|
||
type ModelConfig struct {
|
||
// MaxSizeMB 是允許上傳的單一模型檔案大小上限(MB)。
|
||
// PRD §8.4 規範 Phase 0 為 100 MB;可由 VISIONA_MODEL_MAX_SIZE_MB 覆寫。
|
||
MaxSizeMB int
|
||
}
|
||
|
||
// TunnelConfig 控制 tunnel 心跳與掉線判定閾值,對齊 tunnel.md §4.2。
|
||
type TunnelConfig struct {
|
||
// HeartbeatInterval 為 yamux KeepAliveInterval 值。預設 10s。
|
||
HeartbeatInterval time.Duration
|
||
// IdleTimeout 為判定對端失聯的時間。預設 30s(= 3 次心跳未回)。
|
||
IdleTimeout time.Duration
|
||
}
|
||
|
||
// LoggerConfig 控制結構化 logger 的輸出等級。
|
||
type LoggerConfig struct {
|
||
Level string // VISIONA_LOG_LEVEL:debug / info / warn / error,預設 "info"
|
||
}
|
||
|
||
// ConversionConfig 控制 Phase 0.8 轉檔功能整合。
|
||
//
|
||
// 對齊 .autoflow/04-architecture/conversion.md §5.3。
|
||
//
|
||
// 啟用判定(由 main.go 在 wire 階段檢查):當 ConverterBaseURL 與 FAABaseURL 都非空時,
|
||
// 才會 wire conversion.Service 進 api.Deps。其中之一為空 → 不啟用(5 個 endpoint 回 501)。
|
||
//
|
||
// 進一步:啟用時 ServiceClientID/Secret 必須非空(轉檔依賴 service token 機制);
|
||
// 不對齊時 main.go fatal log 退出(避免半設定狀態跑進生產)。
|
||
type ConversionConfig struct {
|
||
// ConverterBaseURL 是 kneron_model_converter task-scheduler 服務的 base URL。
|
||
// 例:http://192.168.0.130:9501(dev / stage) / https://converter.visiona.cloud(prod)
|
||
// 對齊 VISIONA_CONVERTER_BASE_URL;留空 = 不啟用 Phase 0.8 轉檔功能。
|
||
ConverterBaseURL string
|
||
|
||
// FAABaseURL 是 File Access Agent 的 base URL。
|
||
// 例:http://192.168.0.130:5081(dev / stage) / https://faa.innovedus.com(prod)
|
||
// 對齊 VISIONA_FAA_BASE_URL;留空 = 不啟用 Phase 0.8 轉檔功能。
|
||
FAABaseURL string
|
||
|
||
// TenantID 是 visionA 在 Member Center 註冊的 tenant id(單一 tenant)。
|
||
// 在跟 MC 換 delegated download token 時當 request body 的 tenant_id 欄位用。
|
||
// 對齊 VISIONA_OIDC_TENANT_ID。
|
||
TenantID string
|
||
|
||
// DelegatedTTLSeconds 是 MC 簽 delegated download token 的 TTL(秒)。
|
||
// 預設 300(5 分鐘);可調整範圍 60-900。對齊 VISIONA_FAA_DELEGATED_TTL_SECONDS。
|
||
// 見 conversion.md §10.2 安全考量。
|
||
DelegatedTTLSeconds int
|
||
|
||
// MaxModelSizeMB 是 visionA-backend 端對上傳模型檔的大小上限(MB)。
|
||
// 與 converter 端 limit 對齊(converter 預設 500 MB)。
|
||
// 對齊 VISIONA_CONVERTER_MAX_MODEL_SIZE_MB;預設 500。
|
||
MaxModelSizeMB int
|
||
}
|
||
|
||
// Enabled 回傳 Phase 0.8 conversion 是否啟用。
|
||
//
|
||
// main.go 在 wire 時用此判斷是否要 init conversion.Service。
|
||
func (c ConversionConfig) Enabled() bool {
|
||
return c.ConverterBaseURL != "" && c.FAABaseURL != ""
|
||
}
|
||
|
||
// CORSConfig 控制 api-server 對瀏覽器的 CORS 白名單。
|
||
//
|
||
// AllowedOrigins 為逗號分隔字串解析後的 slice;
|
||
// 空時 api.Deps.validate() 會 fallback 到 http://localhost:3000(前端 dev server)。
|
||
type CORSConfig struct {
|
||
AllowedOrigins []string // VISIONA_CORS_ALLOWED_ORIGINS(逗號分隔)
|
||
}
|
||
|
||
// Validate 在 Load() 之後檢查交叉依賴與必填欄位。
|
||
//
|
||
// OB5 起 OIDC 是唯一認證路徑,所有 OIDC 必填欄位永遠都要非空:
|
||
// - IssuerURL / ClientID / RedirectURL / PostLoginURL
|
||
// - UserSession.Secret(cookie HMAC 簽章)
|
||
//
|
||
// ClientSecret 為**選填**(A1, 2026-05-01):
|
||
// - 有值 → confidential client mode(標準 OAuth + PKCE 雙保險)
|
||
// - 留空 → PKCE-only public client mode(依靠 PKCE 防 code interception)
|
||
//
|
||
// 兩種 mode 由 IdP 決定,visionA 都支援(見 ADR-013、oidc-tdd.md §13.1)。
|
||
//
|
||
// ServiceClientID / ServiceClientSecret 為 client_credentials grant 預留欄位,
|
||
// A1 階段不啟用、不檢查;之後若接服務間 API 呼叫再補 Validate。
|
||
//
|
||
// 缺任何**必填**項 → 回 *MissingEnvError,main.go 啟動時 fatal log 退出。
|
||
// 維持單一 error 而非列表 — caller 只是 fail-fast 紀錄,不需要結構化處理。
|
||
func (c *Config) Validate() error {
|
||
missing := make([]string, 0, 5)
|
||
if c.OIDC.IssuerURL == "" {
|
||
missing = append(missing, "VISIONA_OIDC_ISSUER_URL")
|
||
}
|
||
if c.OIDC.ClientID == "" {
|
||
missing = append(missing, "VISIONA_OIDC_CLIENT_ID")
|
||
}
|
||
// ClientSecret 為選填(public PKCE-only client 留空)— 不檢查。
|
||
if c.OIDC.RedirectURL == "" {
|
||
missing = append(missing, "VISIONA_OIDC_REDIRECT_URL")
|
||
}
|
||
if c.OIDC.PostLoginURL == "" {
|
||
missing = append(missing, "VISIONA_FRONTEND_URL")
|
||
}
|
||
if c.UserSession.Secret == "" {
|
||
missing = append(missing, "VISIONA_SESSION_SECRET")
|
||
}
|
||
|
||
if len(missing) > 0 {
|
||
return &MissingEnvError{Vars: missing}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// MissingEnvError 表示 OIDC 必填環境變數缺少(OB5 起永遠檢查)。
|
||
type MissingEnvError struct {
|
||
Vars []string
|
||
}
|
||
|
||
func (e *MissingEnvError) Error() string {
|
||
return "config: OIDC enabled but required env vars are missing: " + joinStrings(e.Vars, ", ")
|
||
}
|
||
|
||
// joinStrings 是 strings.Join 的本地版本,避免單純為了 join 引入 strings package。
|
||
func joinStrings(parts []string, sep string) string {
|
||
switch len(parts) {
|
||
case 0:
|
||
return ""
|
||
case 1:
|
||
return parts[0]
|
||
}
|
||
n := len(sep) * (len(parts) - 1)
|
||
for _, p := range parts {
|
||
n += len(p)
|
||
}
|
||
out := make([]byte, 0, n)
|
||
out = append(out, parts[0]...)
|
||
for _, p := range parts[1:] {
|
||
out = append(out, sep...)
|
||
out = append(out, p...)
|
||
}
|
||
return string(out)
|
||
}
|