Skip to content

Commit 04eb69a

Browse files
authored
Merge pull request #30 from hrvolapeter/tweaks
Tweaks
2 parents b70c312 + 6f05da7 commit 04eb69a

File tree

15 files changed

+186
-98
lines changed

15 files changed

+186
-98
lines changed

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: cargo
4+
directory: "/"
5+
schedule:
6+
interval: weekly
7+
time: "13:00"
8+
open-pull-requests-limit: 99

.github/workflows/rust.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: ['main']
6+
pull_request:
7+
schedule:
8+
- cron: "25 6 * * 5"
9+
10+
jobs:
11+
test:
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest, macos-latest]
15+
rust: [stable, beta]
16+
exclude:
17+
- os: macos-latest
18+
rust: beta
19+
20+
runs-on: ${{ matrix.os }}
21+
22+
steps:
23+
- uses: actions/checkout@v2
24+
- uses: actions-rs/toolchain@v1
25+
with:
26+
profile: minimal
27+
toolchain: ${{ matrix.rust }}
28+
override: true
29+
- uses: actions-rs/cargo@v1
30+
with:
31+
command: check
32+
args: --all-features --all-targets
33+
34+
lint:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v2
38+
- uses: actions-rs/toolchain@v1
39+
with:
40+
profile: minimal
41+
toolchain: stable
42+
override: true
43+
components: rustfmt, clippy
44+
- uses: actions-rs/cargo@v1
45+
with:
46+
command: fmt
47+
args: --all -- --check
48+
- uses: actions-rs/cargo@v1
49+
if: always()
50+
with:
51+
command: clippy
52+
args: --workspace --all-targets --all-features -- -D warnings
53+
54+
audit:
55+
runs-on: ubuntu-latest
56+
steps:
57+
- uses: actions/checkout@v2
58+
- uses: EmbarkStudios/cargo-deny-action@v1

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ url = "2"
2929
async-trait = "0.1"
3030
thiserror = "1.0"
3131
dirs-next = "2.0"
32+
33+
[dev-dependencies]
34+
env_logger = "0.9"
35+
tokio = { version = "1.1", features = ["macros", "rt-multi-thread"] }

deny.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[licenses]
2+
allow-osi-fsf-free = "either"
3+
copyleft = "deny"
4+
allow = ["MPL-2.0"]
5+
6+
[[licenses.clarify]]
7+
name = "ring"
8+
expression = "ISC AND MIT AND OpenSSL"
9+
license-files = [{ path = "LICENSE", hash = 0xbd0eed23 }]

examples/Cargo.toml

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/simple.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
async fn main() -> Result<(), Box<dyn std::error::Error>> {
33
env_logger::init();
44
let authentication_manager = gcp_auth::init().await?;
5-
let _token = authentication_manager.get_token(&["https://www.googleapis.com/auth/cloud-platform"]).await?;
5+
let _token = authentication_manager
6+
.get_token(&["https://www.googleapis.com/auth/cloud-platform"])
7+
.await?;
68
Ok(())
79
}

src/authentication_manager.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use crate::prelude::*;
1+
use async_trait::async_trait;
2+
3+
use crate::error::Error;
4+
use crate::types::{HyperClient, Token};
25

