Retain source history for inspection
This commit is contained in:
@@ -144,6 +144,8 @@ view behavior stays safe. No raw credential values are retained.
|
||||
|
||||
## Stage 3 — Support source-tree inspection history
|
||||
|
||||
**Complete.**
|
||||
|
||||
Read `load_input.go`, `history_model.go`, `history_source.go`,
|
||||
`history_inheritance.go`, `load_metadata.go`, `metadata.go` and `explain.go`.
|
||||
Extract the source/inheritance prefix construction currently embedded in
|
||||
|
||||
@@ -109,6 +109,39 @@ func prepareInspectionHistory[T any](resolved resolvedLoad[T], counter *retained
|
||||
return inspection, true
|
||||
}
|
||||
|
||||
// prepareSourceInspection retains only the source and completed inheritance
|
||||
// evidence represented by input; it does not construct typed defaults.
|
||||
func prepareSourceInspection(input assembledInput, counter *retainedNodeCounter) (*Inspection, bool) {
|
||||
if input.document == nil {
|
||||
return nil, false
|
||||
}
|
||||
metadata := Metadata{fields: make(map[string]FieldInfo), historyBuilder: newHistoryBuilder()}
|
||||
var origins map[string]Origin
|
||||
if input.inheritance != nil {
|
||||
origins = input.inheritance.origins
|
||||
} else if input.composition != nil {
|
||||
origins = input.composition.origins
|
||||
}
|
||||
walkDocument(input.document, Path{}, func(path Path, node *document.Node) {
|
||||
origin, found := origins[path.String()]
|
||||
if !found && node.Location.SourceID != "" {
|
||||
origin = Origin{Layer: LayerMain, Locations: []Location{publicLocation(node.Location)}}
|
||||
}
|
||||
metadata.fields[path.String()] = FieldInfo{Explicit: true, Null: node.Kind == document.Null, Origin: cloneOrigin(origin)}
|
||||
})
|
||||
observeSourcePrefix(metadata.historyBuilder, input)
|
||||
observation := historyPhaseSources
|
||||
if input.inheritance != nil {
|
||||
observation = historyPhaseInheritance
|
||||
}
|
||||
sealMetadataHistory(&metadata, input.document, observation)
|
||||
inspection := &Inspection{metadata: metadata, history: metadata.historyModel, final: input.document}
|
||||
if !addInspectionHistory(counter, inspection) {
|
||||
return nil, false
|
||||
}
|
||||
return inspection, true
|
||||
}
|
||||
|
||||
func publishLoadResult[T any](schema *schemaDescriptor, definition Definition[T], resolved resolvedLoad[T], inspection *Inspection, references map[string]string, accumulator *diagnosticAccumulator) (*Loaded[T], error) {
|
||||
// A read-only validator can replace a value with an equal borrowed object.
|
||||
// Detach without re-reading the environment or discarding validated state.
|
||||
|
||||
@@ -144,15 +144,19 @@ func (m Metadata) hasHistoricalPath(path Path) bool {
|
||||
}
|
||||
|
||||
func freezeMetadataHistory(m *Metadata, final *document.Node) {
|
||||
finalizeEffectiveMetadataPaths(m, final)
|
||||
builder := m.historyBuilder
|
||||
if builder == nil || !builder.prefixReady {
|
||||
panic("configkit: metadata history prefix is not ready")
|
||||
}
|
||||
sealMetadataHistory(m, final, historyPhaseResolution)
|
||||
}
|
||||
|
||||
func sealMetadataHistory(m *Metadata, final *document.Node, observation historyPhase) {
|
||||
finalizeEffectiveMetadataPaths(m, final)
|
||||
builder := m.historyBuilder
|
||||
builder.model.final = final
|
||||
// A resolver may change dynamic field tags without changing canonical values
|
||||
// or producing a resolution snapshot. Always observe the final classification.
|
||||
builder.observeDocument(final, historyPhaseResolution)
|
||||
// Include represented paths and sensitivity even without a later operation.
|
||||
builder.observeDocument(final, observation)
|
||||
m.historyModel = builder.freeze()
|
||||
m.historyBuilder = nil
|
||||
}
|
||||
|
||||
@@ -20,21 +20,7 @@ func preparePostDecodeOverrideContext(input assembledInput, before decodedConfig
|
||||
|
||||
metadata := metadataFromDecoded(before)
|
||||
cliRecords := overrideMetadataRecords(before, assigned.overrides)
|
||||
applications := structuralHistoryApplications(input)
|
||||
var structuralOverlays []overlayRecord
|
||||
if input.inheritance != nil && input.composition != nil {
|
||||
structuralOverlays = input.composition.overlays
|
||||
metadata.historyBuilder.setScratch(input.composition.root)
|
||||
metadata.historyBuilder.observeSourceEvidence(input.composition.root, input.composition.origins, applications, structuralOverlays)
|
||||
metadata.historyBuilder.setScratch(input.composition.root)
|
||||
metadata.historyBuilder.observeInheritanceEvidence(input.inheritance.operations)
|
||||
} else if input.composition != nil {
|
||||
structuralOverlays = input.composition.overlays
|
||||
metadata.historyBuilder.setScratch(input.composition.root)
|
||||
metadata.historyBuilder.observeSourceEvidence(input.composition.root, input.composition.origins, applications, structuralOverlays)
|
||||
} else if len(applications) != 0 {
|
||||
metadata.historyBuilder.observeSourceEvidence(input.document, nil, applications, nil)
|
||||
}
|
||||
observeSourcePrefix(metadata.historyBuilder, input)
|
||||
metadata.historyBuilder.observePreCLICheckpoint(before.final, before.origins)
|
||||
var postDecodeApps []historyApplication
|
||||
for _, override := range assigned.overrides {
|
||||
@@ -82,6 +68,24 @@ func preparePostDecodeOverrideContext(input assembledInput, before decodedConfig
|
||||
}
|
||||
}
|
||||
|
||||
func observeSourcePrefix(builder *historyBuilder, input assembledInput) {
|
||||
applications := structuralHistoryApplications(input)
|
||||
var structuralOverlays []overlayRecord
|
||||
if input.inheritance != nil && input.composition != nil {
|
||||
structuralOverlays = input.composition.overlays
|
||||
builder.setScratch(input.composition.root)
|
||||
builder.observeSourceEvidence(input.composition.root, input.composition.origins, applications, structuralOverlays)
|
||||
builder.setScratch(input.composition.root)
|
||||
builder.observeInheritanceEvidence(input.inheritance.operations)
|
||||
} else if input.composition != nil {
|
||||
structuralOverlays = input.composition.overlays
|
||||
builder.setScratch(input.composition.root)
|
||||
builder.observeSourceEvidence(input.composition.root, input.composition.origins, applications, structuralOverlays)
|
||||
} else if len(applications) != 0 {
|
||||
builder.observeSourceEvidence(input.document, nil, applications, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func overrideMetadataRecords(before decodedConfiguration, overrides []preparedOverride) []overlayRecord {
|
||||
if len(overrides) == 0 {
|
||||
return nil
|
||||
|
||||
106
internal/engine/source_inspection_test.go
Normal file
106
internal/engine/source_inspection_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSourceInspectionRetainsRawOverlayHistoryAndSensitivity(t *testing.T) {
|
||||
type settings struct {
|
||||
Count int `yaml:"count"`
|
||||
Private map[string]string `yaml:"private" configkit:"secret"`
|
||||
Default string `yaml:"default"`
|
||||
}
|
||||
base := parseCompositionDocument(t, "source-1", "count: 1\nprivate: {token: old-canary}\n")
|
||||
composed, diagnostics := composeDocuments(base)
|
||||
if len(diagnostics) != 0 {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
overlay := parseCompositionDocument(t, "source-2", "count: invalid\nprivate: {token: new-canary}\n")
|
||||
composed, diagnostics = overlayDocuments(composed, overlay)
|
||||
if len(diagnostics) != 0 {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
input := assembledInput{document: composed.root, composition: composed}
|
||||
schema, issue := buildSchema(reflect.TypeFor[settings](), testSecretBinding())
|
||||
if issue != nil {
|
||||
t.Fatal(issue)
|
||||
}
|
||||
definition := Definition[settings]{Defaults: func() settings { return settings{Default: "invented"} }}
|
||||
if _, diagnostics := decodeLoadInput(schema, definition, input); len(diagnostics) == 0 {
|
||||
t.Fatal("expected the raw count to fail typed decoding")
|
||||
}
|
||||
inspection, ok := prepareSourceInspection(input, newRetainedNodeCounter(historyNodeLimit))
|
||||
if !ok {
|
||||
t.Fatal("source inspection unavailable")
|
||||
}
|
||||
accumulator := &diagnosticAccumulator{}
|
||||
failure := accumulator.loadError()
|
||||
finishInspection(schema, nil, nil, nil, inspection, nil, accumulator, failure, InspectionStageInherited)
|
||||
if _, found := inspection.Metadata().Lookup(Keys("default")); found {
|
||||
t.Fatal("source inspection invented a default")
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := inspection.WriteEffective(&output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "count: invalid") || strings.Contains(output.String(), "canary") {
|
||||
t.Fatalf("unsafe or incorrect effective output: %s", output.String())
|
||||
}
|
||||
explanation, found := inspection.Explain(Keys("private", "token"))
|
||||
if !found || len(explanation.History) < 2 {
|
||||
t.Fatalf("missing displaced history: %#v", explanation)
|
||||
}
|
||||
for _, event := range explanation.History {
|
||||
if strings.Contains(event.Before.YAML+event.After.YAML, "canary") {
|
||||
t.Fatalf("unredacted history: %#v", event)
|
||||
}
|
||||
}
|
||||
info, found := inspection.Metadata().Lookup(Keys("count"))
|
||||
if !found || !info.Explicit || len(info.Origin.Locations) == 0 || info.Origin.Locations[0].SourceID != "source-2" {
|
||||
t.Fatalf("wrong source origin: %#v", info)
|
||||
}
|
||||
if _, ok := prepareSourceInspection(input, newRetainedNodeCounter(0)); ok {
|
||||
t.Fatal("source inspection bypassed history budget")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceInspectionRetainsCompletedInheritance(t *testing.T) {
|
||||
type entry struct {
|
||||
Count int `yaml:"count"`
|
||||
}
|
||||
type settings struct {
|
||||
Parent entry `yaml:"parent"`
|
||||
Child entry `yaml:"child"`
|
||||
}
|
||||
root := parseCompositionDocument(t, "source-1", "parent: {count: invalid}\nchild: {extends: parent}\n")
|
||||
composed, diagnostics := composeDocuments(root)
|
||||
if len(diagnostics) != 0 {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
input, _, expanded, diagnostics := expandStructuralInheritance(assembledInput{document: composed.root, composition: composed}, nil, sequencePolicy{})
|
||||
if !expanded || len(diagnostics) != 0 {
|
||||
t.Fatalf("inheritance: expanded=%v diagnostics=%v", expanded, diagnostics)
|
||||
}
|
||||
schema, issue := buildSchema(reflect.TypeFor[settings](), testSecretBinding())
|
||||
if issue != nil {
|
||||
t.Fatal(issue)
|
||||
}
|
||||
if _, diagnostics := decodeLoadInput(schema, Definition[settings]{}, input); len(diagnostics) == 0 {
|
||||
t.Fatal("expected inherited invalid value to fail decoding")
|
||||
}
|
||||
inspection, ok := prepareSourceInspection(input, newRetainedNodeCounter(historyNodeLimit))
|
||||
if !ok {
|
||||
t.Fatal("source inspection unavailable")
|
||||
}
|
||||
explanation, found := inspection.Explain(Keys("child", "count"))
|
||||
if !found || !strings.Contains(explanation.Effective.YAML, "invalid") || len(explanation.History) == 0 {
|
||||
t.Fatalf("inherited value or history unavailable: %#v", explanation)
|
||||
}
|
||||
info, found := inspection.Metadata().Lookup(Keys("child", "count"))
|
||||
if !found || !info.Explicit || len(info.Origin.Inheritance) == 0 {
|
||||
t.Fatalf("inherited origin unavailable: %#v", info)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user