How to use an AWS RDS token as a password in postres uri connection string?

Viewed 347

I am using a tool that connects to a postgres database usinga URI string to connect. Normally this has worked fine for me by just doing postgres://<username>:<password>@<host>:<port>/<database>. However, I have moved my database to AWS RDS and now for the database password I am now using an auth token. The token looks like:

rdspostgres.cdgmuqiadpid.us-west-2.rds.amazonaws.com:5432/?Action=connect&DBUser=jane_doe&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=900...

When I put this in for the password it is not able to parse correctly because of the token format. For example i get the error psql: invalid URI query parameter: "Action". How do I use tokens as passwords for a string? Is there a way I need to encode it?

Also worth noting that for sanity check I tried connecting with psql -d $DATABASE -p $PORT -U $USER --no-password, having set $PGPASSWORD as an environment variable. This worked great and I was able to connect, so it has to be something with parsing the password string.

2 Answers

I run into the same issue and was able to solve it using urlencoded-byte-serializer. I'm using Rust, so the code looks the following:

fn urlencode(token: &str) -> String {
    url::form_urlencoded::byte_serialize(token.as_bytes()).collect()
}

Here is how the token looks after the encoding: my-proxy.proxy-qzf4e5cf6ve.us-east-1.rds.amazonaws.com%3A5432%2F%3FAction%3Dconnect%26DBUser%3Dusers%26X-Amz-Algorithm%3DAWS4-HMAC-SHA256...

Have you checked that the parameters you pass through the URI, have to go through an options section of your tool?

Maybe, your problem is not password. I think, your problem is the tool can't understand yours params. Because the error begins when begins yours params.

Check, your syntax. I am for example using DataGrip, and in this tool you need split each part of your URI and put in the correct label, like a form.

Related