Skip to content

fix: redact credentials in log messages - #65

Merged
uniqueg merged 4 commits into
devfrom
fix/log-credentials
Nov 6, 2026
Merged

uniqueg merged 4 commits into
devfrom
fix/log-credentials

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Log messages name objects by their identifiers, so that none of them contains access tokens or other credentials:

  • New repositories, users, subscriptions and builds are logged by identifier instead of as whole objects; debug messages while storing them no longer include the object (repositories.py, users.py, subscriptions.py, builds.py).
  • Creating a build pod logs the pod's name instead of the created pod. Errors of the Kubernetes API when creating or deleting a pod are logged by status and reason, and the errors raised for them no longer carry the API error as their context (FOCA logs exceptions with their context).
  • A failed request to GitHub for image signing is logged by the type of the error, as its message can quote headers, and answered with 500 as before (now a problem detail without context). The GitHub token from the environment is stripped of surrounding whitespace, and an unset one is sent empty instead of as None.
  • A log message for missing builds now has its f-prefix.
  • docs/deployment.md says what log messages contain.

Closes #64

Verification

  • make test (Python 3.13): 201 passed, 100% coverage. New tests capture the log records (at debug level) of these paths and check exact messages and levels, that identifiers are there, and that tokens, a deployment file's token, response bodies of the Kubernetes API and the GitHub token appear neither in the log nor in the formatted exception that FOCA logs; they fail without the change.
  • CI: all checks passed (PR title, Lint, Test, Chart, Docs, Smoke test, Upgrade test, Scan image, CodeQL, Codecov, Read the Docs), CI passed.

Review

Full review (adversarial-reviewer and quality-reviewer, two rounds). Round 1 fixed: exception context of pod errors, the GitHub request's errors, identifiers for subscriptions and builds, exact test assertions with a real deployment file, the f-prefix, import order and the docs note. Round 2 fixed: comment wording, line length, and the docs on which GitHub failures are logged.

Sourcery: 2 threads fixed and resolved (pod name read safely before the API call, so a manifest without one still gets CreatePodError without context; pod tests independent of KUBERNETES_SERVICE_HOST).

Declined review findings

  • FOCA's request-validation messages quote values of the wrong JSON type (low): they come from FOCA, not Pubgrade.
  • Checking GitHub's response status and a timeout for the signing request (low): unchanged behaviour of dev beyond log messages; the docs now say that responses aren't checked.

Open questions

None.

🤖 Generated with Claude Code

uniqueg and others added 3 commits November 6, 2026 18:15
Name new users and repositories in log messages by their identifiers,
leave stored objects out of debug messages for subscriptions and
builds, name created pods by name, and log errors of the Kubernetes
API by status and reason, so that no log message contains access
tokens or other credentials. Tests check the log records of these
paths.

Closes #64

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Raise the errors for failed pod calls without the API error as their
  context, as FOCA logs exceptions with their context.
- Log failed requests to GitHub for image signing by type, answer them
  with 500, and strip the GitHub token read from the environment.
- Log new subscriptions and builds by identifier, and fix a log message
  that lacked its f-prefix.
- Test exact messages and levels, with a deployment file that holds a
  token, and document what log messages contain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Clarify the comment on logging the type only, wrap long test lines, and
say that responses of GitHub aren't checked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Review the safer logging for repository, user, subscription, and build registration, including the corrected missing-build message.

  • pubgrade/modules/endpoints/builds.py L167, L180, L262
  • pubgrade/modules/endpoints/repositories.py L106
  • pubgrade/modules/endpoints/subscriptions.py L109, L125
  • pubgrade/modules/endpoints/users.py L112

Review pod creation and deletion logging, which reports safe identifiers and Kubernetes status and reason without retaining the API exception as context.

  • pubgrade/modules/endpoints/builds.py L533-539, L650-651

Review GitHub signing-request handling, which normalizes the token and logs only the exception type on request failure.

  • pubgrade/modules/endpoints/builds.py L674-689

Documentation

Document which identifiers and failure details appear in logs, and clarify that GitHub responses are not checked.

  • docs/deployment.md L58-63

Tests

