Skip to content
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

Connection String works for psql but not for sqlx #1716

Closed
libardoabella opened this issue Feb 19, 2022 · 2 comments
Closed

Connection String works for psql but not for sqlx #1716

libardoabella opened this issue Feb 19, 2022 · 2 comments

Comments

@libardoabella
Copy link

I've got a PostgreSQL instance set up to only accept encrypted connections with certificates.

I am able to connect to it via psql just fine:

psql "postgresql://my_db_user@localhost:5432/my_db_name?sslmode=verify-ca&sslrootcert=root.crt&sslkey=pg_client.key&sslcert=pg_client.crt"

The session starts without issues:

psql (13.5 (Ubuntu 13.5-0ubuntu0.21.10.1), server 14.2 (Ubuntu 14.2-1.pgdg20.04+1))
WARNING: psql major version 13, server major version 14.
         Some psql features might not work.
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

my_db_name=#

However, it doesn't play as nicely in sqlx land:

Cargo.toml

[package]
name = "backend"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sqlx = { version = "0.5", features = [ "runtime-tokio-native-tls", "postgres" ] }
tokio = { version = "1", features = ["full"] }

main.rs --slighly adapted from the README.md example:

use sqlx::postgres::PgPoolOptions;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    assert!(Path::new("root.crt").exists());
    assert!(Path::new("pg_client.key").exists());
    assert!(Path::new("pg_client.crt").exists());

    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(
            "postgresql://my_db_udser@localhost:5432/my_db_name?sslmode=verify-ca&sslrootcert=root.crt&sslkey=pg_client.key&sslcert=pg_client.crt")
        .await?;

    // Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL)
    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool)
        .await?;

    assert_eq!(row.0, 150);

    Ok(())
}

produces the error:

Error: Database(PgDatabaseError { severity: Fatal, code: "28000", message: "connection requires a valid client certificate", detail: None, hint: None, position: None, where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("auth.c"), line: Some(429), routine: Some("ClientAuthentication") })

What could be causing this?

@abonander
Copy link
Collaborator

Likely #1162

@abonander
Copy link
Collaborator

Closing as duplicate of the above.

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

No branches or pull requests

2 participants