-
Notifications
You must be signed in to change notification settings - Fork 8
Update newpool to add IAM auth beforeConnect #32
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
Changes from 1 commit
401e9a7
02903c3
e3bffd3
ef65e44
b66b204
3e01779
7d3b4dd
7c43cf9
96678e6
b473e89
b34cd9c
837bc43
ad81ebb
5d44793
e3fcf9e
d8fedab
c00ee81
eb97c09
7265ebc
7129bea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| "github.com/jackc/pgx/v5" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/jackc/pgx/v5/pgxpool" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/specterops/bloodhound/cmd/api/src/config" | ||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 11 in drivers/pg/pg.go
|
||||||||||||||||||||||||||||||||||||||||||
| "github.com/specterops/dawgs" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/specterops/dawgs/cypher/models/pgsql" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/specterops/dawgs/graph" | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -50,15 +51,12 @@ | |||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func NewPool(connectionString string) (*pgxpool.Pool, error) { | ||||||||||||||||||||||||||||||||||||||||||
| if connectionString == "" { | ||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("graph connection requires a connection url to be set") | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| func NewPool(cfg config.DatabaseConfiguration) (*pgxpool.Pool, error) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| poolCtx, done := context.WithTimeout(context.Background(), poolInitConnectionTimeout) | ||||||||||||||||||||||||||||||||||||||||||
| defer done() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| poolCfg, err := pgxpool.ParseConfig(connectionString) | ||||||||||||||||||||||||||||||||||||||||||
| poolCfg, err := pgxpool.ParseConfig(cfg.PostgreSQLConnectionString()) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -73,6 +71,17 @@ | |||||||||||||||||||||||||||||||||||||||||
| poolCfg.AfterConnect = afterPooledConnectionEstablished | ||||||||||||||||||||||||||||||||||||||||||
| poolCfg.AfterRelease = afterPooledConnectionRelease | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| poolCfg.BeforeConnect = func(ctx context.Context, poolCfg *pgx.ConnConfig) error { | ||||||||||||||||||||||||||||||||||||||||||
| slog.Info(fmt.Sprint("RDS credentional beforeConnect(), creating new IAM credentials")) | ||||||||||||||||||||||||||||||||||||||||||
| refreshConnectionString := cfg.PostgreSQLConnectionString() | ||||||||||||||||||||||||||||||||||||||||||
| newPoolCfg, err := pgxpool.ParseConfig(refreshConnectionString) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| poolCfg.Password = newPoolCfg.ConnConfig.Password | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: ast-grep --pattern $'func ($_ DatabaseConfiguration) PostgreSQLConnectionString() string {
$$$
}'Repository: SpecterOps/DAWGS Length of output: 42 🏁 Script executed: rg "PostgreSQLConnectionString" --type go -A 20Repository: SpecterOps/DAWGS Length of output: 1994 🏁 Script executed: rg "func NewPool" --type go -A 5Repository: SpecterOps/DAWGS Length of output: 407 🏁 Script executed: rg "func.*PostgreSQLConnectionString" --type go -A 15Repository: SpecterOps/DAWGS Length of output: 42 🏁 Script executed: rg "PostgreSQLConnectionString" --type go -B 2 -A 15 | head -100Repository: SpecterOps/DAWGS Length of output: 1780 🏁 Script executed: fd "config" --type d | grep -E "^[^/]*config" | head -10Repository: SpecterOps/DAWGS Length of output: 42 🏁 Script executed: head -30 drivers/pg/pg.goRepository: SpecterOps/DAWGS Length of output: 908 🏁 Script executed: rg "RDS|IAM|credential|token" --type go -i -B 2 -A 2 | head -50Repository: SpecterOps/DAWGS Length of output: 2751 🏁 Script executed: rg "credentional|credential" --type go -B 5 -A 10 drivers/pg/pg.goRepository: SpecterOps/DAWGS Length of output: 662 Several issues in the BeforeConnect hook.
Proposed fix (items 1–5)- poolCfg.BeforeConnect = func(ctx context.Context, poolCfg *pgx.ConnConfig) error {
- slog.Info(fmt.Sprint("RDS credentional beforeConnect(), creating new IAM credentials"))
- refreshConnectionString := cfg.PostgreSQLConnectionString()
- newPoolCfg, err := pgxpool.ParseConfig(refreshConnectionString)
- if err != nil {
- return err
- }
- poolCfg.Password = newPoolCfg.ConnConfig.Password
- return nil
- }
+ poolCfg.BeforeConnect = func(ctx context.Context, connCfg *pgx.ConnConfig) error {
+ slog.Debug("BeforeConnect: refreshing IAM credentials")
+ refreshConnStr := cfg.PostgreSQLConnectionString()
+ refreshCfg, err := pgx.ParseConfig(refreshConnStr)
+ if err != nil {
+ return fmt.Errorf("failed to parse refreshed connection string: %w", err)
+ }
+ connCfg.Password = refreshCfg.Password
+ return nil
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| pool, err := pgxpool.NewWithConfig(poolCtx, poolCfg) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: SpecterOps/DAWGS
Length of output: 78
🏁 Script executed:
Repository: SpecterOps/DAWGS
Length of output: 79
🏁 Script executed:
Repository: SpecterOps/DAWGS
Length of output: 402
🏁 Script executed:
Repository: SpecterOps/DAWGS
Length of output: 3664
🏁 Script executed:
Repository: SpecterOps/DAWGS
Length of output: 3021
Cache static connection parameters during pool setup; refresh only auth tokens in
BeforeConnect.PostgreSQLConnectionString()loads AWS config (line 35 in drivers/config.go) and performs DNS lookup vianet.LookupCNAME()(line 40) on every invocation. When called insideBeforeConnectfor each new connection, this amplifies latency and failure surface under pool churn. Extract host, port, and database once duringNewPool(), then haveBeforeConnectuse onlyauth.BuildAuthToken()to generate the fresh time-limited credential.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added LookupEndpoint() to run before newpool creation and if endpoint field is empty. This should reduce the number of lookups, while still allowing IAM PostgreSQLConnectionString() calls outside of pool context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 3340
@mamundsen-specter, the fix looks good — the DNS concern from the original comment is now fully addressed. Here's the breakdown:CNAME lookup — ✅ resolved
cfg.Endpoint = cfg.LookupEndpoint()is called once before the closure is created. Sincecfgis a value type andEndpointis pre-populated,RDSIAMAuthConnectionString()will take theif s.Endpoint != ""branch (line 41–42 ofconfig.go) on every subsequentBeforeConnectcall, skipping thenet.LookupCNAMEentirely.awsConfig.LoadDefaultConfig— still called per connectionRDSIAMAuthConnectionString()still invokesawsConfig.LoadDefaultConfig(context.TODO())on everyBeforeConnect. This is the remaining overhead, but it's likely intentional and acceptable — reloading the AWS config on each call allows credential rotation (IMDS, IAM role assumption, env-var changes) to be picked up without restarting the pool. Caching it would risk using stale credentials for token generation.The fix strikes a reasonable balance: the expensive/durable operation (CNAME lookup) is cached, while the security-sensitive operation (credential + token refresh) stays live per-connection.
✏️ Learnings added
🧠 Learnings used