Description
Postgres filter: implement (equivalent) Postgres HBA in Envoy: enabling offloading Postgres' HBA (Host-Based Authentication) to Envoy, leveraging Envoy's Postgres filter.
Postgres uses a mechanism to accept or reject connections called HBA ("Host-Based Authentication"), that is controlled by configuration file pg_hba.conf
. This file can be reloaded anytime to update the configuration, and supports accepting or rejecting connections based on connection metadata such as the source IP/CIDR, username, database, authentication method and whether the connection is SSL/is not.
What if Envoy's Postgres filter would implement this functionality? It would allow offloading these checks off of Postgres, potentially saving Postgres from "DoS attacks" or basically too many illegal connections. It is also necessary in essence when fronting Postgres with Envoy, as a) SSL status of the original downstream connection would be unknown to Postgres, making it impossible to filter based on SSL enforcement or not; b) we also don't preserve the source IP (there's some separate research on this area).
The Postgres filter could capture this connection metadata (user, database, auth method, source host/port, destination host/port, downstream SSL or not) easily, on connection establishment, and use this metadata to allow/reject the connection. Please note that unlike current's dynamic metadata, we don't need any SQL parsing here to generate the connection metadata, it is easily extracted from the connection message.
Ideally, we could pass this metadata to the RBAC filter, and accept/reject there the connection. But RBAC filter knows nothing about PG wire protocol, and thus may only drop the connection --which will have the side effect of not returning an appropriate error downstream, as a normal Postgres would do under this circumstance. And alternative would be to implement a Postgres filter "submodule", called HBA, to emulate this Postgres' HBA functionality within the Envoy filter, without relying on the RBAC filter.
Supporting HBA on Envoy's side, apart from solving the mentioned problems (lack of visibility about downstream SSL/not SSL and source IP information) could extend HBA to more advanced filtering mechanism, potentially combining with other technologies, enabling for example accepting/rejecting connections based on pod labels, to name one.
There are several possible options, subject to discussion:
- Gather the extra metadata and pass to RBAC filter. Easiest to implement, but denying the connection is not the same experience that current Postgres users get when rejected by HBA reasons (they get a nice appropriate error message, indicating even possible causes).
- Don't use RBAC module for this use case and rebuild the relevant functionality part on the Postgres filter. Shouldn't bee too hard, but definitely requires some effort and in some sense is a bit reinventing a wheel.
- Raise a broader discussion to see if it is possible / makes sense to have the RBAC filter piggy back to Postgres filter. This may or may not require some bigger architectural changes, as filters normally operate in a forward chain.