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
+2 -2
View File
@@ -85,9 +85,9 @@ pub struct Authorizer {
}
impl Authorizer {
pub fn new(krill_auth_token: &str) -> Self {
pub fn new(krill_auth_token: &Token) -> Self {
Authorizer {
krill_auth_token: Token::from(krill_auth_token)
krill_auth_token: krill_auth_token.clone()
}
}
+5 -4
View File
@@ -13,6 +13,7 @@ use serde::{Deserialize, Deserializer};
use toml;
use krill_commons::util::ext_serde;
use crate::http::ssl;
use krill_commons::api::admin::Token;
const SERVER_NAME: &str = "Krill";
@@ -35,11 +36,11 @@ impl ConfigDefaults {
fn log_type() -> LogType { LogType::Stderr }
fn syslog_facility() -> Facility { Facility::LOG_DAEMON }
fn log_file() -> PathBuf { PathBuf::from("./krill.log")}
fn auth_token() -> String {
fn auth_token() -> Token {
use std::env;
match env::var("KRILL_AUTH_TOKEN") {
Ok(token) => token,
Ok(token) => Token::from(token),
Err(_) => {
eprintln!("You MUST provide a value for the master API key, either by setting \"auth_token\" in the config file, or by setting the KRILL_AUTH_TOKEN environment variable.");
::std::process::exit(1);
@@ -101,7 +102,7 @@ pub struct Config {
log_file: PathBuf,
#[serde(default = "ConfigDefaults::auth_token")]
pub auth_token: String
pub auth_token: Token
}
/// # Accessors
@@ -164,7 +165,7 @@ impl Config {
let mut log_file = data_dir.clone();
log_file.push("krill.log");
let syslog_facility = ConfigDefaults::syslog_facility();
let auth_token = "secret".to_string();
let auth_token = Token::from("secret");
Config {
ip,
+2 -2
View File
@@ -216,8 +216,8 @@ pub fn init_trust_anchor(req: &HttpRequest) -> HttpResponse {
render_empty_res(rw_server(req).init_trust_anchor())
}
pub fn publish_trust_anchor(req: &HttpRequest) -> HttpResponse {
render_empty_res(ro_server(req).publish_trust_anchor())
pub fn republish_all(req: &HttpRequest) -> HttpResponse {
render_empty_res(ro_server(req).republish_all())
}
pub fn tal(req: &HttpRequest) -> HttpResponse {
+6 -5
View File
@@ -16,7 +16,7 @@ use actix_web::http::{Method, StatusCode};
use bcder::decode;
use futures::Future;
use openssl::ssl::{SslMethod, SslAcceptor, SslAcceptorBuilder, SslFiletype};
use crate::auth::{self, Authorizer, CheckAuthorisation, Credentials};
use crate::auth::{self, CheckAuthorisation, Credentials};
use crate::config::Config;
use crate::endpoints;
use crate::http::ssl;
@@ -70,10 +70,12 @@ impl PubServerApp {
r.method(Method::POST).f(endpoints::init_trust_anchor);
})
.resource("/api/v1/publish/ta", |r| {
r.method(Method::POST).f(endpoints::publish_trust_anchor)
.resource("/api/v1/republish", |r| {
r.method(Method::POST).f(endpoints::republish_all)
})
.resource("/ta/ta.tal", |r| {
r.method(Method::GET).f(endpoints::tal);
})
@@ -135,14 +137,13 @@ impl PubServerApp {
pub fn create_server(
config: &Config
) -> Result<Arc<RwLock<KrillServer>>, Error> {
let authorizer = Authorizer::new(&config.auth_token);
let pub_server = KrillServer::build(
&config.data_dir,
&config.rsync_base,
config.service_uri(),
&config.rrdp_base_uri,
authorizer,
&config.auth_token,
)?;
Ok(Arc::new(RwLock::new(pub_server)))
+30 -6
View File
@@ -22,6 +22,8 @@ use krill_pubd::PubServer;
use krill_pubd::publishers::Publisher;
use crate::auth::Authorizer;
use republisher::Republisher;
//------------ KrillServer ---------------------------------------------------
@@ -58,7 +60,12 @@ pub struct KrillServer {
caserver: CaServer<OpenSslSigner>,
// CMS+XML proxy server for non-Krill clients
proxy_server: ProxyServer
proxy_server: ProxyServer,
// Responsible for republishing periodically
#[allow(dead_code)] // keep this in scope
republisher: Republisher
}
/// # Set up and initialisation
@@ -70,11 +77,13 @@ impl KrillServer {
base_uri: &uri::Rsync,
service_uri: uri::Https,
rrdp_base_uri: &uri::Https,
authorizer: Authorizer,
token: &Token,
) -> Result<Self, Error> {
let mut repo_dir = work_dir.clone();
repo_dir.push("repo");
let authorizer = Authorizer::new(token);
let pubserver = PubServer::build(
base_uri.clone(),
rrdp_base_uri.clone(),
@@ -91,6 +100,11 @@ impl KrillServer {
let pub_clients = Arc::new(PubClients::build(work_dir)?);
let caserver = CaServer::build(work_dir, pub_clients.clone(), signer)?;
let republisher = {
let publish_uri = format!("{}api/v1/republish", service_uri);
Republisher::new(publish_uri, token)
};
Ok(
KrillServer {
service_uri,
@@ -99,7 +113,8 @@ impl KrillServer {
pubserver,
pub_clients,
caserver,
proxy_server
proxy_server,
republisher
}
)
}
@@ -270,11 +285,20 @@ impl KrillServer {
self.pub_clients.add(req)?;
// Add TA
self.caserver.init_ta(repo_info, ta_aia, vec![ta_uri]).map_err(Error::CaServerError)
self.caserver.init_ta(
repo_info,
ta_aia,
vec![ta_uri]
).map_err(Error::CaServerError)?;
// Force initial publication
self.caserver.publish_ta()?;
Ok(())
}
pub fn publish_trust_anchor(&self) -> Result<(), Error> {
self.caserver.publish_ta()?;
pub fn republish_all(&self) -> Result<(), Error> {
self.caserver.republish_all()?;
Ok(())
}
}
+2
View File
@@ -5,6 +5,7 @@ extern crate bytes;
extern crate bcder;
extern crate chrono;
extern crate clap;
extern crate clokwerk;
extern crate core;
#[macro_use] extern crate derive_more;
extern crate futures;
@@ -34,6 +35,7 @@ pub mod config;
pub mod endpoints;
pub mod krillserver;
pub mod http;
mod republisher;
pub mod test;
+39
View File
@@ -0,0 +1,39 @@
use clokwerk::{Scheduler, ScheduleHandle, TimeUnits};
use std::time::Duration;
use krill_commons::util::httpclient;
use krill_commons::api::admin::Token;
/// This type is responsible for periodically calling the
/// API to republish all CAs. Only CAs that *need* to republish
/// will do so (i.e. if there are no changes, and the nextUpdate
/// is still comfortably far in the future, this is a no-op).
///
/// This is done by calling the actual HTTPS end-point. While
/// this may seem somewhat convoluted, this eliminates the need
/// for this type to share state with the main application.
pub struct Republisher {
// Responsible for background tasks, e.g. re-publishing
#[allow(dead_code)] // just need to keep this in scope
tasks_thread: ScheduleHandle
}
impl Republisher {
pub fn new(publish_trigger_uri: String, token: &Token) -> Self {
let token = token.clone();
let mut scheduler = Scheduler::new();
scheduler.every(5.seconds()).run(move || {
if let Err(e) = httpclient::post_empty(
&publish_trigger_uri,
Some(&token)
) {
error!("Could not publish: {}", e);
}
});
Republisher {
tasks_thread: scheduler.watch_thread(Duration::from_millis(100))
}
}
}