apicize_lib 0.35.4

Library supporting Apicize request dispatch, testing and serialization
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use std::{
    env, path::{self, Path, PathBuf}
};

use dirs::config_dir;
use serde::{Deserialize, Serialize};

use crate::{
    ApicizeError, Authorization, Certificate, Identifiable, ParameterLockStatus, Proxy, Scenario,
    SerializationSaveSuccess, Validated, delete_data_file, open_data_file, save_data_file,
};

/// Type of encyrption used to encrypt sensitive parameter info
#[derive(Copy, Clone, Default, Serialize, Deserialize, PartialEq)]
pub enum ParameterEncryption {
    #[default]
    Aes256Gcm,
}

/// Trait describing behaviors of encryptable parameters
pub trait EncryptableParameter: Sized {
    /// Returns True if the parameter is encrypted
    fn is_encrypted(&self) -> bool;
    /// Returns an encrypted copy of the parameter
    fn encrypt(&self, password: &str, method: ParameterEncryption) -> Result<Self, ApicizeError>;
    /// Returns a decrypted copy of the parameter
    fn decrypt(&self, password: &str, method: ParameterEncryption) -> Result<Self, ApicizeError>;
}

#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct ParameterCipher {
    /// Uniquely identifies entity
    pub id: String,
    /// Name of entity
    pub name: String,
    // Encrypted data of entity information
    pub data: String,
}

impl Identifiable for ParameterCipher {
    fn get_id(&self) -> &str {
        &self.id
    }

    fn get_name(&self) -> &str {
        &self.name
    }

    fn get_title(&self) -> String {
        if self.name.is_empty() {
            "(Unnamed)".to_string()
        } else {
            self.name.to_string()
        }
    }
}

/// Stored parameters, authorization, client certificates and proxies
/// stored outside of a workbook, either globally or alongside a workbook
#[derive(Serialize, Deserialize, PartialEq, Clone)]
pub struct Parameters {
    /// Version of  format (should not be changed manually)
    pub version: f32,

    /// Encryption
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encryption: Option<ParameterEncryption>,

    /// Scenarios
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scenarios: Option<Vec<Scenario>>,

    /// Authorizations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authorizations: Option<Vec<Authorization>>,

    /// Certificates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub certificates: Option<Vec<Certificate>>,

    /// Proxy servers
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proxies: Option<Vec<Proxy>>,
}

impl Default for Parameters {
    fn default() -> Self {
        Self {
            version: 1.0,
            encryption: Default::default(),
            scenarios: Default::default(),
            authorizations: Default::default(),
            certificates: Default::default(),
            proxies: Default::default(),
        }
    }
}

impl Parameters {
    pub fn new(
        scenarios: Option<Vec<Scenario>>,
        authorizations: Option<Vec<Authorization>>,
        certificates: Option<Vec<Certificate>>,
        proxies: Option<Vec<Proxy>>,
    ) -> Self {
        Self {
            version: 1.0,
            encryption: Default::default(),
            scenarios,
            authorizations,
            certificates,
            proxies,
        }
    }

    /// Open parameters from parameters file; or, if specified, create default if parameters file does not exist
    pub fn open(
        file_name: &PathBuf,
        create_new_if_missing: bool,
    ) -> Result<Parameters, ApicizeError> {
        if Path::new(&file_name).is_file() {
            let params = open_data_file::<Parameters>(file_name)?.data;
            Ok(params)
        } else if create_new_if_missing {
            Ok(Parameters::default())
        } else {
            Err(ApicizeError::FileAccess {
                file_name: Some(file_name.to_string_lossy().to_string()),
                description: "Not found".to_string(),
            })
        }
    }