Review regression tests that verify identifiers remain useful in logs while credentials, Kubernetes response bodies, and GitHub header values stay out of logs and propagated exception context.

  • tests/pubgrade/endpoints/test_builds.py L130-157, L229-244, L731-856 (+2 small edits)
  • tests/pubgrade/endpoints/test_repositories.py L2, L57-65
  • tests/pubgrade/endpoints/test_subscriptions.py L3, L110-122
  • tests/pubgrade/endpoints/test_users.py L2, L64-72

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@read-the-docs-community

read-the-docs-community Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 Pubgrade | 🛠️ Build #35704819 | 📁 Comparing a3c2363 against latest (dd9ab9d)

  🔍 Preview build  

1 file changed
± deployment/index.html

@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (dd9ab9d) to head (a3c2363).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev       #65   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines          745       752    +7     
=========================================
+ Hits           745       752    +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 6, 2026 17:55
@uniqueg
uniqueg requested a review from akash2237778 as a code owner November 6, 2026 17:55

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="pubgrade/modules/endpoints/builds.py" line_range="538" />
<code_context>
-                "AppsV1Api->create_namespaced_pod: "
-                "%s\n" % e
+                f"Creating pod {dep['metadata']['name']} failed: "
+                f"{e.status} {e.reason}"
             )
-            raise CreatePodError
</code_context>
<issue_to_address>
**API errors expose credentials**

When the Kubernetes API rejects a valid manifest without `metadata.name`, `dep['metadata']['name']` raises `KeyError` before `CreatePodError from None` runs, leaving the caught `ApiException` as context; FOCA exception logging can include its response body, which may contain credentials, and the caller sees the wrong exception.

Use a safe lookup for the manifest name in the log message and raise `CreatePodError from None` without letting logging mask the API error.
</issue_to_address>

### Comment 2
<location path="tests/pubgrade/endpoints/test_builds.py" line_range="731" />
<code_context>
         del os.environ["NAMESPACE"]
         del os.environ["KUBERNETES_SERVICE_HOST"]

+    @patch("kubernetes.config.load_kube_config", mocked_load_cluster_config)
+    @patch(
+        "kubernetes.client.api.core_v1_api.CoreV1Api.create_namespaced_pod",
</code_context>
<issue_to_address>
**In-cluster environment breaks pod tests**

When the test process has `KUBERNETES_SERVICE_HOST` set, `build_push_image_using_kaniko` calls the unpatched `load_incluster_config` before reaching the mocked pod call, so `test_build_push_image_using_kaniko_logs_no_token` and `test_build_push_image_using_kaniko_error_logs_no_body` fail before their assertions.

Clear `KUBERNETES_SERVICE_HOST` with `monkeypatch.delenv` in these tests, or patch `load_incluster_config` as well.

Also at `tests/pubgrade/endpoints/test_builds.py:762`.
</issue_to_address>

Linked issues

#64 fix: redact credentials in log messages · 6 of 6 met

# Requirement Status Notes
1 Log new repositories by identifier instead of logging the whole object. ✅ Met The registration log names the repository ID, and a test checks that its access token is absent.
2 Log new users by identifier instead of logging the whole object. ✅ Met The registration log names the user UID, and a test checks that the user's access token is absent.
3 Keep subscription objects out of debug messages while storing them. ✅ Met The debug message no longer includes the subscription data; the test checks that the subscription token is absent.
4 Keep build objects out of debug messages while storing them. ✅ Met The debug message no longer includes the build data; the test checks that the Docker Hub token is absent.
5 Log the created build pod by name, and log Kubernetes API errors by status and reason. ✅ Met Pod creation logs its name, and pod creation and deletion errors log status and reason without retaining the API error as exception context.
6 Ensure the named log paths do not expose credentials, with tests checking their log records. ✅ Met The added tests check identifiers and credential absence for registration and pod creation, and check Kubernetes error logs and exception formatting.

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: pubgrade/modules/endpoints/builds.py:538, tests/pubgrade/endpoints/test_builds.py:731


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread pubgrade/modules/endpoints/builds.py Resolved
Comment thread tests/pubgrade/endpoints/test_builds.py Resolved
Read the pod's name before calling the API, so that a manifest without
one still raises CreatePodError without the API error as its context,
and keep the pod tests independent of KUBERNETES_SERVICE_HOST.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@uniqueg
uniqueg merged commit f7d74eb into dev Nov 6, 2026
33 of 43 checks passed
@uniqueg
uniqueg deleted the fix/log-credentials branch November 6, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: redact credentials in log messages

1 participant