log.collect: no artifact is written whose redaction is not proven

Credential discovery read the declared log files only while the
collection's time budget lasted: a slow run (as under the race detector)
skipped the later files, and an earlier file was written with a
credential only a skipped one named. The declared log files are now read
whole for discovery whatever time is left (each is bounded at 4 MiB, at
most 32), after the declared commands and before the status reads. A
declared source the budget still cuts (a -unit's commands) makes the
bundle hold only its manifest, which says the redaction is not proven
and names the cut sources; the command reports it and exits 3. Each log
file is discovered once, not twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 04:23:52 -08:00
1 parent 2340d9a523
commit f009b9269a
3 files changed
+159 -13

No files matched your search

+61 -9
View File
@@ -34,8 +34,15 @@ type logBundle struct {
Since time.Time `json:"since"`
Until time.Time `json:"until"`
Artifacts []bundleArtifact `json:"artifacts"`
// Redaction is "not_proven" when the collection's time budget ended
// before every declared source (-log-file, -unit, -pid) was read for
// credential discovery; the bundle then holds no artifact but this
// manifest. Skipped names them.
Redaction string `json:"redaction,omitempty"`
Skipped []string `json:"skipped,omitempty"`
bytes int
redactor *bundleRedactor
ctx context.Context // the collection's time budget
}
func (b *logBundle) discover(data []byte) {
@@ -48,10 +55,32 @@ func (b *logBundle) discover(data []byte) {
}
b.redactor.discover(data)
}
// add adds a source the bundle collects on its own (the master's and the
// holders' status): one the time budget cut adds no bytes, and none of its
// own reach the bundle.
func (b *logBundle) add(name string, data []byte, err error) {
b.addSource(name, data, err, true)
}
// addDeclared adds a declared source the time budget bounds (a -unit's
// command): one that fails once the budget has ended was cut before its
// credentials could be discovered.
func (b *logBundle) addDeclared(name string, data []byte, err error) {
if err != nil && b.ctx != nil && b.ctx.Err() != nil {
b.Skipped = append(b.Skipped, name)
}
b.addSource(name, data, err, true)
}
// addSource adds a source; discover says its bytes are still to be
// discovered (a log file's were, whole, as it was read).
func (b *logBundle) addSource(name string, data []byte, err error, discover bool) {
// Even a source omitted by the aggregate artifact budget can identify a
// credential repeated in a retained artifact. Keep only its bounded secrets.
b.discover(data)
if discover {
b.discover(data)
}
if err != nil {
b.discover([]byte(err.Error()))
}
@@ -81,6 +110,16 @@ func (b *logBundle) write(dir string) error {
if r.overflow {
return errors.New("redaction discovery bound exceeded")
}
// A source cut by the time budget may hold a credential repeated in
// another artifact: no artifact is written whose redaction is not proven.
if len(b.Skipped) > 0 {
b.Redaction = "not_proven"
for i := range b.Artifacts {
if a := &b.Artifacts[i]; a.data != nil {
a.data, a.State, a.Error = nil, "withheld", "redaction not proven: the time budget ended before every source was read"
}
}
}
outputBytes := 0
for i := range b.Artifacts {
a := &b.Artifacts[i]
@@ -142,25 +181,38 @@ func (s inspectionShell) collectLogs(args []string, stdout, stderr io.Writer) in
b := logBundle{Version: 1, Since: now.Add(-*since), Until: now}
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
b.ctx = ctx
if len(files) == 0 && len(units) == 0 {
discovered, err := defaultBundleUnits(ctx)
units = discovered
b.addJSON("unit-discovery.json", map[string]any{"units": discovered}, err)
}
for i, p := range files {
if ctx.Err() != nil {
b.add(fmt.Sprintf("file-%03d.log", i+1), nil, ctx.Err())
continue
}
data, err := readBundleLog(p, b.Since, b.Until, b.discover)
b.add(fmt.Sprintf("file-%03d.log", i+1), data, err)
}
// The declared sources first: the commands the time budget bounds (-unit,
// -pid), then the log files, each read whole for discovery whatever time
// is left (each is bounded at 4 MiB, at most 32 sources), so the budget
// never cuts their discovery. The status reads, not declared, come last.
b.collectLocal(ctx, units, pids)
for i, p := range files {
data, err := readBundleLog(p, b.Since, b.Until, b.discover)
b.addSource(fmt.Sprintf("file-%03d.log", i+1), data, err, false)
}
b.collectStatus(ctx, s)
if err := b.write(dir); err != nil {
fmt.Fprintln(stderr, "cannot write bundle")
return 1
}
if b.Redaction == "not_proven" {
if *format == "json" {
_ = json.NewEncoder(stdout).Encode(map[string]any{"bundle_written": true, "complete": false, "redaction": "not_proven", "skipped": b.Skipped, "manifest": "manifest.json"})
} else {
named := b.Skipped
if len(named) > 8 {
named = append(append([]string(nil), named[:8]...), fmt.Sprintf("and %d more", len(b.Skipped)-8))
}
fmt.Fprintf(stdout, "bundle written; incomplete: redaction not proven (not read in time: %s); only manifest.json was written\n", strings.Join(named, ", "))
}
return 3
}
complete := true
for _, a := range b.Artifacts {
if a.State != "collected" {
@@ -77,17 +77,17 @@ func defaultBundleUnits(ctx context.Context) (bundleStrings, error) {
func (b *logBundle) collectLocal(ctx context.Context, units, pids []string) {
if len(units) == 0 && len(pids) == 0 {
b.add("startup-flags.json", nil, errors.New("no service unit or local process selected"))
b.addSource("startup-flags.json", nil, errors.New("no service unit or local process selected"), true)
}
for i, u := range units {
flags, err := bundleCommand(ctx, "systemctl", "show", "--property=ExecStart", "--value", "--", u)
b.add(fmt.Sprintf("unit-%03d-flags.txt", i+1), flags, err)
b.addDeclared(fmt.Sprintf("unit-%03d-flags.txt", i+1), flags, err)
raw, err := bundleCommand(ctx, "journalctl", "--no-pager", "--utc", "--output=short-iso-precise", "--since="+b.Since.UTC().Format("2006-01-02 15:04:05.000000 UTC"), "--until="+b.Until.UTC().Format("2006-01-02 15:04:05.000000 UTC"), "--unit="+u)
b.discover(raw)
if err == nil {
raw, err = recentBundleLog(raw, b.Since, b.Until)
}
b.add(fmt.Sprintf("unit-%03d.log", i+1), raw, err)
b.addDeclared(fmt.Sprintf("unit-%03d.log", i+1), raw, err)
}
for i, pid := range pids {
var raw []byte
@@ -116,6 +116,6 @@ func (b *logBundle) collectLocal(ctx context.Context, units, pids []string) {
if err != nil {
err = errors.New("process startup arguments unavailable")
}
b.add(fmt.Sprintf("process-%03d-flags.json", i+1), data, err)
b.addSource(fmt.Sprintf("process-%03d-flags.json", i+1), data, err, true)
}
}
@@ -531,3 +531,97 @@ func TestLogCollectUsesOneInventoryForChecksAndVolumeArtifacts(t *testing.T) {
}
}
}
// A time budget spent before the declared log files are read never cuts
// their credential discovery: a credential one file names (a flag) is
// redacted where another file repeats it.
func TestLogCollectSpentBudgetStillDiscoversEveryLogFile(t *testing.T) {
addr, closeMaster := startInspectionMaster(t)
defer closeMaster()
dir := t.TempDir()
bundle := filepath.Join(dir, "bundle")
stamp := time.Now().UTC().Format("2006/01/02 15:04:05.000000")
const secret = "spent-budget-planted-55120"
opaque := filepath.Join(dir, "opaque.log")
flags := filepath.Join(dir, "flags.log")
if err := os.WriteFile(opaque, []byte(stamp+" opaque "+secret+"\n"), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(flags, []byte(stamp+" --password="+secret+"\n"), 0600); err != nil {
t.Fatal(err)
}
command := fmt.Sprintf("log.collect -out '%s' -log-file '%s' -log-file '%s'", filepath.ToSlash(bundle), filepath.ToSlash(opaque), filepath.ToSlash(flags))
var out, errout bytes.Buffer
code := run([]string{"shell", "-master", addr, "-timeout", "1ns", "-c", command}, &out, &errout)
if code != 3 {
t.Fatalf("exit %d %s %s", code, &out, &errout)
}
raw, err := os.ReadFile(filepath.Join(bundle, "file-001.log"))
if err != nil || !bytes.Contains(raw, []byte("opaque")) {
t.Fatalf("the first log file was not collected: %v %q", err, raw)
}
if n := bundleOccurrences(t, bundle, secret); n != 0 {
t.Fatalf("%d unredacted occurrences of the credential in the bundle", n)
}
}
// A declared source the time budget cut (a -unit's commands) may hold a
// credential another artifact repeats: the bundle then holds only its
// manifest, says the redaction is not proven, names the cut sources, and
// no byte of the bundle carries the credential.
func TestLogCollectCutDeclaredSourceWithholdsEveryArtifact(t *testing.T) {
addr, closeMaster := startInspectionMaster(t)
defer closeMaster()
dir := t.TempDir()
bundle := filepath.Join(dir, "bundle")
stamp := time.Now().UTC().Format("2006/01/02 15:04:05.000000")
const secret = "cut-source-planted-77031"
file := filepath.Join(dir, "daemon.log")
if err := os.WriteFile(file, []byte(stamp+" opaque "+secret+"\n"), 0600); err != nil {
t.Fatal(err)
}
command := fmt.Sprintf("log.collect -out '%s' -unit 'sw-blockvolume@v1.service' -log-file '%s'", filepath.ToSlash(bundle), filepath.ToSlash(file))
var out, errout bytes.Buffer
code := run([]string{"shell", "-master", addr, "-timeout", "1ns", "-c", command}, &out, &errout)
if code != 3 || !strings.Contains(out.String(), "incomplete: redaction not proven") || !strings.Contains(out.String(), "unit-001.log") {
t.Fatalf("exit %d %q %s", code, &out, &errout)
}
entries, err := os.ReadDir(bundle)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
if e.Name() != "manifest.json" {
t.Errorf("artifact %s written although redaction is not proven", e.Name())
}
}
var m struct {
Redaction string
Skipped []string
}
raw, err := os.ReadFile(filepath.Join(bundle, "manifest.json"))
if err != nil || json.Unmarshal(raw, &m) != nil || m.Redaction != "not_proven" || len(m.Skipped) == 0 {
t.Fatalf("manifest %v %s", err, raw)
}
if n := bundleOccurrences(t, bundle, secret); n != 0 {
t.Fatalf("%d unredacted occurrences of the credential in the bundle", n)
}
}
// bundleOccurrences counts secret over every byte of every file in the bundle.
func bundleOccurrences(t *testing.T, bundle, secret string) int {
t.Helper()
n := 0
err := filepath.WalkDir(bundle, func(p string, d os.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
raw, err := os.ReadFile(p)
n += bytes.Count(raw, []byte(secret))
return err
})
if err != nil {
t.Fatal(err)
}
return n
}