04 / PACKAGE TESTED TOOLS · LESSON 14

Write useful logs without secret fields

Export an allowlisted audit view instead of copying raw command output.

22 min · learn at your pace

More output is not always better evidence

A raw command response can contain tokens, credentials, personal information or terminal control characters. Copying that response into every audit log spreads sensitive material and makes a small operational question harder to answer. Decide which fields the report actually needs.

This lesson exports only a timestamp, host, supported operation and recognised status. Secret and detail fields are deliberately absent from the output schema. That is more reliable than searching free text for words such as password: an unfamiliar credential format would evade a blacklist.

Keep schema and validation separate

The input fixture already guarantees valid timestamps and host identifiers, with exactly six TSV fields and no embedded tabs or newlines. You still validate the operation and status against the audit contract. Unsupported values are omitted rather than copied into an apparently trusted status column.

An allowlisted schema is one layer of a logging policy, not a guarantee that arbitrary identifiers are harmless. Real systems should validate those fields at collection, control who can read logs and define retention. Here the test includes credential-like data in omitted fields and verifies the exact output bytes. The lesson focuses on what a downstream report is allowed to contain, without collecting or transmitting any learner secrets or host evidence.

Try an example.sh
printf 'Safe audit fields: time, host, operation, status\n'
YOUR TURN

Put it into practice.

events.tsv has time<TAB>host<TAB>operation<TAB>status<TAB>secret<TAB>detail. Rows have exactly six fields; time/host are validated, and fields contain no tabs/newlines. Replace audit.tsv with only time<TAB>host<TAB>operation<TAB>status, preserving accepted row order. Accept operations scan/inventory/stop and statuses ok/failed/unknown; omit all other rows. Never copy secret or detail, including credential-like text or terminal escapes. Repeated runs replace old output. Leave events.tsv unchanged.

See an explained solution
One way to solve it.sh
printf 'time\thost\toperation\tstatus\n' > audit.tsv
awk -F '\t' 'NR>1 {
 if($3!="scan" && $3!="inventory" && $3!="stop")next
 if($4!="ok" && $4!="failed" && $4!="unknown")next
 printf "%s\t%s\t%s\t%s\n",$1,$2,$3,$4
}' events.tsv >> audit.tsv

The export contract allows only the fields needed to explain an operation. Exact field selection avoids accidental inclusion of raw details, and explicit value lists prevent unsupported operations from appearing as trusted audit records.