Skip to content

feat(logging): add query parameter logging for /api/v2 requests - BED-8563 - #2895

Merged
cami-specter merged 8 commits into
mainfrom
wip-BED-8563-add-query-params-logging
Aug 12, 2026
Merged

feat(logging): add query parameter logging for /api/v2 requests - BED-8563#2895
cami-specter merged 8 commits into
mainfrom
wip-BED-8563-add-query-params-logging

Conversation

@cami-specter

@cami-specter cami-specter commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Description

Add query parameter logging for /api/v2 requests for easier Elastic/Kibana lookups. This logs the raw string and makes it easier to filter and search in Elastic without parsing the full request_uri field.

  • adds query_parameters slog attribute in LoggingMiddleware
  • only logs for /api/v2 endpoints with non-empty query strings
  • ignores SSO callback endpoint and a few others (see link for full audit). Even if params are already being logged via request_uri, it is probably better not to add them to the new query_parameters field.
  • adds table-driven tests

Note:
After some deliberation, decided not to add nested logging due to potential Elastic index mapping problems. (If we were to add a large list of parameters (in the hundreds) to our Elastic daily indexes mapping, that could cause issues – indexes are already > 1000).

Motivation and Context

Resolves BED-8563.

There's a general benefit to having query_parameters as a dedicated field since it makes it easier to filter and aggregate on query param usage across most endpoints.

Specifically, we were looking at non-default limit and skip parameters queries for the api/v2/search endpoint

How Has This Been Tested?

  • Added unit tests (table-driven) covering non-/api/v2 paths, empty query strings, ignoring a specific endpoint, etc.
  • Manually tested output with EnableAPILogging enabled, hit various /api/v2 endpoints, verified logs

Screenshots (optional):

log output showing query_parameters field (/api/v2 request with a single parameter)

1

pretty print of the above log output (disabling text logging)

2

log output showing query_parameters field (/api/v2 request with multiple parameters)

3

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

Summary by CodeRabbit

Improvements

  • Request logs now omit query-parameter details for login-support, SSO callback, and SAML authentication endpoints.
  • Query parameters continue to be logged for other API v2 requests, including repeated parameters and complete query strings.

Tests

  • Added coverage for query-parameter logging and exclusion behavior across API, SSO, login-support, and SAML URL patterns.

@cami-specter cami-specter self-assigned this Jun 17, 2026
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

logging.go excludes query-parameter logging for support-login, SSO callback, and SAML ACS paths. Other non-empty /api/v2/ queries remain logged. Table-driven tests cover exclusions, path boundaries, repeated parameters, and raw query preservation.

Changes

Query Parameter Logging Exclusions

Layer / File(s) Summary
Logging exclusion rules and middleware integration
cmd/api/src/api/middleware/logging.go
Adds path matching for support-login, SSO callback, and SAML ACS routes. The middleware logs non-empty queries only for non-excluded /api/v2/ paths.
Query logging behavior validation
cmd/api/src/api/middleware/logging_test.go
Adds table-driven tests for excluded routes, API path boundaries, repeated parameters, and exact raw-query preservation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: superlinkx

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the addition of query parameter logging for /api/v2 requests and includes the associated ticket.
Description check ✅ Passed The description covers the change, motivation, testing, screenshots, change type, associated ticket, and completed checklist items.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch wip-BED-8563-add-query-params-logging

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/api/src/api/middleware/logging_test.go`:
- Line 28: The import statement in logging_test.go at line 28 references an
invalid module path `github.com/specterops/bloodhound/cmd/api/src/ctx` that does
not exist in the repository, causing compilation failures. Replace this import
statement with the correct request-context package path that is already used by
the middleware code in this repository. Additionally, update all references to
this context package in lines 105-109 to use the corrected import path so the
tests can compile successfully.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: febec8aa-0b3e-483d-be6e-c7123e5de545

📥 Commits

Reviewing files that changed from the base of the PR and between 102b0fb and 51bfc3e.

📒 Files selected for processing (2)
  • cmd/api/src/api/middleware/logging.go
  • cmd/api/src/api/middleware/logging_test.go

Comment thread cmd/api/src/api/middleware/logging_test.go Outdated
@cami-specter cami-specter added api A pull request containing changes affecting the API code. infrastructure A pull request containing changes affecting the infrastructure code. labels Jun 17, 2026

@superlinkx superlinkx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows a lot of attention to detail and will make logging far more valuable

Comment thread cmd/api/src/api/middleware/logging.go Outdated
Comment on lines +128 to +132
return pathSegments[0] == "api" &&
pathSegments[1] == "v2" &&
pathSegments[2] == "sso" &&
pathSegments[4] == "callback"
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to note that there are old but still maintained due to still in use saml endpoints that likely also should be ignored and those are "versionless"

Further if sso is a concern, there is the sso/{id}/metadata endpoint as well. Might be worth considering a way to flag these in the route layer to be skipped 🤔 vs maintaining them here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function has been modified to add a few additional paths, thanks for bringing up one of them! See https://specterops.atlassian.net/browse/BED-8722 for full list of considered endpoints.

v1 is already excluded where the helper is called, but added a preventive guard for a deprecated path anyway.
As for the architecture, that's an interesting suggestion that I looked into. It is more involved however, although we could consider that in the future.

@mistahj67 mistahj67 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocked to verify intent

@mistahj67 mistahj67 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving to remove block. discussed offline. will likely benefit from a longer audit into logging queries. thank you for your patience and waiting 🙇

@cami-specter
cami-specter force-pushed the wip-BED-8563-add-query-params-logging branch from c41382f to 6e40fbf Compare August 12, 2026 18:34
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cmd/api/src/api/middleware/logging_test.go (1)

34-35: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use descriptive fixture and context names.

Replace testURL1 and testURL2 with names that identify their routes. Replace bhCtx with requestContext.

As per coding guidelines, “Prefer descriptive variable names, such as databaseInterface, instead of abbreviated names such as di or dbi.”

Also applies to: 147-151

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/api/src/api/middleware/logging_test.go` around lines 34 - 35, Rename the
test fixtures testURL1 and testURL2 to descriptive names identifying their
bloodhound-users and search routes, respectively, and rename bhCtx to
requestContext throughout the affected logging tests while preserving behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@cmd/api/src/api/middleware/logging_test.go`:
- Around line 34-35: Rename the test fixtures testURL1 and testURL2 to
descriptive names identifying their bloodhound-users and search routes,
respectively, and rename bhCtx to requestContext throughout the affected logging
tests while preserving behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: a2c94618-8f27-4a5e-9fa5-36d1690fd825

📥 Commits

Reviewing files that changed from the base of the PR and between dd452e3 and 6e40fbf.

📒 Files selected for processing (2)
  • cmd/api/src/api/middleware/logging.go
  • cmd/api/src/api/middleware/logging_test.go

@cami-specter

cami-specter commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Full audit on logging queries is now complete:
https://specterops.atlassian.net/browse/BED-8722 (audit linked in ticket)

Adds two additional exclusions: the login/support already pointed out by TJ + a guard against a deprecated path (v2 acs as v1 is already excluded). See details in the audit.

@cami-specter
cami-specter force-pushed the wip-BED-8563-add-query-params-logging branch from 6e40fbf to 724e83e Compare August 12, 2026 19:29
@cami-specter
cami-specter merged commit 2c28b4a into main Aug 12, 2026
13 checks passed
@cami-specter
cami-specter deleted the wip-BED-8563-add-query-params-logging branch August 12, 2026 19:40
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api A pull request containing changes affecting the API code. infrastructure A pull request containing changes affecting the infrastructure code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants