Automate (re)-publication. Closes #33.

This commit is contained in:
Tim Bruijnzeels
2019-06-05 17:15:27 +02:00
parent 448c7357f0
commit 16ce1db2ec
20 changed files with 188 additions and 91 deletions
+17 -9
View File
@@ -96,16 +96,24 @@ impl<S: CaSigner> CaServer<S> {
}
}
pub fn publish_ta(&self) -> CaResult<(), S> {
let handle = ta_handle();
let ta = self.ta_store.get_latest(&handle)?;
let ta_publish_cmd = TrustAnchorCommandDetails::publish(
&handle,
self.signer.clone()
);
pub fn republish_all(&self) -> CaResult<(), S> {
self.publish_ta()
}
let events = ta.process_command(ta_publish_cmd)?;
self.ta_store.update(&handle, ta, events)?;
pub fn publish_ta(&self) -> CaResult<(), S> {
// if there is a TA, publish it
let ta_handle = ta_handle();
if let Ok(ta) = self.ta_store.get_latest(&ta_handle) {
let ta_republish = TrustAnchorCommandDetails::republish(
&ta_handle,
self.signer.clone()
);
let events = ta.process_command(ta_republish)?;
if ! events.is_empty() {
self.ta_store.update(&ta_handle, ta, events)?;
}
}
Ok(())
}
+1
View File
@@ -3,6 +3,7 @@ extern crate bytes;
extern crate core;
#[macro_use] extern crate derive_more;
extern crate hex;
#[macro_use] extern crate log;
extern crate rand;
#[macro_use] extern crate serde;
extern crate serde_json;
+32 -28
View File
@@ -1,6 +1,6 @@
//! Supports publishing signed objects.
use std::io;
use std::{io, thread};
use std::path::PathBuf;
use krill_commons::api::admin::{
@@ -77,7 +77,7 @@ impl CommandDetails for PubClientCommandDetails {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PubClient {
id: Handle,
handle: Handle,
version: u64,
server: PubServerInfo
}
@@ -89,12 +89,11 @@ impl Aggregate for PubClient {
type Error = Error;
fn init(event: Self::InitEvent) -> Result<Self, Self::Error> {
let (id, _version, details) = event.unwrap();
let id = Handle::from(id);
let (handle, _version, details) = event.unwrap();
let version = 1;
let server = details.0;
Ok (PubClient { id, version, server })
Ok (PubClient { handle, version, server })
}
fn version(&self) -> u64 {
@@ -105,7 +104,10 @@ impl Aggregate for PubClient {
unimplemented!() // no events to process, yet
}
fn process_command(&self, _command: Self::Command) -> Result<Vec<Self::Event>, Self::Error> {
fn process_command(
&self,
_command: Self::Command
) -> Result<Vec<Self::Event>, Self::Error> {
unimplemented!() // no commands to process, yet
}
}
@@ -139,30 +141,32 @@ impl PubClients {
match client.server_info() {
PubServerInfo::KrillServer(service_uri, token) => {
let service_uri = service_uri.as_str();
let uri = format!("{}publication/{}", service_uri, handle);
let service_uri = service_uri.clone();
let token = token.clone();
// Note, I could not think of a convenient way to pass down the test
// context, since there are different threads involved when testing.
// So, for now, just setting test mode whenever the publication is done
// at localhost.
if service_uri.starts_with("https://localhost") {
httpclient::TEST_MODE.with(|m| { *m.borrow_mut() = true; });
}
thread::spawn(move ||{
// Note, I could not think of a convenient way to pass down
// the test context, since there are different threads
// involved when testing. So, for now, just setting test
// mode whenever the publication is done at localhost.
if service_uri.as_str().starts_with("https://localhost") {
httpclient::TEST_MODE.with(|m| { *m.borrow_mut() = true; });
}
match httpclient::post_json(&uri, delta, Some(&token)) {
Err(httpclient::Error::ErrorWithJson(_code, err)) => {
if err.code() == 2007 || err.code() == 2008 {
// TODO, do full sync!
unimplemented!()
} else {
error!("{}", err)
}
},
Err(e) => error!("{}", e),
Ok(()) => {}
}
});
let uri = format!("{}publication/{}", service_uri, handle.as_str());
match httpclient::post_json(&uri, delta, Some(token)) {
Err(httpclient::Error::ErrorWithJson(_code, err)) => {
if err.code() == 2007 || err.code() == 2008 {
// TODO, do full sync!
unimplemented!()
} else {
panic!("{}", err)
}
},
Err(e) => panic!("{}", e),
Ok(()) => {}
}
}
}
}
+1 -1
View File
@@ -72,7 +72,7 @@ impl CaSignSupport {
// TODO for now only publish MFT and CRL, revoking old MFTs only
if let Some(mft) = old_mft {
let revocation = Revocation::from(mft);
revocations.add(revocation.clone());
revocations.add(revocation);
revocations_delta.add(revocation);
}
+16 -13
View File
@@ -119,7 +119,7 @@ impl TrustAnchorInitDetails {
cert.set_v4_resources(Some(resources.v4().deref().clone()));
cert.set_v6_resources(Some(resources.v6().deref().clone()));
cert.into_cert(signer.as_ref(), key).map_err(|e| Error::signer_error(e))
cert.into_cert(signer.as_ref(), key).map_err(Error::signer_error)
}
}
@@ -163,7 +163,7 @@ pub type TrustAnchorCommand<S> = SentCommand<TrustAnchorCommandDetails<S>>;
#[derive(Clone, Debug)]
pub enum TrustAnchorCommandDetails<S: CaSigner> {
Publish(Arc<S>)
Republish(Arc<S>)
}
impl<S: CaSigner> CommandDetails for TrustAnchorCommandDetails<S> {
@@ -171,11 +171,11 @@ impl<S: CaSigner> CommandDetails for TrustAnchorCommandDetails<S> {
}
impl<S: CaSigner> TrustAnchorCommandDetails<S> {
pub fn publish(handle: &Handle, signer: Arc<S>) -> TrustAnchorCommand<S> {
pub fn republish(handle: &Handle, signer: Arc<S>) -> TrustAnchorCommand<S> {
SentCommand::new(
&handle,
None,
TrustAnchorCommandDetails::Publish(signer)
TrustAnchorCommandDetails::Republish(signer)
)
}
}
@@ -190,7 +190,7 @@ type TaResult<R> = Result<R, Error>;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TrustAnchor<S: CaSigner> {
id: Handle,
handle: Handle,
version: u64,
repo_info: RepoInfo,
@@ -233,15 +233,19 @@ impl<S: CaSigner> TrustAnchor<S> {
impl<S: CaSigner> TrustAnchor<S> {
fn publish(&self, signer: Arc<S>) -> TaResult<Vec<TrustAnchorEvent>> {
fn republish(&self, signer: Arc<S>) -> TaResult<Vec<TrustAnchorEvent>> {
if !self.current_key.needs_publication() {
return Ok(vec![])
}
let delta = CaSignSupport::publish(
signer,
&self.current_key,
self.repo_info(),
""
).map_err(|e| Error::signer_error(e))?;
).map_err(Error::signer_error)?;
Ok(vec![TrustAnchorEventDetails::published(&self.id, self.version, delta)])
Ok(vec![TrustAnchorEventDetails::published(&self.handle, self.version, delta)])
}
}
@@ -252,8 +256,7 @@ impl<S: CaSigner> Aggregate for TrustAnchor<S> {
type Error = Error;
fn init(event: Self::InitEvent) -> Result<Self, Self::Error> {
let (id, _version, init) = event.unwrap();
let id = Handle::from(id);
let (handle, _version, init) = event.unwrap();
let version = 1; // after applying init
let repo_info = init.repo_info;
@@ -262,7 +265,7 @@ impl<S: CaSigner> Aggregate for TrustAnchor<S> {
Ok(
TrustAnchor {
id,
handle,
version,
repo_info,
current_key,
@@ -287,7 +290,7 @@ impl<S: CaSigner> Aggregate for TrustAnchor<S> {
fn process_command(&self, command: Self::Command) -> TaResult<Vec<TrustAnchorEvent>> {
match command.into_details() {
TrustAnchorCommandDetails::Publish(signer) => self.publish(signer)
TrustAnchorCommandDetails::Republish(signer) => self.republish(signer)
}
}
}
@@ -369,7 +372,7 @@ mod tests {
store.add(init).unwrap();
let ta = store.get_latest(&handle).unwrap();
let publish_cmd = TrustAnchorCommandDetails::publish(&handle, signer);
let publish_cmd = TrustAnchorCommandDetails::republish(&handle, signer);
let events = ta.process_command(publish_cmd).unwrap();
let _ta = store.update(&handle, ta, events).unwrap();