2de7a43286
页面原本没有任何生产入口,授权/重排/回滚只能带 Bearer 调接口,实际还要 admin 会话。 现在三个端点支持两条通道,门禁语义不变: - 运维通道:Authorization: Bearer <token>,审计记 PRODUCTION_RELEASE_AUTHORIZER_REF - 页面通道:已认证 admin 会话 + 固定确认语句,审计记该管理员用户名 - 请求门禁 / 备份恢复门禁 / Release Worker 执行门禁三者不受影响 - 发布管理第 6–8 步加「授权发布 / 重排 / 回滚」按钮,点击需逐字输入确认语句, 仅 admin 可见;重排沿用原执行授权人,不改写历史 - openapi 与 P6 契约同步;测试:双通道各一条 + 非 admin/错短语拒绝 + 前端按钮与确认门禁
119 lines
4.6 KiB
Go
119 lines
4.6 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"knowledge-governance/internal/releasebundle"
|
|
releaseworkflow "knowledge-governance/internal/workflows/release"
|
|
)
|
|
|
|
type targetContractDB interface {
|
|
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
|
|
}
|
|
|
|
type elasticsearchMappingInspector interface {
|
|
MappingSHA256(ctx context.Context) (string, error)
|
|
}
|
|
|
|
type TargetContractInspectorConfig struct {
|
|
DB targetContractDB
|
|
Mapping elasticsearchMappingInspector
|
|
KnowledgeBaseID string
|
|
TenantID uint64
|
|
IndexName string
|
|
// ReadTestedBase makes the inspector read the TESTED base (the source side
|
|
// of a release) instead of the 源库 → 目标库 mapped base. The exporter and
|
|
// the target share this inspector type, so the side must be explicit: with
|
|
// a mapping in play, "which base do I read" is not the same question on
|
|
// both ends.
|
|
ReadTestedBase bool
|
|
}
|
|
|
|
type TargetContractInspector struct {
|
|
config TargetContractInspectorConfig
|
|
}
|
|
|
|
const targetContractSQL = `
|
|
SELECT migration.version,
|
|
migration.dirty,
|
|
knowledge_base.tenant_id,
|
|
model.name,
|
|
COALESCE(NULLIF(model.parameters->'embedding_parameters'->>'dimension', '')::INTEGER, 0),
|
|
COALESCE(vector_store.engine_type, ''),
|
|
COALESCE(vector_store.index_config->>'index_name', '')
|
|
FROM knowledge_bases AS knowledge_base
|
|
JOIN models AS model
|
|
ON model.id = knowledge_base.embedding_model_id
|
|
AND model.deleted_at IS NULL
|
|
JOIN schema_migrations AS migration ON TRUE
|
|
LEFT JOIN vector_stores AS vector_store
|
|
ON vector_store.id = knowledge_base.vector_store_id
|
|
AND vector_store.deleted_at IS NULL
|
|
WHERE knowledge_base.id = $1
|
|
AND knowledge_base.deleted_at IS NULL
|
|
`
|
|
|
|
func NewTargetContractInspector(config TargetContractInspectorConfig) (*TargetContractInspector, error) {
|
|
config.KnowledgeBaseID = strings.TrimSpace(config.KnowledgeBaseID)
|
|
config.IndexName = strings.TrimSpace(config.IndexName)
|
|
if config.DB == nil || config.Mapping == nil || config.TenantID == 0 || !validSearchIdentifier(config.KnowledgeBaseID) || !validSearchIdentifier(config.IndexName) {
|
|
return nil, errors.New("invalid target contract inspector configuration")
|
|
}
|
|
return &TargetContractInspector{config: config}, nil
|
|
}
|
|
|
|
func (inspector *TargetContractInspector) Contract(ctx context.Context, item releaseworkflow.PlanItem) (releasebundle.TargetContract, error) {
|
|
if item.TestKnowledgeBaseID != inspector.config.KnowledgeBaseID {
|
|
return releasebundle.TargetContract{}, errors.New("release plan knowledge base does not match target contract")
|
|
}
|
|
// The route is keyed by the TESTED base (where the content was verified). The
|
|
// contract is read for the base this side actually owns: the source side reads
|
|
// the tested base (what the bundle is exported from), the target side reads the
|
|
// 源库 → 目标库 mapped base (what the release writes into).
|
|
targetKnowledgeBaseID := item.TargetBase()
|
|
if inspector.config.ReadTestedBase {
|
|
targetKnowledgeBaseID = item.TestKnowledgeBaseID
|
|
}
|
|
if !validSearchIdentifier(targetKnowledgeBaseID) {
|
|
return releasebundle.TargetContract{}, errors.New("invalid release target knowledge base")
|
|
}
|
|
var (
|
|
migrationVersion int
|
|
dirty bool
|
|
tenantID uint64
|
|
embeddingModel string
|
|
dimension int
|
|
engineType string
|
|
indexName string
|
|
)
|
|
if err := inspector.config.DB.QueryRow(ctx, targetContractSQL, targetKnowledgeBaseID).Scan(
|
|
&migrationVersion, &dirty, &tenantID, &embeddingModel, &dimension, &engineType, &indexName,
|
|
); err != nil {
|
|
return releasebundle.TargetContract{}, errors.New("read target WeKnora contract")
|
|
}
|
|
if migrationVersion != releasebundle.WeKnoraMigrationV071 || dirty || tenantID != inspector.config.TenantID ||
|
|
!validSearchIdentifier(embeddingModel) || dimension < 1 ||
|
|
(engineType != "" && engineType != "elasticsearch") ||
|
|
(indexName != "" && indexName != inspector.config.IndexName) {
|
|
return releasebundle.TargetContract{}, errors.New("target WeKnora contract mismatch")
|
|
}
|
|
mappingSHA256, err := inspector.config.Mapping.MappingSHA256(ctx)
|
|
if err != nil || !validLowerSHA256(mappingSHA256) {
|
|
return releasebundle.TargetContract{}, errors.New("read target Elasticsearch mapping contract")
|
|
}
|
|
return releasebundle.TargetContract{
|
|
WeKnoraVersion: releasebundle.WeKnoraVersionV071,
|
|
MigrationVersion: releasebundle.WeKnoraMigrationV071,
|
|
TenantID: tenantID,
|
|
KnowledgeBaseID: targetKnowledgeBaseID,
|
|
RetrieverEngine: "elasticsearch_v8",
|
|
EmbeddingModel: embeddingModel,
|
|
EmbeddingDimension: dimension,
|
|
ElasticsearchMappingSHA256: mappingSHA256,
|
|
}, nil
|
|
}
|