Skip to content

Conversation

@git-hyagi
Copy link
Collaborator

@git-hyagi git-hyagi commented Jan 2, 2026

Summary by Sourcery

Extract user identity from incoming requests and include it in API access logs.

Enhancements:

  • Add WSGI middleware to derive REMOTE_USER from the X-RH-IDENTITY header and expose it to Gunicorn.
  • Update API deployment configuration to load the custom Gunicorn middleware at startup.
  • Include REMOTE_USER in the Gunicorn access log format for the pulp API container.

Deployment:

  • Extend the container image to ship the custom log middleware script used by Gunicorn.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 2, 2026

Reviewer's Guide

Adds middleware and configuration so Gunicorn populates REMOTE_USER from the X-RH-IDENTITY header and includes it in API access logs.

Sequence diagram for REMOTE_USER extraction and logging

sequenceDiagram
    actor Client
    participant GunicornWorker
    participant UserExtractionMiddleware
    participant WSGIApp

    Client->>GunicornWorker: HTTP request with header X-RH-IDENTITY
    activate GunicornWorker
    GunicornWorker->>UserExtractionMiddleware: call wsgi(environ, start_response)
    activate UserExtractionMiddleware
    UserExtractionMiddleware->>UserExtractionMiddleware: decode base64 HTTP_X_RH_IDENTITY
    UserExtractionMiddleware->>UserExtractionMiddleware: parse JSON identity_data
    UserExtractionMiddleware->>UserExtractionMiddleware: determine username
    alt username found
        UserExtractionMiddleware->>UserExtractionMiddleware: set environ[REMOTE_USER] = username
    else no username
        UserExtractionMiddleware->>UserExtractionMiddleware: log warning
    end
    UserExtractionMiddleware->>WSGIApp: call app(environ, start_response)
    activate WSGIApp
    WSGIApp-->>UserExtractionMiddleware: response
    deactivate WSGIApp
    UserExtractionMiddleware-->>GunicornWorker: response
    deactivate UserExtractionMiddleware
    GunicornWorker->>GunicornWorker: log access with %({REMOTE_USER}e)s
    GunicornWorker-->>Client: HTTP response
    deactivate GunicornWorker
Loading

Class diagram for UserExtractionMiddleware and Gunicorn integration

classDiagram
    class UserExtractionMiddleware {
        - app
        + UserExtractionMiddleware(app)
        + __call__(environ, start_response)
    }

    class GunicornWorker {
        + wsgi
        + post_worker_init(worker)
    }

    GunicornWorker o--> UserExtractionMiddleware
Loading

File-Level Changes

Change Details Files
Include REMOTE_USER in API access log lines and wire Gunicorn to use a custom logging middleware.
  • Update Gunicorn access-logformat in the clowdapp deployment to log REMOTE_USER from the WSGI environ instead of the default %(u)s field
  • Set GUNICORN_CMD_ARGS in the API deployment to load a custom Gunicorn config module
  • Copy the new log_middleware.py module into the application image so it is available at runtime
  • Implement UserExtractionMiddleware WSGI middleware that decodes X-RH-IDENTITY, derives a username or org identifier from its identity payload, logs anomalies, and sets environ['REMOTE_USER'] when available
  • Register a post_worker_init Gunicorn hook in log_middleware.py that wraps the worker.wsgi app with UserExtractionMiddleware so the REMOTE_USER is populated before Gunicorn access logging runs
deploy/clowdapp.yaml
Dockerfile
images/assets/log_middleware.py

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

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.

1 participant