-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.rs
219 lines (200 loc) · 8 KB
/
config.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::{CLIENT, MAuthInfo};
use mauth_core::signer::Signer;
use reqwest::Client;
use reqwest::Url;
use reqwest_middleware::ClientBuilder;
use serde::Deserialize;
use std::io;
use thiserror::Error;
use uuid::Uuid;
const CONFIG_FILE: &str = ".mauth_config.yml";
impl MAuthInfo {
/// Construct the MAuthInfo struct based on the contents of the config file `.mauth_config.yml`
/// present in the current user's home directory. Returns an enum error type that includes the
/// error types of all crates used.
pub fn from_default_file() -> Result<MAuthInfo, ConfigReadError> {
Self::from_config_section(&Self::config_section_from_default_file()?)
}
pub(crate) fn config_section_from_default_file() -> Result<ConfigFileSection, ConfigReadError> {
let mut home = dirs::home_dir().unwrap();
home.push(CONFIG_FILE);
let config_data = std::fs::read_to_string(&home)?;
let config_data_value: serde_yml::Value = serde_yml::from_slice(&config_data.into_bytes())?;
let common_section = config_data_value
.get("common")
.ok_or(ConfigReadError::InvalidFile(None))?;
let common_section_typed: ConfigFileSection =
serde_yml::from_value(common_section.clone())?;
Ok(common_section_typed)
}
/// Construct the MAuthInfo struct based on a passed-in ConfigFileSection instance. The
/// optional input_keystore is present to support internal cloning and need not be provided
/// if being used outside of the crate.
pub fn from_config_section(section: &ConfigFileSection) -> Result<MAuthInfo, ConfigReadError> {
let full_uri: Url = format!(
"{}/mauth/{}/security_tokens/",
§ion.mauth_baseurl, §ion.mauth_api_version
)
.parse()?;
let mut pk_data = section.private_key_data.clone();
if pk_data.is_none() && section.private_key_file.is_some() {
pk_data = Some(std::fs::read_to_string(
section.private_key_file.as_ref().unwrap(),
)?);
}
if pk_data.is_none() {
return Err(ConfigReadError::NoPrivateKey);
}
let mauth_info = MAuthInfo {
app_id: Uuid::parse_str(§ion.app_uuid)?,
mauth_uri_base: full_uri,
sign_with_v1_also: !section.v2_only_sign_requests.unwrap_or(false),
allow_v1_auth: !section.v2_only_authenticate.unwrap_or(false),
signer: Signer::new(section.app_uuid.clone(), pk_data.unwrap())?,
};
CLIENT.get_or_init(|| {
let builder = ClientBuilder::new(Client::new()).with(mauth_info.clone());
#[cfg(any(
feature = "tracing-otel-26",
feature = "tracing-otel-27",
feature = "tracing-otel-28"
))]
let builder = builder.with(reqwest_tracing::TracingMiddleware::default());
builder.build()
});
Ok(mauth_info)
}
}
/// All of the configuration data needed to set up a MAuthInfo struct. Implements Deserialize
/// to be read from a YAML file easily, or can be created manually.
#[derive(Deserialize, Clone)]
pub struct ConfigFileSection {
pub app_uuid: String,
pub mauth_baseurl: String,
pub mauth_api_version: String,
pub private_key_file: Option<String>,
pub private_key_data: Option<String>,
pub v2_only_sign_requests: Option<bool>,
pub v2_only_authenticate: Option<bool>,
}
impl Default for ConfigFileSection {
fn default() -> Self {
Self {
app_uuid: "".to_string(),
mauth_baseurl: "".to_string(),
mauth_api_version: "v1".to_string(),
private_key_file: None,
private_key_data: None,
v2_only_sign_requests: Some(true),
v2_only_authenticate: Some(true),
}
}
}
/// All of the possible errors that can take place when attempting to read a config file. Errors
/// are specific to the libraries that created them, and include the details from those libraries.
#[derive(Debug, Error)]
pub enum ConfigReadError {
#[error("File Read Error: {0}")]
FileReadError(#[from] io::Error),
#[error("Not a valid maudit config file: {0:?}")]
InvalidFile(Option<serde_yml::Error>),
#[error("MAudit URI not valid: {0}")]
InvalidUri(#[from] url::ParseError),
#[error("App UUID not valid: {0}")]
InvalidAppUuid(#[from] uuid::Error),
#[error("Unable to parse RSA private key: {0}")]
PrivateKeyDecodeError(String),
#[error("Neither private_key_file nor private_key_data were provided")]
NoPrivateKey,
}
impl From<mauth_core::error::Error> for ConfigReadError {
fn from(err: mauth_core::error::Error) -> ConfigReadError {
match err {
mauth_core::error::Error::PrivateKeyDecodeError(pkey_err) => {
ConfigReadError::PrivateKeyDecodeError(format!("{}", pkey_err))
}
_ => panic!("should not be possible to get this error type from signer construction"),
}
}
}
impl From<serde_yml::Error> for ConfigReadError {
fn from(err: serde_yml::Error) -> ConfigReadError {
ConfigReadError::InvalidFile(Some(err))
}
}
#[cfg(test)]
mod test {
use super::*;
use tokio::fs;
#[tokio::test]
async fn invalid_uri_returns_right_error() {
let bad_config = ConfigFileSection {
app_uuid: "".to_string(),
mauth_baseurl: "dfaedfaewrfaew".to_string(),
mauth_api_version: "".to_string(),
private_key_file: Some("".to_string()),
private_key_data: None,
v2_only_sign_requests: None,
v2_only_authenticate: None,
};
let load_result = MAuthInfo::from_config_section(&bad_config);
assert!(matches!(load_result, Err(ConfigReadError::InvalidUri(_))));
}
#[tokio::test]
async fn bad_file_path_returns_right_error() {
let bad_config = ConfigFileSection {
app_uuid: "".to_string(),
mauth_baseurl: "https://example.com/".to_string(),
mauth_api_version: "v1".to_string(),
private_key_file: Some("no_such_file".to_string()),
private_key_data: None,
v2_only_sign_requests: None,
v2_only_authenticate: None,
};
let load_result = MAuthInfo::from_config_section(&bad_config);
assert!(matches!(
load_result,
Err(ConfigReadError::FileReadError(_))
));
}
#[tokio::test]
async fn bad_key_file_returns_right_error() {
let filename = "dummy_file";
fs::write(&filename, b"definitely not a key").await.unwrap();
let bad_config = ConfigFileSection {
app_uuid: "c7db7fde-2448-11ef-b358-125eb8485a60".to_string(),
mauth_baseurl: "https://example.com/".to_string(),
mauth_api_version: "v1".to_string(),
private_key_file: Some(filename.to_string()),
private_key_data: None,
v2_only_sign_requests: None,
v2_only_authenticate: None,
};
let load_result = MAuthInfo::from_config_section(&bad_config);
fs::remove_file(&filename).await.unwrap();
assert!(matches!(
load_result,
Err(ConfigReadError::PrivateKeyDecodeError(_))
));
}
#[tokio::test]
async fn bad_uuid_returns_right_error() {
let filename = "valid_key_file";
fs::write(&filename, "invalid data").await.unwrap();
let bad_config = ConfigFileSection {
app_uuid: "".to_string(),
mauth_baseurl: "https://example.com/".to_string(),
mauth_api_version: "v1".to_string(),
private_key_file: Some(filename.to_string()),
private_key_data: None,
v2_only_sign_requests: None,
v2_only_authenticate: None,
};
let load_result = MAuthInfo::from_config_section(&bad_config);
fs::remove_file(&filename).await.unwrap();
assert!(matches!(
load_result,
Err(ConfigReadError::InvalidAppUuid(_))
));
}
}