    /// Save parametesr to file if it contains entries, otherwise, delete it
    pub fn save(
        &self,
        file_name: &PathBuf,
        destination_name: &str,
        password: &Option<String>,
    ) -> Result<SerializationSaveSuccess, ApicizeError> {
        let method = ParameterEncryption::default();
        let scenarios = match &self.scenarios {
            Some(entities) => entities.iter().map(|e| e.to_owned()).collect(),
            None => vec![],
        };
        let auths = match &self.authorizations {
            Some(entities) => entities.iter().map(|e| e.to_owned()).collect(),
            None => vec![],
        };
        let certs = match &self.certificates {
            Some(entities) => entities.iter().map(|e| e.to_owned()).collect(),
            None => vec![],
        };
        let proxies = match &self.proxies {
            Some(entities) => entities.iter().map(|e| e.to_owned()).collect(),
            None => vec![],
        };

        if scenarios.iter().any(|s| s.is_encrypted())
            || auths.iter().any(|a| a.is_encrypted())
            || certs.iter().any(|c| c.is_encrypted())
            || proxies.iter().any(|s| matches!(s, Proxy::Cipher(_)))
        {
            return Err(ApicizeError::Encryption {
                description: format!("{destination_name} must be unlocked before saving"),
            });
        }

        if scenarios.is_empty() && auths.is_empty() && certs.is_empty() && proxies.is_empty() {
            delete_data_file(file_name)
        } else {
            let (encryption, scenarios, authorizations, certificates, proxies) =
                if let Some(password) = password {
                    let encrypted_scenarios = if scenarios.is_empty() {
                        None
                    } else {
                        Some(
                            scenarios
                                .iter()
                                .map(|s| s.encrypt(password, method))
                                .collect::<Result<Vec<Scenario>, _>>()?,
                        )
                    };
                    let encrypted_auths = if auths.is_empty() {
                        None
                    } else {
                        Some(
                            auths
                                .iter()
                                .map(|p| p.encrypt(password, method))
                                .collect::<Result<Vec<Authorization>, _>>()?,
                        )
                    };
                    let encrypted_certs = if certs.is_empty() {
                        None
                    } else {
                        Some(
                            certs
                                .iter()
                                .map(|p| p.encrypt(password, method))
                                .collect::<Result<Vec<Certificate>, _>>()?,
                        )
                    };
                    let encrypted_proxies = if proxies.is_empty() {
                        None
                    } else {
                        Some(
                            proxies
                                .iter()
                                .map(|p| p.encrypt(password, method))
                                .collect::<Result<Vec<Proxy>, _>>()?,
                        )
                    };
                    (
                        Some(method),
                        encrypted_scenarios,
                        encrypted_auths,
                        encrypted_certs,
                        encrypted_proxies,
                    )
                } else {
                    (
                        None,
                        (!scenarios.is_empty()).then_some(scenarios),
                        (!auths.is_empty()).then_some(auths),
                        (!certs.is_empty()).then_some(certs),
                        (!proxies.is_empty()).then_some(proxies),
                    )
                };

            save_data_file(
                file_name,
                &Parameters {
                    version: 1.0,
                    encryption,
                    scenarios,
                    authorizations,
                    certificates,
                    proxies,
                },
            )
        }
    }

    pub fn get_parameter_count(&self) -> usize {
        self.scenarios.as_ref().map_or(0, |v| v.len())
            + self.authorizations.as_ref().map_or(0, |v| v.len())
            + self.certificates.as_ref().map_or(0, |v| v.len())
            + self.proxies.as_ref().map_or(0, |v| v.len())
    }

    /// Return the file name for globals
    pub fn get_globals_filename() -> path::PathBuf {
        if let Some(directory) = config_dir() {
            directory.join("apicize").join("globals.json")
        } else {
            panic!("Operating system did not provide configuration directory")
        }
    }

    /// Return the file name for a workbook's private parameters file
    pub fn get_workbook_private_filename(workbook_path: &Path) -> path::PathBuf {
        let mut private_path = PathBuf::from(workbook_path);
        private_path.set_extension("apicize-priv");
        private_path
    }

    /// Returns true if any parameters are encrypted
    fn any_encrypted_parameters<T>(parameters: &Option<Vec<T>>) -> bool
    where
        T: EncryptableParameter,
    {
        parameters
            .as_ref()
            .is_some_and(|v| v.iter().any(|item| item.is_encrypted()))
    }

