Skip to content

Commit

Permalink
add test for neon_proxy_params_compat option
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Dec 3, 2024
1 parent c84e5d3 commit c103af7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
10 changes: 6 additions & 4 deletions proxy/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ impl ConnCfg {
}

/// Apply startup message params to the connection config.
pub(crate) fn set_startup_params(&mut self, params: &StartupMessageParams) {
let arbitrary_params = false;

pub(crate) fn set_startup_params(
&mut self,
params: &StartupMessageParams,
arbitrary_params: bool,
) {
for (k, v) in params.iter() {
match k {
// Only set `user` if it's not present in the config.
Expand Down Expand Up @@ -426,7 +428,7 @@ mod tests {
let params = "project = foo";
assert_eq!(filtered_options(params).as_deref(), Some("project = foo"));

let params = "project = foo neon_endpoint_type:read_write neon_lsn:0/2";
let params = "project = foo neon_endpoint_type:read_write neon_lsn:0/2 neon_proxy_params_compat:true";
assert_eq!(filtered_options(params).as_deref(), Some("project = foo"));
}
}
1 change: 1 addition & 0 deletions proxy/src/console_redirect_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pub(crate) async fn handle_client<S: AsyncRead + AsyncWrite + Unpin>(
let mut node = connect_to_compute(
ctx,
&TcpMechanism {
params_compat: true,
params: &params,
locks: &config.connect_compute_locks,
},
Expand Down
4 changes: 3 additions & 1 deletion proxy/src/proxy/connect_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub(crate) trait ComputeConnectBackend {
}

pub(crate) struct TcpMechanism<'a> {
pub(crate) params_compat: bool,

/// KV-dictionary with PostgreSQL connection params.
pub(crate) params: &'a StartupMessageParams,

Expand All @@ -92,7 +94,7 @@ impl ConnectMechanism for TcpMechanism<'_> {
}

fn update_connect_config(&self, config: &mut compute::ConnCfg) {
config.set_startup_params(self.params);
config.set_startup_params(self.params, self.params_compat);
}
}

Expand Down
14 changes: 12 additions & 2 deletions proxy/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,15 @@ pub(crate) async fn handle_client<S: AsyncRead + AsyncWrite + Unpin>(
}
};

let params_compat = match &user_info {
auth::Backend::ControlPlane(_, info) => info.info.options.get("proxy_params_compat").is_some(),
auth::Backend::Local(_) => false,
};

let mut node = connect_to_compute(
ctx,
&TcpMechanism {
params_compat,
params: &params,
locks: &config.connect_compute_locks,
},
Expand Down Expand Up @@ -415,13 +421,17 @@ impl NeonOptions {
.map(Self::parse_from_iter)
.unwrap_or_default()
}

pub(crate) fn parse_options_raw(options: &str) -> Self {
Self::parse_from_iter(StartupMessageParams::parse_options_raw(options))
}

pub(crate) fn get(&self, key: &str) -> Option<SmolStr> {
self.0.iter().find_map(|(k, v)| (k == key).then_some(v)).cloned()
}

pub(crate) fn is_ephemeral(&self) -> bool {
// Currently, neon endpoint options are all reserved for ephemeral endpoints.
!self.0.is_empty()
self.0.iter().filter(|(k, _)| k != "proxy_params_compat").count() == 0
}

fn parse_from_iter<'a>(options: impl Iterator<Item = &'a str>) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion test_runner/fixtures/neon_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ async def connect_async(self, **kwargs: Any) -> asyncpg.Connection:
for match in re.finditer(r"-c(\w*)=(\w*)", options):
key = match.group(1)
val = match.group(2)
if "server_options" in conn_options:
if "server_settings" in conn_options:
conn_options["server_settings"].update({key: val})
else:
conn_options["server_settings"] = {key: val}
Expand Down
19 changes: 19 additions & 0 deletions test_runner/regress/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
from contextlib import closing
import json
import subprocess
import time
Expand Down Expand Up @@ -131,6 +132,24 @@ def test_proxy_options(static_proxy: NeonProxy, option_name: str):
assert out[0][0] == " str"


@pytest.mark.asyncio
async def test_proxy_arbitrary_params(static_proxy: NeonProxy):
with closing(
await static_proxy.connect_async(server_settings={"IntervalStyle": "iso_8601"})
) as conn:
out = await conn.fetchval("select to_json('0 seconds'::interval)")
assert out == '"00:00:00"'

options = "neon_proxy_params_compat:true"
with closing(
await static_proxy.connect_async(
server_settings={"IntervalStyle": "iso_8601", "options": options}
)
) as conn:
out = await conn.fetchval("select to_json('0 seconds'::interval)")
assert out == '"PT0S"'


def test_auth_errors(static_proxy: NeonProxy):
"""
Check that we throw very specific errors in some unsuccessful auth scenarios.
Expand Down

0 comments on commit c103af7

Please sign in to comment.