Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into container-retention-policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Porges committed Sep 25, 2023
2 parents 386aabb + e3c4a40 commit d45c1f3
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/Functions/Jobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class Jobs {
"job");
}

await _context.Events.SendEvent(new EventJobCreated(job.JobId, job.Config, job.UserInfo));
await _context.Events.SendEvent(new EventJobCreated(job.JobId, job.Config, job.UserInfo, _context.ServiceConfiguration.OneFuzzVersion));
return await RequestHandling.Ok(req, JobResponse.ForJob(job, taskInfo: null));
}

Expand Down
3 changes: 2 additions & 1 deletion src/ApiService/ApiService/OneFuzzTypes/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ TaskConfig Config
public record EventJobCreated(
Guid JobId,
JobConfig Config,
StoredUserInfo? UserInfo
StoredUserInfo? UserInfo,
string OneFuzzVersion
) : BaseEvent();


Expand Down
78 changes: 78 additions & 0 deletions src/agent/onefuzz-task/src/check_for_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::process::Stdio;

use anyhow::Result;
use serde_json::Value;

pub fn run(onefuzz_built_version: &str) -> Result<()> {
// Find onefuzz cli
let common_names = ["onefuzz", "onefuzz.exe", "onefuzz.cmd"];
let mut valid_commands: Vec<_> = common_names
.into_iter()
.map(|name| {
(
name,
std::process::Command::new(name)
.stderr(Stdio::null())
.stdout(Stdio::null())
.arg("-h")
.spawn(),
)
})
.filter_map(|(name, child)| child.ok().map(|c| (name, c)))
.collect();

if valid_commands.is_empty() {
bail!(
"Could not find any of the following common names for the onefuzz-cli: {:?}",
common_names
);
}

let (name, child) = valid_commands
.first_mut()
.expect("Expected valid_commands to not be empty");

info!("Found the onefuzz cli at: {}", name);

// We just used this to check if it exists, we'll invoke it again later
let _ = child.kill();

// Run onefuzz info get
let output = std::process::Command::new(&name)
.args(["info", "get"])
.output()?;

if !output.status.success() {
bail!(
"Failed to run command `{} info get`. stderr: {:?}, stdout: {:?}",
name,
String::from_utf8(output.stderr),
String::from_utf8(output.stdout)
)
}

let stdout = String::from_utf8(output.stdout)?;
let info: Value = serde_json::from_str(&stdout)?;

if let Some(onefuzz_service_version) = info["versions"]["onefuzz"]["version"].as_str() {
if onefuzz_service_version == onefuzz_built_version {
println!("You are up to date!");
} else {
println!(
"Version mismatch. onefuzz-task version: {} | onefuzz service version: {}",
onefuzz_built_version, onefuzz_service_version
);
println!(
"To update, please run the following command: {} tools get .",
name
);
println!("Then extract the onefuzz-task binary from the appropriate OS folder");
}
return Ok(());
}

bail!(
"Failed to get onefuzz service version from cli response: {}",
stdout
)
}
8 changes: 4 additions & 4 deletions src/agent/onefuzz-task/src/local/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,16 @@ impl RunContext {
name: impl AsRef<str>,
path: impl AsRef<Path>,
) -> Result<SyncedDir> {
if !path.as_ref().exists() {
std::fs::create_dir_all(&path)?;
}

self.to_sync_dir(name, path)?
.monitor_count(&self.event_sender)
}

pub fn to_sync_dir(&self, name: impl AsRef<str>, path: impl AsRef<Path>) -> Result<SyncedDir> {
let path = path.as_ref();
if !path.exists() {
std::fs::create_dir_all(path)?;
}

let name = name.as_ref();
let current_dir = std::env::current_dir()?;
if self.create_job_dir {
Expand Down
14 changes: 12 additions & 2 deletions src/agent/onefuzz-task/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,38 @@ extern crate onefuzz;

use anyhow::Result;
use clap::{ArgMatches, Command};

use std::io::{stdout, Write};

mod check_for_update;
mod local;
mod managed;
mod tasks;

const LICENSE_CMD: &str = "licenses";
const LOCAL_CMD: &str = "local";
const MANAGED_CMD: &str = "managed";
const CHECK_FOR_UPDATE: &str = "check_for_update";

const ONEFUZZ_BUILT_VERSION: &str = env!("ONEFUZZ_VERSION");

fn main() -> Result<()> {
let built_version = format!(
"{} onefuzz:{} git:{}",
crate_version!(),
env!("ONEFUZZ_VERSION"),
ONEFUZZ_BUILT_VERSION,
env!("GIT_VERSION")
);

let app = Command::new("onefuzz-task")
.version(built_version)
.subcommand(managed::cmd::args(MANAGED_CMD))
.subcommand(local::cmd::args(LOCAL_CMD))
.subcommand(Command::new(LICENSE_CMD).about("display third-party licenses"));
.subcommand(Command::new(LICENSE_CMD).about("display third-party licenses"))
.subcommand(
Command::new(CHECK_FOR_UPDATE)
.about("compares the version of onefuzz-task with the onefuzz service"),
);

let matches = app.get_matches();

Expand All @@ -55,6 +64,7 @@ async fn run(args: ArgMatches) -> Result<()> {
Some((LICENSE_CMD, _)) => licenses(),
Some((LOCAL_CMD, sub)) => local::cmd::run(sub.to_owned()).await,
Some((MANAGED_CMD, sub)) => managed::cmd::run(sub).await,
Some((CHECK_FOR_UPDATE, _)) => check_for_update::run(ONEFUZZ_BUILT_VERSION),
_ => anyhow::bail!("No command provided. Run with 'help' to see available commands."),
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/agent/onefuzz-task/tests/template_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use path_absolutize::Absolutize;
use tokio::fs;

use anyhow::Result;
Expand Down Expand Up @@ -131,25 +132,20 @@ async fn create_test_directory(config: &Path, target_exe: &Path) -> Result<TestL
test_directory = test_directory.canonicalize()?;

let mut inputs_directory = PathBuf::from(&test_directory).join("inputs");
fs::create_dir(&inputs_directory).await?;
inputs_directory = inputs_directory.canonicalize()?;
inputs_directory = inputs_directory.absolutize()?.into();

let mut crashes_directory = PathBuf::from(&test_directory).join("crashes");
fs::create_dir(&crashes_directory).await?;
crashes_directory = crashes_directory.canonicalize()?;
crashes_directory = crashes_directory.absolutize()?.into();

let mut crashdumps_directory = PathBuf::from(&test_directory).join("crashdumps");
fs::create_dir(&crashdumps_directory).await?;
crashdumps_directory = crashdumps_directory.canonicalize()?;
crashdumps_directory = crashdumps_directory.absolutize()?.into();

let mut coverage_directory = PathBuf::from(&test_directory).join("coverage");
fs::create_dir(&coverage_directory).await?;
coverage_directory = coverage_directory.canonicalize()?;
coverage_directory = coverage_directory.absolutize()?.into();

let mut regression_reports_directory =
PathBuf::from(&test_directory).join("regression_reports");
fs::create_dir(&regression_reports_directory).await?;
regression_reports_directory = regression_reports_directory.canonicalize()?;
regression_reports_directory = regression_reports_directory.absolutize()?.into();

let mut target_in_test = PathBuf::from(&test_directory).join("fuzz.exe");
fs::copy(target_exe, &target_in_test).await?;
Expand Down

0 comments on commit d45c1f3

Please sign in to comment.