22
33use std:: {
44 collections:: hash_map,
5- fs,
5+ error:: Error ,
6+ fmt, fs,
67 io:: { self , BufReader } ,
78 sync:: Arc ,
89} ;
910
1011use bytes:: Bytes ;
1112use camino:: Utf8Path ;
12- use domain:: base:: Name ;
13+ use domain:: { base:: Name , dep :: octseq :: Array } ;
1314use serde:: { Deserialize , Serialize } ;
1415use tracing:: warn;
1516
1617use crate :: {
1718 loader:: zone:: LoaderState ,
1819 policy:: { Policy , PolicyVersion } ,
20+ tsig:: TsigStore ,
1921 zone:: ZoneState ,
2022} ;
2123
@@ -39,7 +41,8 @@ impl Spec {
3941 self ,
4042 zone_name : & Name < Bytes > ,
4143 policies : & mut foldhash:: HashMap < Box < str > , Policy > ,
42- ) -> ZoneState {
44+ tsig_store : & TsigStore ,
45+ ) -> Result < ZoneState , LoadError > {
4346 /// Synchronize a loaded policy with global state.
4447 fn sync_policy (
4548 known_version : PolicyVersion ,
@@ -93,7 +96,9 @@ impl Spec {
9396 history,
9497 } ) => {
9598 let loader = LoaderState {
96- source : source. parse ( ) ,
99+ source : source
100+ . parse ( tsig_store)
101+ . map_err ( LoadError :: MissingSourceTsigKey ) ?,
97102 ..Default :: default ( )
98103 } ;
99104
@@ -106,7 +111,7 @@ impl Spec {
106111 }
107112 let policy = policy. map ( |p| p. latest . clone ( ) ) ;
108113
109- ZoneState {
114+ Ok ( ZoneState {
110115 policy,
111116 min_expiration,
112117 next_min_expiration,
@@ -119,7 +124,7 @@ impl Spec {
119124 loader,
120125 history,
121126 ..Default :: default ( )
122- }
127+ } )
123128 }
124129 }
125130 }
@@ -146,3 +151,65 @@ impl Spec {
146151 crate :: util:: write_file ( path, text. as_bytes ( ) )
147152 }
148153}
154+
155+ //============ Errors ==========================================================
156+
157+ //----------- LoadError --------------------------------------------------------
158+
159+ /// An error loading a zone state file.
160+ #[ derive( Debug ) ]
161+ pub enum LoadError {
162+ /// The file could not be read.
163+ Read {
164+ /// The path being read from.
165+ path : Box < Utf8Path > ,
166+
167+ /// The I/O error.
168+ error : io:: Error ,
169+ } ,
170+
171+ /// The TSIG key for the zone source could not be found.
172+ MissingSourceTsigKey ( MissingTsigKeyError ) ,
173+ }
174+
175+ impl Error for LoadError {
176+ fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
177+ match self {
178+ Self :: Read { error, .. } => Some ( error) ,
179+ Self :: MissingSourceTsigKey ( error) => Some ( error) ,
180+ }
181+ }
182+ }
183+
184+ impl fmt:: Display for LoadError {
185+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
186+ match self {
187+ Self :: Read { path, error } => {
188+ write ! ( f, "could not read the zone state file '{path}': {error}" )
189+ }
190+ Self :: MissingSourceTsigKey ( error) => {
191+ write ! ( f, "could not load the zone source setting: {error}" )
192+ }
193+ }
194+ }
195+ }
196+
197+ //----------- MissingTsigKeyError ----------------------------------------------
198+
199+ /// A TSIG key could not be found.
200+ ///
201+ /// A zone's state file indicated that it was using a TSIG key, but the key
202+ /// could not be found in the configured TSIG key store.
203+ #[ derive( Clone , Debug ) ]
204+ pub struct MissingTsigKeyError {
205+ /// The name of the TSIG key.
206+ pub name : Box < Name < Array < 255 > > > ,
207+ }
208+
209+ impl Error for MissingTsigKeyError { }
210+
211+ impl fmt:: Display for MissingTsigKeyError {
212+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
213+ write ! ( f, "TSIG key '{}' could not be found" , self . name)
214+ }
215+ }
0 commit comments