Skip to content

refactor: remove auth middleware #4059

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

Closed

Conversation

taimoorzaeem
Copy link
Collaborator

Refactor for #3507.

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

This commit removes auth middleware as it hides
side effects and obscures logic. The auth operations
are now done in its own stage in the request-response
cycle.
Comment on lines -66 to +65
Wai.setApacheRequestFilter (\_ res -> shouldLogResponse logLevel $ Wai.responseStatus res) &
Wai.setApacheUserGetter getAuthRole
Wai.setApacheRequestFilter (\_ res -> shouldLogResponse logLevel $ Wai.responseStatus res)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now with this approach we can't get the role here, I think we should do this change as a separate break/fix?

Copy link
Member

Choose a reason for hiding this comment

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

I think we should not do the change at all. Why remove the role from the log? It was added there on purpose, I even worked upstream (IIRC wai-logger) to support it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess I couldn't figure out yet how to get the role after removing the auth middleware. So, I removed it temporarily. I'll try to restore this change.

Copy link
Member

Choose a reason for hiding this comment

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

Let me put it this way: I remember introducing the middleware precisely to be able to add the role to the log output. So maybe this refactor is a dead-end. But if you come with a nice way to do it and will overall make the code better, sure.

@@ -974,35 +976,35 @@ def test_log_level(level, defaultenv):
output[0],
)
assert re.match(
r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"',
r'- - - \[.+\] "GET /unknown HTTP/1.1" 404 \d+ "" "python-requests/.+"',
Copy link
Member

Choose a reason for hiding this comment

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

This is not a refactor, this is a regression. The user's role was supposed to be in the log here and that's not the case anymore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, sorry, forgot to mark this PR as draft. This changed isn't supposed to be done with this refactor. I changed this here to discuss about this.

@taimoorzaeem taimoorzaeem marked this pull request as draft May 4, 2025 12:53
@taimoorzaeem
Copy link
Collaborator Author

Hmm, it looks like we also want to remove the Logger middleware in the future. Not sure but maybe if we can do it cleanly without breaking anything, that would solve the user/role logging issue as discussed earlier 🤔 .

-- TODO stop using this middleware to reuse the same "observer" pattern for all our logs
middleware :: LogLevel -> (Wai.Request -> Maybe BS.ByteString) -> Wai.Middleware
middleware logLevel getAuthRole =
unsafePerformIO $
Wai.mkRequestLogger Wai.defaultRequestLoggerSettings
{ Wai.outputFormat =

steve-chavez added a commit to steve-chavez/postgrest that referenced this pull request May 5, 2025
Benefits:

- Removes `unsafePerformIO` from Logger
- Reuses our cached time getter (`stateGetZTime`) instead of having another unnecessary one.
  See https://github.com/yesodweb/wai//blob/d50e1184600631a114e3d2ad119abdf0ef08834b/wai-extra/Network/Wai/Middleware/RequestLogger/Internal.hs#L22-L37.
- Reuses Observation module, simplified logic
- Will help with log buffering PostgREST#4061

TODO:

- [ ] Currently we vendor `apacheLogStr`, but might be possible to
  expose this function upstream. If not, it's not a big problem since
  the function is small.
- [ ] Time in logging differs a bit:
```
  Old: postgrest_test_anonymous [04/May/2025:14:43:06 -0500]
  New: postgrest_test_anonymous [2025-05-04 22:43:01.28098247 -05]
```
  + Otherwise all logging works the same.
- [ ] Auth error logging relies on old logger middleware
  + We might need to put `observer` in Auth.hs
  + Or otherwise try to combine this with PostgREST#4059
steve-chavez added a commit to steve-chavez/postgrest that referenced this pull request May 5, 2025
Benefits:

- Removes `unsafePerformIO` from Logger
- Reuses our cached time getter (`stateGetZTime`) instead of having another unnecessary one.
  See https://github.com/yesodweb/wai//blob/d50e1184600631a114e3d2ad119abdf0ef08834b/wai-extra/Network/Wai/Middleware/RequestLogger/Internal.hs#L22-L37.
- Reuses Observation module, simplified logic
- Will help with log buffering PostgREST#4061

TODO:

- [ ] Currently we vendor `apacheLogStr`, but might be possible to
  expose this function upstream. If not, it's not a big problem since
  the function is small.
- [ ] Time in logging differs a bit:
```
  Old: postgrest_test_anonymous [04/May/2025:14:43:06 -0500]
  New: postgrest_test_anonymous [2025-05-04 22:43:01.28098247 -05]
```
  + Otherwise all logging works the same.
- [ ] Auth error logging relies on old logger middleware
  + We might need to put `observer` in Auth.hs
  + Or otherwise try to combine this with PostgREST#4059
@steve-chavez
Copy link
Member

@taimoorzaeem Gave a shot at removing Logger middleware: #4062. I think we need to solve that one first.

@taimoorzaeem
Copy link
Collaborator Author

Closing as continued on #4064.

@taimoorzaeem taimoorzaeem deleted the refactor/auth-middleware branch May 31, 2025 16:48
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