[control] Phase 2 control plane: routes repo via git, journaled apply with trial/watch/revert, undo and restore, push with retry, share links, API tokens, REST API with OpenAPI, --dev
This commit is contained in:
+183
-5
@@ -2,11 +2,189 @@
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! Placeholder for ianitor-control (ROADMAP phase 2). It deliberately opens no socket.
|
||||
//! ```text
|
||||
//! ianitor-control --config <control.toml> run the control plane
|
||||
//! ianitor-control --dev [--edge-bin <ianitor-edge>] temporary everything on 127.0.0.1
|
||||
//! ianitor-control token create --name <name> [--expires-days <n> | --never] (--config <c> | --state-dir <d>)
|
||||
//! ianitor-control token list (--config <c> | --state-dir <d>)
|
||||
//! ianitor-control token revoke <id> (--config <c> | --state-dir <d>)
|
||||
//! ianitor-control openapi print the OpenAPI document
|
||||
//! ianitor-control check --config <control.toml> check the config and exit
|
||||
//! ```
|
||||
//! The log level comes from `$IANITOR_LOG`. As `GIT_ASKPASS` (with `$IANITOR_ASKPASS=1`),
|
||||
//! the binary answers git's prompt from the push credential file.
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"ianitor-control {}: placeholder. The control plane (API on 127.0.0.1) arrives in phase 2.",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
use std::path::PathBuf;
|
||||
use std::process::ExitCode;
|
||||
|
||||
use ianitor_control::{Config, logger, tokens::Tokens};
|
||||
|
||||
mod dev;
|
||||
|
||||
fn usage() -> ExitCode {
|
||||
eprintln!(
|
||||
"usage: ianitor-control --config <control.toml> | --dev [--edge-bin <path>] | \
|
||||
token create --name <name> [--expires-days <n> | --never] (--config <c> | --state-dir <d>) | \
|
||||
token list (...) | token revoke <id> (...) | openapi | check --config <c>"
|
||||
);
|
||||
ExitCode::from(2)
|
||||
}
|
||||
|
||||
fn askpass() -> ExitCode {
|
||||
// git asks "Username for 'https://…':" and "Password for 'https://…':".
|
||||
let prompt = std::env::args().nth(1).unwrap_or_default();
|
||||
if prompt.starts_with("Username") {
|
||||
println!(
|
||||
"{}",
|
||||
std::env::var("IANITOR_ASKPASS_USER").unwrap_or_default()
|
||||
);
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
let Some(file) = std::env::var_os("IANITOR_ASKPASS_FILE") else {
|
||||
return ExitCode::FAILURE;
|
||||
};
|
||||
match std::fs::read_to_string(file) {
|
||||
Ok(v) => {
|
||||
println!("{}", v.trim_end_matches(['\r', '\n']));
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Err(_) => ExitCode::FAILURE,
|
||||
}
|
||||
}
|
||||
|
||||
fn state_dir(args: &[String]) -> Result<PathBuf, String> {
|
||||
let mut it = args.iter();
|
||||
while let Some(a) = it.next() {
|
||||
match a.as_str() {
|
||||
"--state-dir" => {
|
||||
return it
|
||||
.next()
|
||||
.map(PathBuf::from)
|
||||
.ok_or("--state-dir needs a path".into());
|
||||
}
|
||||
"--config" => {
|
||||
let p = it.next().ok_or("--config needs a path")?;
|
||||
return Config::load(&PathBuf::from(p))
|
||||
.map(|c| c.state_dir)
|
||||
.map_err(|e| e.to_string());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Err("give --config <control.toml> or --state-dir <dir>".into())
|
||||
}
|
||||
|
||||
fn token(args: &[String]) -> Result<(), String> {
|
||||
let dir = state_dir(args)?;
|
||||
std::fs::create_dir_all(&dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
||||
let tokens = Tokens::new(&dir);
|
||||
match args.first().map(String::as_str) {
|
||||
Some("create") => {
|
||||
let mut name = None;
|
||||
let mut expires = Some(ianitor_control::util::now() + 365 * 86_400);
|
||||
let mut it = args.iter().skip(1);
|
||||
while let Some(a) = it.next() {
|
||||
match a.as_str() {
|
||||
"--name" => name = it.next().cloned(),
|
||||
"--never" => expires = None,
|
||||
"--expires-days" => {
|
||||
let d: u64 = it
|
||||
.next()
|
||||
.and_then(|d| d.parse().ok())
|
||||
.filter(|d| (1..=3650).contains(d))
|
||||
.ok_or("--expires-days must be 1 to 3650")?;
|
||||
expires = Some(ianitor_control::util::now() + d * 86_400);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let name = name.ok_or("--name <name> is required")?;
|
||||
let (rec, value) = tokens.create(&name, expires).map_err(|e| {
|
||||
if e == "token.name" {
|
||||
"the name must be 1 to 64 letters, digits, spaces, dots, dashes or underscores"
|
||||
.to_owned()
|
||||
} else {
|
||||
e
|
||||
}
|
||||
})?;
|
||||
eprintln!(
|
||||
"Token {} ({}) created. It is shown once; save it in Custos now:",
|
||||
rec.id, rec.name
|
||||
);
|
||||
println!("{value}");
|
||||
Ok(())
|
||||
}
|
||||
Some("list") => {
|
||||
for t in tokens.list()? {
|
||||
println!(
|
||||
"{} {} created {}{}{}",
|
||||
t.id,
|
||||
t.name,
|
||||
ianitor_control::util::rfc3339(t.created_at),
|
||||
t.expires_at
|
||||
.map(|e| format!(" expires {}", ianitor_control::util::rfc3339(e)))
|
||||
.unwrap_or_default(),
|
||||
t.revoked_at
|
||||
.map(|e| format!(" REVOKED {}", ianitor_control::util::rfc3339(e)))
|
||||
.unwrap_or_default()
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some("revoke") => {
|
||||
let id = args.get(1).ok_or("token revoke <id>")?;
|
||||
match tokens.revoke(id)? {
|
||||
Some(_) => {
|
||||
eprintln!("Token {id} revoked.");
|
||||
Ok(())
|
||||
}
|
||||
None => Err(format!("there is no token {id}")),
|
||||
}
|
||||
}
|
||||
_ => Err("token create | list | revoke".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
if std::env::var("IANITOR_ASKPASS").as_deref() == Ok("1") {
|
||||
return askpass();
|
||||
}
|
||||
logger::init(std::env::var("IANITOR_LOG").ok().as_deref());
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let rt = match tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
{
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
eprintln!("ianitor-control: cannot start: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
let result = match args.first().map(String::as_str) {
|
||||
Some("token") => token(&args[1..]),
|
||||
Some("openapi") => {
|
||||
println!("{}", ianitor_control::api::openapi_json());
|
||||
Ok(())
|
||||
}
|
||||
Some("check") => state_dir(&args[1..]).map(|_| eprintln!("config is valid")),
|
||||
Some("--dev") => rt.block_on(dev::run(&args[1..])),
|
||||
Some("--config") => match args.get(1) {
|
||||
Some(p) => match Config::load(&PathBuf::from(p)) {
|
||||
Ok(cfg) => rt.block_on(ianitor_control::serve(cfg, ianitor_control::stop_signal())),
|
||||
Err(e) => Err(e.to_string()),
|
||||
},
|
||||
None => return usage(),
|
||||
},
|
||||
Some("--help" | "-h") | None => return usage(),
|
||||
Some(_) => return usage(),
|
||||
};
|
||||
match result {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(e) => {
|
||||
log::error!(target: "ianitor::control", "{e}");
|
||||
eprintln!("ianitor-control: {e}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user