11use std:: sync:: Arc ;
22
3-
43use snix_castore:: { blobservice:: BlobService , directoryservice:: DirectoryService } ;
54use snix_store:: { nar:: NarCalculationService , pathinfoservice:: PathInfoService } ;
65
@@ -12,6 +11,7 @@ use crate::keys::{CacheKey, PasetoKey};
1211tonic:: include_proto!( "snarf.v1" ) ;
1312
1413pub enum ServerCommand {
14+ MarkInitialized ,
1515 Shutdown ,
1616}
1717
@@ -88,6 +88,10 @@ impl ServerState {
8888 pub fn cache_key ( & self ) -> CacheKey {
8989 self . cache_key . clone ( )
9090 }
91+
92+ pub fn initialize ( & mut self ) {
93+ self . initialized = true ;
94+ }
9195}
9296
9397impl Default for ServerState {
@@ -232,16 +236,17 @@ pub fn server_routes(
232236 path_info_service : Arc < dyn PathInfoService > ,
233237 nar_calculation_service : Box < dyn NarCalculationService > ,
234238) -> tonic:: service:: Routes {
239+ let authenticator = PasetoAuthInterceptor :: from ( & server_state. paseto_key ) ;
235240 tonic:: service:: Routes :: new (
236241 snix_castore:: proto:: blob_service_server:: BlobServiceServer :: with_interceptor (
237242 snix_castore:: proto:: GRPCBlobServiceWrapper :: new ( blob_service) ,
238- PasetoAuthInterceptor :: from ( server_state ) ,
243+ authenticator . clone ( ) ,
239244 ) ,
240245 )
241246 . add_service (
242247 snix_castore:: proto:: directory_service_server:: DirectoryServiceServer :: with_interceptor (
243248 snix_castore:: proto:: GRPCDirectoryServiceWrapper :: new ( directory_service) ,
244- PasetoAuthInterceptor :: from ( server_state ) ,
249+ authenticator . clone ( ) ,
245250 ) ,
246251 )
247252 . add_service (
@@ -250,25 +255,35 @@ pub fn server_routes(
250255 path_info_service. clone ( ) ,
251256 nar_calculation_service,
252257 ) ,
253- PasetoAuthInterceptor :: from ( server_state ) ,
258+ authenticator . clone ( ) ,
254259 ) ,
255260 )
256261 . add_service ( management_service_server:: ManagementServiceServer :: new (
257- ManagementServiceServer :: new ( command_channel, server_state) ,
262+ ManagementServiceServer :: new (
263+ command_channel,
264+ & server_state. paseto_key ,
265+ server_state. initialized ,
266+ ) ,
258267 ) )
259268}
260269
261270#[ derive( Clone ) ]
262271pub struct ManagementServiceServer {
263- server_state : ServerState ,
264- _command_channel : mpsc:: Sender < ServerCommand > ,
272+ initialized : bool ,
273+ paseto_key : PasetoKey ,
274+ command_channel : mpsc:: Sender < ServerCommand > ,
265275}
266276
267277impl ManagementServiceServer {
268- fn new ( command_channel : & mpsc:: Sender < ServerCommand > , server_state : & ServerState ) -> Self {
278+ fn new (
279+ command_channel : & mpsc:: Sender < ServerCommand > ,
280+ paseto_key : & PasetoKey ,
281+ initialized : bool ,
282+ ) -> Self {
269283 Self {
270- server_state : server_state. clone ( ) ,
271- _command_channel : command_channel. clone ( ) ,
284+ initialized,
285+ paseto_key : paseto_key. clone ( ) ,
286+ command_channel : command_channel. clone ( ) ,
272287 }
273288 }
274289}
@@ -278,17 +293,22 @@ impl management_service_server::ManagementService for ManagementServiceServer {
278293 async fn create_client_token (
279294 & self ,
280295 _: tonic:: Request < NewClientTokenRequest > ,
281- ) -> Result < tonic:: Response < ClientToken > , tonic:: Status > {
296+ ) -> anyhow :: Result < tonic:: Response < ClientToken > , tonic:: Status > {
282297 // TODO: check token
283- if self . server_state . initialized {
298+ if self . initialized {
284299 return Err ( tonic:: Status :: permission_denied (
285300 "Server is already initialized" ,
286301 ) ) ;
287302 }
288303
304+ self . command_channel
305+ . send ( ServerCommand :: MarkInitialized )
306+ . await
307+ . map_err ( |_| tonic:: Status :: internal ( "Unable to mark initialized" ) ) ?;
308+
289309 Ok ( tonic:: Response :: new ( ClientToken {
290310 token : self
291- . server_state
311+ . paseto_key
292312 . public_token ( )
293313 . map_err ( |_| tonic:: Status :: internal ( "Unable to generate token from state" ) ) ?,
294314 } ) )
0 commit comments