Skip to content
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

WIP: add support for ClientAuthentication_hook #1785

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pgrx-pg-sys/include/pg13.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "miscadmin.h"
#include "pgstat.h"

#include "libpq/auth.h"
#include "access/amapi.h"
#include "access/genam.h"
#include "access/generic_xlog.h"
Expand Down
13 changes: 12 additions & 1 deletion pgrx-tests/src/tests/hooks_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ mod tests {
events: u32,
}
impl PgHooks for TestHook {

fn client_authentication(
&mut self,
port: PgBox<Port>,
status: i32,
prev_hook: fn(port: PgBox<Port>, status: i32) -> HookResult<()>,
) -> HookResult<()> {
self.events += 1;
prev_hook(port, status)
}

/// Hook before the logs are being processed by PostgreSQL itself
fn emit_log(
&mut self,
Expand Down Expand Up @@ -128,7 +139,7 @@ mod tests {
// To trigger the emit_log hook, we need something to log.
// We therefore ensure the select statement will be logged.
Spi::run("SET local log_statement to 'all'; SELECT 1").expect("SPI failed");
assert_eq!(8, HOOK.events);
assert_eq!(9, HOOK.events);

// TODO: it'd be nice to also test that .commit() and .abort() also get called
// but I don't see how to do that since we're running *inside* a transaction here
Expand Down
31 changes: 31 additions & 0 deletions pgrx/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ impl<T> Deref for HookResult<T> {
}

pub trait PgHooks {
fn client_authentication(
&mut self,
port: PgBox<pg_sys::Port>,
status: i32,
prev_hook: fn(port: PgBox<pg_sys::Port>, status: i32) -> HookResult<()>,
) -> HookResult<()> {
prev_hook(port, status)
}

/// Hook before the logs are being processed by PostgreSQL itself
fn emit_log(
&mut self,
Expand Down Expand Up @@ -186,6 +195,7 @@ pub trait PgHooks {

struct Hooks {
current_hook: Box<&'static mut (dyn PgHooks)>,
client_authentication: pg_sys::ClientAuthentication_hook_type,
prev_emit_log_hook: pg_sys::emit_log_hook_type,
prev_executor_start_hook: pg_sys::ExecutorStart_hook_type,
prev_executor_run_hook: pg_sys::ExecutorRun_hook_type,
Expand Down Expand Up @@ -238,6 +248,7 @@ pub unsafe fn register_hook(hook: &'static mut (dyn PgHooks)) {
prev_post_parse_analyze_hook: pg_sys::post_parse_analyze_hook
.replace(pgrx_post_parse_analyze),
prev_emit_log_hook: pg_sys::emit_log_hook.replace(pgrx_emit_log),
client_authentication: pg_sys::ClientAuthentication_hook.replace(pgrx_client_auth),
});

#[pg_guard]
Expand Down Expand Up @@ -483,6 +494,26 @@ unsafe extern "C" fn pgrx_planner(
pgrx_planner_impl(parse, std::ptr::null(), cursor_options, bound_params)
}

#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15", feature = "pg16"))]
#[pg_guard]
unsafe extern "C" fn pgrx_client_auth(
port: *mut pg_sys::Port,
status: i32
) {
fn prev(port: PgBox<pg_sys::Port>, status: i32) -> HookResult<()> {
HookResult::new(unsafe {
match HOOKS.as_mut().unwrap().client_authentication.as_ref() {
None => (),
Some(f) => (f)(port.as_ptr(), status),
}
})
}

let hook = &mut HOOKS.as_mut().unwrap().current_hook;
hook.client_authentication(PgBox::from_pg(port), status, prev).inner
}


#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15", feature = "pg16"))]
#[pg_guard]
unsafe extern "C" fn pgrx_planner(
Expand Down
Loading