1use configparser::ini::Ini;
2use openssl::pkey::{PKey, Private};
3use openssl::rsa::Rsa;
4use std::fs;
5
6pub struct AuthConfig {
7 pub user: String,
8 pub fingerprint: String,
9 pub tenancy: String,
10 pub region: String,
11 pub keypair: PKey<Private>,
12}
13
14impl AuthConfig {
15 pub fn new(
16 user: String,
17 key_file: String,
18 fingerprint: String,
19 tenancy: String,
20 region: String,
21 passphrase: String,
22 ) -> AuthConfig {
23 let key = fs::read_to_string(&key_file).expect("key_file doest not exists");
24
25 let keypair =
26 Rsa::private_key_from_pem_passphrase(key.as_bytes(), passphrase.as_bytes()).unwrap();
27 let keypair = PKey::from_rsa(keypair).unwrap();
28
29 return AuthConfig {
30 user,
31 fingerprint,
32 tenancy,
33 region,
34 keypair,
35 };
36 }
37
38 pub fn from_file(file_path: Option<String>, profile_name: Option<String>) -> AuthConfig {
39 let fp;
40 let pn = profile_name.unwrap_or("DEFAULT".to_string());
41
42 if file_path.is_none() {
43 let home_dir_path = home::home_dir().expect("Impossible to get your home dir!");
44
45 fp = format!(
46 "{}/.oci/config",
47 home_dir_path.to_str().expect("null value")
48 );
49 } else {
50 fp = file_path.expect("file path is not string");
51 }
52
53 let config_content =
54 fs::read_to_string(&fp).expect(&format!("config file '{}' doest not exists", fp));
55
56 let mut config = Ini::new();
57 config
58 .read(String::from(config_content))
59 .expect("invalid config file");
60
61 return AuthConfig::new(
62 config.get(&pn, "user").unwrap(),
63 config.get(&pn, "key_file").unwrap(),
64 config.get(&pn, "fingerprint").unwrap(),
65 config.get(&pn, "tenancy").unwrap(),
66 config.get(&pn, "region").unwrap(),
67 config.get(&pn, "passphrase").unwrap_or("".to_string()),
68 );
69 }
70}