@@ -520,13 +520,13 @@ impl<P, S> Repository<P, S> {
520520 }
521521 }
522522
523- let key = find_key_in_backend ( & self . be , & password, None ) ?;
523+ let ( key, key_id ) = find_key_in_backend ( & self . be , & password, None ) ?;
524524
525525 info ! ( "repository {}: password is correct." , self . name) ;
526526
527527 let dbe = DecryptBackend :: new ( self . be . clone ( ) , key) ;
528528 let config: ConfigFile = dbe. get_file ( & config_id) ?;
529- self . open_raw ( key, config)
529+ self . open_raw ( key, key_id , config)
530530 }
531531
532532 /// Initialize a new repository with given options using the password defined in `RepositoryOptions`
@@ -604,9 +604,9 @@ impl<P, S> Repository<P, S> {
604604 . attach_context ( "name" , self . name ) ) ;
605605 }
606606
607- let ( key, config) = commands:: init:: init ( & self , pass, key_opts, config_opts) ?;
607+ let ( key, key_id , config) = commands:: init:: init ( & self , pass, key_opts, config_opts) ?;
608608
609- self . open_raw ( key, config)
609+ self . open_raw ( key, key_id , config)
610610 }
611611
612612 /// Initialize a new repository with given password and a ready [`ConfigFile`].
@@ -632,9 +632,9 @@ impl<P, S> Repository<P, S> {
632632 key_opts : & KeyOptions ,
633633 config : ConfigFile ,
634634 ) -> RusticResult < Repository < P , OpenStatus > > {
635- let key = commands:: init:: init_with_config ( & self , password, key_opts, & config) ?;
635+ let ( key, key_id ) = commands:: init:: init_with_config ( & self , password, key_opts, & config) ?;
636636 info ! ( "repository {} successfully created." , config. id) ;
637- self . open_raw ( key, config)
637+ self . open_raw ( key, key_id , config)
638638 }
639639
640640 /// Open the repository with given [`Key`] and [`ConfigFile`].
@@ -652,7 +652,12 @@ impl<P, S> Repository<P, S> {
652652 ///
653653 /// * If the config file has `is_hot` set to `true` but the repository is not hot
654654 /// * If the config file has `is_hot` set to `false` but the repository is hot
655- fn open_raw ( mut self , key : Key , config : ConfigFile ) -> RusticResult < Repository < P , OpenStatus > > {
655+ fn open_raw (
656+ mut self ,
657+ key : Key ,
658+ key_id : KeyId ,
659+ config : ConfigFile ,
660+ ) -> RusticResult < Repository < P , OpenStatus > > {
656661 match ( config. is_hot == Some ( true ) , self . be_hot . is_some ( ) ) {
657662 ( true , false ) => return Err (
658663 RusticError :: new (
@@ -684,7 +689,12 @@ impl<P, S> Repository<P, S> {
684689 dbe. set_zstd ( config. zstd ( ) ?) ;
685690 dbe. set_extra_verify ( config. extra_verify ( ) ) ;
686691
687- let open = OpenStatus { cache, dbe, config } ;
692+ let open = OpenStatus {
693+ cache,
694+ dbe,
695+ config,
696+ key_id,
697+ } ;
688698
689699 Ok ( Repository {
690700 name : self . name ,
@@ -755,58 +765,32 @@ impl<P: ProgressBars, S> Repository<P, S> {
755765
756766/// A repository which is open, i.e. the password has been checked and the decryption key is available.
757767pub trait Open {
758- /// Get the cache
759- fn cache ( & self ) -> Option < & Cache > ;
760-
761- /// Get the [`DecryptBackend`]
762- fn dbe ( & self ) -> & DecryptBackend < Key > ;
763-
764- /// Get the [`ConfigFile`]
765- fn config ( & self ) -> & ConfigFile ;
768+ /// Get the open status
769+ fn open_status ( & self ) -> & OpenStatus ;
766770}
767771
768772impl < P , S : Open > Open for Repository < P , S > {
769- /// Get the cache
770- fn cache ( & self ) -> Option < & Cache > {
771- self . status . cache ( )
772- }
773-
774- /// Get the [`DecryptBackend`]
775- fn dbe ( & self ) -> & DecryptBackend < Key > {
776- self . status . dbe ( )
777- }
778-
779- /// Get the [`ConfigFile`]
780- fn config ( & self ) -> & ConfigFile {
781- self . status . config ( )
773+ fn open_status ( & self ) -> & OpenStatus {
774+ self . status . open_status ( )
782775 }
783776}
784777
785778/// Open Status: This repository is open, i.e. the password has been checked and the decryption key is available.
786779#[ derive( Debug ) ]
787780pub struct OpenStatus {
788781 /// The cache
789- cache : Option < Cache > ,
782+ pub ( crate ) cache : Option < Cache > ,
790783 /// The [`DecryptBackend`]
791784 dbe : DecryptBackend < Key > ,
792785 /// The [`ConfigFile`]
793786 config : ConfigFile ,
787+ /// The [`KeyId`] of the used key
788+ key_id : KeyId ,
794789}
795790
796791impl Open for OpenStatus {
797- /// Get the cache
798- fn cache ( & self ) -> Option < & Cache > {
799- self . cache . as_ref ( )
800- }
801-
802- /// Get the [`DecryptBackend`]
803- fn dbe ( & self ) -> & DecryptBackend < Key > {
804- & self . dbe
805- }
806-
807- /// Get the [`ConfigFile`]
808- fn config ( & self ) -> & ConfigFile {
809- & self . config
792+ fn open_status ( & self ) -> & OpenStatus {
793+ self
810794 }
811795}
812796
@@ -863,12 +847,26 @@ impl<P, S: Open> Repository<P, S> {
863847
864848 /// Get the repository configuration
865849 pub fn config ( & self ) -> & ConfigFile {
866- self . status . config ( )
850+ & self . open_status ( ) . config
867851 }
868852
869853 // TODO: add documentation!
870854 pub ( crate ) fn dbe ( & self ) -> & DecryptBackend < Key > {
871- self . status . dbe ( )
855+ & self . open_status ( ) . dbe
856+ }
857+
858+ /// Get the [`KeyId`] of the key used to open the repository
859+ pub fn key_id ( & self ) -> & KeyId {
860+ & self . open_status ( ) . key_id
861+ }
862+
863+ /// Delete the given key from the repository.
864+ ///
865+ /// # Errors
866+ ///
867+ /// * If the key could not be removed.
868+ pub fn delete_key ( & self , id : & KeyId ) -> RusticResult < ( ) > {
869+ self . dbe ( ) . remove ( FileType :: Key , id, false )
872870 }
873871}
874872
@@ -1557,16 +1555,8 @@ impl<P, S: IndexedFull> IndexedFull for Repository<P, S> {
15571555}
15581556
15591557impl < T , S : Open > Open for IndexedStatus < T , S > {
1560- fn cache ( & self ) -> Option < & Cache > {
1561- self . open . cache ( )
1562- }
1563-
1564- fn dbe ( & self ) -> & DecryptBackend < Key > {
1565- self . open . dbe ( )
1566- }
1567-
1568- fn config ( & self ) -> & ConfigFile {
1569- self . open . config ( )
1558+ fn open_status ( & self ) -> & OpenStatus {
1559+ & self . open . open_status ( )
15701560 }
15711561}
15721562
0 commit comments