Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33e13f4918 | ||
|
|
f676a6aa97 | ||
|
|
a35f9fa077 |
No files matched your search
Generated
+20
-20
@@ -502,7 +502,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-chain"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"openssl",
|
||||
@@ -512,7 +512,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-cli"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-chain",
|
||||
"mpc-core",
|
||||
@@ -537,14 +537,14 @@ version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "mpc-data"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mpc-demo"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-data",
|
||||
@@ -566,7 +566,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-examples"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-data",
|
||||
@@ -585,7 +585,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-fl"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-vault",
|
||||
@@ -593,14 +593,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-he"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mpc-hostd"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"mpc-core",
|
||||
@@ -611,7 +611,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-ir"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-proto",
|
||||
@@ -620,7 +620,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-ml"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-ir",
|
||||
@@ -630,7 +630,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-node"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-ml",
|
||||
@@ -641,22 +641,22 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-orch"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mpc-policy"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
|
||||
[[package]]
|
||||
name = "mpc-proto"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
|
||||
[[package]]
|
||||
name = "mpc-psi"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"curve25519-dalek",
|
||||
"mpc-core",
|
||||
@@ -664,18 +664,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-ray"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
|
||||
[[package]]
|
||||
name = "mpc-runtime"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mpc-sql"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-ir",
|
||||
@@ -694,7 +694,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-vault"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-proto",
|
||||
@@ -702,7 +702,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mpc-zkp"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"mpc-core",
|
||||
"mpc-he",
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ resolver = "2"
|
||||
members = ["crates/*", "demo", "examples"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
rust-version = "1.85"
|
||||
|
||||
+24
-25
@@ -103,6 +103,23 @@ mod chain;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
/// CLI 输入层的平台解析:规范名走 [`mpc_tee::PlatformKind::parse`] 这一份
|
||||
/// 协议口径,历史别名只在 CLI 这一层放宽。
|
||||
///
|
||||
/// 早先四个子命令各写一份 `match`,新增变体时总有一处漏(issue #3 的
|
||||
/// 同类漂移)。现在规范名只有枚举定义处一份,这里只管别名。
|
||||
pub(crate) fn parse_platform_loose(raw: &str) -> Option<mpc_tee::PlatformKind> {
|
||||
use mpc_tee::PlatformKind as K;
|
||||
if let Some(kind) = K::parse(raw) {
|
||||
return Some(kind);
|
||||
}
|
||||
match raw {
|
||||
"snp" | "amd" => Some(K::SevSnp),
|
||||
"csv" | "hygon" => Some(K::HygonCsv),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let raw: Vec<String> = std::env::args().skip(1).collect();
|
||||
// `--report` 是全局开关,出现在子命令前后都认;摘掉再分发,
|
||||
@@ -839,17 +856,9 @@ fn parse_party_tee(spec: &str) -> Result<mpc_tee::PartyTee, String> {
|
||||
}
|
||||
None => (body, 0),
|
||||
};
|
||||
let kind = match kind_s {
|
||||
"optee" => mpc_tee::PlatformKind::Optee,
|
||||
"sev-snp" | "snp" => mpc_tee::PlatformKind::SevSnp,
|
||||
"tdx" => mpc_tee::PlatformKind::Tdx,
|
||||
"hygon-csv" | "csv" => mpc_tee::PlatformKind::HygonCsv,
|
||||
other => {
|
||||
return Err(format!(
|
||||
"未知平台 {other:?}(可选 none / optee / sev-snp / tdx / hygon-csv)"
|
||||
))
|
||||
}
|
||||
};
|
||||
let kind = parse_platform_loose(kind_s).ok_or_else(|| {
|
||||
format!("未知平台 {kind_s:?}(可选 none / optee / sev-snp / tdx / hygon-csv)")
|
||||
})?;
|
||||
if kind == mpc_tee::PlatformKind::None {
|
||||
return Err("平台 none 请写成 `site=none`".into());
|
||||
}
|
||||
@@ -1056,13 +1065,8 @@ fn cmd_tee_verify(args: &[String]) -> Result<(), String> {
|
||||
};
|
||||
|
||||
let platform = match parse_flag(args, "--platform") {
|
||||
Some(p) => match p {
|
||||
"snp" | "sev-snp" => mpc_tee::PlatformKind::SevSnp,
|
||||
"csv" | "hygon" | "hygon-csv" => mpc_tee::PlatformKind::HygonCsv,
|
||||
"tdx" => mpc_tee::PlatformKind::Tdx,
|
||||
"optee" => mpc_tee::PlatformKind::Optee,
|
||||
other => return Err(format!("--platform {other:?} 只认 snp/csv/tdx/optee")),
|
||||
},
|
||||
Some(p) => parse_platform_loose(p)
|
||||
.ok_or_else(|| format!("--platform {p:?} 只认 none/optee/snp/csv/tdx"))?,
|
||||
None => node
|
||||
.as_ref()
|
||||
.map(|n| n.platform.kind)
|
||||
@@ -1294,13 +1298,8 @@ fn cmd_tee_attest_check(args: &[String]) -> Result<(), String> {
|
||||
let bundle = mpc_tee::bundle::AttestationBundle::from_bytes(&raw).map_err(|e| e.to_string())?;
|
||||
|
||||
let platform = match parse_flag(args, "--platform") {
|
||||
Some(p) => match p {
|
||||
"snp" | "sev-snp" => mpc_tee::PlatformKind::SevSnp,
|
||||
"csv" | "hygon" | "hygon-csv" => mpc_tee::PlatformKind::HygonCsv,
|
||||
"tdx" => mpc_tee::PlatformKind::Tdx,
|
||||
"optee" => mpc_tee::PlatformKind::Optee,
|
||||
other => return Err(format!("--platform {other:?} 只认 snp/csv/tdx/optee")),
|
||||
},
|
||||
Some(p) => parse_platform_loose(p)
|
||||
.ok_or_else(|| format!("--platform {p:?} 只认 none/optee/snp/csv/tdx"))?,
|
||||
None => bundle.platform,
|
||||
};
|
||||
|
||||
|
||||
@@ -53,14 +53,9 @@ pub fn cmd_sa_serve(args: &[String]) -> Result<(), String> {
|
||||
.map_err(|_| format!("--listen {listen:?} 不是 host:port"))?;
|
||||
|
||||
// ★ 平台类型:默认 `none` = 诚实自曝「本 SA 没有硬件保护」。
|
||||
let platform = match parse_flag(args, "--platform").unwrap_or("none") {
|
||||
"none" => mpc_tee::PlatformKind::None,
|
||||
"optee" => mpc_tee::PlatformKind::Optee,
|
||||
"snp" | "sev-snp" => mpc_tee::PlatformKind::SevSnp,
|
||||
"tdx" => mpc_tee::PlatformKind::Tdx,
|
||||
"csv" | "hygon-csv" => mpc_tee::PlatformKind::HygonCsv,
|
||||
other => return Err(format!("--platform {other:?} 只认 none/optee/snp/tdx/csv")),
|
||||
};
|
||||
let platform_raw = parse_flag(args, "--platform").unwrap_or("none");
|
||||
let platform = crate::parse_platform_loose(platform_raw)
|
||||
.ok_or_else(|| format!("--platform {platform_raw:?} 只认 none/optee/snp/tdx/csv"))?;
|
||||
let tcb: u32 = match parse_flag(args, "--tcb") {
|
||||
Some(v) => v
|
||||
.parse()
|
||||
|
||||
@@ -467,13 +467,17 @@ fn hex_nibble(c: u8) -> Option<u8> {
|
||||
}
|
||||
|
||||
/// 解析平台名。
|
||||
///
|
||||
/// 规范名走 [`PlatformKind::parse`] 这一份协议口径;历史别名只在宿主 API
|
||||
/// 这一层放宽。这样枚举新增变体时,节点描述的 `kind=` 与 `/desired` 的
|
||||
/// `platform` 不会一个认识、一个不认识(issue #3)。
|
||||
pub fn parse_platform(raw: &str) -> Option<PlatformKind> {
|
||||
if let Some(kind) = PlatformKind::parse(raw) {
|
||||
return Some(kind);
|
||||
}
|
||||
match raw {
|
||||
"sev-snp" | "snp" | "amd" => Some(PlatformKind::SevSnp),
|
||||
"hygon-csv" | "csv" | "hygon" => Some(PlatformKind::HygonCsv),
|
||||
"tdx" => Some(PlatformKind::Tdx),
|
||||
"optee" => Some(PlatformKind::Optee),
|
||||
"none" => Some(PlatformKind::None),
|
||||
"snp" | "amd" => Some(PlatformKind::SevSnp),
|
||||
"csv" | "hygon" => Some(PlatformKind::HygonCsv),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -1889,13 +1893,34 @@ mod tests {
|
||||
fn platform_parsing_covers_all_kinds() {
|
||||
assert_eq!(parse_platform("sev-snp"), Some(PlatformKind::SevSnp));
|
||||
assert_eq!(parse_platform("snp"), Some(PlatformKind::SevSnp));
|
||||
assert_eq!(parse_platform("amd"), Some(PlatformKind::SevSnp));
|
||||
assert_eq!(parse_platform("csv"), Some(PlatformKind::HygonCsv));
|
||||
assert_eq!(parse_platform("hygon"), Some(PlatformKind::HygonCsv));
|
||||
assert_eq!(parse_platform("hygon-csv"), Some(PlatformKind::HygonCsv));
|
||||
assert_eq!(parse_platform("tdx"), Some(PlatformKind::Tdx));
|
||||
assert_eq!(parse_platform("optee"), Some(PlatformKind::Optee));
|
||||
assert_eq!(parse_platform("none"), Some(PlatformKind::None));
|
||||
assert_eq!(parse_platform("nope"), None);
|
||||
}
|
||||
|
||||
/// ★ issue #3:宿主侧不得再维护一份规范名表 —— 枚举每个变体都得认。
|
||||
#[test]
|
||||
fn platform_parsing_shares_the_protocol_names() {
|
||||
for kind in PlatformKind::ALL {
|
||||
assert_eq!(
|
||||
parse_platform(kind.as_str()),
|
||||
Some(kind),
|
||||
"{} 必须在宿主侧也认得",
|
||||
kind.as_str()
|
||||
);
|
||||
}
|
||||
// 历史别名继续可用,但不会被协议层当成规范名。
|
||||
for alias in ["snp", "amd", "csv", "hygon"] {
|
||||
assert!(parse_platform(alias).is_some(), "{alias} 别名必须继续可用");
|
||||
assert_eq!(PlatformKind::parse(alias), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeout_endpoint_updates_record() {
|
||||
let rt = TeeRuntime::new();
|
||||
|
||||
@@ -484,6 +484,42 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// ★ issue #3:OP-TEE / 无 TEE 计算节点自报(能力位全 false)必须能登记。
|
||||
///
|
||||
/// 这条只走「自报 → 注册表 → GET /nodes」,**不**经过 `admit()`:
|
||||
/// 起 guest 的准入判据不该拦住一台只是来报到的机器。
|
||||
#[test]
|
||||
fn optee_and_none_self_report_lands_in_registry() {
|
||||
for (kind, host) in [("optee", "192.168.1.233"), ("none", "192.168.1.234")] {
|
||||
let cp = cp();
|
||||
let descriptor = format!(
|
||||
"site=carol\nhost={host}\nkind={kind}\ntcb=1\ndevice_present=false\n\
|
||||
qemu_present=false\nfirmware_present=false\nimage_present=false\n\
|
||||
kbs_reachable=false\nmeasurement={}\nkbc=offline_fs_kbc\n",
|
||||
"25".repeat(32)
|
||||
);
|
||||
let mut o = JsonObject::new();
|
||||
o.push_str("descriptor", &descriptor);
|
||||
o.push_int("capacity", 1);
|
||||
let body = o.to_string();
|
||||
|
||||
let resp = dispatch_control_plane(&cp, &req("POST", "/registry", &body));
|
||||
assert_eq!(resp.status, Status::NoContent, "kind={kind} 自报必须 204");
|
||||
|
||||
let list = dispatch_control_plane(&cp, &req("GET", "/nodes", ""));
|
||||
assert_eq!(list.status, Status::Ok);
|
||||
let text = String::from_utf8_lossy(&list.body).to_string();
|
||||
let v = crate::json::parse(&text).unwrap();
|
||||
let arr = v.get_arr("nodes").expect("必须有 nodes 数组");
|
||||
assert_eq!(arr.len(), 1, "kind={kind} 必须登记进注册表");
|
||||
let key = format!("carol@{host}");
|
||||
assert_eq!(arr[0].get_str("key"), Some(key.as_str()));
|
||||
assert_eq!(arr[0].get_str("platform"), Some(kind), "platform 必须是规范名");
|
||||
assert_eq!(arr[0].get_str("host"), Some(host));
|
||||
assert_eq!(arr[0].get_bool("device_present"), Some(false), "能力位如实带出");
|
||||
}
|
||||
}
|
||||
|
||||
/// ★ 负向:坏自报 ⇒ 400 且**不污染注册表**。
|
||||
#[test]
|
||||
fn bad_self_report_is_rejected_and_registry_stays_clean() {
|
||||
|
||||
@@ -67,16 +67,50 @@ pub enum PlatformKind {
|
||||
HygonCsv,
|
||||
}
|
||||
|
||||
impl fmt::Display for PlatformKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let s = match self {
|
||||
impl PlatformKind {
|
||||
/// 全部变体。新增平台只改这里与 [`Self::as_str`]。
|
||||
///
|
||||
/// 为什么要有这张表:解析与序列化都从它派生,不再让每个调用点各写一份
|
||||
/// `match`。节点描述里的 `kind=` 认不认识某个平台,取决于枚举本身,
|
||||
/// 而不是取决于某个下游模块有没有顺手补上分支(2026-11-11 issue #3:
|
||||
/// `parse_kind` 只认 sev-snp / hygon-csv,导致 optee 节点自报被 400)。
|
||||
pub const ALL: [Self; 5] = [
|
||||
Self::None,
|
||||
Self::Optee,
|
||||
Self::SevSnp,
|
||||
Self::Tdx,
|
||||
Self::HygonCsv,
|
||||
];
|
||||
|
||||
/// 协议里的稳定规范名。
|
||||
///
|
||||
/// 这是**对外契约**:节点描述 `kind=`、注册表 `GET /nodes` 的 `platform`
|
||||
/// 字段、站点侧输入都用这一串值。`snp` / `csv` / `amd` 之类历史别名
|
||||
/// 只属于各自的输入层,不进协议口径。
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::None => "none",
|
||||
Self::Optee => "optee",
|
||||
Self::SevSnp => "sev-snp",
|
||||
Self::Tdx => "tdx",
|
||||
Self::HygonCsv => "hygon-csv",
|
||||
};
|
||||
f.write_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
/// 规范名 → 平台的**唯一解析口径**。
|
||||
///
|
||||
/// 与 [`Self::as_str`] / [`fmt::Display`] 严格互逆:
|
||||
/// `parse(k.as_str()) == Some(k)` 对 [`Self::ALL`] 里每个变体都成立。
|
||||
/// 节点自报(`TeeNode::parse`)与宿主侧 API 都复用这一份;各输入层的
|
||||
/// 历史别名先在这里试,再由那一层自行回落。
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
Self::ALL.iter().copied().find(|k| k.as_str() == s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PlatformKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str((*self).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -546,6 +580,38 @@ impl TrustPlan {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// 平台规范名是**对外契约**:新增 / 改名必须显式改这张硬编码表。
|
||||
/// 刻意不遍历 `ALL` 反查自己 —— 那会变成「被测对象证明自己」的恒绿判据。
|
||||
#[test]
|
||||
fn platform_names_are_one_round_trip_contract() {
|
||||
let expected = [
|
||||
(PlatformKind::None, "none"),
|
||||
(PlatformKind::Optee, "optee"),
|
||||
(PlatformKind::SevSnp, "sev-snp"),
|
||||
(PlatformKind::Tdx, "tdx"),
|
||||
(PlatformKind::HygonCsv, "hygon-csv"),
|
||||
];
|
||||
assert_eq!(
|
||||
PlatformKind::ALL.len(),
|
||||
expected.len(),
|
||||
"新增平台变体必须同步更新规范名契约"
|
||||
);
|
||||
for (kind, name) in expected {
|
||||
assert_eq!(kind.as_str(), name);
|
||||
assert_eq!(format!("{kind}"), name, "Display 必须与 as_str 同源");
|
||||
assert_eq!(PlatformKind::parse(name), Some(kind), "{name} 必须能解析回来");
|
||||
assert!(PlatformKind::ALL.contains(&kind), "{name} 必须在 ALL 里");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_platform_names_are_rejected() {
|
||||
// 历史别名不进协议口径,由各输入层自己放宽。
|
||||
for bad in ["", "SNP", "snp", "csv", "optee ", " OP-TEE", "arm", "nope"] {
|
||||
assert_eq!(PlatformKind::parse(bad), None, "{bad:?} 不该被当成规范名");
|
||||
}
|
||||
}
|
||||
|
||||
fn snp(site: &str, attested: bool) -> PartyTee {
|
||||
PartyTee::with_tee(site, PlatformKind::SevSnp, 5, attested)
|
||||
}
|
||||
|
||||
@@ -317,6 +317,13 @@ impl PlatformProfile {
|
||||
/// 按平台取内置规格;尚未支持直接启动的平台返回 `None`。
|
||||
///
|
||||
/// 海光不指定代际时按实测的 CSV2 处理,不做"猜一个能跑的"降级。
|
||||
///
|
||||
/// ★ `None` / `Optee` / `Tdx` 没有 qemu 启动规格是**刻意的**,不是漏项:
|
||||
/// 这几种平台不是靠本框架起 guest 的执行形态。节点自报登记走
|
||||
/// `TeeNode::parse` → `NodeRegistry::register`,**不经过** [`crate::schedule::admit`];
|
||||
/// 所以 `GET /nodes` 能看到它们,并不代表它们能被选址起 guest。
|
||||
/// 别为了让某平台「能注册」就在这里补一份假规格 —— `admit()` 会把它
|
||||
/// 当成可启动,fail-closed 就破了(2026-11-11 issue #3)。
|
||||
pub fn builtin(kind: PlatformKind, variant: Option<CsvVariant>) -> Option<Self> {
|
||||
match kind {
|
||||
PlatformKind::SevSnp => Some(Self::amd_sev_snp()),
|
||||
|
||||
@@ -192,7 +192,9 @@ impl TeeNode {
|
||||
"site" => site = String::from(val),
|
||||
"host" => host = String::from(val),
|
||||
"kind" => {
|
||||
kind = Some(parse_kind(val).ok_or_else(|| {
|
||||
// 规范名解析只有一份口径,就在枚举定义处;这里不再另写
|
||||
// 一份 match —— issue #3 的 optee 节点自报 400 就是这么漏的。
|
||||
kind = Some(PlatformKind::parse(val).ok_or_else(|| {
|
||||
TeeError::BadInput(format!("第 {line_no} 行:不认识的平台 {val:?}"))
|
||||
})?)
|
||||
}
|
||||
@@ -260,14 +262,6 @@ impl TeeNode {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_kind(s: &str) -> Option<PlatformKind> {
|
||||
match s {
|
||||
"sev-snp" => Some(PlatformKind::SevSnp),
|
||||
"hygon-csv" => Some(PlatformKind::HygonCsv),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_bool(v: &str, line_no: usize) -> Result<bool, TeeError> {
|
||||
match v {
|
||||
"true" | "1" => Ok(true),
|
||||
@@ -1001,7 +995,7 @@ mod tests {
|
||||
fn node_profile_errors_name_the_line() {
|
||||
for (bad, needle) in [
|
||||
("foo=1\n", "第 1 行"),
|
||||
("kind=tdx\n", "第 1 行"),
|
||||
("kind=nope\n", "第 1 行"),
|
||||
("measurement=zz\n", "第 1 行"),
|
||||
("device_present=maybe\n", "第 1 行"),
|
||||
("tcb=x\n", "第 1 行"),
|
||||
@@ -1014,6 +1008,30 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// ★ issue #3:`kind` 必须认得 `PlatformKind` 的**每一个**变体,
|
||||
/// 且注册表落盘的 descriptor 能被同一份解析读回来。
|
||||
#[test]
|
||||
fn node_profile_roundtrips_every_platform_kind() {
|
||||
for kind in PlatformKind::ALL {
|
||||
let src = format!(
|
||||
"site=carol\nhost=192.168.1.233\nkind={kind}\ntcb=1\n\
|
||||
device_present=false\nqemu_present=false\nfirmware_present=false\n\
|
||||
image_present=false\nkbs_reachable=false\nmeasurement={}\n\
|
||||
kbc=offline_fs_kbc\n",
|
||||
"25".repeat(32)
|
||||
);
|
||||
let node = TeeNode::parse(&src)
|
||||
.unwrap_or_else(|e| panic!("kind={kind} 应当能解析: {e}"));
|
||||
assert_eq!(node.platform.kind, kind);
|
||||
// 能力位全 false 不影响「登记」:parse 只记录自报事实,
|
||||
// 判能力的是 admit,而那条路只在起 guest 时走。
|
||||
assert!(!node.device_present);
|
||||
let back = TeeNode::parse(&node.descriptor())
|
||||
.unwrap_or_else(|e| panic!("kind={kind} 的 descriptor 必须能回环: {e}"));
|
||||
assert_eq!(back, node, "descriptor() 与 parse() 必须严格互逆");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parsed_profile_without_capability_bits_still_denies() {
|
||||
let mut src = amd_profile();
|
||||
|
||||
+34
-31
@@ -29,6 +29,7 @@
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
@@ -89,10 +90,18 @@ def git_credential():
|
||||
|
||||
|
||||
def api(method, path, user, pwd, data=None, raw=None, ctype=None):
|
||||
"""调用 Gitea API。data = json body;raw = 二进制 body。"""
|
||||
"""调用 Gitea API。data = json body;raw = 二进制 body。
|
||||
|
||||
★ 必须**主动带 Authorization 头**,不能用 urllib 的 HTTPBasicAuthHandler:
|
||||
本仓 Gitea 的读接口允许匿名访问(GET 不带凭据也过),
|
||||
而写接口返回的 401 **不带 WWW-Authenticate 挑战头** ⇒
|
||||
BasicAuthHandler 永远不会触发重试 ⇒ POST 一律 401「token is required」。
|
||||
症状:GET 全绿、POST 全 401,看起来像「凭据不对」,其实是「压根没带上」。
|
||||
"""
|
||||
url = API + path
|
||||
body = None
|
||||
headers = {}
|
||||
headers = {"Authorization": "Basic " + base64.b64encode(
|
||||
("%s:%s" % (user, pwd)).encode()).decode()}
|
||||
if data is not None:
|
||||
body = json.dumps(data).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
@@ -101,11 +110,8 @@ def api(method, path, user, pwd, data=None, raw=None, ctype=None):
|
||||
if ctype:
|
||||
headers["Content-Type"] = ctype
|
||||
req = urllib.request.Request(url, data=body, headers=headers, method=method)
|
||||
mgr = urllib.request.HTTPBasicAuthHandler()
|
||||
mgr.add_password(realm=None, uri=url, user=user, passwd=pwd)
|
||||
opener = urllib.request.build_opener(mgr)
|
||||
try:
|
||||
with opener.open(req, timeout=300) as r:
|
||||
with urllib.request.urlopen(req, timeout=300) as r:
|
||||
payload = r.read()
|
||||
if not payload:
|
||||
return {}
|
||||
@@ -123,47 +129,44 @@ def list_releases(user, pwd):
|
||||
|
||||
|
||||
def remote_asset_index(user, pwd):
|
||||
"""扫远端所有 Release 的资产 ⇒ {sha256: {name, tag, id, size}}。
|
||||
"""扫远端所有 Release 的资产 ⇒ {sha256: {name, tag, id, size, url}}。
|
||||
|
||||
★ 这是「只发一次」的唯一判据:内容寻址,不靠文件名。
|
||||
★ 代价:要把每个资产下载一遍算 sha256(依赖资产有 50MB 级的)。
|
||||
资产数量不大(每版十来个),可接受;换来的是「判据唯一且不会错」。
|
||||
"""
|
||||
index = {}
|
||||
for rel in list_releases(user, pwd):
|
||||
tag = rel.get("tag_name")
|
||||
for a in rel.get("assets", []):
|
||||
aid = a.get("id")
|
||||
url = a.get("browser_download_url") or ""
|
||||
dl = a.get("browser_download_url") or ""
|
||||
# API 给的是 localhost,要换 127.0.0.1 才连得上
|
||||
url = url.replace("http://localhost:13300", "http://127.0.0.1:13300")
|
||||
idx_url = "%s/api/v1/repos/%s/releases/%s/assets/%s" % (API, REPO, rel["id"], aid)
|
||||
meta = api("GET", idx_url, user, pwd)
|
||||
dl = meta.get("browser_download_url") or url
|
||||
dl = dl.replace("http://localhost:13300", "http://127.0.0.1:13300")
|
||||
try:
|
||||
data = api("GET", "/repos/%s/releases/%s/assets/%s" % (REPO, rel["id"], aid),
|
||||
user, pwd)
|
||||
digest = sha256_file_from_url(dl, user, pwd)
|
||||
except Exception:
|
||||
digest = None
|
||||
if digest:
|
||||
index[digest] = {
|
||||
"name": a.get("name"),
|
||||
"tag": tag,
|
||||
"id": aid,
|
||||
"size": a.get("size"),
|
||||
"url": dl,
|
||||
}
|
||||
except Exception as e:
|
||||
print("WARN: 资产 %s 算 sha256 失败(%s)" % (a.get("name"), e))
|
||||
continue
|
||||
index[digest] = {
|
||||
"name": a.get("name"),
|
||||
"tag": tag,
|
||||
"id": a.get("id"),
|
||||
"size": a.get("size"),
|
||||
"url": dl,
|
||||
}
|
||||
return index
|
||||
|
||||
|
||||
def sha256_file_from_url(url, user, pwd):
|
||||
"""下载并算 sha256(流式,不把大文件整个读进内存)。"""
|
||||
req = urllib.request.Request(url)
|
||||
mgr = urllib.request.HTTPBasicAuthHandler()
|
||||
mgr.add_password(realm=None, uri=url, user=user, passwd=pwd)
|
||||
opener = urllib.request.build_opener(mgr)
|
||||
"""下载并算 sha256(流式,不把大文件整个读进内存)。
|
||||
|
||||
★ 同样必须主动带 Authorization 头(见 api() 的说明)。
|
||||
"""
|
||||
h = hashlib.sha256()
|
||||
with opener.open(req, timeout=600) as r:
|
||||
headers = {"Authorization": "Basic " + base64.b64encode(
|
||||
("%s:%s" % (user, pwd)).encode()).decode()}
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
with urllib.request.urlopen(req, timeout=600) as r:
|
||||
for chunk in iter(lambda: r.read(1024 * 1024), b""):
|
||||
h.update(chunk)
|
||||
return h.hexdigest()
|
||||
|
||||
Reference in new issue
Block a user