Publish domain artifacts without reporting sidecars
This commit is contained in:
@@ -25,11 +25,6 @@ const chunkMapFileName = "chunk-map.json"
|
||||
|
||||
const evidenceContextFileName = "evidence-context.json"
|
||||
|
||||
const (
|
||||
warningsSchemaVersion = "notarius.warnings.v2"
|
||||
diagnosticsSchemaVersion = "notarius.diagnostics.v1"
|
||||
)
|
||||
|
||||
var safeOutputFileChar = regexp.MustCompile(`[^A-Za-z0-9._-]`)
|
||||
|
||||
var _ contracts.OutputEncoder = (*Encoder)(nil)
|
||||
@@ -231,73 +226,13 @@ func decodeEvidenceLaneIDs(value any) ([]string, error) {
|
||||
return lanes, nil
|
||||
}
|
||||
|
||||
type indexFile struct {
|
||||
ManifestFile string `json:"manifest_file"`
|
||||
OutputFiles []outputFileIndex `json:"output_files"`
|
||||
RejectedFile string `json:"rejected_file"`
|
||||
WarningsFile string `json:"warnings_file"`
|
||||
DiagnosticsFile string `json:"diagnostics_file"`
|
||||
ChunkMap *artifactIndex `json:"chunk_map,omitempty"`
|
||||
EvidenceContext *artifactIndex `json:"evidence_context,omitempty"`
|
||||
}
|
||||
|
||||
type artifactIndex struct {
|
||||
ArtifactKind contracts.ArtifactKind `json:"artifact_kind"`
|
||||
File string `json:"file"`
|
||||
MediaType string `json:"media_type"`
|
||||
SchemaID string `json:"schema_id"`
|
||||
SchemaName string `json:"schema_name"`
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
}
|
||||
|
||||
type outputFileIndex struct {
|
||||
LaneID string `json:"lane_id"`
|
||||
MediaType string `json:"media_type,omitempty"`
|
||||
File string `json:"file"`
|
||||
ModuleKey string `json:"module_key,omitempty"`
|
||||
SchemaID string `json:"schema_id,omitempty"`
|
||||
SchemaName string `json:"schema_name,omitempty"`
|
||||
SchemaVer string `json:"schema_version,omitempty"`
|
||||
}
|
||||
|
||||
type rejectedFile struct {
|
||||
Rejected []contracts.RejectedOutput `json:"rejected"`
|
||||
}
|
||||
|
||||
type warningsFile struct {
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
GroupCount int `json:"group_count"`
|
||||
OccurrenceCount int `json:"occurrence_count"`
|
||||
Groups []contracts.DiagnosticGroup `json:"groups"`
|
||||
}
|
||||
|
||||
type diagnosticsFile struct {
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
GroupCount int `json:"group_count"`
|
||||
OccurrenceCount int `json:"occurrence_count"`
|
||||
Truncated bool `json:"truncated"`
|
||||
UnrepresentedOccurrenceCount int `json:"unrepresented_occurrence_count"`
|
||||
Groups []contracts.DiagnosticGroup `json:"groups"`
|
||||
}
|
||||
|
||||
func logicalFiles(req contracts.OutputRequest, options Options) ([]contracts.OutputFile, error) {
|
||||
diagnosticProjection, err := contracts.ProjectDiagnosticCollection(req.Diagnostics)
|
||||
if err != nil {
|
||||
return nil, encoderErrorf("diagnostics: %w", err)
|
||||
}
|
||||
outputs := cloneNormalizeOutputs(req.NormalizeOutputs)
|
||||
sort.SliceStable(outputs, func(i, j int) bool {
|
||||
return outputs[i].LaneID < outputs[j].LaneID
|
||||
})
|
||||
|
||||
outputIndexes := make([]outputFileIndex, 0, len(outputs))
|
||||
files := make([]contracts.OutputFile, 0, len(outputs)+6)
|
||||
manifestFile, err := jsonFile("manifest.json", req.Manifest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, manifestFile)
|
||||
|
||||
files := make([]contracts.OutputFile, 0, len(outputs)+2)
|
||||
usedOutputFiles := make(map[string]string, len(outputs))
|
||||
for _, output := range outputs {
|
||||
name, err := outputFileName(output.LaneID)
|
||||
@@ -308,15 +243,6 @@ func logicalFiles(req contracts.OutputRequest, options Options) ([]contracts.Out
|
||||
return nil, encoderErrorf("lanes %q and %q produce duplicate output file %q", existingLane, output.LaneID, name)
|
||||
}
|
||||
usedOutputFiles[name] = output.LaneID
|
||||
outputIndexes = append(outputIndexes, outputFileIndex{
|
||||
LaneID: output.LaneID,
|
||||
MediaType: output.Artifact.MediaType,
|
||||
File: name,
|
||||
ModuleKey: output.NormalizerKey,
|
||||
SchemaID: output.Artifact.Schema.ID,
|
||||
SchemaName: output.Artifact.Schema.Name,
|
||||
SchemaVer: output.Artifact.Schema.Version,
|
||||
})
|
||||
file, err := serializedOutputFile(name, output.Artifact)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -324,103 +250,63 @@ func logicalFiles(req contracts.OutputRequest, options Options) ([]contracts.Out
|
||||
files = append(files, file)
|
||||
}
|
||||
|
||||
index := indexFile{
|
||||
ManifestFile: "manifest.json",
|
||||
OutputFiles: outputIndexes,
|
||||
RejectedFile: "rejected.json",
|
||||
WarningsFile: "warnings.json",
|
||||
DiagnosticsFile: "diagnostics.json",
|
||||
}
|
||||
if options.IncludeChunkMap && req.ChunkMap != nil {
|
||||
chunkMapOutput, chunkMapDescriptor, err := serializedChunkMapFile(*req.ChunkMap)
|
||||
chunkMapOutput, err := serializedChunkMapFile(*req.ChunkMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, chunkMapOutput)
|
||||
index.ChunkMap = &chunkMapDescriptor
|
||||
}
|
||||
if req.EvidenceContext != nil {
|
||||
evidenceOutput, evidenceDescriptor, err := serializedEvidenceContextFile(*req.EvidenceContext)
|
||||
if options.EvidenceContext.Enabled && req.EvidenceContext != nil {
|
||||
evidenceOutput, err := serializedEvidenceContextFile(*req.EvidenceContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, evidenceOutput)
|
||||
index.EvidenceContext = &evidenceDescriptor
|
||||
}
|
||||
indexOutput, err := jsonFile("index.json", index)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rejectedOutput, err := jsonFile("rejected.json", rejectedFile{Rejected: cloneRejected(req.Rejected)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
warningsOutput, err := jsonFile("warnings.json", newWarningsFile(diagnosticProjection))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
diagnosticsOutput, err := jsonFile("diagnostics.json", newDiagnosticsFile(req.Diagnostics, diagnosticProjection))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, indexOutput, rejectedOutput, warningsOutput, diagnosticsOutput)
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].Name < files[j].Name
|
||||
})
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func serializedChunkMapFile(artifact contracts.SerializedArtifact) (contracts.OutputFile, artifactIndex, error) {
|
||||
func serializedChunkMapFile(artifact contracts.SerializedArtifact) (contracts.OutputFile, error) {
|
||||
if artifact.Kind != chunkmap.ArtifactKind {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("chunk map has unexpected artifact kind %q", artifact.Kind)
|
||||
return contracts.OutputFile{}, encoderErrorf("chunk map has unexpected artifact kind %q", artifact.Kind)
|
||||
}
|
||||
if artifact.Schema.ID != chunkmap.SchemaID || artifact.Schema.Name != chunkmap.SchemaName || artifact.Schema.Version != chunkmap.SchemaVersion {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("chunk map has unexpected schema identity")
|
||||
return contracts.OutputFile{}, encoderErrorf("chunk map has unexpected schema identity")
|
||||
}
|
||||
if strings.TrimSpace(artifact.MediaType) != chunkmap.MediaType {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("chunk map has unsupported media type %q", artifact.MediaType)
|
||||
return contracts.OutputFile{}, encoderErrorf("chunk map has unsupported media type %q", artifact.MediaType)
|
||||
}
|
||||
if _, err := chunkmap.New().Decode(artifact.Content); err != nil {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("decode chunk map: %w", err)
|
||||
return contracts.OutputFile{}, encoderErrorf("decode chunk map: %w", err)
|
||||
}
|
||||
file, err := serializedOutputFile(chunkMapFileName, artifact)
|
||||
if err != nil {
|
||||
return contracts.OutputFile{}, artifactIndex{}, err
|
||||
return contracts.OutputFile{}, err
|
||||
}
|
||||
return file, artifactIndex{
|
||||
ArtifactKind: artifact.Kind,
|
||||
File: chunkMapFileName,
|
||||
MediaType: chunkmap.MediaType,
|
||||
SchemaID: artifact.Schema.ID,
|
||||
SchemaName: artifact.Schema.Name,
|
||||
SchemaVersion: artifact.Schema.Version,
|
||||
}, nil
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func serializedEvidenceContextFile(artifact contracts.SerializedArtifact) (contracts.OutputFile, artifactIndex, error) {
|
||||
func serializedEvidenceContextFile(artifact contracts.SerializedArtifact) (contracts.OutputFile, error) {
|
||||
codec := evidencecontext.New()
|
||||
expected := codec.Schema()
|
||||
if artifact.Kind != evidencecontext.ArtifactKind ||
|
||||
artifact.Schema.ID != expected.ID || artifact.Schema.Name != expected.Name || artifact.Schema.Version != expected.Version ||
|
||||
contracts.DigestArtifactSchema(artifact.Schema) != contracts.DigestArtifactSchema(expected) ||
|
||||
strings.TrimSpace(artifact.MediaType) != evidencecontext.MediaType {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("evidence context artifact is invalid")
|
||||
return contracts.OutputFile{}, encoderErrorf("evidence context artifact is invalid")
|
||||
}
|
||||
if _, err := codec.Decode(artifact.Content); err != nil {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("evidence context artifact is invalid")
|
||||
return contracts.OutputFile{}, encoderErrorf("evidence context artifact is invalid")
|
||||
}
|
||||
file, err := serializedOutputFile(evidenceContextFileName, artifact)
|
||||
if err != nil {
|
||||
return contracts.OutputFile{}, artifactIndex{}, encoderErrorf("evidence context artifact is invalid")
|
||||
return contracts.OutputFile{}, encoderErrorf("evidence context artifact is invalid")
|
||||
}
|
||||
return file, artifactIndex{
|
||||
ArtifactKind: evidencecontext.ArtifactKind,
|
||||
File: evidenceContextFileName,
|
||||
MediaType: evidencecontext.MediaType,
|
||||
SchemaID: expected.ID,
|
||||
SchemaName: expected.Name,
|
||||
SchemaVersion: expected.Version,
|
||||
}, nil
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func serializedOutputFile(name string, artifact contracts.SerializedArtifact) (contracts.OutputFile, error) {
|
||||
@@ -464,18 +350,6 @@ func isJSONMediaType(mediaType string) bool {
|
||||
return strings.EqualFold(base, contentTypeJSON)
|
||||
}
|
||||
|
||||
func jsonFile(name string, value any) (contracts.OutputFile, error) {
|
||||
data, err := marshalPretty(value)
|
||||
if err != nil {
|
||||
return contracts.OutputFile{}, encoderErrorf("encode %s: %w", name, err)
|
||||
}
|
||||
return contracts.OutputFile{
|
||||
Name: name,
|
||||
ContentType: contentTypeJSON,
|
||||
Bytes: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func marshalPretty(value any) ([]byte, error) {
|
||||
data, err := stdjson.MarshalIndent(value, "", " ")
|
||||
if err != nil {
|
||||
@@ -507,33 +381,6 @@ func cloneNormalizeOutputs(outputs []contracts.SerializedOutput) []contracts.Ser
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRejected(rejected []contracts.RejectedOutput) []contracts.RejectedOutput {
|
||||
if len(rejected) == 0 {
|
||||
return []contracts.RejectedOutput{}
|
||||
}
|
||||
return append([]contracts.RejectedOutput(nil), rejected...)
|
||||
}
|
||||
|
||||
func newWarningsFile(projection contracts.DiagnosticProjection) warningsFile {
|
||||
return warningsFile{
|
||||
SchemaVersion: warningsSchemaVersion,
|
||||
GroupCount: len(projection.Warnings),
|
||||
OccurrenceCount: projection.WarningOccurrenceCount,
|
||||
Groups: projection.Warnings,
|
||||
}
|
||||
}
|
||||
|
||||
func newDiagnosticsFile(collection contracts.DiagnosticCollection, projection contracts.DiagnosticProjection) diagnosticsFile {
|
||||
return diagnosticsFile{
|
||||
SchemaVersion: diagnosticsSchemaVersion,
|
||||
GroupCount: len(projection.Diagnostics),
|
||||
OccurrenceCount: projection.DiagnosticOccurrenceCount,
|
||||
Truncated: collection.Truncated,
|
||||
UnrepresentedOccurrenceCount: collection.UnrepresentedOccurrenceCount,
|
||||
Groups: projection.Diagnostics,
|
||||
}
|
||||
}
|
||||
|
||||
func encoderErrorf(format string, args ...any) error {
|
||||
return fmt.Errorf("json output encoder: "+format, args...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user