Skip to content

Commit

Permalink
Make KeyValueStore async. Implementations are still sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Bruijnzeels committed Nov 19, 2023
1 parent 764e88a commit 0656251
Show file tree
Hide file tree
Showing 56 changed files with 2,904 additions and 2,368 deletions.
43 changes: 27 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exclude = [
]

[dependencies]
async-trait = "0.1.74"
backoff = { version = "0.3.0", optional = true }
base64 = "^0.13"
basic-cookies = { version = "^0.1", optional = true }
Expand Down Expand Up @@ -54,7 +55,12 @@ regex = { version = "1.5.5", optional = true, default_features = false, features
] }
reqwest = { version = "0.11", features = ["json"] }
rpassword = { version = "^5.0", optional = true }
rpki = { version = "0.17.2", features = ["ca", "compat", "rrdp"] }
# rpki = { version = "0.17.2", features = ["ca", "compat", "rrdp"] }
rpki = { version = "0.17.3-dev", path = "../rpki-rs", features = [
"ca",
"compat",
"rrdp",
] }
# rpki = { version = "0.16.0-dev", git = "https://github.com/nLnetLabs/rpki-rs", branch = "csr-ca-repo-trailing-slash", features = [ "ca", "compat", "rrdp" ] }
scrypt = { version = "^0.6", optional = true, default-features = false }
serde = { version = "^1.0", features = ["derive", "rc"] }
Expand Down
7 changes: 4 additions & 3 deletions src/bin/krillup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use krill::{
};
use url::Url;

