6ae5150fdf
TargetIdentity 与业务调用原先各自 Resolve:身份核验通过后,上传/轮询/验收/ 移除的后续调用仍可能在下一次解析时切到新实例。现在 BeginTask 只解析一次, 用同一连接读取对象租户并把该不可变连接钉进返回 context;后续上传、列库恢复、 查询、chunk 读取、删除与删除后重查全部复用它。 TaskConnector 优先于 IdentityProvider;仅实现 TargetIdentity 的窄假客户端仍可 工作,生产路径不会走双解析。新增假端点测试断言一次处理只解析一次、绑定后改 当前实例仍全部打到原实例;四个 handler 的 TaskConnector 假客户端断言所有业务 调用携带同一连接令牌,覆盖上传恢复和删除重查。
57 lines
2.2 KiB
Go
57 lines
2.2 KiB
Go
package testweknora
|
|
|
|
import (
|
|
"context"
|
|
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
// TaskConnector is implemented by the production client. BeginTask resolves the
|
|
// endpoint/key/binding exactly once, verifies the object's owning tenant through
|
|
// that connection and returns a context that pins the same immutable connection
|
|
// for every later call of this job (including recovery and read-back).
|
|
type TaskConnector interface {
|
|
BeginTask(context.Context, string) (context.Context, weknora.InstanceIdentity, error)
|
|
}
|
|
|
|
// IdentityProvider remains for narrow fakes/tests that only expose identity.
|
|
type IdentityProvider interface {
|
|
TargetIdentity(context.Context, string) (weknora.InstanceIdentity, error)
|
|
}
|
|
|
|
// resolveIdentity prefers BeginTask so upload/poll/validate/remove pass the
|
|
// pinned context to every follow-up call. Returning the original ctx for
|
|
// identity-only fakes keeps the unit seam simple; production never takes that
|
|
// path.
|
|
func resolveIdentity(ctx context.Context, client any, knowledgeBaseID string) (context.Context, weknora.InstanceIdentity, bool, error) {
|
|
if connector, ok := client.(TaskConnector); ok {
|
|
bound, identity, err := connector.BeginTask(ctx, knowledgeBaseID)
|
|
if err != nil {
|
|
return ctx, weknora.InstanceIdentity{}, true, err
|
|
}
|
|
identity.BaseURL = weknora.NormalizeInstanceURL(identity.BaseURL)
|
|
return bound, identity, true, nil
|
|
}
|
|
provider, ok := client.(IdentityProvider)
|
|
if !ok {
|
|
return ctx, weknora.InstanceIdentity{}, false, nil
|
|
}
|
|
identity, err := provider.TargetIdentity(ctx, knowledgeBaseID)
|
|
if err != nil {
|
|
return ctx, weknora.InstanceIdentity{}, true, err
|
|
}
|
|
identity.BaseURL = weknora.NormalizeInstanceURL(identity.BaseURL)
|
|
return ctx, identity, true, nil
|
|
}
|
|
|
|
// identityMatches requires BOTH the instance address and the owning tenant to
|
|
// match. A cloned instance can expose the same knowledge base id and tenant id;
|
|
// an empty stored identity is never treated as "matches anything".
|
|
func identityMatches(storedURL string, storedTenant uint64, current weknora.InstanceIdentity) bool {
|
|
if storedURL == "" || storedTenant == 0 {
|
|
return false
|
|
}
|
|
return weknora.NormalizeInstanceURL(storedURL) == weknora.NormalizeInstanceURL(current.BaseURL) &&
|
|
storedTenant == current.TenantID
|
|
}
|