Report configured bootstrap evidence at invocation completion
This commit is contained in:
55
internal/app/bootstrap_reporting.go
Normal file
55
internal/app/bootstrap_reporting.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"go.maximumdirect.net/commandkit"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
type bootstrapEvidenceKey struct{}
|
||||
type bootstrapEvidence struct {
|
||||
reports []*configkit.SecretsReport
|
||||
diagnostics [][]configkit.Diagnostic
|
||||
}
|
||||
|
||||
func (e *bootstrapEvidence) Text() (string, error) {
|
||||
var output strings.Builder
|
||||
for _, report := range e.reports {
|
||||
if _, err := report.WriteTo(&output); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
for _, diagnostics := range e.diagnostics {
|
||||
if err := configkit.WriteDiagnostics(&output, diagnostics); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
var bootstrapTemplate = template.Must(commandkit.ParseTextTemplate("bootstrap", "{{.Text}}", nil))
|
||||
|
||||
func bootstrapFeature() commandkit.InvocationFeature {
|
||||
return commandkit.NewInvocationFeature(commandkit.InvocationFeatureSpec[struct{}]{Start: func(inv commandkit.Invocation, _ struct{}) (context.Context, func() commandkit.Report) {
|
||||
evidence := &bootstrapEvidence{}
|
||||
return context.WithValue(inv.Context, bootstrapEvidenceKey{}, evidence), func() commandkit.Report { return commandkit.Report{Value: evidence, TextTemplate: bootstrapTemplate} }
|
||||
}})
|
||||
}
|
||||
|
||||
func recordBootstrapEvidence(ctx context.Context, report *configkit.SecretsReport, err error) {
|
||||
evidence, _ := ctx.Value(bootstrapEvidenceKey{}).(*bootstrapEvidence)
|
||||
if evidence == nil {
|
||||
return
|
||||
}
|
||||
if report != nil {
|
||||
evidence.reports = append(evidence.reports, report)
|
||||
}
|
||||
var secretsError *configkit.SecretsError
|
||||
if errors.As(err, &secretsError) {
|
||||
evidence.diagnostics = append(evidence.diagnostics, secretsError.Diagnostics())
|
||||
}
|
||||
}
|
||||
92
internal/app/bootstrap_reporting_test.go
Normal file
92
internal/app/bootstrap_reporting_test.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func TestConfiguredBootstrapReportsSafeEvidence(t *testing.T) {
|
||||
const name = "NARRATIO_REPORT_TEST_KEY"
|
||||
const secret = "synthetic-report-secret"
|
||||
restoreEnvAfterTest(t, name)
|
||||
root := t.TempDir()
|
||||
pipeline := filepath.Join(root, "pipeline.yml")
|
||||
secrets := filepath.Join(root, "secrets")
|
||||
mustWriteSecretFile(t, filepath.Join(secrets, name), secret)
|
||||
mustWriteTestFile(t, pipeline, "config: {secrets_dir: secrets}\nwhisperx: {transcribe_url: https://example.test/transcribe}\n")
|
||||
for _, quiet := range []bool{false, true} {
|
||||
if err := os.Unsetenv(name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
args := []string{"config", "show", "--config", pipeline, "--format", "json"}
|
||||
if quiet {
|
||||
args = append(args, "--quiet")
|
||||
}
|
||||
var out, diagnostic bytes.Buffer
|
||||
result := Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if result.Status != 0 {
|
||||
t.Fatalf("status=%d diagnostics=%s", result.Status, diagnostic.String())
|
||||
}
|
||||
if strings.Contains(out.String()+diagnostic.String(), secret) {
|
||||
t.Fatal("bootstrap exposed secret")
|
||||
}
|
||||
if quiet && (out.Len() != 0 || diagnostic.Len() != 0) {
|
||||
t.Fatalf("quiet output=%s diagnostic=%s", out.String(), diagnostic.String())
|
||||
}
|
||||
if !quiet && (!strings.Contains(diagnostic.String(), name) || strings.Contains(out.String(), name)) {
|
||||
t.Fatalf("report was not separated: output=%s diagnostic=%s", out.String(), diagnostic.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapEvidenceSurvivesFailureAndDeliveryError(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
pipeline := filepath.Join(root, "pipeline.yml")
|
||||
mustWriteTestFile(t, pipeline, "config: {secrets_dir: secrets}\nwhisperx: {transcribe_url: https://example.test/transcribe}\n")
|
||||
mustWriteSecretFile(t, filepath.Join(root, "secrets", "INVALID-NAME"), "synthetic-private-value")
|
||||
var out, diagnostic bytes.Buffer
|
||||
result := Execute(context.Background(), []string{"config", "show", "--config", pipeline, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if result.Status != 1 || result.Cause == nil || !strings.Contains(diagnostic.String(), "INVALID-NAME") || strings.Contains(diagnostic.String(), "synthetic-private-value") {
|
||||
t.Fatalf("result=%#v diagnostic=%s", result, diagnostic.String())
|
||||
}
|
||||
if err := os.Remove(filepath.Join(root, "secrets", "INVALID-NAME")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const name = "NARRATIO_REPORT_WRITER_TEST"
|
||||
restoreEnvAfterTest(t, name)
|
||||
mustWriteSecretFile(t, filepath.Join(root, "secrets", name), "synthetic")
|
||||
failure := errors.New("diagnostic writer failed")
|
||||
result = Execute(context.Background(), []string{"config", "show", "--config", pipeline}, commandkit.Streams{Out: &out, Err: runReportWriteFunc(func([]byte) (int, error) { return 0, failure })})
|
||||
if result.Status != 1 {
|
||||
t.Fatalf("writer failure result=%#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapEvidenceAfterLoadFailureAndCancellation(t *testing.T) {
|
||||
const name = "NARRATIO_REPORT_FAILURE_KEY"
|
||||
restoreEnvAfterTest(t, name)
|
||||
root := t.TempDir()
|
||||
pipeline := filepath.Join(root, "pipeline.yml")
|
||||
mustWriteSecretFile(t, filepath.Join(root, "secrets", name), "synthetic-value")
|
||||
mustWriteTestFile(t, pipeline, "config: {secrets_dir: secrets}\nunknown_field: invalid\n")
|
||||
var out, diagnostic bytes.Buffer
|
||||
result := Execute(context.Background(), []string{"config", "show", "--config", pipeline}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if result.Status == 0 || os.Getenv(name) != "synthetic-value" || !strings.Contains(diagnostic.String(), name) || strings.Contains(diagnostic.String(), "synthetic-value") {
|
||||
t.Fatalf("result=%#v diagnostic=%s", result, diagnostic.String())
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
out.Reset()
|
||||
diagnostic.Reset()
|
||||
result = Execute(ctx, []string{"config", "show", "--config", pipeline}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if result.Status != 1 || !errors.Is(result.Cause, context.Canceled) || strings.Contains(diagnostic.String(), name) {
|
||||
t.Fatalf("canceled result=%#v diagnostic=%s", result, diagnostic.String())
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
@@ -12,7 +11,6 @@ import (
|
||||
|
||||
type commandDependencies struct {
|
||||
reporter logging.Reporter
|
||||
diagnostics io.Writer
|
||||
pipelineSearch *configkit.Search
|
||||
preparePipelineSource func(configkit.Source) (*configkit.SecretsReport, configkit.Source, error)
|
||||
loadPipelineSource func(configkit.Source, config.PipelineLoadOptions) (*config.PipelineConfig, error)
|
||||
@@ -23,9 +21,6 @@ type commandDependencies struct {
|
||||
}
|
||||
|
||||
func normalizeCommandDependencies(deps commandDependencies) commandDependencies {
|
||||
if deps.diagnostics == nil {
|
||||
deps.diagnostics = io.Discard
|
||||
}
|
||||
if deps.preparePipelineSource == nil {
|
||||
deps.preparePipelineSource = func(source configkit.Source) (*configkit.SecretsReport, configkit.Source, error) {
|
||||
return configkit.LoadSecretsIntoEnvironmentFromConfig(source)
|
||||
|
||||
@@ -44,13 +44,5 @@ func consoleFields(fields []any) commandkit.Detail {
|
||||
|
||||
func withCommandReporting(deps commandDependencies, invocation commandkit.Invocation) commandDependencies {
|
||||
deps.reporter = nativeConsole{reporter: invocation.Reporter()}
|
||||
deps.diagnostics = bootstrapInfoWriter{reporter: invocation.Reporter()}
|
||||
return deps
|
||||
}
|
||||
|
||||
type bootstrapInfoWriter struct{ reporter *commandkit.Reporter }
|
||||
|
||||
func (w bootstrapInfoWriter) Write(body []byte) (int, error) {
|
||||
_ = w.reporter.Info("", func(destination io.Writer) error { _, err := destination.Write(body); return err })
|
||||
return len(body), nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func newApplication(deps commandDependencies, executor pipelineExecutor) (*comma
|
||||
return commandkit.New(
|
||||
commandkit.Identity{Name: "narratio", Version: buildinfo.Version},
|
||||
commandkit.Completion("narratio"),
|
||||
configurationCommand(deps),
|
||||
commandkit.WithInvocationFeature(configurationCommand(deps), bootstrapFeature()),
|
||||
executionCommand("run", "Execute a session pipeline.", false, deps, executor),
|
||||
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts.",
|
||||
artifactCatalogCommand(deps),
|
||||
|
||||
@@ -73,6 +73,7 @@ func (a *configurationAcquisition) Pipeline(ctx context.Context) (*config.Pipeli
|
||||
source := config.PipelineSource(a.selection.Config.Value, opts)
|
||||
report, prepared, err := a.deps.preparePipelineSource(source)
|
||||
a.bootstrapReport = report
|
||||
recordBootstrapEvidence(ctx, report, err)
|
||||
if err = errors.Join(err, ctx.Err()); err != nil {
|
||||
return nil, fmt.Errorf("prepare pipeline configuration: %w", err)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
@@ -12,25 +10,12 @@ import (
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
type configurationInspectionState struct {
|
||||
pendingDiagnostics *bytes.Buffer
|
||||
deps commandDependencies
|
||||
independent bool
|
||||
acquisitions map[configurationSelection]*configurationAcquisition
|
||||
}
|
||||
type configurationInspectionState struct{ deps commandDependencies }
|
||||
|
||||
func (s *configurationInspectionState) acquisition(selection ckconfig.Selection, input configurationSelection) *configurationAcquisition {
|
||||
input.Config, input.Overlay = selection.Config, selection.Overlay
|
||||
if s.independent || len(selection.Overrides) != 0 {
|
||||
acquired := newConfigurationAcquisition(input, s.deps)
|
||||
acquired.pipelineOverrides = append([]configkit.Override(nil), selection.Overrides...)
|
||||
return acquired
|
||||
}
|
||||
acquired := s.acquisitions[input]
|
||||
if acquired == nil {
|
||||
acquired = newConfigurationAcquisition(input, s.deps)
|
||||
s.acquisitions[input] = acquired
|
||||
}
|
||||
acquired := newConfigurationAcquisition(input, s.deps)
|
||||
acquired.pipelineOverrides = append([]configkit.Override(nil), selection.Overrides...)
|
||||
return acquired
|
||||
}
|
||||
|
||||
@@ -71,29 +56,9 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
|
||||
return cfg.Canonical.ConfigKitResult(), nil
|
||||
})
|
||||
scopes := []ckconfig.Scope[configurationSelection, *configurationInspectionState]{app, campaign, session, party}
|
||||
// Comparison sides share bootstrap services, but always load their own results.
|
||||
for index := range scopes {
|
||||
acquire := scopes[index].Acquire
|
||||
scopes[index].Acquire = func(ctx context.Context, request ckconfig.Request, input configurationSelection, state *configurationInspectionState) (commandkit.Acquired[ckconfig.View], error) {
|
||||
result, err := acquire(ctx, request, input, state)
|
||||
result.Diagnostics = append(result.Diagnostics, state.bootstrapDiagnostics()...)
|
||||
return result, err
|
||||
}
|
||||
compare := scopes[index].Compare
|
||||
scopes[index].Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[configurationSelection], state *configurationInspectionState) (commandkit.Acquired[[]configkit.Difference], error) {
|
||||
separate := *state
|
||||
separate.independent = true
|
||||
result, err := compare(ctx, request, &separate)
|
||||
result.Diagnostics = append(result.Diagnostics, state.bootstrapDiagnostics()...)
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
return ckconfig.Command(ckconfig.Suite[configurationSelection, *configurationInspectionState]{
|
||||
NewState: func() *configurationInspectionState {
|
||||
invocationDeps := normalizeCommandDependencies(deps)
|
||||
pending := &bytes.Buffer{}
|
||||
invocationDeps.diagnostics = pending
|
||||
return &configurationInspectionState{deps: invocationDeps, pendingDiagnostics: pending, acquisitions: make(map[configurationSelection]*configurationAcquisition)}
|
||||
return &configurationInspectionState{deps: normalizeCommandDependencies(deps)}
|
||||
},
|
||||
CheckInput: func(_ string, selection ckconfig.Selection, input configurationSelection) error {
|
||||
input.Config, input.Overlay = selection.Config, selection.Overlay
|
||||
@@ -109,12 +74,3 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
|
||||
Scopes: scopes,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *configurationInspectionState) bootstrapDiagnostics() []commandkit.Diagnostic {
|
||||
if s.pendingDiagnostics == nil || s.pendingDiagnostics.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
text := s.pendingDiagnostics.String()
|
||||
s.pendingDiagnostics.Reset()
|
||||
return []commandkit.Diagnostic{{Severity: commandkit.SeverityInfo, Write: func(w io.Writer) error { _, err := io.WriteString(w, text); return err }}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user