36
#[async_trait]
4-
pub trait ServiceAccount: Send + Sync {
7+
pub(crate) trait ServiceAccount: Send + Sync {
58
async fn project_id(&self, client: &HyperClient) -> Result<String, Error>;
69
fn get_token(&self, scopes: &[&str]) -> Option<Token>;
710
async fn refresh_token(&self, client: &HyperClient, scopes: &[&str]) -> Result<Token, Error>;

src/custom_service_account.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
use crate::authentication_manager::ServiceAccount;
2-
use crate::error::Error::AplicationProfileMissing;
3-
use crate::prelude::*;
1+
use std::collections::HashMap;
2+
use std::path::Path;
43
use std::sync::RwLock;
4+
5+
use async_trait::async_trait;
6+
use serde::{Deserialize, Serialize};
57
use tokio::fs;
68

9+
use crate::authentication_manager::ServiceAccount;
10+
use crate::error::Error;
11+
use crate::types::{HyperClient, Token};
12+
use crate::util::HyperExt;
13+
714
#[derive(Debug)]
8-
pub struct CustomServiceAccount {
15+
pub(crate) struct CustomServiceAccount {
916
tokens: RwLock<HashMap<Vec<String>, Token>>,
1017
credentials: ApplicationCredentials,
1118
}
1219

1320
impl CustomServiceAccount {
14-
pub async fn from_file(path: &str) -> Result<Self, Error> {
15-
let credentials = ApplicationCredentials::from_file(path).await?;
21+
pub(crate) async fn from_file(path: &Path) -> Result<Self, Error> {
1622
Ok(Self {
17-
credentials,
23+
credentials: ApplicationCredentials::from_file(path).await?,
1824
tokens: RwLock::new(HashMap::new()),
1925
})
2026
}
21-
22-
pub async fn from_env() -> Result<Self, Error> {
23-
const GOOGLE_APPLICATION_CREDENTIALS: &str = "GOOGLE_APPLICATION_CREDENTIALS";
24-
let path =
25-
std::env::var(GOOGLE_APPLICATION_CREDENTIALS).map_err(|_| AplicationProfileMissing)?;
26-
CustomServiceAccount::from_file(&path).await
27-
}
2827
}
2928

3029
#[async_trait]
@@ -43,12 +42,12 @@ impl ServiceAccount for CustomServiceAccount {
4342

4443
async fn refresh_token(&self, client: &HyperClient, scopes: &[&str]) -> Result<Token, Error> {
4544
use crate::jwt::Claims;
46-
use crate::jwt::JWTSigner;
45+
use crate::jwt::JwtSigner;
4746
use crate::jwt::GRANT_TYPE;
4847
use hyper::header;
4948
use url::form_urlencoded;
5049

51-
let signer = JWTSigner::new(&self.credentials.private_key)?;
50+
let signer = JwtSigner::new(&self.credentials.private_key)?;
5251

5352
let claims = Claims::new(&self.credentials, scopes, None);
5453
let signed = signer.sign_claims(&claims).map_err(Error::TLSError)?;
@@ -73,33 +72,33 @@ impl ServiceAccount for CustomServiceAccount {
7372
}
7473

7574
#[derive(Serialize, Deserialize, Debug, Clone)]
76-
pub struct ApplicationCredentials {
77-
pub r#type: Option<String>,
75+
pub(crate) struct ApplicationCredentials {
76+
pub(crate) r#type: Option<String>,
7877
/// project_id
79-
pub project_id: Option<String>,
78+
pub(crate) project_id: Option<String>,
8079
/// private_key_id
81-
pub private_key_id: Option<String>,
80+
pub(crate) private_key_id: Option<String>,
8281
/// private_key
83-
pub private_key: String,
82+
pub(crate) private_key: String,
8483
/// client_email
85-
pub client_email: String,
84+
pub(crate) client_email: String,
8685
/// client_id
87-
pub client_id: Option<String>,
86+
pub(crate) client_id: Option<String>,
8887
/// auth_uri
89-
pub auth_uri: Option<String>,
88+
pub(crate) auth_uri: Option<String>,
9089
/// token_uri
91-
pub token_uri: String,
90+
pub(crate) token_uri: String,
9291
/// auth_provider_x509_cert_url
93-
pub auth_provider_x509_cert_url: Option<String>,
92+
pub(crate) auth_provider_x509_cert_url: Option<String>,
9493
/// client_x509_cert_url
95-
pub client_x509_cert_url: Option<String>,
94+
pub(crate) client_x509_cert_url: Option<String>,
9695
}
9796

9897
impl ApplicationCredentials {
9998
async fn from_file<T: AsRef<Path>>(path: T) -> Result<ApplicationCredentials, Error> {
10099
let content = fs::read_to_string(path)
101100
.await
102-
.map_err(Error::AplicationProfilePath)?;
103-
Ok(serde_json::from_str(&content).map_err(Error::AplicationProfileFormat)?)
101+
.map_err(Error::ApplicationProfilePath)?;
102+
Ok(serde_json::from_str(&content).map_err(Error::ApplicationProfileFormat)?)
104103
}
105104
}

src/default_authorized_user.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
use crate::authentication_manager::ServiceAccount;
2-
use crate::prelude::*;
3-
use hyper::body::Body;
4-
use hyper::Method;
1+
use std::path::Path;
52
use std::sync::RwLock;
3+
4+
use async_trait::async_trait;
5+
use hyper::body::Body;
6+
use hyper::{Method, Request};
7+
use serde::{Deserialize, Serialize};
68
use tokio::fs;
79

10+
use crate::authentication_manager::ServiceAccount;
11+
use crate::error::Error;
12+
use crate::types::{HyperClient, Token};
13+
use crate::util::HyperExt;
14+
815
#[derive(Debug)]
9-
pub struct DefaultAuthorizedUser {
16+
pub(crate) struct DefaultAuthorizedUser {
1017
token: RwLock<Token>,
1118
}
1219

@@ -15,7 +22,7 @@ impl DefaultAuthorizedUser {
1522
const USER_CREDENTIALS_PATH: &'static str =
1623
".config/gcloud/application_default_credentials.json";
1724

18-
pub async fn new(client: &HyperClient) -> Result<Self, Error> {
25+
pub(crate) async fn new(client: &HyperClient) -> Result<Self, Error> {
1926
let token = RwLock::new(Self::get_token(client).await?);
2027
Ok(Self { token })
2128
}
@@ -33,10 +40,8 @@ impl DefaultAuthorizedUser {
3340
log::debug!("Loading user credentials file");
3441
let mut home = dirs_next::home_dir().ok_or(Error::NoHomeDir)?;
3542
home.push(Self::USER_CREDENTIALS_PATH);
36-
let cred =
37-
UserCredentials::from_file(home.display().to_string())
38-
.await?;
39-
let req = Self::build_token_request(&RerfeshRequest {
43+
let cred = UserCredentials::from_file(home.display().to_string()).await?;
44+
let req = Self::build_token_request(&RefreshRequest {
4045
client_id: cred.client_id,
4146
client_secret: cred.client_secret,
4247
grant_type: "refresh_token".to_string(),
@@ -70,7 +75,7 @@ impl ServiceAccount for DefaultAuthorizedUser {
7075
}
7176

7277
#[derive(Serialize, Debug)]
73-
struct RerfeshRequest {
78+
struct RefreshRequest {
7479
client_id: String,
7580
client_secret: String,
7681
grant_type: String,
@@ -80,13 +85,13 @@ struct RerfeshRequest {
8085
#[derive(Serialize, Deserialize, Debug, Clone)]
8186
struct UserCredentials {
8287
/// Client id
83-
pub client_id: String,
88+
pub(crate) client_id: String,
8489
/// Client secret
85-
pub client_secret: String,
90+
pub(crate) client_secret: String,
8691
/// Refresh Token
87-
pub refresh_token: String,
92+
pub(crate) refresh_token: String,
8893
/// Type
89-
pub r#type: String,
94+
pub(crate) r#type: String,
9095
}
9196

9297
impl UserCredentials {

src/default_service_account.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
use crate::authentication_manager::ServiceAccount;
2-
use crate::prelude::*;
3-
use hyper::body::Body;
4-
use hyper::Method;
51
use std::str;
62
use std::sync::RwLock;
73

4+
use async_trait::async_trait;
5+
use hyper::body::Body;
6+
use hyper::{Method, Request};
7+
8+
use crate::authentication_manager::ServiceAccount;
9+
use crate::error::Error;
10+
use crate::types::{HyperClient, Token};
11+
use crate::util::HyperExt;
12+
813
#[derive(Debug)]
9-
pub struct DefaultServiceAccount {
14+
pub(crate) struct DefaultServiceAccount {
1015
token: RwLock<Token>,
1116
}
1217

@@ -15,7 +20,7 @@ impl DefaultServiceAccount {
1520
"http://metadata.google.internal/computeMetadata/v1/project/project-id";
1621
const DEFAULT_TOKEN_GCP_URI: &'static str = "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token";
1722

18-
pub async fn new(client: &HyperClient) -> Result<Self, Error> {
23+
pub(crate) async fn new(client: &HyperClient) -> Result<Self, Error> {
1924
let token = RwLock::new(Self::get_token(client).await?);
2025
Ok(Self { token })
2126
}

0 commit comments

Comments
 (0)