quantrs2-device 0.2.0

Quantum device connectors for the QuantRS2 framework
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Credential management for quantum cloud providers.
//!
//! Abstracts credential storage behind a trait so the same code works
//! with environment variables, files, or future vault integrations.

use std::collections::HashMap;

/// A secret string that zeroes its memory on drop to reduce secret leakage.
///
/// Does not implement `Clone` to prevent accidental copies. Access the
/// underlying value with [`SecretString::expose_secret`] in a short-lived context.
pub struct SecretString {
    inner: Vec<u8>,
}

impl SecretString {
    /// Wrap a string value as a secret
    pub fn new(s: impl Into<String>) -> Self {
        Self {
            inner: s.into().into_bytes(),
        }
    }

    /// Access the secret value as a string slice.
    ///
    /// Keep the borrow as short-lived as possible to limit exposure.
    pub fn expose_secret(&self) -> &str {
        // SAFETY: We stored valid UTF-8 from Into<String>.
        std::str::from_utf8(&self.inner).unwrap_or("")
    }

    /// Return the length in bytes
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Return true if the secret is empty
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

impl Drop for SecretString {
    fn drop(&mut self) {
        // Volatile write prevents the compiler from optimizing away the zeroing.
        for b in self.inner.iter_mut() {
            // SAFETY: `b` is a valid mutable reference to a byte within our Vec.
            unsafe {
                std::ptr::write_volatile(b, 0u8);
            }
        }
    }
}

impl std::fmt::Debug for SecretString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("[REDACTED]")
    }
}

impl std::fmt::Display for SecretString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("[REDACTED]")
    }
}

/// Error type for credential operations
#[derive(Debug)]
#[non_exhaustive]
pub enum CredentialError {
    /// The requested credential key was not found
    NotFound(String),
    /// File permissions are too permissive
    PermissionDenied(String),
    /// Underlying I/O failure
    IoError(std::io::Error),
    /// Failed to parse credential file
    ParseError(String),
}

impl std::fmt::Display for CredentialError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CredentialError::NotFound(k) => write!(f, "credential not found: {}", k),
            CredentialError::PermissionDenied(p) => write!(f, "permission denied: {}", p),
            CredentialError::IoError(e) => write!(f, "credential IO error: {}", e),
            CredentialError::ParseError(s) => write!(f, "credential parse error: {}", s),
        }
    }
}

impl std::error::Error for CredentialError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CredentialError::IoError(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for CredentialError {
    fn from(e: std::io::Error) -> Self {
        CredentialError::IoError(e)
    }
}

/// Abstraction for credential storage backends
pub trait CredentialProvider: Send + Sync {
    /// Retrieve a credential by key, returning a zeroing `SecretString`
    fn get_credential(&self, key: &str) -> Result<SecretString, CredentialError>;

    /// List available credential keys.
    ///
    /// May return an empty vec for security reasons (e.g. env-var providers
    /// avoid enumerating the process environment).
    fn available_keys(&self) -> Vec<String>;
}

/// Reads credentials from environment variables.
///
/// Keys are looked up as `{PREFIX}_{KEY}` (both uppercased). If the prefix
/// is empty the key is used directly, uppercased.
///
/// # Example
///
/// ```rust,no_run
/// # use quantrs2_device::security::credentials::{EnvVarCredentialProvider, CredentialProvider};
/// let provider = EnvVarCredentialProvider::new("QUANTRS");
/// // looks for env var "QUANTRS_IBM_TOKEN"
/// let _ = provider.get_credential("IBM_TOKEN");
/// ```
pub struct EnvVarCredentialProvider {
    prefix: String,
}

impl EnvVarCredentialProvider {
    /// Create a new provider with the given environment variable prefix
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into().to_uppercase(),
        }
    }

    fn env_key(&self, key: &str) -> String {
        if self.prefix.is_empty() {
            key.to_uppercase()
        } else {
            format!("{}_{}", self.prefix, key.to_uppercase())
        }
    }
}

impl CredentialProvider for EnvVarCredentialProvider {
    fn get_credential(&self, key: &str) -> Result<SecretString, CredentialError> {
        let env_key = self.env_key(key);
        std::env::var(&env_key)
            .map(SecretString::new)
            .map_err(|_| CredentialError::NotFound(env_key))
    }

    fn available_keys(&self) -> Vec<String> {
        // Don't enumerate environment variables — it's a security risk
        vec![]
    }
}

/// Reads credentials from a JSON file (e.g. `~/.quantrs/credentials.json`).
///
/// The file must contain a flat JSON object mapping string keys to string values.
/// On Unix systems the file must have mode `0o600`; looser permissions are rejected.
///
/// # Example file
///
/// ```json
/// {
///   "IBM_TOKEN": "my-ibm-token",
///   "AWS_SECRET": "my-aws-secret"
/// }
/// ```
pub struct FileCredentialProvider {
    path: std::path::PathBuf,
}

impl FileCredentialProvider {
    /// Create a provider that reads from the given JSON credentials file
    pub fn new(path: impl Into<std::path::PathBuf>) -> Self {
        Self { path: path.into() }
    }

    fn load(&self) -> Result<HashMap<String, String>, CredentialError> {
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            let meta = std::fs::metadata(&self.path).map_err(CredentialError::IoError)?;
            if meta.mode() & 0o077 != 0 {
                return Err(CredentialError::PermissionDenied(format!(
                    "credentials file {:?} must have mode 0600",
                    self.path
                )));
            }
        }

        let content = std::fs::read_to_string(&self.path).map_err(CredentialError::IoError)?;
        serde_json::from_str::<HashMap<String, String>>(&content)
            .map_err(|e| CredentialError::ParseError(e.to_string()))
    }
}

impl CredentialProvider for FileCredentialProvider {
    fn get_credential(&self, key: &str) -> Result<SecretString, CredentialError> {
        let map = self.load()?;
        map.get(key)
            .map(|v| SecretString::new(v.clone()))
            .ok_or_else(|| CredentialError::NotFound(key.to_string()))
    }

    fn available_keys(&self) -> Vec<String> {
        self.load()
            .map(|m| m.into_keys().collect())
            .unwrap_or_default()
    }
}

/// Tries multiple [`CredentialProvider`]s in order until one succeeds.
///
/// Useful for layering: environment variables override file credentials, which
/// override compiled-in defaults.
///
/// # Example
///
/// ```rust,no_run
/// # use quantrs2_device::security::credentials::{
/// #     CompositeCredentialProvider, EnvVarCredentialProvider, CredentialProvider
/// # };
/// let provider = CompositeCredentialProvider::new()
///     .with_provider(EnvVarCredentialProvider::new("QUANTRS"));
/// let _ = provider.get_credential("IBM_TOKEN");
/// ```
pub struct CompositeCredentialProvider {
    providers: Vec<Box<dyn CredentialProvider>>,
}

impl CompositeCredentialProvider {
    /// Create an empty composite provider (no sources yet)
    pub fn new() -> Self {
        Self { providers: vec![] }
    }

    /// Append a credential provider source (tried in insertion order)
    pub fn with_provider(mut self, p: impl CredentialProvider + 'static) -> Self {
        self.providers.push(Box::new(p));
        self
    }
}

impl Default for CompositeCredentialProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl CredentialProvider for CompositeCredentialProvider {
    fn get_credential(&self, key: &str) -> Result<SecretString, CredentialError> {
        for p in &self.providers {
            if let Ok(s) = p.get_credential(key) {
                return Ok(s);
            }
        }
        Err(CredentialError::NotFound(key.to_string()))
    }

    fn available_keys(&self) -> Vec<String> {
        self.providers
            .iter()
            .flat_map(|p| p.available_keys())
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::io::Write;

    #[test]
    fn test_secret_string_expose() {
        let secret = SecretString::new("my-api-key");
        assert_eq!(secret.expose_secret(), "my-api-key");
    }

    #[test]
    fn test_secret_string_debug_redacted() {
        let secret = SecretString::new("super-secret");
        assert_eq!(format!("{:?}", secret), "[REDACTED]");
        assert_eq!(format!("{}", secret), "[REDACTED]");
    }

    #[test]
    fn test_secret_string_len() {
        let secret = SecretString::new("hello");
        assert_eq!(secret.len(), 5);
        assert!(!secret.is_empty());
    }

    #[test]
    fn test_secret_string_empty() {
        let secret = SecretString::new("");
        assert!(secret.is_empty());
        assert_eq!(secret.len(), 0);
    }

    #[test]
    fn test_env_var_provider_found() {
        let key = format!("QUANTRS_TEST_KEY_{}", fastrand::u64(..));
        env::set_var(&key, "test-token");

        let provider = EnvVarCredentialProvider::new("");
        let secret = provider.get_credential(&key).expect("should find env var");
        assert_eq!(secret.expose_secret(), "test-token");

        env::remove_var(&key);
    }

    #[test]
    fn test_env_var_provider_with_prefix() {
        let suffix = fastrand::u64(..);
        let env_var = format!("QUANTRS_TEST_{}", suffix);
        env::set_var(&env_var, "prefixed-value");

        let provider = EnvVarCredentialProvider::new("QUANTRS");
        let secret = provider
            .get_credential(&format!("TEST_{}", suffix))
            .expect("should find prefixed env var");
        assert_eq!(secret.expose_secret(), "prefixed-value");

        env::remove_var(&env_var);
    }

    #[test]
    fn test_env_var_provider_not_found() {
        let provider = EnvVarCredentialProvider::new("QUANTRS");
        let result = provider.get_credential("DEFINITELY_NONEXISTENT_KEY_12345");
        assert!(matches!(result, Err(CredentialError::NotFound(_))));
    }

    #[test]
    fn test_env_var_provider_available_keys_empty() {
        let provider = EnvVarCredentialProvider::new("QUANTRS");
        assert!(provider.available_keys().is_empty());
    }

    #[test]
    fn test_file_credential_provider() {
        let dir = env::temp_dir();
        let path = dir.join(format!("quantrs_creds_{}.json", fastrand::u64(..)));

        let mut file = std::fs::File::create(&path).expect("create file");
        write!(
            file,
            r#"{{"IBM_TOKEN":"ibm-secret","AWS_KEY":"aws-secret"}}"#
        )
        .expect("write credentials");
        drop(file);

        // Set mode 0600 on Unix so the permission check passes
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(0o600);
            std::fs::set_permissions(&path, perms).expect("set permissions");
        }

        let provider = FileCredentialProvider::new(&path);
        let secret = provider
            .get_credential("IBM_TOKEN")
            .expect("should find IBM_TOKEN");
        assert_eq!(secret.expose_secret(), "ibm-secret");

        let keys = provider.available_keys();
        assert!(keys.contains(&"IBM_TOKEN".to_string()) || keys.contains(&"AWS_KEY".to_string()));

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_file_credential_provider_not_found() {
        let dir = env::temp_dir();
        let path = dir.join(format!("quantrs_creds_nf_{}.json", fastrand::u64(..)));

        let mut file = std::fs::File::create(&path).expect("create file");
        write!(file, r#"{{"IBM_TOKEN":"ibm-secret"}}"#).expect("write credentials");
        drop(file);

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(0o600);
            std::fs::set_permissions(&path, perms).expect("set permissions");
        }

        let provider = FileCredentialProvider::new(&path);
        let result = provider.get_credential("NONEXISTENT");
        assert!(matches!(result, Err(CredentialError::NotFound(_))));

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_composite_provider_fallthrough() {
        // First provider has nothing; second has the key
        let suffix = fastrand::u64(..);
        let env_var = format!("QUANTRS_COMPOSITE_{}", suffix);
        env::set_var(&env_var, "composite-value");

        // Env provider with wrong prefix → won't find it
        let missing_provider = EnvVarCredentialProvider::new("WRONG_PREFIX");
        // Env provider with no prefix → will find it as-is
        let found_provider = EnvVarCredentialProvider::new("");

        let composite = CompositeCredentialProvider::new()
            .with_provider(missing_provider)
            .with_provider(found_provider);

        let secret = composite
            .get_credential(&env_var)
            .expect("composite should find via second provider");
        assert_eq!(secret.expose_secret(), "composite-value");

        env::remove_var(&env_var);
    }

    #[test]
    fn test_composite_provider_all_fail() {
        let composite = CompositeCredentialProvider::new().with_provider(
            EnvVarCredentialProvider::new("DEFINITELY_MISSING_PREFIX_XYZ"),
        );

        let result = composite.get_credential("NONEXISTENT");
        assert!(matches!(result, Err(CredentialError::NotFound(_))));
    }

    #[test]
    fn test_composite_provider_empty() {
        let composite = CompositeCredentialProvider::new();
        let result = composite.get_credential("any_key");
        assert!(matches!(result, Err(CredentialError::NotFound(_))));
        assert!(composite.available_keys().is_empty());
    }

    #[test]
    fn test_credential_error_display() {
        let e = CredentialError::NotFound("MY_KEY".to_string());
        assert!(e.to_string().contains("MY_KEY"));

        let e = CredentialError::PermissionDenied("/path/to/file".to_string());
        assert!(e.to_string().contains("permission denied"));

        let e = CredentialError::ParseError("invalid json".to_string());
        assert!(e.to_string().contains("parse error"));
    }
}