126 lines
4.5 KiB
Go
126 lines
4.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"droboticflow/internal/deliveryindex"
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// Read only the audit produced by the same completed, tenant-scoped package.
|
|
// Customer JSON deliberately contains no private run pointers.
|
|
func (s *server) classificationPackageAudit(r *http.Request, packages []domain.DeliveryPackageRecord, target deliveryDownloadTarget, partBytes []byte) ([]byte, string, error) {
|
|
var part struct {
|
|
Capture struct {
|
|
ID string `json:"captureId"`
|
|
} `json:"capture"`
|
|
PartID string `json:"partId"`
|
|
Revision int `json:"revision"`
|
|
}
|
|
if json.Unmarshal(partBytes, &part) != nil {
|
|
return nil, "", fmt.Errorf("invalid classification part")
|
|
}
|
|
var matched []byte
|
|
sourceRun := ""
|
|
for _, pkg := range packages {
|
|
run, err := s.store.GetRun(r.Context(), pkg.ChildRunID)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if run == nil || run.OrganizationID != s.requestOrganizationID(r) || run.ProjectID != pkg.ProjectID || run.Status != domain.RunStatusCompleted {
|
|
continue
|
|
}
|
|
ownsTarget := false
|
|
for _, a := range run.Artifacts {
|
|
if !deliveryPackageArtifactAllowed(&pkg, a.ID) || a.RunID != run.ID || a.Payload == nil || a.Payload.Ref == nil {
|
|
continue
|
|
}
|
|
ref := a.Payload.Ref
|
|
if ref.ConnectionID == target.connectionID && ref.Bucket == target.bucket && strings.Trim(ref.Prefix, "/") == target.prefix && ref.ManifestKey == target.prefix+"/manifest.json" {
|
|
ownsTarget = true
|
|
}
|
|
}
|
|
if !ownsTarget {
|
|
continue
|
|
}
|
|
for _, a := range run.Artifacts {
|
|
if a.RunID != run.ID || !domain.IsInternalDeliveryAudit(a) {
|
|
continue
|
|
}
|
|
data, readErr := s.readClassificationAudit(r, run, a)
|
|
if readErr != nil {
|
|
return nil, "", readErr
|
|
}
|
|
raw, err := json.Marshal(data)
|
|
if err != nil || len(raw) > 8<<20 {
|
|
return nil, "", fmt.Errorf("内部追溯不可读或超过 8 MiB")
|
|
}
|
|
var audit struct {
|
|
Schema string `json:"schemaVersion"`
|
|
CaptureID string `json:"captureId"`
|
|
PartID string `json:"partId"`
|
|
Revision int `json:"revision"`
|
|
PartSHA string `json:"partSha256"`
|
|
Lineage struct {
|
|
RunID string `json:"sourceRunId"`
|
|
} `json:"lineage"`
|
|
}
|
|
if json.Unmarshal(raw, &audit) != nil || audit.Schema != "droboticflow.delivery.review-history/v2" || audit.CaptureID != part.Capture.ID || audit.PartID != part.PartID || audit.Revision != part.Revision || audit.PartSHA != deliveryindex.Hash(partBytes) || audit.Lineage.RunID != pkg.ParentRunID {
|
|
return nil, "", fmt.Errorf("内部追溯与固定验收结果不一致")
|
|
}
|
|
if matched != nil {
|
|
return nil, "", fmt.Errorf("交付分类对应多个内部追溯")
|
|
}
|
|
matched, sourceRun = raw, audit.Lineage.RunID
|
|
}
|
|
}
|
|
if matched == nil || sourceRun == "" {
|
|
return nil, "", fmt.Errorf("缺少同一正式交付任务的内部验收证据")
|
|
}
|
|
return matched, sourceRun, nil
|
|
}
|
|
|
|
func (s *server) readClassificationAudit(r *http.Request, run *domain.RunRecord, a domain.ExecutionArtifact) (map[string]any, error) {
|
|
if a.Payload != nil && a.Payload.Ref != nil {
|
|
ref := a.Payload.Ref
|
|
if ref.Kind != domain.ArtifactRefKindObject || ref.SizeBytes <= 0 || ref.SizeBytes > 8<<20 || len(ref.SHA256) != 64 {
|
|
return nil, fmt.Errorf("内部追溯引用缺少完整校验")
|
|
}
|
|
connection, err := s.resolveArtifactStorageConnection(r.Context(), run, a, ref.Bucket)
|
|
if err != nil || connection == nil || connection.ID != ref.ConnectionID {
|
|
return nil, fmt.Errorf("内部追溯连接不可访问")
|
|
}
|
|
browser, cleanup, err := storageConnectionBrowserResolver(r.Context(), s.storageConnectionWithResolvedEndpoint(connection))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if cleanup != nil {
|
|
defer cleanup()
|
|
}
|
|
raw, _, err := browser.ReadObjectPreview(r.Context(), ref.Bucket, ref.Key, 8<<20+1)
|
|
if err != nil || int64(len(raw)) != ref.SizeBytes || deliveryindex.Hash(raw) != ref.SHA256 {
|
|
return nil, fmt.Errorf("内部追溯文件校验失败")
|
|
}
|
|
var data map[string]any
|
|
if json.Unmarshal(raw, &data) != nil {
|
|
return nil, fmt.Errorf("内部追溯 JSON 无效")
|
|
}
|
|
return data, nil
|
|
}
|
|
if a.Payload != nil && a.Payload.Mode == domain.ArtifactTransferModeInline {
|
|
raw, err := json.Marshal(a.Payload.Inline)
|
|
if err != nil || len(raw) > 8<<20 {
|
|
return nil, fmt.Errorf("内部追溯过大")
|
|
}
|
|
var data map[string]any
|
|
if json.Unmarshal(raw, &data) != nil {
|
|
return nil, fmt.Errorf("内部追溯 JSON 无效")
|
|
}
|
|
return data, nil
|
|
}
|
|
return artifactStructuredData(&a), nil
|
|
}
|