Verify running board build marker against CI artifact
next-build / candidate (push) Canceled after 0s
next-build / candidate (push) Canceled after 0s
This commit is contained in:
@@ -18,6 +18,84 @@ pub enum Probe {
|
||||
KernelRelease,
|
||||
MtdTable,
|
||||
DeviceTreeModel,
|
||||
AlkaneBuildMarker,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct AlkaneBuildMarker {
|
||||
pub board: String,
|
||||
pub sdk_revision: String,
|
||||
pub os_revision: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum BuildMarkerError {
|
||||
InvalidFormat,
|
||||
InvalidBoard,
|
||||
InvalidRevision,
|
||||
UnexpectedBuild,
|
||||
}
|
||||
|
||||
/// A running board must report the identity embedded in the CI artifact.
|
||||
/// This parser accepts the exact three-line marker emitted by the SDK.
|
||||
pub fn parse_build_marker(bytes: &[u8]) -> Result<AlkaneBuildMarker, BuildMarkerError> {
|
||||
if bytes.len() > 256 {
|
||||
return Err(BuildMarkerError::InvalidFormat);
|
||||
}
|
||||
let text = std::str::from_utf8(bytes).map_err(|_| BuildMarkerError::InvalidFormat)?;
|
||||
let mut lines = text.lines();
|
||||
let board = lines
|
||||
.next()
|
||||
.and_then(|line| line.strip_prefix("board="))
|
||||
.ok_or(BuildMarkerError::InvalidFormat)?;
|
||||
let sdk_revision = lines
|
||||
.next()
|
||||
.and_then(|line| line.strip_prefix("sdk_revision="))
|
||||
.ok_or(BuildMarkerError::InvalidFormat)?;
|
||||
let os_revision = lines
|
||||
.next()
|
||||
.and_then(|line| line.strip_prefix("os_revision="))
|
||||
.ok_or(BuildMarkerError::InvalidFormat)?;
|
||||
if lines.next().is_some() || !text.ends_with('\n') {
|
||||
return Err(BuildMarkerError::InvalidFormat);
|
||||
}
|
||||
if !matches!(
|
||||
board,
|
||||
"am335x-bb"
|
||||
| "zynq-7010"
|
||||
| "cv1835"
|
||||
| "a113d"
|
||||
| "allwinner-h3"
|
||||
| "allwinner-h6"
|
||||
| "allwinner-h616"
|
||||
) {
|
||||
return Err(BuildMarkerError::InvalidBoard);
|
||||
}
|
||||
for revision in [sdk_revision, os_revision] {
|
||||
if revision.len() != 40
|
||||
|| !revision
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
|
||||
{
|
||||
return Err(BuildMarkerError::InvalidRevision);
|
||||
}
|
||||
}
|
||||
Ok(AlkaneBuildMarker {
|
||||
board: board.to_owned(),
|
||||
sdk_revision: sdk_revision.to_owned(),
|
||||
os_revision: os_revision.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn verify_build_marker(
|
||||
observed: &AlkaneBuildMarker,
|
||||
expected: &AlkaneBuildMarker,
|
||||
) -> Result<(), BuildMarkerError> {
|
||||
if observed == expected {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(BuildMarkerError::UnexpectedBuild)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -170,6 +248,7 @@ impl Probe {
|
||||
Self::KernelRelease => "uname -r",
|
||||
Self::MtdTable => "cat /proc/mtd",
|
||||
Self::DeviceTreeModel => "cat /proc/device-tree/model",
|
||||
Self::AlkaneBuildMarker => "cat /etc/alkane-build",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -423,4 +502,36 @@ mtd1: 02000000 00020000 \"ramfs\"\n",
|
||||
Err(MtdMatchError::MissingIndex)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn running_build_marker_must_match_the_artifact() {
|
||||
let sha = "0123456789abcdef0123456789abcdef01234567";
|
||||
let marker = format!("board=am335x-bb\nsdk_revision={sha}\nos_revision={sha}\n");
|
||||
let expected = parse_build_marker(marker.as_bytes()).unwrap();
|
||||
assert_eq!(verify_build_marker(&expected, &expected), Ok(()));
|
||||
let other = AlkaneBuildMarker {
|
||||
board: "zynq-7010".to_owned(),
|
||||
..expected.clone()
|
||||
};
|
||||
assert_eq!(
|
||||
verify_build_marker(&expected, &other),
|
||||
Err(BuildMarkerError::UnexpectedBuild)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_build_marker(
|
||||
marker
|
||||
.replace("board=am335x-bb", "board=unknown")
|
||||
.as_bytes()
|
||||
),
|
||||
Err(BuildMarkerError::InvalidBoard)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_build_marker(marker.replace(sha, "not-a-commit").as_bytes()),
|
||||
Err(BuildMarkerError::InvalidRevision)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_build_marker(format!("{marker}extra=value\n").as_bytes()),
|
||||
Err(BuildMarkerError::InvalidFormat)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user