Skip to content

Commit

Permalink
Move Error type into crate root
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed May 23, 2024
1 parent d98a0d2 commit c0636b2
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 114 deletions.
3 changes: 1 addition & 2 deletions src/custom_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use tokio::sync::RwLock;
use tracing::{instrument, Level};
use url::form_urlencoded;

use crate::error::Error;
use crate::types::{HttpClient, Signer, Token};
use crate::TokenProvider;
use crate::{Error, TokenProvider};

/// A custom service account containing credentials
///
Expand Down
3 changes: 1 addition & 2 deletions src/default_authorized_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use tracing::{instrument, Level};

use crate::error::Error;
use crate::types::{HttpClient, Token};
use crate::TokenProvider;
use crate::{Error, TokenProvider};

#[derive(Debug)]
pub(crate) struct ConfigDefaultCredentials {
Expand Down
3 changes: 1 addition & 2 deletions src/default_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use hyper::{Method, Request};
use tokio::sync::RwLock;
use tracing::{instrument, Level};

use crate::TokenProvider;
use crate::error::Error;
use crate::types::{HttpClient, Token};
use crate::{Error, TokenProvider};

#[derive(Debug)]
pub(crate) struct MetadataServiceAccount {
Expand Down
102 changes: 0 additions & 102 deletions src/error.rs

This file was deleted.

5 changes: 2 additions & 3 deletions src/gcloud_authorized_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use async_trait::async_trait;
use tokio::sync::RwLock;
use which::which;

use crate::error::Error;
use crate::error::Error::{GCloudError, GCloudNotFound, GCloudParseError};
use crate::types::Token;
use crate::TokenProvider;
use crate::Error::{GCloudError, GCloudNotFound, GCloudParseError};
use crate::{Error, TokenProvider};

#[derive(Debug)]
pub(crate) struct GCloudAuthorizedUser {
Expand Down
106 changes: 103 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
use std::sync::Arc;

use async_trait::async_trait;
use thiserror::Error;
use tracing::{instrument, Level};

mod custom_service_account;
Expand All @@ -102,9 +103,6 @@ use default_authorized_user::ConfigDefaultCredentials;
mod default_service_account;
use default_service_account::MetadataServiceAccount;

mod error;
pub use error::Error;

mod gcloud_authorized_user;
use gcloud_authorized_user::GCloudAuthorizedUser;

Expand Down Expand Up @@ -175,3 +173,105 @@ pub trait TokenProvider: Send + Sync {
/// Get the project ID for the authentication context
async fn project_id(&self) -> Result<Arc<str>, Error>;
}

/// Enumerates all possible errors returned by this library.
#[derive(Error, Debug)]
pub enum Error {
/// No available authentication method was discovered
///
/// Application can authenticate against GCP using:
///
/// - Default service account - available inside GCP platform using GCP Instance Metadata server
/// - GCloud authorized user - retrieved using `gcloud auth` command
///
/// All authentication methods have been tested and none succeeded.
/// Service account file can be downloaded from GCP in json format.
#[error("No available authentication method was discovered")]
NoAuthMethod(Box<Error>, Box<Error>, Box<Error>),

/// Error in underlying RustTLS library.
/// Might signal problem with establishing secure connection using trusted certificates
#[error("TLS error")]
TlsError(#[source] rustls::Error),

/// Error when establishing connection to OAuth server
#[error("Could not establish connection with OAuth server")]
OAuthConnectionError(#[source] hyper::Error),

/// Wrong path to custom service account credentials provided
///
/// By default, the custom service account credentials are parsed from the path pointed to by the
/// `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
#[error("Invalid path to custom service account")]
CustomServiceAccountPath(#[source] std::io::Error),

/// Failed to parse the application credentials provided
///
/// By default, the custom service account credentials are parsed from the path pointed to by the
/// `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
#[error("Unable to parse custom service account credentials")]
CustomServiceAccountCredentials(#[source] serde_json::error::Error),

/// Default user profile not found
///
/// User can authenticate locally during development using `gcloud auth login` which results in creating
/// `~/.config/gcloud/application_default_credentials.json` which couldn't be find on the machine
#[error("User authentication profile not found")]
UserProfilePath(#[source] std::io::Error),

/// Wrong format of user profile
#[error("User profile was not parsable")]
UserProfileFormat(#[source] serde_json::error::Error),

/// Could not connect to server
#[error("Could not establish connection with server")]
ConnectionError(#[from] hyper::Error),

/// Could not parse response from server
#[error("Could not parse server response")]
ParsingError(#[source] serde_json::error::Error),

/// Could not connect to server
#[error("Server unavailable: {0}")]
ServerUnavailable(String),

/// Could not sign requested message
#[error("Could not sign")]
SignerFailed,

/// Could not initialize signer
#[error("Couldn't initialize signer")]
SignerInit,

/// Could not find Home directory in the environment
#[error("Home directory not found")]
NoHomeDir,

/// Project ID not supported for current authentication method
#[error("Project ID not supported for current authentication method")]
NoProjectId,

/// Project ID not found through current authentication method
#[error("Project ID not found through current authentication method")]
ProjectIdNotFound,

/// Project ID is invalid UTF-8
#[error("Project ID is invalid UTF-8")]
ProjectIdNonUtf8,

/// GCloud executable not found
#[error("GCloud executable not found in $PATH")]
GCloudNotFound,

/// GCloud returned an error status
#[error("GCloud returned a non OK status")]
GCloudError,

/// GCloud output couldn't be parsed
#[error("Failed to parse output of GCloud")]
GCloudParseError,

/// Represents all other cases of `std::io::Error`.
#[error(transparent)]
IOError(#[from] std::io::Error),
}

0 comments on commit c0636b2

Please sign in to comment.