Skip to content

Builder panics on malformed GCS private key instead of returning error #419

Description

@hugocasa

Describe the bug
When building a GCS client in object_store, if the provided private key is malformed, the builder panics instead of returning an error. This breaks expected behavior and makes error handling impossible.

To Reproduce

use object_store::gcp::GoogleCloudStorageBuilder;

#[tokio::main]
async fn main() {
    let client_builder = GoogleCloudStorageBuilder::new()
        .with_bucket_name("asd")
        .with_service_account_key("{\"private_key\": \"-----BEGIN PRIVATE KEY-----\", \"private_key_id\": \"hey\", \"client_email\": \"asd@asd.com\", \"type\": \"service_account\"}");

    if let Err(err) = client_builder.build() {
        println!("Error: {:?}", err);
    }
}
thread 'main' panicked at XXX/object_store-0.12.2/src/gcp/credential.rs:134:53:
called `Result::unwrap()` on an `Err` value: Custom { kind: InvalidData, error: "section end \"-----END PRIVATE KEY-----\" missing" }

Expected behavior
The builder should return an error if the private key is malformed, not panic.

Additional context
The panic occurs due to this code:

// Reading from string is infallible
match rustls_pemfile::read_one(&mut reader).unwrap() {
    Some(Item::Pkcs8Key(key)) => Self::from_pkcs8(key.secret_pkcs8_der()),
    Some(Item::Pkcs1Key(key)) => Self::from_der(key.secret_pkcs1_der()),
    _ => Err(Error::MissingKey),
}

The unwrap() assumes infallibility, which is incorrect for malformed inputs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions