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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Gatekeeper manages access to a vault.
use crate::{
    crypto::{AccessKey, KeyDerivation, PrivateKey},
    decode, encode,
    events::{ReadEvent, WriteEvent},
    search::SearchIndex,
    vault::{
        secret::{Secret, SecretId, SecretMeta},
        SharedAccess, Summary, Vault, VaultAccess, VaultCommit, VaultEntry,
        VaultId, VaultMeta, VaultWriter,
    },
    vfs, Error, Result,
};
//use parking_lot::RwLock;

use std::{collections::HashSet, sync::Arc};
use tokio::sync::RwLock;

use uuid::Uuid;

/// Access to an in-memory vault optionally mirroring changes to disc.
///
/// It stores the derived private key in memory so should only be
/// used on client implementations.
///
/// Calling `lock()` will zeroize the private key in memory and prevent
/// any access to the vault until `unlock()` is called successfully.
///
/// To allow for meta data to be displayed before secret decryption
/// certain parts of a vault are encrypted separately which means that
/// technically it would be possible to use different private keys for
/// different secrets and for the meta data however this would be
/// a very poor user experience and would lead to confusion so the
/// gatekeeper is also responsible for ensuring the same private key
/// is used to encrypt the different chunks.
pub struct Gatekeeper {
    /// The private key.
    private_key: Option<PrivateKey>,
    /// The underlying vault.
    vault: Vault,
    /// Mirror in-memory vault changes to a writer.
    mirror: Option<VaultWriter<vfs::File>>,
    /// Search index.
    index: Arc<RwLock<SearchIndex>>,
}

impl Gatekeeper {
    /// Create a new gatekeeper.
    pub fn new(
        vault: Vault,
        index: Option<Arc<RwLock<SearchIndex>>>,
    ) -> Self {
        Self {
            vault,
            private_key: None,
            mirror: None,
            index: index
                .unwrap_or_else(|| Arc::new(RwLock::new(SearchIndex::new()))),
        }
    }

    /// Create a new gatekeeper that writes in-memory
    /// changes to a file.
    pub fn new_mirror(
        vault: Vault,
        mirror: VaultWriter<vfs::File>,
        index: Option<Arc<RwLock<SearchIndex>>>,
    ) -> Self {
        Self {
            vault,
            private_key: None,
            mirror: Some(mirror),
            index: index
                .unwrap_or_else(|| Arc::new(RwLock::new(SearchIndex::new()))),
        }
    }

    /// Get the vault.
    pub fn vault(&self) -> &Vault {
        &self.vault
    }

    /// Get a mutable reference to the vault.
    pub fn vault_mut(&mut self) -> &mut Vault {
        &mut self.vault
    }

    /// Replace this vault with a new updated vault
    /// and update the search index if possible.
    ///
    /// When a password is being changed then we need to use
    /// the new private key for the vault.
    pub async fn replace_vault(
        &mut self,
        vault: Vault,
        new_key: Option<PrivateKey>,
    ) -> Result<()> {
        let derived_key = new_key.as_ref().or(self.private_key.as_ref());

        if let Some(derived_key) = derived_key {
            let derived_key = Some(derived_key);
            let existing_keys = self.vault.keys().collect::<HashSet<_>>();
            let updated_keys = vault.keys().collect::<HashSet<_>>();

            let mut writer = self.index.write().await;

            for added_key in updated_keys.difference(&existing_keys) {
                if let Some((meta, secret)) = self
                    .read_secret(added_key, Some(&vault), derived_key)
                    .await?
                {
                    writer.add(self.vault().id(), added_key, meta, &secret);
                }
            }

            for deleted_key in existing_keys.difference(&updated_keys) {
                writer.remove(self.vault().id(), deleted_key);
            }

            for maybe_updated in updated_keys.union(&existing_keys) {
                if let (
                    Some(VaultCommit(existing_hash, _)),
                    Some(VaultCommit(updated_hash, _)),
                ) =
                    (self.vault.get(maybe_updated), vault.get(maybe_updated))
                {
                    if existing_hash != updated_hash {
                        if let Some((meta, secret)) = self
                            .read_secret(
                                maybe_updated,
                                Some(&vault),
                                derived_key,
                            )
                            .await?
                        {
                            writer.update(
                                self.vault().id(),
                                maybe_updated,
                                meta,
                                &secret,
                            );
                        }
                    }
                }
            }
        }

        self.vault = vault;
        Ok(())
    }

    /// Set the vault.
    pub fn set_vault(&mut self, vault: Vault) {
        self.vault = vault;
    }

    /// Get the search index.
    pub fn index(&self) -> Arc<RwLock<SearchIndex>> {
        Arc::clone(&self.index)
    }

    /// Get the summary for the vault.
    pub fn summary(&self) -> &Summary {
        self.vault.summary()
    }

    /// Get the identifier for the vault.
    pub fn id(&self) -> &VaultId {
        self.vault.id()
    }

    /// Get the public name for the vault.
    pub fn name(&self) -> &str {
        self.vault.name()
    }

    /// Set the public name for the vault.
    pub async fn set_vault_name(
        &mut self,
        name: String,
    ) -> Result<WriteEvent<'_>> {
        if let Some(mirror) = self.mirror.as_mut() {
            mirror.set_vault_name(name.clone()).await?;
        }
        self.vault.set_vault_name(name).await
    }

    /// Attempt to decrypt the meta data for the vault
    /// using the key assigned to this gatekeeper.
    pub async fn vault_meta(&self) -> Result<VaultMeta> {
        let private_key =
            self.private_key.as_ref().ok_or(Error::VaultLocked)?;

        if let Some(meta_aead) = self.vault.header().meta() {
            let meta_blob = self
                .vault
                .decrypt(private_key, meta_aead)
                .await
                .map_err(|_| Error::PassphraseVerification)?;
            let meta_data: VaultMeta = decode(&meta_blob).await?;
            Ok(meta_data)
        } else {
            Err(Error::VaultNotInit)
        }
    }

    /// Set the meta data for the vault.
    pub async fn set_vault_meta(
        &mut self,
        meta_data: VaultMeta,
    ) -> Result<WriteEvent<'_>> {
        let private_key =
            self.private_key.as_ref().ok_or(Error::VaultLocked)?;
        let meta_blob = encode(&meta_data).await?;
        let meta_aead = self.vault.encrypt(private_key, &meta_blob).await?;
        if let Some(mirror) = self.mirror.as_mut() {
            mirror.set_vault_meta(Some(meta_aead.clone())).await?;
        }
        self.vault.set_vault_meta(Some(meta_aead)).await
    }

    /// Get a secret from the vault.
    async fn read_secret(
        &self,
        id: &SecretId,
        from: Option<&Vault>,
        private_key: Option<&PrivateKey>,
    ) -> Result<Option<(SecretMeta, Secret)>> {
        let private_key = private_key
            .or(self.private_key.as_ref())
            .ok_or(Error::VaultLocked)?;

        let from = from.unwrap_or(&self.vault);

        if let (Some(value), _payload) = from.read(id).await? {
            let VaultCommit(_commit, VaultEntry(meta_aead, secret_aead)) =
                value.as_ref();
            let meta_blob = from.decrypt(private_key, meta_aead).await?;
            let secret_meta: SecretMeta = decode(&meta_blob).await?;

            let secret_blob = from.decrypt(private_key, secret_aead).await?;
            let secret: Secret = decode(&secret_blob).await?;
            Ok(Some((secret_meta, secret)))
        } else {
            Ok(None)
        }
    }

    /// Ensure that if shared access is set to readonly that
    /// this user is allowed to write.
    async fn enforce_shared_readonly(&self, key: &PrivateKey) -> Result<()> {
        if let SharedAccess::ReadOnly(aead) = self.vault.shared_access() {
            self.vault
                .decrypt(key, aead)
                .await
                .map_err(|_| Error::PermissionDenied)?;
        }
        Ok(())
    }

    /// Add a secret to the vault.
    pub async fn create(
        &mut self,
        secret_meta: SecretMeta,
        secret: Secret,
    ) -> Result<WriteEvent<'_>> {
        let private_key =
            self.private_key.as_ref().ok_or(Error::VaultLocked)?;

        self.enforce_shared_readonly(private_key).await?;

        let vault_id = *self.vault().id();
        let id = Uuid::new_v4();

        #[cfg(feature = "keyring")]
        {
            if let Secret::Account {
                account,
                password,
                url,
                ..
            } = &secret
            {
                let native_keyring = crate::get_native_keyring();
                let keyring = native_keyring.lock().await;
                let _ = keyring.create_entry(
                    &id,
                    secret_meta.label(),
                    account,
                    password,
                    url.as_ref(),
                );
            }
        }

        //let reader = self.index.read().await;

        /*
        if reader
            .find_by_label(&vault_id, secret_meta.label(), None)
            .is_some()
        {
            return Err(Error::SecretAlreadyExists(
                secret_meta.label().to_string(),
            ));
        }
        */

        let meta_blob = encode(&secret_meta).await?;
        let meta_aead = self.vault.encrypt(private_key, &meta_blob).await?;

        let secret_blob = encode(&secret).await?;
        let secret_aead =
            self.vault.encrypt(private_key, &secret_blob).await?;

        let (commit, _) =
            Vault::commit_hash(&meta_aead, &secret_aead).await?;

        if let Some(mirror) = self.mirror.as_mut() {
            mirror
                .insert(
                    id,
                    commit,
                    VaultEntry(meta_aead.clone(), secret_aead.clone()),
                )
                .await?;
        }

        let result = self
            .vault
            .insert(id, commit, VaultEntry(meta_aead, secret_aead))
            .await?;

        //drop(reader);

        let mut writer = self.index.write().await;
        writer.add(&vault_id, &id, secret_meta, &secret);

        Ok(result)
    }

    /// Get a secret and it's meta data from the vault.
    pub async fn read(
        &self,
        id: &SecretId,
    ) -> Result<Option<(SecretMeta, Secret, ReadEvent)>> {
        let payload = ReadEvent::ReadSecret(*id);
        Ok(self
            .read_secret(id, None, None)
            .await?
            .map(|(meta, secret)| (meta, secret, payload)))
    }

    /// Update a secret in the vault.
    pub async fn update(
        &mut self,
        id: &SecretId,
        secret_meta: SecretMeta,
        secret: Secret,
    ) -> Result<Option<WriteEvent<'_>>> {
        let private_key =
            self.private_key.as_ref().ok_or(Error::VaultLocked)?;

        self.enforce_shared_readonly(private_key).await?;

        let vault_id = *self.vault().id();

        #[cfg(feature = "keyring")]
        {
            if let Secret::Account {
                account,
                password,
                url,
                ..
            } = &secret
            {
                let native_keyring = crate::get_native_keyring();
                let keyring = native_keyring.lock().await;

                // Delete an existing entry first
                if let Some((meta, secret)) =
                    self.read_secret(id, None, None).await?
                {
                    if let Secret::Account { account, url, .. } = &secret {
                        let _ = keyring.delete_entry(
                            &id,
                            meta.label(),
                            account,
                            url.as_ref(),
                        );
                    }
                }

                // Create the new entry
                let _ = keyring.create_entry(
                    &id,
                    secret_meta.label(),
                    account,
                    password,
                    url.as_ref(),
                );
            }
        }

        /*
        let reader = self.index.read().await;

        let doc = reader
            .find_by_id(&vault_id, id)
            .ok_or(Error::SecretDoesNotExist(*id))?;

        // Label has changed, so ensure uniqueness
        if doc.meta().label() != secret_meta.label()
            && reader
                .find_by_label(&vault_id, secret_meta.label(), Some(id))
                .is_some()
        {
            return Err(Error::SecretAlreadyExists(
                secret_meta.label().to_string(),
            ));
        }
        */

        let meta_blob = encode(&secret_meta).await?;
        let meta_aead = self.vault.encrypt(private_key, &meta_blob).await?;

        let secret_blob = encode(&secret).await?;
        let secret_aead =
            self.vault.encrypt(private_key, &secret_blob).await?;

        let (commit, _) =
            Vault::commit_hash(&meta_aead, &secret_aead).await?;

        if let Some(mirror) = self.mirror.as_mut() {
            mirror
                .update(
                    id,
                    commit,
                    VaultEntry(meta_aead.clone(), secret_aead.clone()),
                )
                .await?;
        }

        let event = self
            .vault
            .update(id, commit, VaultEntry(meta_aead, secret_aead))
            .await?;

        //drop(reader);

        let mut writer = self.index.write().await;
        writer.update(&vault_id, id, secret_meta, &secret);

        Ok(event)
    }

    /// Delete a secret and it's meta data from the vault.
    pub async fn delete(
        &mut self,
        id: &SecretId,
    ) -> Result<Option<WriteEvent<'_>>> {
        let private_key =
            self.private_key.as_ref().ok_or(Error::VaultLocked)?;
        self.enforce_shared_readonly(private_key).await?;

        #[cfg(feature = "keyring")]
        {
            if let Some((meta, secret)) =
                self.read_secret(id, None, None).await?
            {
                if let Secret::Account { account, url, .. } = &secret {
                    let native_keyring = crate::get_native_keyring();
                    let keyring = native_keyring.lock().await;
                    let _ = keyring.delete_entry(
                        &id,
                        meta.label(),
                        account,
                        url.as_ref(),
                    );
                }
            }
        }

        let vault_id = *self.vault().id();
        if let Some(mirror) = self.mirror.as_mut() {
            mirror.delete(id).await?;
        }
        let event = self.vault.delete(id).await?;
        let mut writer = self.index.write().await;
        writer.remove(&vault_id, id);
        Ok(event)
    }

    /// Verify an encryption passphrase.
    pub async fn verify(&self, key: &AccessKey) -> Result<()> {
        self.vault.verify(key).await
    }

    /// Add the meta data for the vault entries to a search index..
    pub async fn create_search_index(&mut self) -> Result<()> {
        let private_key =
            self.private_key.as_ref().ok_or(Error::VaultLocked)?;
        let mut writer = self.index.write().await;
        for (id, value) in self.vault.iter() {
            let VaultCommit(_commit, VaultEntry(meta_aead, secret_aead)) =
                value;
            let meta_blob =
                self.vault.decrypt(private_key, meta_aead).await?;
            let secret_meta: SecretMeta = decode(&meta_blob).await?;

            let secret_blob =
                self.vault.decrypt(private_key, secret_aead).await?;
            let secret: Secret = decode(&secret_blob).await?;

            writer.add(self.vault().id(), id, secret_meta, &secret);
        }
        Ok(())
    }

    /// Unlock the vault by setting the private key from a passphrase.
    ///
    /// The private key is stored in memory by this gatekeeper.
    pub async fn unlock(&mut self, key: AccessKey) -> Result<VaultMeta> {
        if let Some(salt) = self.vault.salt() {
            match key {
                AccessKey::Password(passphrase) => {
                    let salt = KeyDerivation::parse_salt(salt)?;
                    let deriver = self.vault.deriver();
                    let private_key = deriver.derive(
                        &passphrase,
                        &salt,
                        self.vault.seed(),
                    )?;
                    self.private_key =
                        Some(PrivateKey::Symmetric(private_key));
                    self.vault_meta().await
                }
                AccessKey::Identity(id) => {
                    self.private_key =
                        Some(PrivateKey::Asymmetric(id.clone()));
                    self.vault_meta().await
                }
            }
        } else {
            Err(Error::VaultNotInit)
        }
    }

    /// Lock the vault by deleting the stored passphrase
    /// associated with the vault, securely zeroing the
    /// underlying memory.
    pub fn lock(&mut self) {
        self.private_key = None;
    }
}

impl From<Vault> for Gatekeeper {
    fn from(value: Vault) -> Self {
        Gatekeeper::new(value, None)
    }
}

impl From<Gatekeeper> for Vault {
    fn from(value: Gatekeeper) -> Self {
        value.vault
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::*;
    use crate::{
        constants::DEFAULT_VAULT_NAME,
        vault::{secret::Secret, VaultBuilder},
    };
    use anyhow::Result;
    use secrecy::SecretString;

    #[tokio::test]
    async fn gatekeeper_secret_note() -> Result<()> {
        set_mock_credential_builder().await;

        let passphrase = SecretString::new("mock-passphrase".to_owned());
        let name = String::from(DEFAULT_VAULT_NAME);
        let description = String::from("Mock Vault Description");

        let vault = VaultBuilder::new()
            .public_name(name)
            .description(description.clone())
            .password(passphrase.clone(), None)
            .await?;

        let mut keeper = Gatekeeper::new(vault, None);
        keeper.unlock(passphrase.into()).await?;

        //// Decrypt the initialized meta data.
        let meta = keeper.vault_meta().await?;

        assert_eq!(&description, meta.description());

        let secret_label = "Mock Secret".to_string();
        let secret_value = "Super Secret Note".to_string();
        let secret = Secret::Note {
            text: SecretString::new(secret_value),
            user_data: Default::default(),
        };
        let secret_meta = SecretMeta::new(secret_label, secret.kind());

        if let WriteEvent::CreateSecret(secret_uuid, _) =
            keeper.create(secret_meta.clone(), secret.clone()).await?
        {
            let (saved_secret_meta, saved_secret) =
                keeper.read_secret(&secret_uuid, None, None).await?.unwrap();
            assert_eq!(secret, saved_secret);
            assert_eq!(secret_meta, saved_secret_meta);
        } else {
            panic!("test create secret got wrong payload variant");
        }

        keeper.lock();

        Ok(())
    }

    #[tokio::test]
    async fn gatekeeper_secret_account() -> Result<()> {
        set_mock_credential_builder().await;

        let passphrase = SecretString::new("mock-passphrase".to_owned());
        let name = String::from(DEFAULT_VAULT_NAME);
        let description = String::from("Mock Vault Description");

        let vault = VaultBuilder::new()
            .public_name(name)
            .description(description.clone())
            .password(passphrase.clone(), None)
            .await?;

        let mut keeper = Gatekeeper::new(vault, None);
        keeper.unlock(passphrase.into()).await?;

        //// Decrypt the initialized meta data.
        let meta = keeper.vault_meta().await?;

        assert_eq!(&description, meta.description());

        let secret_label = "Mock Account Secret".to_string();
        let secret_value = "super-secret-password".to_string();
        let secret = Secret::Account {
            account: "mock-username".to_string(),
            password: SecretString::new(secret_value),
            url: Some("https://example.com".parse()?),
            user_data: Default::default(),
        };
        let secret_meta = SecretMeta::new(secret_label, secret.kind());

        let id = if let WriteEvent::CreateSecret(secret_uuid, _) =
            keeper.create(secret_meta.clone(), secret.clone()).await?
        {
            let (saved_secret_meta, saved_secret) =
                keeper.read_secret(&secret_uuid, None, None).await?.unwrap();
            assert_eq!(secret, saved_secret);
            assert_eq!(secret_meta, saved_secret_meta);
            secret_uuid
        } else {
            panic!("test create secret got wrong payload variant");
        };

        let new_secret_label = "Mock New Account".to_string();
        let new_secret_value = "new-secret-password".to_string();
        let new_secret = Secret::Account {
            account: "mock-new-username".to_string(),
            password: SecretString::new(new_secret_value),
            url: Some("https://example.com/new".parse()?),
            user_data: Default::default(),
        };
        let new_secret_meta =
            SecretMeta::new(new_secret_label.clone(), new_secret.kind());

        keeper.update(&id, new_secret_meta, new_secret).await?;

        keeper.lock();

        Ok(())
    }
}