Skip to content

Commit 6c19265

Browse files
committed
Improve init
The token gen infra (allocator, creds provider) are setup once, globally. This lets us cache creds and do a fast-fail if there are no creds.
1 parent 560789d commit 6c19265

8 files changed

Lines changed: 320 additions & 81 deletions

File tree

scripts/build-dsql.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ else
5151
echo " aws-dsql-auth submodules already initialized."
5252
fi
5353

54-
if [ ! -f "aws-dsql-auth/build/install/lib64/libaws-dsql-auth.a" ]; then
54+
if [ ! -d "aws-dsql-auth/build/install/" ]; then
5555
# Build aws-dsql-auth
5656
echo " Building aws-dsql-auth library..."
5757
cd aws-dsql-auth

src/bin/pgbench/pgbench.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "pgbench.h"
6969
#include "port/pg_bitutils.h"
7070
#include "portability/instr_time.h"
71+
#include "fe-dsql-auth.h"
7172

7273
/* X/Open (XSI) requires <math.h> to provide M_PI, but core POSIX does not */
7374
#ifndef M_PI
@@ -7124,6 +7125,29 @@ main(int argc, char **argv)
71247125
setenv("PGDSQL", "1", 1);
71257126
is_no_vacuum = true;
71267127
foreign_keys = false;
7128+
7129+
/* Initialize DSQL token generator */
7130+
if (dsql_initialize_token_generator() != 0)
7131+
{
7132+
pg_fatal("Failed to initialize DSQL token generator");
7133+
}
7134+
7135+
/* Validate AWS credentials */
7136+
{
7137+
char *err_msg = NULL;
7138+
if (dsql_validate_aws_credentials(&err_msg) != 0)
7139+
{
7140+
if (err_msg)
7141+
{
7142+
pg_fatal("DSQL credential validation failed: %s", err_msg);
7143+
free(err_msg);
7144+
}
7145+
else
7146+
{
7147+
pg_fatal("DSQL credential validation failed");
7148+
}
7149+
}
7150+
}
71277151
}
71287152

71297153
/* set default script if none */

src/bin/psql/startup.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* src/bin/psql/startup.c
77
*/
88
#include "postgres_fe.h"
9+
#include "libpq-fe.h"
910

1011
#ifndef WIN32
1112
#include <unistd.h>
@@ -26,6 +27,8 @@
2627
#include "mainloop.h"
2728
#include "settings.h"
2829

30+
#include "fe-dsql-auth.h"
31+
2932
/*
3033
* Global psql options
3134
*/
@@ -221,6 +224,29 @@ main(int argc, char *argv[])
221224
{
222225
setenv("PGDSQL", "1", 1);
223226
pset.getPassword = TRI_NO;
227+
228+
/* Initialize DSQL token generator */
229+
if (dsql_initialize_token_generator() != 0)
230+
{
231+
pg_fatal("Failed to initialize DSQL token generator");
232+
}
233+
234+
/* Validate AWS credentials */
235+
{
236+
char *err_msg = NULL;
237+
if (dsql_validate_aws_credentials(&err_msg) != 0)
238+
{
239+
if (err_msg)
240+
{
241+
pg_fatal("DSQL credential validation failed: %s", err_msg);
242+
free(err_msg);
243+
}
244+
else
245+
{
246+
pg_fatal("DSQL credential validation failed");
247+
}
248+
}
249+
}
224250
}
225251

226252
/*

src/interfaces/libpq/Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,19 @@ ifeq ($(PORTNAME), linux)
9090
# Link with AWS libraries using start-group to resolve dependencies
9191
$(LD) -r -o fe-dsql-auth-with-aws.o fe-dsql-auth-temp.o --start-group $(AWS_DSQL_AUTH_ALL_LIBS) --end-group
9292
# Create a list of symbols to keep (only the public API from fe-dsql-auth.h)
93-
echo "generate_dsql_token" > keep-symbols.txt
94-
echo "dsql_auth_cleanup" >> keep-symbols.txt
93+
echo "dsql_initialize_token_generator" > keep-symbols.txt
94+
echo "dsql_generate_token" >> keep-symbols.txt
95+
echo "dsql_validate_aws_credentials" >> keep-symbols.txt
96+
echo "dsql_cleanup" >> keep-symbols.txt
9597
# Hide all symbols except the ones we want to keep
9698
objcopy --keep-global-symbols=keep-symbols.txt fe-dsql-auth-with-aws.o $@
9799
rm -f fe-dsql-auth-temp.o fe-dsql-auth-with-aws.o keep-symbols.txt
98100
else ifeq ($(PORTNAME), darwin)
99101
# macOS: Use ld with exported symbols list
100-
echo "_generate_dsql_token" > exported-symbols.txt
101-
echo "_dsql_auth_cleanup" >> exported-symbols.txt
102+
echo "_dsql_initialize_token_generator" > exported-symbols.txt
103+
echo "_dsql_generate_token" >> exported-symbols.txt
104+
echo "_dsql_validate_aws_credentials" >> exported-symbols.txt
105+
echo "_dsql_cleanup" >> exported-symbols.txt
102106
$(LD) -r -o $@ fe-dsql-auth-temp.o -exported_symbols_list exported-symbols.txt $(AWS_DSQL_AUTH_ALL_LIBS)
103107
rm -f fe-dsql-auth-temp.o exported-symbols.txt
104108
else

src/interfaces/libpq/exports.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,7 @@ PQgetAuthDataHook 208
211211
PQdefaultAuthDataHook 209
212212
PQfullProtocolVersion 210
213213
appendPQExpBufferVA 211
214+
dsql_initialize_token_generator 212
215+
dsql_generate_token 213
216+
dsql_validate_aws_credentials 214
217+
dsql_cleanup 215

src/interfaces/libpq/fe-connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ pqConnectOptions2(PGconn *conn)
14411441
pwhost = conn->connhost[i].hostaddr;
14421442

14431443
is_admin = strcmp("admin", conn->pguser) == 0;
1444-
token = generate_dsql_token(pwhost, is_admin, &err_msg);
1444+
token = dsql_generate_token(pwhost, is_admin, &err_msg);
14451445
if (!token)
14461446
{
14471447
libpq_append_conn_error(conn, "DSQL token generation failed for host=%s: %s",

0 commit comments

Comments
 (0)