|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cyberark/conjur-inspect/pkg/test" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestContainerCommandHistoryDescribe(t *testing.T) { |
| 15 | + provider := &test.ContainerProvider{} |
| 16 | + cch := &ContainerCommandHistory{Provider: provider} |
| 17 | + assert.Equal(t, "Test Container Provider command history", cch.Describe()) |
| 18 | +} |
| 19 | + |
| 20 | +func TestContainerCommandHistoryRunSuccessful(t *testing.T) { |
| 21 | + // Create a mock container provider with bash history |
| 22 | + bashHistory := strings.Join([]string{ |
| 23 | + "ls -la", |
| 24 | + "cd /tmp", |
| 25 | + "echo 'hello world'", |
| 26 | + }, "\n") |
| 27 | + |
| 28 | + provider := &test.ContainerProvider{ |
| 29 | + ExecResponses: map[string]test.ExecResponse{ |
| 30 | + "sh -c tail -n 100 /root/.bash_history 2>/dev/null || true": { |
| 31 | + Stdout: strings.NewReader(bashHistory), |
| 32 | + Stderr: strings.NewReader(""), |
| 33 | + Error: nil, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + cch := &ContainerCommandHistory{Provider: provider} |
| 39 | + runContext := test.NewRunContext("container123") |
| 40 | + results := cch.Run(&runContext) |
| 41 | + |
| 42 | + // Should return empty results on success |
| 43 | + assert.Empty(t, results) |
| 44 | + |
| 45 | + // Verify the file was saved to the output store |
| 46 | + items, err := runContext.OutputStore.Items() |
| 47 | + require.NoError(t, err) |
| 48 | + require.Len(t, items, 1) |
| 49 | + info, err := items[0].Info() |
| 50 | + require.NoError(t, err) |
| 51 | + // Provider name is "Test Container Provider" and gets ToLower() -> "test container provider" |
| 52 | + assert.Equal(t, "test container provider-command-history.txt", info.Name()) |
| 53 | +} |
| 54 | + |
| 55 | +func TestContainerCommandHistorySanitization(t *testing.T) { |
| 56 | + // Create history with sensitive data that should be redacted |
| 57 | + bashHistory := strings.Join([]string{ |
| 58 | + "mysql -u root -pMyPassword123", |
| 59 | + "api_key=sk_live_abc123xyz", |
| 60 | + "token=secret_token_value", |
| 61 | + "echo 'normal command'", |
| 62 | + }, "\n") |
| 63 | + |
| 64 | + provider := &test.ContainerProvider{ |
| 65 | + ExecResponses: map[string]test.ExecResponse{ |
| 66 | + "sh -c tail -n 100 /root/.bash_history 2>/dev/null || true": { |
| 67 | + Stdout: strings.NewReader(bashHistory), |
| 68 | + Stderr: strings.NewReader(""), |
| 69 | + Error: nil, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + cch := &ContainerCommandHistory{Provider: provider} |
| 75 | + runContext := test.NewRunContext("container123") |
| 76 | + results := cch.Run(&runContext) |
| 77 | + |
| 78 | + assert.Empty(t, results) |
| 79 | + |
| 80 | + // Verify sanitization occurred |
| 81 | + items, err := runContext.OutputStore.Items() |
| 82 | + require.NoError(t, err) |
| 83 | + require.Len(t, items, 1) |
| 84 | + |
| 85 | + outputStoreItemReader, cleanup, err := items[0].Open() |
| 86 | + defer cleanup() |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + savedContent, err := io.ReadAll(outputStoreItemReader) |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + // Verify sensitive data was redacted |
| 93 | + assert.NotContains(t, string(savedContent), "sk_live_abc123xyz") |
| 94 | + assert.NotContains(t, string(savedContent), "secret_token_value") |
| 95 | + // Normal command should still be present |
| 96 | + assert.Contains(t, string(savedContent), "echo 'normal command'") |
| 97 | + // The api_key and token patterns should have been redacted with [REDACTED] |
| 98 | + assert.Contains(t, string(savedContent), "[REDACTED]") |
| 99 | +} |
| 100 | + |
| 101 | +func TestContainerCommandHistoryEmptyContainerID(t *testing.T) { |
| 102 | + provider := &test.ContainerProvider{} |
| 103 | + cch := &ContainerCommandHistory{Provider: provider} |
| 104 | + runContext := test.NewRunContext("") |
| 105 | + results := cch.Run(&runContext) |
| 106 | + |
| 107 | + // Should return empty results when container ID is empty |
| 108 | + assert.Empty(t, results) |
| 109 | + |
| 110 | + // Verify nothing was saved |
| 111 | + items, err := runContext.OutputStore.Items() |
| 112 | + require.NoError(t, err) |
| 113 | + assert.Len(t, items, 0) |
| 114 | +} |
| 115 | + |
| 116 | +func TestContainerCommandHistoryExecError(t *testing.T) { |
| 117 | + provider := &test.ContainerProvider{ |
| 118 | + ExecResponses: map[string]test.ExecResponse{ |
| 119 | + "sh -c tail -n 100 /root/.bash_history 2>/dev/null || true": { |
| 120 | + Stdout: nil, |
| 121 | + Stderr: nil, |
| 122 | + Error: fmt.Errorf("file not found"), |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + cch := &ContainerCommandHistory{Provider: provider} |
| 128 | + runContext := test.NewRunContext("container123") |
| 129 | + results := cch.Run(&runContext) |
| 130 | + |
| 131 | + // Should return error result |
| 132 | + require.Len(t, results, 1) |
| 133 | + assert.Equal(t, "Test Container Provider command history", results[0].Title) |
| 134 | + assert.Contains(t, results[0].Message, "failed to retrieve command history from container") |
| 135 | +} |
| 136 | + |
| 137 | +func TestContainerCommandHistoryNoExecResponse(t *testing.T) { |
| 138 | + // Provider with empty ExecResponses - should error on unknown command |
| 139 | + provider := &test.ContainerProvider{ |
| 140 | + ExecResponses: map[string]test.ExecResponse{}, |
| 141 | + } |
| 142 | + |
| 143 | + cch := &ContainerCommandHistory{Provider: provider} |
| 144 | + runContext := test.NewRunContext("container123") |
| 145 | + results := cch.Run(&runContext) |
| 146 | + |
| 147 | + // Should return error result |
| 148 | + require.Len(t, results, 1) |
| 149 | + assert.Equal(t, "Test Container Provider command history", results[0].Title) |
| 150 | + assert.Contains(t, results[0].Message, "failed to retrieve command history from container") |
| 151 | +} |
| 152 | + |
| 153 | +func TestContainerCommandHistoryEmptyHistory(t *testing.T) { |
| 154 | + // Test with empty history file |
| 155 | + provider := &test.ContainerProvider{ |
| 156 | + ExecResponses: map[string]test.ExecResponse{ |
| 157 | + "sh -c tail -n 100 /root/.bash_history 2>/dev/null || true": { |
| 158 | + Stdout: strings.NewReader(""), |
| 159 | + Stderr: strings.NewReader(""), |
| 160 | + Error: nil, |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | + |
| 165 | + cch := &ContainerCommandHistory{Provider: provider} |
| 166 | + runContext := test.NewRunContext("container123") |
| 167 | + results := cch.Run(&runContext) |
| 168 | + |
| 169 | + // Should return empty results (empty history is valid) |
| 170 | + assert.Empty(t, results) |
| 171 | + |
| 172 | + // Verify empty file was saved |
| 173 | + items, err := runContext.OutputStore.Items() |
| 174 | + require.NoError(t, err) |
| 175 | + require.Len(t, items, 1) |
| 176 | +} |
| 177 | + |
| 178 | +func TestContainerCommandHistoryStderr(t *testing.T) { |
| 179 | + // Test that stderr is logged but doesn't cause failure |
| 180 | + bashHistory := strings.Join([]string{ |
| 181 | + "ls -la", |
| 182 | + "pwd", |
| 183 | + }, "\n") |
| 184 | + |
| 185 | + provider := &test.ContainerProvider{ |
| 186 | + ExecResponses: map[string]test.ExecResponse{ |
| 187 | + "sh -c tail -n 100 /root/.bash_history 2>/dev/null || true": { |
| 188 | + Stdout: strings.NewReader(bashHistory), |
| 189 | + Stderr: strings.NewReader("some warning message"), |
| 190 | + Error: nil, |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + cch := &ContainerCommandHistory{Provider: provider} |
| 196 | + runContext := test.NewRunContext("container123") |
| 197 | + results := cch.Run(&runContext) |
| 198 | + |
| 199 | + // Should still succeed despite stderr |
| 200 | + assert.Empty(t, results) |
| 201 | + |
| 202 | + // Verify the file was saved |
| 203 | + items, err := runContext.OutputStore.Items() |
| 204 | + require.NoError(t, err) |
| 205 | + require.Len(t, items, 1) |
| 206 | +} |
| 207 | + |
| 208 | +func TestContainerCommandHistoryWhitespaceContainerID(t *testing.T) { |
| 209 | + provider := &test.ContainerProvider{} |
| 210 | + cch := &ContainerCommandHistory{Provider: provider} |
| 211 | + runContext := test.NewRunContext(" ") |
| 212 | + results := cch.Run(&runContext) |
| 213 | + |
| 214 | + // Should return empty results when container ID is only whitespace |
| 215 | + assert.Empty(t, results) |
| 216 | + |
| 217 | + // Verify nothing was saved |
| 218 | + items, err := runContext.OutputStore.Items() |
| 219 | + require.NoError(t, err) |
| 220 | + assert.Len(t, items, 0) |
| 221 | +} |
0 commit comments