Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
512 changes: 483 additions & 29 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ members = [
"gateway-client",
"gateway-messages",
"gateway-sp-comms",
"internal-dns",
"internal-dns-client",
"nexus",
"nexus/src/db/db-macros",
"nexus/test-utils",
Expand All @@ -30,6 +32,8 @@ default-members = [
"gateway",
"gateway-client",
"gateway-messages",
"internal-dns",
"internal-dns-client",
"nexus",
"nexus/src/db/db-macros",
"package",
Expand Down
19 changes: 19 additions & 0 deletions internal-dns-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "internal-dns-client"
version = "0.1.0"
edition = "2021"
license = "MPL-2.0"

[dependencies]
anyhow = "1.0"
clap = { version = "3.1", features = [ "derive" ] }
progenitor = { git = "https://github.com/oxidecomputer/progenitor" }
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
slog = { version = "2.5.0", features = [ "max_level_trace", "release_max_level_debug" ] }
slog-term = "2.7"
slog-async = "2.7"
slog-envlogger = "2.2"
structopt = "0.3"
tokio = { version = "1.17", features = [ "full" ] }
reqwest = { version = "0.11", features = ["json", "rustls-tls", "stream"] }
117 changes: 117 additions & 0 deletions internal-dns-client/src/bin/dnsadm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.

use anyhow::Result;
use internal_dns_client::{
types::{DnsKv, DnsRecord, DnsRecordKey, Srv},
Client,
};
use slog::{Drain, Logger};
use std::net::Ipv6Addr;
use structopt::{clap::AppSettings::*, StructOpt};

#[derive(Debug, StructOpt)]
#[structopt(
name = "dnsadm",
about = "Administer DNS records",
global_setting(ColorAuto),
global_setting(ColoredHelp)
)]
struct Opt {
#[structopt(short, long)]
address: Option<String>,

#[structopt(short, long)]
port: Option<usize>,

#[structopt(subcommand)]
subcommand: SubCommand,
}

#[derive(Debug, StructOpt)]
enum SubCommand {
ListRecords,
AddAAAA(AddAAAACommand),
AddSRV(AddSRVCommand),
DeleteRecord(DeleteRecordCommand),
}

#[derive(Debug, StructOpt)]
struct AddAAAACommand {
name: String,
addr: Ipv6Addr,
}

#[derive(Debug, StructOpt)]
struct AddSRVCommand {
name: String,
prio: u16,
weight: u16,
port: u16,
target: String,
}

#[derive(Debug, StructOpt)]
struct DeleteRecordCommand {
name: String,
}

#[tokio::main]
async fn main() -> Result<()> {
let opt = Opt::from_args();
let log = init_logger();

let addr = match opt.address {
Some(a) => a,
None => "localhost".into(),
};
let port = opt.port.unwrap_or(5353);

let endpoint = format!("http://{}:{}", addr, port);
let client = Client::new(&endpoint, log.clone());

let opt = Opt::from_args();
match opt.subcommand {
SubCommand::ListRecords => {
let records = client.dns_records_get().await?;
println!("{:#?}", records);
}
SubCommand::AddAAAA(cmd) => {
client
.dns_records_set(&vec![DnsKv {
key: DnsRecordKey { name: cmd.name },
record: DnsRecord::Aaaa(cmd.addr),
}])
.await?;
}
SubCommand::AddSRV(cmd) => {
client
.dns_records_set(&vec![DnsKv {
key: DnsRecordKey { name: cmd.name },
record: DnsRecord::Srv(Srv {
prio: cmd.prio,
weight: cmd.weight,
port: cmd.port,
target: cmd.target,
}),
}])
.await?;
}
SubCommand::DeleteRecord(cmd) => {
client
.dns_records_delete(&vec![DnsRecordKey { name: cmd.name }])
.await?;
}
}

Ok(())
}

fn init_logger() -> Logger {
let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::FullFormat::new(decorator).build().fuse();
let drain = slog_envlogger::new(drain).fuse();
let drain = slog_async::Async::new(drain).chan_size(0x2000).build().fuse();
slog::Logger::root(drain, slog::o!())
}
18 changes: 18 additions & 0 deletions internal-dns-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.

progenitor::generate_api!(
spec = "../openapi/internal-dns.json",
inner_type = slog::Logger,
pre_hook = (|log: &slog::Logger, request: &reqwest::Request| {
slog::debug!(log, "client request";
"method" => %request.method(),
"uri" => %request.url(),
"body" => ?&request.body(),
);
}),
post_hook = (|log: &slog::Logger, result: &Result<_, _>| {
slog::debug!(log, "client response"; "result" => ?result);
}),
);
36 changes: 36 additions & 0 deletions internal-dns/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "internal-dns"
version = "0.1.0"
edition = "2021"
license = "MPL-2.0"

[dependencies]
anyhow = "1.0"
clap = { version = "3.1", features = [ "derive" ] }
dropshot = { git = "https://github.com/oxidecomputer/dropshot" }
pretty-hex = "0.2.1"
schemars = "0.8"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
sled = "0.34"
slog = { version = "2.5.0", features = [ "max_level_trace", "release_max_level_debug" ] }
slog-term = "2.7"
slog-async = "2.7"
slog-envlogger = "2.2"
structopt = "0.3"
tempdir = "0.3"
tokio = { version = "1.17", features = [ "full" ] }
toml = "0.5"
trust-dns-proto = "0.21"
trust-dns-server = "0.21"

[dev-dependencies]
expectorate = "1.0.4"
internal-dns-client = { path = "../internal-dns-client" }
omicron-test-utils = { path = "../test-utils" }
openapiv3 = "1.0"
openapi-lint = { git = "https://github.com/oxidecomputer/openapi-lint", branch = "main" }
portpicker = "0.1"
serde_json = "1.0"
subprocess = "0.2.8"
trust-dns-resolver = "0.21"
27 changes: 27 additions & 0 deletions internal-dns/src/bin/apigen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.

use anyhow::{bail, Result};
use internal_dns::dropshot_server::api;
use std::fs::File;
use std::io;

fn usage(args: &Vec<String>) -> String {
format!("{} [output path]", args[0])
}

fn main() -> Result<()> {
let args: Vec<String> = std::env::args().collect();

let mut out = match args.len() {
1 => Box::new(io::stdout()) as Box<dyn io::Write>,
2 => Box::new(File::create(args[1].clone())?) as Box<dyn io::Write>,
_ => bail!(usage(&args)),
};

let api = api();
let openapi = api.openapi("Internal DNS", "v0.1.0");
openapi.write(&mut out)?;
Ok(())
}
56 changes: 56 additions & 0 deletions internal-dns/src/bin/dns-server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.

// See RFD 248
// See https://github.com/oxidecomputer/omicron/issues/718
//
// Milestones:
// - Dropshot server
// - Sqlite task
// - DNS task

use anyhow::anyhow;
use anyhow::Context;
use clap::Parser;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Parser, Debug)]
struct Args {
#[clap(long)]
config_file: PathBuf,
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let args = Args::parse();
let config_file = &args.config_file;
let config_file_contents = std::fs::read_to_string(config_file)
.with_context(|| format!("read config file {:?}", config_file))?;
let config: internal_dns::Config = toml::from_str(&config_file_contents)
.with_context(|| format!("parse config file {:?}", config_file))?;
eprintln!("{:?}", config);

let log = config
.log
.to_logger("internal-dns")
.context("failed to create logger")?;

let db = Arc::new(sled::open(&config.data.storage_path)?);

{
let db = db.clone();
let log = log.clone();
let config = config.dns.clone();

tokio::spawn(async move {
internal_dns::dns_server::run(log, db, config).await
});
}

let server = internal_dns::start_server(config, log, db).await?;
server
.await
.map_err(|error_message| anyhow!("server exiting: {}", error_message))
}
Loading