Skip to content

Replace PostgresqlConnector with ConnectionStringProvider#18

Open
andrew-corbalt wants to merge 12 commits intomainfrom
acremins-rds-iam-dsn
Open

Replace PostgresqlConnector with ConnectionStringProvider#18
andrew-corbalt wants to merge 12 commits intomainfrom
acremins-rds-iam-dsn

Conversation

@andrew-corbalt
Copy link
Contributor

@andrew-corbalt andrew-corbalt commented Dec 23, 2025

PR Description

To simplify handling both RDS IAM auth and conventional password auth postgres connections, PostgresqlConnector is replaced with ConnectionStringProvider which adds the concept of a custom DSN postgres+rds-iam://<user>@<host>[:<port>]/<dbname> for IAM auth. Alternatively, a normal postgres DSN can be used with conventional password auth.

Tests:
Updated the ProfessorMAC app and microservices to use ConnectionStringProvider and verified function

PR Checklist

  • New automated tests have been written to the extent possible.
  • The code has been checked for structural/syntactic validity.
    • AMI/application: a build was performed
    • terraform changes: "terraform plan" checked on every affected environment
  • (If applicable) the code has been manually tested on our infrastructure.
    • AMI/application: deployed an a test or dev environment
    • terraform changes: applied to test or dev environment
    • script: run against test or dev environment
  • Likely failure points and new functionality have been identified and tested manually.
    Examples:
    • Application manually run in a way that triggers any new branches
    • AMI logged into and changes verified from login shell
  • Pull request description includes a description of all the manual steps performed to accomplish the above.

To provide feedback on this template, visit https://docs.google.com/document/d/1YfTv7Amyop5G_8w1c2GJ_Mu-70L0KkZHhm9f9umDi3U/edit

jonahb
jonahb previously requested changes Dec 29, 2025
Copy link

@jonahb jonahb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. I like the new approach. I guess it'd also be worth thinking about whether we want to tweak things per Slack thread and how this change might fit into that.

// - assume_role_session_name: only used when assume_role_arn is set; defaults to "pgutils-rds-iam" if omitted.
//
// IAM example 2: postgres+rds-iam://<user>@<host>[:<port>]/<dbname>?assume_role_arn=...&assume_role_session_name=...
func NewPostgresqlConnectorFromDSN(ctx context.Context, dsn string) (*PostgresqlConnector, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function only accepts URLs (not any DSN / connection string), so I think we should make that clear, e.g.:

func NewPostgresqlConnectorFromURL(ctx context.Context, u *url.URL) (*PostgresqlConnector, error)

We could also provide a helper that parses the URL string (NewPostgresqlConnectorFromURLString).

cfg.AssumeRoleSessionName = q.Get("assume_role_session_name")
}

return NewPostgresqlConnectorWithIAMAuth(ctx, cfg)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good while clients are changing to connection strings (DSNs), but per engineering sync, the plan is eventually to remove the IAM and connection string-specific constructors.

Copy link
Contributor

@hundt-corbalt hundt-corbalt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

return db
}

func parseURL(rawURL string) (*url.URL, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this wrapper do anything useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its only used in 2 places right now. We could just remove the function and do the parsing checks in each function. What do you think?

return str
}

func CensorDSN(dsn string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For usability, what do you think about not returning an error (only string) and if the URL is unparseable you can just return "<cannot parse URL to censor>" or something like that? Then callers can avoid the hassle of checking the return value and can use this return value directly as an argument to a logging call. (In the extremely unlikely event that they pass an invalid URL, all that will happen is the logs will be less useful.) Maybe rename to CensorDSNForLogs to make clear that you should not try to use this string for anything else.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost did exactly that, but since the original version was already approved in a PR I decided to go that direction. Also I like the idea of renaming it to CensorDSNForLogs() which I think makes it much more appropriate to return an error string. Lets change to this suggestion!

@andrew-corbalt andrew-corbalt changed the title Add NewPostgresqlConnectorFromDSN() to pgutils Replace PostgresqlConnector with ConnectionStringProvider Feb 10, 2026
Copy link
Contributor

@hundt-corbalt hundt-corbalt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM! But PR description still refers to an older version of the code. Can you update that?

@andrew-corbalt
Copy link
Contributor Author

Code LGTM! But PR description still refers to an older version of the code. Can you update that?

done

@andrew-corbalt andrew-corbalt dismissed jonahb’s stale review February 11, 2026 19:01

Code has been rewritten since the requested change was submitted. Feel free to re-review the new version that was made with Chris' input!

Copy link
Contributor

@hundt-corbalt hundt-corbalt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants