Skip to content

Commit

Permalink
Update docs, single log and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matoval committed Sep 26, 2024
1 parent 2d9ebbf commit 557f84e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
10 changes: 9 additions & 1 deletion docs/source/user_guide/configuration_options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ Log level
- string

Add payload debuging using `RECEPTOR_PAYLOAD_DEBUG=int` envorment variable and using log level debug.
`RECEPTOR_PAYLOAD_DEBUG` options [0,1,2,3]
`RECEPTOR_PAYLOAD_DEBUG` options:
- 0: No payload debug log
- 1: Log connection type
- 2: Log connection type and work unit id
- 3: Log connection type, work unit id and payload

**Warning: Payload Debugging May Expose Sensitive Data**

Please be aware that using payload debugging can potentially reveal sensitive information. This includes, but is not limited to, personal data, authentication tokens, and system configurations. Ensure that you only use debugging tools in a secure environment and avoid sharing debug output with unauthorized users. Always follow your organization's data protection policies when handling sensitive information. Proceed with caution!

.. code-block:: yaml
Expand Down
18 changes: 10 additions & 8 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,29 @@ func (rl *ReceptorLogger) Debug(format string, v ...interface{}) {

// Debug payload data.
func (rl *ReceptorLogger) DebugPayload(payloadDebug int, payload string, workUnitID string, connectionType string) {
var payloadMessage string
var workunitIDMessage string
var connectionTypeMessage string
switch payloadDebug {
case 3:
if workUnitID != "" {
rl.Debug("Work unit %v stdin: %v", workUnitID, payload)
} else {
rl.Debug("Response reading from conn: %v", payload)
}
payloadMessage = fmt.Sprintf(" with a payload of: %s", payload)

fallthrough
case 2:
if payloadDebug == 2 && workUnitID != "" {
rl.Debug("Work unit %v received command\n", workUnitID)
if workUnitID != "" {
workunitIDMessage = fmt.Sprintf(" with work unit %s", workUnitID)
} else {
workunitIDMessage = ", work unit not created yet"
}

fallthrough
case 1:
if connectionType != "" {
rl.Debug("Reading from %v", connectionType)
connectionTypeMessage = fmt.Sprintf("Reading from %s", connectionType)
}
default:
}
rl.Debug(fmt.Sprintf("PACKET TRACING ENABLED: %s%s%s", connectionTypeMessage, workunitIDMessage, payloadMessage))

Check failure on line 175 in pkg/logger/logger.go

View workflow job for this annotation

GitHub Actions / lint-receptor

printf: non-constant format string in call to (*github.com/ansible/receptor/pkg/logger.ReceptorLogger).Debug (govet)
}

// SanitizedDebug contains extra information helpful to developers.
Expand Down
22 changes: 11 additions & 11 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ func TestDebugPayload(t *testing.T) {
payload string
workUnitID string
connectionType string
expectedLogs []string
expectedLog string
}{
{name: "debugPayload no log", debugPayload: 0, payload: "", workUnitID: "", connectionType: "", expectedLogs: []string{}},
{name: "debugPayload log level 1", debugPayload: 1, payload: "", workUnitID: "", connectionType: connectionType, expectedLogs: []string{fmt.Sprintf("Reading from %v", connectionType)}},
{name: "debugPayload log level 2 with workUnitID", debugPayload: 2, payload: "", workUnitID: workUnitID, connectionType: connectionType, expectedLogs: []string{fmt.Sprintf("Reading from %v", connectionType), fmt.Sprintf("Work unit %v received command", workUnitID)}},
{name: "debugPayload log level 2 without workUnitID", debugPayload: 2, payload: "", workUnitID: "", connectionType: connectionType, expectedLogs: []string{fmt.Sprintf("Reading from %v", connectionType)}},
{name: "debugPayload log level 3 with workUnitID", debugPayload: 3, payload: payload, workUnitID: workUnitID, connectionType: connectionType, expectedLogs: []string{fmt.Sprintf("Reading from %v", connectionType), fmt.Sprintf("Work unit %v stdin: %v", workUnitID, payload)}},
{name: "debugPayload log level 3 without workUnitID", debugPayload: 3, payload: payload, workUnitID: "", connectionType: connectionType, expectedLogs: []string{fmt.Sprintf("Reading from %v", connectionType), fmt.Sprintf("Response reading from conn: %v", payload)}},
{name: "debugPayload no log", debugPayload: 0, payload: "", workUnitID: "", connectionType: "", expectedLog: ""},
{name: "debugPayload log level 1", debugPayload: 1, payload: "", workUnitID: "", connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v", connectionType)},
{name: "debugPayload log level 2 with workUnitID", debugPayload: 2, payload: "", workUnitID: workUnitID, connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v with work unit %v", connectionType, workUnitID)},
{name: "debugPayload log level 2 without workUnitID", debugPayload: 2, payload: "", workUnitID: "", connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v", connectionType)},
{name: "debugPayload log level 3 with workUnitID", debugPayload: 3, payload: payload, workUnitID: workUnitID, connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v with work unit %v with a payload of: %v", connectionType, workUnitID, payload)},
{name: "debugPayload log level 3 without workUnitID", debugPayload: 3, payload: payload, workUnitID: "", connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v, work unit not created yet with a payload of: %v", connectionType, payload)},
{name: "debugPayload log level 3 without workUnitID and payload is new line", debugPayload: 3, payload: "\n", workUnitID: "", connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v, work unit not created yet with a payload of: %v", connectionType, "\n")},
{name: "debugPayload log level 3 without workUnitID or payload", debugPayload: 3, payload: "", workUnitID: "", connectionType: connectionType, expectedLog: fmt.Sprintf("PACKET TRACING ENABLED: Reading from %v, work unit not created yet with a payload of: %v", connectionType, "")},
}

for _, testCase := range debugPayloadTestCases {
Expand All @@ -109,10 +111,8 @@ func TestDebugPayload(t *testing.T) {
if err != nil {
t.Error("error reading test-output file")
}
for _, expectedlog := range testCase.expectedLogs {
if !bytes.Contains(testOutput, []byte(expectedlog)) {
t.Errorf("failed to log correctly, expected: %v got %v", expectedlog, string(testOutput))
}
if !bytes.Contains(testOutput, []byte(testCase.expectedLog)) {
t.Errorf("failed to log correctly, expected: %v got %v", testCase.expectedLog, string(testOutput))
}
if err := os.Truncate(logFilePath, 0); err != nil {
t.Errorf("failed to truncate: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/workceptor/stdio_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (sr *STDinReader) Read(p []byte) (n int, err error) {
}()
if isNotEmpty {
payload := string(p)
MainInstance.nc.GetLogger().DebugPayload(payloadDebug, payload, sr.workUnit, "")
MainInstance.nc.GetLogger().DebugPayload(payloadDebug, payload, sr.workUnit, "kube api")
}
}
n, err = sr.reader.Read(p)
Expand Down

0 comments on commit 557f84e

Please sign in to comment.