fn main() {
#[tokio::main]
async fn main() {
let matches = make_matches();

match parse_matches(matches) {
Expand All @@ -36,7 +37,7 @@ fn main() {
}
};

match prepare_upgrade_data_migrations(UpgradeMode::PrepareOnly, &config, &properties_manager) {
match prepare_upgrade_data_migrations(UpgradeMode::PrepareOnly, &config, &properties_manager).await {
Err(e) => {
eprintln!("*** Error Preparing Data Migration ***");
eprintln!("{}", e);
Expand All @@ -62,7 +63,7 @@ fn main() {
}
}
KrillUpMode::Migrate { config, target } => {
if let Err(e) = migrate(config, target) {
if let Err(e) = migrate(config, target).await {
eprintln!("*** Error Migrating DATA ***");
eprintln!("{}", e);
eprintln!();
Expand Down
40 changes: 21 additions & 19 deletions src/cli/ta_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,11 @@ impl TrustAnchorClient {
let signer_manager = TrustAnchorSignerManager::create(signer_command.config)?;

match signer_command.details {
SignerCommandDetails::Init(info) => signer_manager.init(info),
SignerCommandDetails::ShowInfo => signer_manager.show(),
SignerCommandDetails::ProcessRequest(request) => signer_manager.process(request),
SignerCommandDetails::ShowLastResponse => signer_manager.show_last_response(),
SignerCommandDetails::ShowExchanges => signer_manager.show_exchanges(),
SignerCommandDetails::Init(info) => signer_manager.init(info).await,
SignerCommandDetails::ShowInfo => signer_manager.show().await,
SignerCommandDetails::ProcessRequest(request) => signer_manager.process(request).await,
SignerCommandDetails::ShowLastResponse => signer_manager.show_last_response().await,
SignerCommandDetails::ShowExchanges => signer_manager.show_exchanges().await,
}
}
}
Expand Down Expand Up @@ -1025,8 +1025,8 @@ impl TrustAnchorSignerManager {
})
}

fn init(&self, info: SignerInitInfo) -> Result<TrustAnchorClientApiResponse, TaClientError> {
if self.store.has(&self.ta_handle)? {
async fn init(&self, info: SignerInitInfo) -> Result<TrustAnchorClientApiResponse, TaClientError> {
if self.store.has(&self.ta_handle).await? {
Err(TaClientError::other("Trust Anchor Signer was already initialised."))
} else {
let cmd = TrustAnchorSignerInitCommand::new(
Expand All @@ -1043,40 +1043,41 @@ impl TrustAnchorSignerManager {
&self.actor,
);

self.store.add(cmd)?;
self.store.add(cmd).await?;

Ok(TrustAnchorClientApiResponse::Empty)
}
}

fn show(&self) -> Result<TrustAnchorClientApiResponse, TaClientError> {
let ta_signer = self.get_signer()?;
async fn show(&self) -> Result<TrustAnchorClientApiResponse, TaClientError> {
let ta_signer = self.get_signer().await?;
let info = ta_signer.get_signer_info();
Ok(TrustAnchorClientApiResponse::TrustAnchorProxySignerInfo(info))
}

fn process(&self, request: TrustAnchorSignedRequest) -> Result<TrustAnchorClientApiResponse, TaClientError> {
async fn process(&self, request: TrustAnchorSignedRequest) -> Result<TrustAnchorClientApiResponse, TaClientError> {
let cmd = TrustAnchorSignerCommand::make_process_request_command(
&self.ta_handle,
request,
self.config.timing_config,
self.signer.clone(),
&self.actor,
);
self.store.command(cmd)?;
self.store.command(cmd).await?;

self.show_last_response()
self.show_last_response().await
}

fn show_last_response(&self) -> Result<TrustAnchorClientApiResponse, TaClientError> {
self.get_signer()?
async fn show_last_response(&self) -> Result<TrustAnchorClientApiResponse, TaClientError> {
self.get_signer()
.await?
.get_latest_exchange()
.map(|exchange| TrustAnchorClientApiResponse::SignerResponse(exchange.response.clone()))
.ok_or_else(|| TaClientError::other("No response found."))
}

fn show_exchanges(&self) -> Result<TrustAnchorClientApiResponse, TaClientError> {
let signer = self.get_signer()?;
async fn show_exchanges(&self) -> Result<TrustAnchorClientApiResponse, TaClientError> {
let signer = self.get_signer().await?;
// In this context it's okay to clone the exchanges.
// If we are afraid that this would become too expensive, then we will
// need to rethink the model where we return data in the enum that we
Expand All @@ -1089,10 +1090,11 @@ impl TrustAnchorSignerManager {
Ok(TrustAnchorClientApiResponse::ProxySignerExchanges(exchanges))
}

fn get_signer(&self) -> Result<Arc<TrustAnchorSigner>, TaClientError> {
if self.store.has(&self.ta_handle)? {
async fn get_signer(&self) -> Result<Arc<TrustAnchorSigner>, TaClientError> {
if self.store.has(&self.ta_handle).await? {
self.store
.get_latest(&self.ta_handle)
.await
.map_err(TaClientError::KrillError)
} else {
Err(TaClientError::other("Trust Anchor Signer is not initialised."))
Expand Down
35 changes: 17 additions & 18 deletions src/commons/api/ca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2192,29 +2192,28 @@ mod test {
assert_eq!(base_uri(), signed_objects_uri)
}

#[test]
fn mft_uri() {
test::test_in_memory(|storage_uri| {
let signer = OpenSslSigner::build(storage_uri, "dummy", None).unwrap();
let key_id = signer.create_key(PublicKeyFormat::Rsa).unwrap();
let pub_key = signer.get_key_info(&key_id).unwrap();
#[tokio::test]
async fn mft_uri() {
let storage_uri = test::mem_storage();
let signer = OpenSslSigner::build(&storage_uri, "dummy", None).unwrap();
let key_id = signer.create_key(PublicKeyFormat::Rsa).await.unwrap();
let pub_key = signer.get_key_info(&key_id).await.unwrap();

let mft_uri = info().resolve("", ObjectName::mft_for_key(&pub_key.key_identifier()).as_ref());
let mft_uri = info().resolve("", ObjectName::mft_for_key(&pub_key.key_identifier()).as_ref());

let mft_path = mft_uri.relative_to(&base_uri()).unwrap();
let mft_path = mft_uri.relative_to(&base_uri()).unwrap();

assert_eq!(44, mft_path.len());
assert_eq!(44, mft_path.len());

// the file name should be the hexencoded pub key info
// not repeating that here, but checking that the name
// part is validly hex encoded.
let name = &mft_path[..40];
hex::decode(name).unwrap();
// the file name should be the hexencoded pub key info
// not repeating that here, but checking that the name
// part is validly hex encoded.
let name = &mft_path[..40];
hex::decode(name).unwrap();

// and the extension is '.mft'
let ext = &mft_path[40..];
assert_eq!(ext, ".mft");
});
// and the extension is '.mft'
let ext = &mft_path[40..];
assert_eq!(ext, ".mft");
}

#[test]
Expand Down
Loading

0 comments on commit 0656251

Please sign in to comment.