-
Notifications
You must be signed in to change notification settings - Fork 2
sqs: add the admin purge/peek audit line and counters (§3.6) #1228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bootjp
wants to merge
3
commits into
main
Choose a base branch
from
design/admin-purge-queue-audit-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "log/slog" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // captureAdminAudit points the server's audit logger at a buffer and returns a | ||
| // reader for the admin.sqs.purge_queue records it emits. | ||
| func captureAdminAudit(t *testing.T, server *SQSServer) func() []map[string]any { | ||
| t.Helper() | ||
|
|
||
| var buf bytes.Buffer | ||
| // A distinguishing attribute, so a record that went out through | ||
| // slog.Default() instead of this logger is detectable rather than just | ||
| // absent. | ||
| server.adminAuditLogger = slog.New(slog.NewJSONHandler(&buf, nil)). | ||
| With(slog.String("component", "admin")) | ||
|
|
||
| return func() []map[string]any { | ||
| var records []map[string]any | ||
| for _, line := range strings.Split(strings.TrimSpace(buf.String()), "\n") { | ||
| if strings.TrimSpace(line) == "" { | ||
| continue | ||
| } | ||
| var rec map[string]any | ||
| require.NoError(t, json.Unmarshal([]byte(line), &rec)) | ||
| if rec["msg"] == "admin.sqs.purge_queue" { | ||
| records = append(records, rec) | ||
| } | ||
| } | ||
| return records | ||
| } | ||
| } | ||
|
|
||
| // TestAdminPurgeQueueAuditsThroughTheConfiguredLogger pins that the §3.6 audit | ||
| // record goes to the configured audit destination. | ||
| // | ||
| // It used to call slog.InfoContext, i.e. the process-wide slog.Default(), so a | ||
| // server built with a dedicated audit sink or the production component="admin" | ||
| // child logger never saw these records and they lost that logger's attributes | ||
| // — unlike every other admin audit entry. | ||
| func TestAdminPurgeQueueAuditsThroughTheConfiguredLogger(t *testing.T) { | ||
| t.Parallel() | ||
| nodes, _, _ := createNode(t, 1) | ||
| defer shutdown(nodes) | ||
| node := sqsLeaderNode(t, nodes) | ||
|
|
||
| _ = createSQSQueueForTest(t, node, "audited") | ||
| records := captureAdminAudit(t, node.sqsServer) | ||
|
|
||
| _, err := node.sqsServer.AdminPurgeQueue(context.Background(), fullAdminPrincipal, "audited") | ||
| require.NoError(t, err) | ||
|
|
||
| got := records() | ||
| require.Len(t, got, 1, "exactly one audit record for one purge") | ||
| require.Equal(t, "admin", got[0]["component"], | ||
| "the record must carry the configured logger's attributes") | ||
| require.Equal(t, "audited", got[0]["queue"]) | ||
| require.Equal(t, adminOutcomeOK, got[0]["outcome"]) | ||
| require.Contains(t, got[0], "generation_before") | ||
| require.Contains(t, got[0], "generation_after") | ||
| } | ||
|
|
||
| // TestAdminPurgeQueueAuditsAPurgeInProgressRefusal is the load-bearing case. | ||
| // | ||
| // A second purge inside the 60-second window returned before the only audit | ||
| // call, so the documented signal for repeated rate-limited attempts produced | ||
| // nothing. The generic HTTP audit middleware records status and path, which | ||
| // cannot say WHY the request was refused — the one thing this record exists to | ||
| // answer. | ||
| func TestAdminPurgeQueueAuditsAPurgeInProgressRefusal(t *testing.T) { | ||
| t.Parallel() | ||
| nodes, _, _ := createNode(t, 1) | ||
| defer shutdown(nodes) | ||
| node := sqsLeaderNode(t, nodes) | ||
|
|
||
| _ = createSQSQueueForTest(t, node, "repeat-purged") | ||
| records := captureAdminAudit(t, node.sqsServer) | ||
| ctx := context.Background() | ||
|
|
||
| _, err := node.sqsServer.AdminPurgeQueue(ctx, fullAdminPrincipal, "repeat-purged") | ||
| require.NoError(t, err) | ||
| _, err = node.sqsServer.AdminPurgeQueue(ctx, fullAdminPrincipal, "repeat-purged") | ||
| require.ErrorIs(t, err, ErrAdminSQSPurgeInProgress) | ||
|
|
||
| got := records() | ||
| require.Len(t, got, 2, "the refusal must be audited, not only the success") | ||
| require.Equal(t, adminOutcomeOK, got[0]["outcome"]) | ||
| require.Equal(t, adminOutcomePurgeInProgress, got[1]["outcome"]) | ||
| require.Equal(t, "repeat-purged", got[1]["queue"]) | ||
|
|
||
| // No invented generation pair: the refusal committed nothing, and | ||
| // recording a state that never existed would corrupt the audit trail. | ||
| require.NotContains(t, got[1], "generation_before") | ||
| require.NotContains(t, got[1], "generation_after") | ||
| } | ||
|
|
||
| // A forbidden principal is refused before any storage work, and must still be | ||
| // audited with its own outcome. | ||
| func TestAdminPurgeQueueAuditsAForbiddenRefusal(t *testing.T) { | ||
| t.Parallel() | ||
| nodes, _, _ := createNode(t, 1) | ||
| defer shutdown(nodes) | ||
| node := sqsLeaderNode(t, nodes) | ||
|
|
||
| _ = createSQSQueueForTest(t, node, "guarded") | ||
| records := captureAdminAudit(t, node.sqsServer) | ||
|
|
||
| _, err := node.sqsServer.AdminPurgeQueue(context.Background(), readOnlyAdminPrincipal, "guarded") | ||
| require.ErrorIs(t, err, ErrAdminForbidden) | ||
|
|
||
| got := records() | ||
| require.Len(t, got, 1) | ||
| require.Equal(t, adminOutcomeForbidden, got[0]["outcome"]) | ||
| require.NotContains(t, got[0], "generation_before") | ||
| } | ||
|
|
||
| // A server built without the option must still audit, through the default. | ||
| func TestAdminPurgeQueueFallsBackToTheDefaultLogger(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var server *SQSServer | ||
| require.NotNil(t, server.adminLogger(), "a nil server must not panic") | ||
| require.NotNil(t, (&SQSServer{}).adminLogger(), | ||
| "a server with no configured audit logger must still have somewhere to audit") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a second purge arrives within the 60-second window, this branch returns before the only
admin.sqs.purge_queuelog call, so no operation-specific audit record withoutcome=purge_in_progressis emitted. The generic HTTP audit middleware records only the status and path, not this outcome, which defeats the documented audit signal for repeated rate-limited purge attempts. Emit the failure audit event in this branch without inventing generation values.Useful? React with 👍 / 👎.