-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docker versions older than 23 no longer receive community-supported security patches. Additionally, twoliter requires dockerfile syntax 1.4.3. Currently the tool pulls this image from the network at build time; however, Docker 23 bundles this version internally via buildkit. We can further isolate twoliter builds from the network by ensuring we are on Docker 23 or greater.
- Loading branch information
Showing
8 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
use crate::common::exec; | ||
use anyhow::{Context, Result}; | ||
use semver::Version; | ||
use tokio::process::Command; | ||
|
||
pub(crate) struct Docker; | ||
|
||
impl Docker { | ||
/// Fetches the version of the docker daemon | ||
pub(crate) async fn server_version() -> Result<Version> { | ||
let version_str = exec( | ||
Command::new("docker").args(["version", "--format", "{{.Server.Version}}"]), | ||
true, | ||
) | ||
.await | ||
// Convert Result<Option<String>> to Option<String> | ||
.ok() | ||
.flatten() | ||
.map(|s| s.trim().to_string()) | ||
.context("Failed to fetch docker version")?; | ||
|
||
Version::parse(&version_str).context("Failed to parse docker version as semver") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ mod commands; | |
mod image; | ||
|
||
pub(crate) use self::image::ImageUri; | ||
pub(crate) use commands::Docker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//! This module performs checks that the current environment is compatible with twoliter, as well | ||
//! as any other "global" setup that must occur before the build process begins. | ||
use anyhow::{ensure, Result}; | ||
use lazy_static::lazy_static; | ||
use semver::{Comparator, Op, Prerelease, VersionReq}; | ||
use which::which_global; | ||
|
||
use crate::docker::Docker; | ||
|
||
const REQUIRED_TOOLS: &[&str] = &["docker", "gzip", "lz4"]; | ||
|
||
lazy_static! { | ||
// Twoliter relies on minimum Dockerfile syntax 1.4.3, which is shipped in Docker 23.0.0 by default | ||
// We do not use explicit `syntax=` directives to avoid network connections during the build. | ||
static ref MINIMUM_DOCKER_VERSION: VersionReq = VersionReq { | ||
comparators: [ | ||
Comparator { | ||
op: Op::GreaterEq, | ||
major: 23, | ||
minor: None, | ||
patch: None, | ||
pre: Prerelease::default(), | ||
} | ||
].into() | ||
}; | ||
} | ||
|
||
/// Runs all common setup required for twoliter. | ||
/// | ||
/// * Ensures that any required system tools are installed an accessible. | ||
/// * Sets up interrupt handler to cleanup on SIGINT | ||
pub(crate) async fn preflight() -> Result<()> { | ||
check_environment().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) async fn check_environment() -> Result<()> { | ||
check_for_required_tools()?; | ||
check_docker_version().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn check_for_required_tools() -> Result<()> { | ||
for tool in REQUIRED_TOOLS { | ||
ensure!( | ||
which_global(tool).is_ok(), | ||
"Failed to find required tool `{tool}` in PATH" | ||
); | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn check_docker_version() -> Result<()> { | ||
let docker_version = Docker::server_version().await?; | ||
|
||
ensure!( | ||
MINIMUM_DOCKER_VERSION.matches(&docker_version), | ||
"docker found in PATH does not meet the minimum version requirements for twoliter: {}", | ||
MINIMUM_DOCKER_VERSION.to_string(), | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use semver::Version; | ||
use test_case::test_case; | ||
|
||
#[test_case(Version::parse("25.0.5").unwrap(), true; "25.0.5 passes")] | ||
#[test_case(Version::parse("27.1.4").unwrap(), true; "27.1.4 passes")] | ||
#[test_case(Version::parse("18.0.9").unwrap(), false; "18.0.9 fails")] | ||
#[test_case(Version::parse("20.10.27").unwrap(), false)] | ||
fn test_docker_version_req(version: Version, is_ok: bool) { | ||
assert_eq!(MINIMUM_DOCKER_VERSION.matches(&version), is_ok) | ||
} | ||
} |