Skip to content

refactor: remove auth and logging middleware#4884

Open
steve-chavez wants to merge 1 commit intoPostgREST:mainfrom
steve-chavez:remove-middleware-auth-logger
Open

refactor: remove auth and logging middleware#4884
steve-chavez wants to merge 1 commit intoPostgREST:mainfrom
steve-chavez:remove-middleware-auth-logger

Conversation

@steve-chavez
Copy link
Copy Markdown
Member

@steve-chavez steve-chavez commented May 6, 2026

Continues #4064 and #4062.

I think it's important to do this now before #3140, otherwise we'll accumulate tech debt.

Benefits:

This commit removes auth middleware for it hides
side effects and obscures logic. The auth operations
are now done in its own stage in the request-response
cycle.

It also removes the logging middleware because now
we instead use observation module to log the response.
@steve-chavez steve-chavez force-pushed the remove-middleware-auth-logger branch from 57aef68 to 403b1c6 Compare May 6, 2026 01:32
@steve-chavez steve-chavez requested a review from taimoorzaeem May 6, 2026 01:32
@steve-chavez steve-chavez marked this pull request as ready for review May 6, 2026 01:33
Comment thread src/PostgREST/App.hs

let bearerAuth = ApiRequest.userBearerAuth req

(jwtTime, authResult@AuthResult{..}) <- withExceptT (Nothing,) $ -- no role at this stage
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is more explicit now, explaining better the behaviors on #4882

Comment thread src/PostgREST/Logger.hs
when (shouldLogResponse logLevel status) $ do
zTime <- stateGetZTime loggerState
let handl = stdout -- doing this indirection since the linter wants to change "hPutStr stdout" to "putStr", but we want "stdout" to appear explicitly
hPutStr handl $ apacheFormat maybeRole (BS.pack $ formatTime defaultTimeLocale "%d/%b/%Y:%T %z" zTime) req status contentLen
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not logWithZTime $ observationMessages o here?

Comment thread src/PostgREST/Logger.hs
WarpServerObs txt ->
pure $ "Warp server: " <> txt
ResponseObs {} ->
mempty
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be better to move the formatting code here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, it would be better, but it's not very straight forward. I think issue here is that observation message includes time from loggerState which is not in scope here. It would need more refactoring.

Maybe add a comment to clarify?

Comment thread test/spec/Main.hs

let
initApp sCache st config = do
initApp sCache _ config = do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe remove this unneeded parameter from everywhere in the module or add a TODO to do it later?

Comment thread src/PostgREST/App.hs
Nothing -> do
lift $ observer SchemaCacheEmptyObs
throwError Error.NoSchemaCacheError
throwError (authRole', Error.NoSchemaCacheError)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, maybe inlining it would be more clear so it matches the function signature (Maybe ByteString, Error)?

Suggested change
throwError (authRole', Error.NoSchemaCacheError)
throwError (Just authRole, Error.NoSchemaCacheError)

Comment thread src/PostgREST/App.hs
-> Wai.Request
-> Handler IO Wai.Response
postgrestResponse appState conf@AppConfig{..} maybeSchemaCache authResult@AuthResult{..} req = do
-> ExceptT (Maybe ByteString, Error) IO (ByteString, Wai.Response)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To be honest passing the role like this (ie. both in the result and in the exception type) using tuples is not too good - this looks like a state monad in disguise.

I understand we need to have the role name available in the caller in both cases (because Error might happen after auth).
I would either:

  • extract authentication from postgrestResponse and pass the role name to it.
  • pass some IORef (Maybe ByteString) explicitly to it to record the state after auth.

The first alternative looks the cleanest.

WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants