Skip to content

Commit

Permalink
Run multiple backends (project-oak#1918)
Browse files Browse the repository at this point in the history
This change:
- Adds the ability to run multiple backend servers in Oak examples
- Makes running `applications` not mandatory (for examples that do not involve Oak modules)
  • Loading branch information
ipetr0v committed Mar 25, 2021
1 parent 57d9652 commit ff213b0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 51 deletions.
11 changes: 10 additions & 1 deletion examples/aggregator/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ use tonic::{
#[derive(StructOpt, Clone)]
#[structopt(about = "Aggregator Backend")]
pub struct Opt {
#[structopt(
long,
help = "Address to listen on for the gRPC server.",
default_value = "[::]:8888"
)]
grpc_listen_address: String,
#[structopt(long, help = "Private RSA key file used by gRPC server.")]
grpc_tls_private_key: String,
#[structopt(
Expand Down Expand Up @@ -75,7 +81,10 @@ async fn main() -> anyhow::Result<()> {

let identity = Identity::from_pem(certificate, private_key);

let address = "[::]:8888".parse().context("Couldn't parse address")?;
let address = opt
.grpc_listen_address
.parse()
.context("Couldn't parse address")?;
let handler = AggregatorBackend::default();

info!("Starting the backend server at {:?}", address);
Expand Down
3 changes: 3 additions & 0 deletions examples/aggregator/example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name = "aggregator"

[backends]
backend = { Cargo = { cargo_manifest = "examples/aggregator/backend/Cargo.toml" }, additional_args = [
"--grpc-listen-address=[::]:8888",
"--grpc-tls-private-key=./examples/certs/local/local.key",
"--grpc-tls-certificate=./examples/certs/local/local.pem",
] }
Expand Down
3 changes: 3 additions & 0 deletions examples/proxy_attestation/example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name = "proxy_attestation"

[backends]
backend = { Cargo = { cargo_manifest = "experimental/proxy_attestation/Cargo.toml" }, additional_args = [
"--grpc-listen-address=[::]:8888",
"--grpc-tls-private-key=./examples/certs/local/local.key",
"--grpc-tls-certificate=./examples/certs/local/local.pem",
] }
Expand Down
11 changes: 10 additions & 1 deletion experimental/proxy_attestation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ use tonic::{
#[derive(StructOpt, Clone)]
#[structopt(about = "Proxy Attestation")]
pub struct Opt {
#[structopt(
long,
help = "Address to listen on for the gRPC server.",
default_value = "[::]:8888"
)]
grpc_listen_address: String,
#[structopt(long, help = "Private RSA key PEM encoded file used by gRPC server.")]
grpc_tls_private_key: String,
#[structopt(
Expand Down Expand Up @@ -166,7 +172,10 @@ async fn main() -> anyhow::Result<()> {
std::fs::read(&opt.grpc_tls_certificate).context("Couldn't load certificate")?;

let identity = Identity::from_pem(certificate, private_key);
let address = "[::]:8888".parse().context("Couldn't parse address")?;
let address = opt
.grpc_listen_address
.parse()
.context("Couldn't parse address")?;

// Create proxy attestation gRPC server.
info!("Starting proxy attestation server at {:?}", address);
Expand Down
132 changes: 83 additions & 49 deletions runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,8 @@ struct Example {
#[serde(default)]
server: ExampleServer,
#[serde(default)]
backend: Option<Executable>,
backends: HashMap<String, Executable>,
#[serde(default)]
applications: HashMap<String, Application>,
clients: HashMap<String, Executable>,
}
Expand Down Expand Up @@ -626,18 +627,6 @@ struct Executable {
}

fn run_example(opt: &RunExamples, example: &Example) -> Step {
let application = example
.applications
.get(opt.application_variant.as_str())
.expect("Unsupported application variant");

let run_server = run_example_server(
&opt.build_server,
&example.server,
opt.server_additional_args.clone(),
&application.out,
&opt.permissions_file,
);
let run_clients = Step::Multiple {
name: "run clients".to_string(),
steps: example
Expand Down Expand Up @@ -667,26 +656,55 @@ fn run_example(opt: &RunExamples, example: &Example) -> Step {
// clients in the foreground.
#[allow(clippy::collapsible_if)]
let run_backend_server_clients: Step = if opt.run_server.unwrap_or(true) {
let run_server_clients = if opt.build_client.client_variant != NO_CLIENTS {
Step::WithBackground {
name: "background server".to_string(),
background: run_server,
foreground: Box::new(run_clients),
let run_server_clients = if example.applications.is_empty() {
if opt.build_client.client_variant == NO_CLIENTS {
panic!("`{}` client variant is not supported when no applications are provided", NO_CLIENTS);
} else {
run_clients
}
} else {
Step::Single {
name: "run server".to_string(),
command: run_server,
let application = example
.applications
.get(opt.application_variant.as_str())
.expect(&format!(
"Unsupported application variant: {} (supported variants include: all, rust, cpp, go, nodejs, none)",
opt.application_variant.as_str())
);

let run_server = run_example_server(
&opt.build_server,
&example.server,
opt.server_additional_args.clone(),
&application.out,
&opt.permissions_file,
);

if opt.build_client.client_variant == NO_CLIENTS {
Step::Single {
name: "run server".to_string(),
command: run_server,
}
} else {
Step::WithBackground {
name: "background server".to_string(),
background: run_server,
foreground: Box::new(run_clients),
}
}
};
match &example.backend {
Some(backend) => Step::WithBackground {
name: "background backend".to_string(),
background: run(&backend, &opt.build_client, Vec::new()),
foreground: Box::new(run_server_clients),
},
None => run_server_clients,
}
// Recursively construct backend steps.
example
.backends
.iter()
// First iteration includes `run_server_clients` as a foreground step.
.fold(run_server_clients, |backend_steps, (name, backend)| {
Step::WithBackground {
name: name.to_string(),
// Each `backend` is included as background step.
background: run(&backend, &opt.build_client, Vec::new()),
foreground: Box::new(backend_steps),
}
})
} else {
if opt.build_client.client_variant != NO_CLIENTS {
run_clients
Expand All @@ -701,20 +719,36 @@ fn run_example(opt: &RunExamples, example: &Example) -> Step {
Step::Multiple {
name: example.name.to_string(),
steps: vec![
vec![
Step::Multiple {
name: "build wasm modules".to_string(),
steps: application
.modules
.iter()
.map(|(name, target)| build_wasm_module(name, target, &example.name))
.collect(),
},
Step::Single {
name: "build application".to_string(),
command: build_application(&application),
},
],
if example.applications.is_empty() {
if opt.build_client.client_variant == NO_CLIENTS {
panic!("`{}` client variant is not supported when no applications are provided", NO_CLIENTS);
} else {
vec![]
}
} else {
let application = example
.applications
.get(opt.application_variant.as_str())
.expect(&format!(
"Unsupported application variant: {} (supported variants include: all, rust, cpp, go, nodejs, none)",
opt.application_variant.as_str())
);

vec![
Step::Multiple {
name: "build wasm modules".to_string(),
steps: application
.modules
.iter()
.map(|(name, target)| build_wasm_module(name, target, &example.name))
.collect(),
},
Step::Single {
name: "build application".to_string(),
command: build_application(&application),
},
]
},
if opt.run_server.unwrap_or(true) {
// Build the server first so that when running it in the next step it will start up
// faster.
Expand All @@ -727,13 +761,13 @@ fn run_example(opt: &RunExamples, example: &Example) -> Step {
} else {
vec![]
},
match &example.backend {
Some(backend) => vec![Step::Single {
name: "build backend".to_string(),
example.backends.iter().map(|(name, backend)| {
Step::Single {
name: name.to_string(),
command: build(&backend.target, &opt.build_client),
}],
None => vec![],
},
}
})
.collect(),
vec![Step::Multiple {
name: "run".to_string(),
steps: vec![run_backend_server_clients],
Expand Down

0 comments on commit ff213b0

Please sign in to comment.