fix(release): rewrite nested bucket in target resource locators
模拟发布暴露的第 8 个契约缺陷:PG 通道改写资源行时只替换了外层 storage://<backend-id> 前缀,内层 oss://<bucket>/<key> 照搬源桶。 WeKnora v0.7.1 官方 GetKnowledgeFile 从路径内层桶名直接 GetObject, 导致目标端所有文档预览以生产凭据读源桶失败(403),前端表现为预览空白。 - PostgresImporterConfig 新增 TargetBucket;nested locator 导入时改写 内层桶名为目标桶,location_hash 随改写后的路径重算 - 未配置目标桶时保持旧行为透传,不做猜测 - 4 个单测锁定:桶改写+hash重算、非嵌套 locator 不变、无配置透传、非法桶名拒绝 - 端到端复验:重封 canary 批次重新发布 4/4 verified,目标端资源行 全部指向 dobot-weknora-test,location_hash 与路径逐行比对一致
This commit is contained in:
@@ -20,6 +20,11 @@ type postgresTransactionalDB interface {
|
||||
type PostgresImporterConfig struct {
|
||||
TargetStorageBackendID string
|
||||
TargetProvider string
|
||||
// TargetBucket rewrites the bucket embedded in nested provider locators
|
||||
// (oss://<bucket>/<key>). WeKnora resolves GetFile from the path's inner
|
||||
// bucket, so a source-env bucket leaking into the target row makes every
|
||||
// preview/download read the wrong bucket with target credentials.
|
||||
TargetBucket string
|
||||
}
|
||||
|
||||
type PostgresImporter struct {
|
||||
@@ -37,10 +42,14 @@ func NewPostgresImporter(db postgresTransactionalDB, options ...PostgresImporter
|
||||
}
|
||||
config.TargetStorageBackendID = strings.TrimSpace(config.TargetStorageBackendID)
|
||||
config.TargetProvider = strings.TrimSpace(config.TargetProvider)
|
||||
config.TargetBucket = strings.TrimSpace(config.TargetBucket)
|
||||
if (config.TargetStorageBackendID == "") != (config.TargetProvider == "") ||
|
||||
(config.TargetStorageBackendID != "" && (!validSearchIdentifier(config.TargetStorageBackendID) || !validObjectProvider(config.TargetProvider))) {
|
||||
return nil, errors.New("invalid target storage backend configuration")
|
||||
}
|
||||
if config.TargetBucket != "" && !validSearchIdentifier(config.TargetBucket) {
|
||||
return nil, errors.New("invalid target storage bucket configuration")
|
||||
}
|
||||
return &PostgresImporter{db: db, config: config}, nil
|
||||
}
|
||||
|
||||
@@ -286,6 +295,17 @@ func (importer *PostgresImporter) prepareResourceRow(raw json.RawMessage, tenant
|
||||
} else if !validObjectLocator(physicalPath) {
|
||||
return nil, resourceIdentityPair{}, errors.New("invalid source storage locator")
|
||||
}
|
||||
// WeKnora's official file service parses the bucket out of the nested
|
||||
// provider locator (oss://<bucket>/<key>) and uses it directly for
|
||||
// GetObject. Carrying the source bucket across environments makes every
|
||||
// target-side preview read the wrong bucket, so rewrite it to the target
|
||||
// bucket whenever the locator embeds one and a target bucket is configured.
|
||||
if inner := strings.Index(providerPath, "://"); inner >= 0 && importer.config.TargetBucket != "" {
|
||||
rest := providerPath[inner+3:]
|
||||
if boundary := strings.Index(rest, "/"); boundary > 0 && rest[:boundary] != importer.config.TargetBucket {
|
||||
providerPath = providerPath[:inner+3] + importer.config.TargetBucket + rest[boundary:]
|
||||
}
|
||||
}
|
||||
targetPath := "storage://" + importer.config.TargetStorageBackendID + "/" + providerPath
|
||||
digest := sha256.Sum256([]byte(targetPath))
|
||||
row["storage_backend_id"] = importer.config.TargetStorageBackendID
|
||||
|
||||
Reference in New Issue
Block a user