Render focused inspection values safely
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Implementation plan: typed result values and focused inspection
|
# Implementation plan: typed result values and focused inspection
|
||||||
|
|
||||||
Status: implementation in progress; Stages 1–3 complete.
|
Status: implementation in progress; Stages 1–4 complete.
|
||||||
|
|
||||||
## Scope and working instructions
|
## Scope and working instructions
|
||||||
|
|
||||||
@@ -179,6 +179,8 @@ exist in either effective or historical data.
|
|||||||
|
|
||||||
## Stage 4 — Render focused effective output with original sensitivity
|
## Stage 4 — Render focused effective output with original sensitivity
|
||||||
|
|
||||||
|
**Complete.**
|
||||||
|
|
||||||
Add `func (i Inspection) WriteEffective(output io.Writer) error` and a scoped
|
Add `func (i Inspection) WriteEffective(output io.Writer) error` and a scoped
|
||||||
engine rendering operation using the saved final tree.
|
engine rendering operation using the saved final tree.
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package configkit_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
configkit "go.maximumdirect.net/configkit"
|
configkit "go.maximumdirect.net/configkit"
|
||||||
@@ -61,6 +63,66 @@ func TestInspectionViewScopeAndQueries(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type viewFailWriter struct{}
|
||||||
|
|
||||||
|
func (viewFailWriter) Write([]byte) (int, error) { return 0, errors.New("write failed") }
|
||||||
|
|
||||||
|
func TestInspectionViewWriteEffective(t *testing.T) {
|
||||||
|
options := configkit.Options[map[string]any]{Definition: configkit.Definition[map[string]any]{
|
||||||
|
Sensitive: func(path configkit.Path) bool { return path.String() == "/private" || path.String() == "/nested/token" },
|
||||||
|
Resolve: func(current *map[string]any, resolution *configkit.Resolution) error {
|
||||||
|
delete(*current, "removed")
|
||||||
|
resolution.RecordNormalization(configkit.Keys("removed"), "remove")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
result, err := configkit.Load(configkit.Bytes("view.yml", []byte("private: {child: canary-private}\nnested: {token: canary-token, name: public}\nitems: [zero, one]\n'0': numeric\n'null': null\nremoved: earlier\n")), options)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
path configkit.Path
|
||||||
|
contains string
|
||||||
|
absent string
|
||||||
|
fails bool
|
||||||
|
}{
|
||||||
|
{configkit.Path{}, "public", "canary-token", false},
|
||||||
|
{configkit.Keys("nested"), "public", "canary-token", false},
|
||||||
|
{configkit.Keys("private", "child"), "REDACTED", "canary-private", false},
|
||||||
|
{configkit.Keys("items"), "one", "", false},
|
||||||
|
{configkit.Keys("items").Index(1), "one", "", false},
|
||||||
|
{configkit.Path{}.Index(0), "numeric", "", false},
|
||||||
|
{configkit.Keys("null"), "null", "", false},
|
||||||
|
{configkit.Keys("removed"), "", "", true},
|
||||||
|
{configkit.Keys("missing"), "", "", true},
|
||||||
|
} {
|
||||||
|
view, ok := result.InspectSubtree(tc.path)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("view unavailable")
|
||||||
|
}
|
||||||
|
var out bytes.Buffer
|
||||||
|
err := view.WriteEffective(&out)
|
||||||
|
if (err != nil) != tc.fails || (tc.fails && out.Len() != 0) || (tc.contains != "" && !strings.Contains(out.String(), tc.contains)) || (tc.absent != "" && strings.Contains(out.String(), tc.absent)) {
|
||||||
|
t.Errorf("%s: %q, %v", tc.path.String(), out.String(), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
view, _ := result.InspectSubtree(configkit.Keys("nested"))
|
||||||
|
result.Config["nested"] = "changed"
|
||||||
|
var out bytes.Buffer
|
||||||
|
if err := view.WriteEffective(&out); err != nil || !strings.Contains(out.String(), "public") {
|
||||||
|
t.Fatalf("frozen output = %q, %v", out.String(), err)
|
||||||
|
}
|
||||||
|
if err := view.WriteEffective(viewFailWriter{}); err == nil {
|
||||||
|
t.Fatal("writer error lost")
|
||||||
|
}
|
||||||
|
if err := view.WriteEffective(nil); err == nil {
|
||||||
|
t.Fatal("nil writer accepted")
|
||||||
|
}
|
||||||
|
if err := (configkit.Inspection{}).WriteEffective(&bytes.Buffer{}); err == nil {
|
||||||
|
t.Fatal("zero view accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInspectionViewUnavailable(t *testing.T) {
|
func TestInspectionViewUnavailable(t *testing.T) {
|
||||||
var result *configkit.Result[map[string]any]
|
var result *configkit.Result[map[string]any]
|
||||||
if _, ok := result.InspectSubtree(configkit.Path{}); ok {
|
if _, ok := result.InspectSubtree(configkit.Path{}); ok {
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ func (i Inspection) HistoricalPaths() []Path {
|
|||||||
return paths
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteEffective renders the saved value at the reporting scope with its
|
||||||
|
// original sensitivity. Unavailable values and nil writers return an error;
|
||||||
|
// writer errors can leave partial output.
|
||||||
|
func (i Inspection) WriteEffective(output io.Writer) error { return i.view.WriteEffective(output) }
|
||||||
|
|
||||||
// Sources returns the full detached source catalog.
|
// Sources returns the full detached source catalog.
|
||||||
func (i Inspection) Sources() []SourceInfo {
|
func (i Inspection) Sources() []SourceInfo {
|
||||||
values := i.view.Inspection().Sources()
|
values := i.view.Inspection().Sources()
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
yamladapter "go.maximumdirect.net/configkit/internal/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
// InspectionView borrows an inspection and selects a canonical reporting scope.
|
// InspectionView borrows an inspection and selects a canonical reporting scope.
|
||||||
type InspectionView struct {
|
type InspectionView struct {
|
||||||
inspection *Inspection
|
inspection *Inspection
|
||||||
@@ -19,6 +25,17 @@ func NewInspectionView(inspection *Inspection, scope Path) (InspectionView, bool
|
|||||||
|
|
||||||
func (v InspectionView) Inspection() *Inspection { return v.inspection }
|
func (v InspectionView) Inspection() *Inspection { return v.inspection }
|
||||||
|
|
||||||
|
func (v InspectionView) WriteEffective(output io.Writer) error {
|
||||||
|
if v.inspection == nil || v.inspection.final == nil || output == nil {
|
||||||
|
return errEffectiveUnavailable
|
||||||
|
}
|
||||||
|
node, ok := documentAtPathWithLookup(v.inspection.final, v.scope, v.inspection.documentLookup())
|
||||||
|
if !ok {
|
||||||
|
return errEffectiveUnavailable
|
||||||
|
}
|
||||||
|
return yamladapter.Write(output, redactedDocument(node, v.inspection.secrets, v.scope))
|
||||||
|
}
|
||||||
|
|
||||||
func (v InspectionView) HistoricalPaths() []Path {
|
func (v InspectionView) HistoricalPaths() []Path {
|
||||||
if v.inspection == nil {
|
if v.inspection == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user