Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Commit 455923e

Browse files
committed
Implement runtime initialization for create-token
1 parent e5bd26b commit 455923e

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/bin/server.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{path::PathBuf, sync::Arc};
22

33
use clap::Parser;
44

5-
use snarf::database::snarf::{DbServerState, store_server_state};
5+
use snarf::database::snarf::store_server_state;
66
use snarf::server::ServerCommand;
77
use snarf::{
88
database::snarf::{connect_database, load_server_state},
@@ -109,6 +109,7 @@ async fn main() -> anyhow::Result<(), Box<dyn std::error::Error + Send + Sync>>
109109
let do_shutdown = Arc::new(std::sync::Mutex::new(false));
110110

111111
let do_shutdown_copy = do_shutdown.clone();
112+
let mut server_state_clone = server_state.clone();
112113
let shutdown = async move {
113114
info!("Press Cltr-C for graceful shutdown.");
114115
tokio::select! {
@@ -117,6 +118,11 @@ async fn main() -> anyhow::Result<(), Box<dyn std::error::Error + Send + Sync>>
117118
}
118119
action = command_receiver.recv() => {
119120
match action {
121+
Some(ServerCommand::MarkInitialized) => {
122+
server_state_clone.initialize();
123+
store_server_state(&db_connection, &server_state_clone.into()).expect("Updating the server state");
124+
*do_shutdown_copy.lock().unwrap() = false;
125+
},
120126
Some(ServerCommand::Shutdown) => {
121127
*do_shutdown_copy.lock().unwrap() = false;
122128
},

src/server.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::sync::Arc;
22

3-
43
use snix_castore::{blobservice::BlobService, directoryservice::DirectoryService};
54
use snix_store::{nar::NarCalculationService, pathinfoservice::PathInfoService};
65

@@ -12,6 +11,7 @@ use crate::keys::{CacheKey, PasetoKey};
1211
tonic::include_proto!("snarf.v1");
1312

1413
pub 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

9397
impl 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)]
262271
pub 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

267277
impl 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

Comments
 (0)