-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcustom_service_account.rs
175 lines (152 loc) · 5.92 KB
/
custom_service_account.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::authentication_manager::{ServiceAccount, TokenStyle};
use crate::error::Error;
use crate::types::{HyperClient, Signer, Token};
use crate::util::HyperExt;
/// A custom service account containing credentials
///
/// Once initialized, a [`CustomServiceAccount`] can be converted into an [`AuthenticationManager`]
/// using the applicable `From` implementation.
///
/// [`AuthenticationManager`]: crate::AuthenticationManager
#[derive(Debug)]
pub struct CustomServiceAccount {
credentials: ApplicationCredentials,
signer: Signer,
tokens: RwLock<HashMap<Vec<String>, Token>>,
}
impl CustomServiceAccount {
/// Check `GOOGLE_APPLICATION_CREDENTIALS` environment variable for a path to JSON credentials
pub fn from_env() -> Result<Option<Self>, Error> {
std::env::var_os("GOOGLE_APPLICATION_CREDENTIALS")
.map(|path| {
tracing::debug!(
"Reading credentials file from GOOGLE_APPLICATION_CREDENTIALS env var"
);
Self::from_file(PathBuf::from(path))
})
.transpose()
}
/// Read service account credentials from the given JSON file
pub fn from_file<T: AsRef<Path>>(path: T) -> Result<Self, Error> {
let file = std::fs::File::open(path.as_ref()).map_err(Error::CustomServiceAccountPath)?;
match serde_json::from_reader::<_, ApplicationCredentials>(file) {
Ok(credentials) => Self::new(credentials),
Err(e) => Err(Error::CustomServiceAccountCredentials(e)),
}
}
/// Read service account credentials from the given JSON string
pub fn from_json(s: &str) -> Result<Self, Error> {
match serde_json::from_str::<ApplicationCredentials>(s) {
Ok(credentials) => Self::new(credentials),
Err(e) => Err(Error::CustomServiceAccountCredentials(e)),
}
}
fn new(credentials: ApplicationCredentials) -> Result<Self, Error> {
Ok(Self {
signer: Signer::new(&credentials.private_key)?,
credentials,
tokens: RwLock::new(HashMap::new()),
})
}
/// The RSA PKCS1 SHA256 [`Signer`] used to sign JWT tokens
pub fn signer(&self) -> &Signer {
&self.signer
}
/// The project ID as found in the credentials
pub fn project_id(&self) -> Option<&str> {
self.credentials.project_id.as_deref()
}
/// The private key as found in the credentials
pub fn private_key_pem(&self) -> &str {
&self.credentials.private_key
}
}
#[async_trait]
impl ServiceAccount for CustomServiceAccount {
fn get_style(&self) -> TokenStyle {
TokenStyle::AccountAndScopes
}
async fn project_id(&self, _: &HyperClient) -> Result<String, Error> {
match &self.credentials.project_id {
Some(pid) => Ok(pid.clone()),
None => Err(Error::ProjectIdNotFound),
}
}
fn get_token(&self, scopes: &[&str]) -> Option<Token> {
let key: Vec<_> = scopes.iter().map(|x| x.to_string()).collect();
self.tokens.read().unwrap().get(&key).cloned()
}
#[tracing::instrument]
async fn refresh_token(&self, client: &HyperClient, scopes: &[&str]) -> Result<Token, Error> {
use crate::jwt::Claims;
use crate::jwt::GRANT_TYPE;
use hyper::header;
use url::form_urlencoded;
let jwt = Claims::new(&self.credentials, scopes, None).to_jwt(&self.signer)?;
let rqbody = form_urlencoded::Serializer::new(String::new())
.extend_pairs(&[("grant_type", GRANT_TYPE), ("assertion", jwt.as_str())])
.finish();
let mut retries = 0;
let response = loop {
let request = hyper::Request::post(&self.credentials.token_uri)
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
.body(hyper::Body::from(rqbody.clone()))
.unwrap();
tracing::debug!("requesting token from service account: {request:?}");
let err = match client.request(request).await {
// Early return when the request succeeds
Ok(response) => break response,
Err(err) => err,
};
tracing::warn!(
"Failed to refresh token with GCP oauth2 token endpoint: {err}, trying again..."
);
retries += 1;
if retries >= RETRY_COUNT {
return Err(Error::OAuthConnectionError(err));
}
};
let token = response.deserialize::<Token>().await?;
let key = scopes.iter().map(|x| (*x).to_string()).collect();
self.tokens.write().unwrap().insert(key, token.clone());
Ok(token)
}
}
#[derive(Serialize, Deserialize, Clone)]
pub(crate) struct ApplicationCredentials {
pub(crate) r#type: Option<String>,
/// project_id
pub(crate) project_id: Option<String>,
/// private_key_id
pub(crate) private_key_id: Option<String>,
/// private_key
pub(crate) private_key: String,
/// client_email
pub(crate) client_email: String,
/// client_id
pub(crate) client_id: Option<String>,
/// auth_uri
pub(crate) auth_uri: Option<String>,
/// token_uri
pub(crate) token_uri: String,
/// auth_provider_x509_cert_url
pub(crate) auth_provider_x509_cert_url: Option<String>,
/// client_x509_cert_url
pub(crate) client_x509_cert_url: Option<String>,
}
impl fmt::Debug for ApplicationCredentials {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ApplicationCredentials")
.field("client_email", &self.client_email)
.field("project_id", &self.project_id)
.finish()
}
}
/// How many times to attempt to fetch a token from the set credentials token endpoint.
const RETRY_COUNT: u8 = 5;