    /// Encrypt parameters with the specified password
    fn encrypt_parameters<T>(
        parameters: &Option<Vec<T>>,
        password: &str,
        method: ParameterEncryption,
    ) -> Result<Option<Vec<T>>, ApicizeError>
    where
        T: EncryptableParameter + Clone,
    {
        match parameters {
            Some(parameters) => Ok(Some(
                parameters
                    .iter()
                    .map(|p| {
                        if p.is_encrypted() {
                            p.encrypt(password, method)
                        } else {
                            Ok(p.clone())
                        }
                    })
                    .collect::<Result<Vec<T>, _>>()?,
            )),
            None => Ok(None),
        }
    }

    /// Decrypt parameters in a list, adding a warning if there is a problem decrypting.
    /// The function will decrypt any parameters it can and leave ciphers in place
    /// with warnings for any failures
    fn decrypt_parameters<T>(
        parameters: &mut Option<Vec<T>>,
        password: &str,
        method: ParameterEncryption,
    ) -> bool
    where
        T: EncryptableParameter + Validated,
    {
        let mut ok = true;
        if let Some(parameters) = parameters {
            for param in parameters.iter_mut() {
                if !param.is_encrypted() {
                    continue;
                }

                match param.decrypt(password, method) {
                    Ok(decrypted) => {
                        *param = decrypted;
                    }
                    Err(err) => {
                        ok = false;
                        param.set_validation_warnings(Some(vec![err.to_string()]));
                    }
                }
            }
        }
        ok
    }

    /// Return true if any paramerers are encrypted
    pub fn any_encyrypted(&self) -> bool {
        Self::any_encrypted_parameters(&self.scenarios)
            || Self::any_encrypted_parameters(&self.authorizations)
            || Self::any_encrypted_parameters(&self.certificates)
            || Self::any_encrypted_parameters(&self.proxies)
    }

    /// Encrypt any plain entries
    pub fn encyrpt(
        &mut self,
        password: &str,
        method: ParameterEncryption,
    ) -> Result<Parameters, ApicizeError> {
        let mut params = Parameters {
            version: self.version,
            encryption: None,
            scenarios: Self::encrypt_parameters(&self.scenarios, password, method)?,
            authorizations: Self::encrypt_parameters(&self.authorizations, password, method)?,
            certificates: Self::encrypt_parameters(&self.certificates, password, method)?,
            proxies: Self::encrypt_parameters(&self.proxies, password, method)?,
        };
        if Self::any_encyrypted(self) {
            params.encryption = Some(ParameterEncryption::Aes256Gcm);
        }
        Ok(params)
    }

    /// Decrypt the listed parameters, when encrypted, using password, if specified,
    /// or fallback env variable,if defined; returns the lock status and active password
    pub fn decrypt(
        &mut self,
        password: Option<&str>,
        fallback_env_variable: Option<&str>,
    ) -> (ParameterLockStatus, Option<String>) {
        let method = self.encryption.unwrap_or_default();

        let is_encrypted = self.any_encyrypted();
        if !is_encrypted {
            return (ParameterLockStatus::UnlockedNoPassword, None);
        }

        let env_password = if password.as_ref().is_none_or(|pw| pw.is_empty()) {
            fallback_env_variable
                .and_then(|var| env::var(var).ok())
                .filter(|pw| !pw.is_empty())
        } else {
            None
        };

        let (password, use_env_var) = if let Some(ref pw) = env_password {
            (Some(pw.as_str()), true)
        } else if password.is_some() {
            (password, false)
        } else {
            (None, false)
        };

        let unlocked = if let Some(password) = &password {
            Self::decrypt_parameters(&mut self.scenarios, password, method)
                && Self::decrypt_parameters(&mut self.authorizations, password, method)
                && Self::decrypt_parameters(&mut self.certificates, password, method)
                && Self::decrypt_parameters(&mut self.proxies, password, method)
        } else {
            false
        };

        let status = if unlocked {
            if password.is_some() {
                if use_env_var {
                    ParameterLockStatus::UnlockedWithEnvVar
                } else {
                    ParameterLockStatus::UnlockedWithPassword
                }
            } else {
                ParameterLockStatus::UnlockedNoPassword
            }
        } else if password.is_some() {
            if use_env_var {
                ParameterLockStatus::LockedInvalidEnvVar
            } else {
                ParameterLockStatus::LockedInvalidPassword
            }
        } else {
            ParameterLockStatus::Locked
        };

        (status, password.map(|p| p.to_string()))
    }
}