Skip to content

Commit 6bd49b0

Browse files
committed
refactor: extract AttestationV1::with_report_data to deduplicate patching logic
The same report_data patching logic was duplicated in three places: - guest-agent-simulator/src/simulator.rs - guest-agent/src/rpc_service.rs (test) - dstack-attest/src/attestation.rs (test) Extract it into AttestationV1::with_report_data() method and replace all three copies with calls to it.
1 parent dd8d9cd commit 6bd49b0

File tree

4 files changed

+50
-143
lines changed

4 files changed

+50
-143
lines changed

dstack-attest/src/attestation.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,50 +1221,7 @@ mod tests {
12211221
use super::*;
12221222

12231223
fn patch_v1_report_data(attestation: AttestationV1, report_data: [u8; 64]) -> AttestationV1 {
1224-
let AttestationV1 {
1225-
version,
1226-
platform,
1227-
stack,
1228-
} = attestation;
1229-
let platform = match platform {
1230-
PlatformEvidence::Tdx {
1231-
mut quote,
1232-
event_log,
1233-
} => {
1234-
if quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
1235-
quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
1236-
}
1237-
PlatformEvidence::Tdx { quote, event_log }
1238-
}
1239-
other => other,
1240-
};
1241-
let stack = match stack {
1242-
StackEvidence::Dstack {
1243-
runtime_events,
1244-
config,
1245-
..
1246-
} => StackEvidence::Dstack {
1247-
report_data: report_data.to_vec(),
1248-
runtime_events,
1249-
config,
1250-
},
1251-
StackEvidence::DstackPod {
1252-
runtime_events,
1253-
config,
1254-
report_data_payload,
1255-
..
1256-
} => StackEvidence::DstackPod {
1257-
report_data: report_data.to_vec(),
1258-
runtime_events,
1259-
config,
1260-
report_data_payload,
1261-
},
1262-
};
1263-
AttestationV1 {
1264-
version,
1265-
platform,
1266-
stack,
1267-
}
1224+
attestation.with_report_data(report_data)
12681225
}
12691226

12701227
fn dummy_tdx_attestation(report_data: [u8; 64]) -> Attestation {

dstack-attest/src/v1.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,51 @@ impl Attestation {
192192
stack: self.stack.into_dstack_pod(report_data_payload),
193193
}
194194
}
195+
196+
/// Return a new attestation with the report_data patched in both platform quote and stack.
197+
pub fn with_report_data(self, report_data: [u8; 64]) -> Self {
198+
use crate::attestation::TDX_QUOTE_REPORT_DATA_RANGE;
199+
200+
let platform = match self.platform {
201+
PlatformEvidence::Tdx {
202+
mut quote,
203+
event_log,
204+
} => {
205+
if quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
206+
quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
207+
}
208+
PlatformEvidence::Tdx { quote, event_log }
209+
}
210+
other => other,
211+
};
212+
let stack = match self.stack {
213+
StackEvidence::Dstack {
214+
runtime_events,
215+
config,
216+
..
217+
} => StackEvidence::Dstack {
218+
report_data: report_data.to_vec(),
219+
runtime_events,
220+
config,
221+
},
222+
StackEvidence::DstackPod {
223+
runtime_events,
224+
config,
225+
report_data_payload,
226+
..
227+
} => StackEvidence::DstackPod {
228+
report_data: report_data.to_vec(),
229+
runtime_events,
230+
config,
231+
report_data_payload,
232+
},
233+
};
234+
Self {
235+
version: self.version,
236+
platform,
237+
stack,
238+
}
239+
}
195240
}
196241

197242
#[cfg(test)]

guest-agent-simulator/src/simulator.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use std::path::Path;
77
use anyhow::{anyhow, Context, Result};
88
use dstack_guest_agent_rpc::{AttestResponse, GetQuoteResponse};
99
use ra_tls::attestation::{
10-
AttestationV1, PlatformEvidence, QuoteContentType, StackEvidence, TdxAttestationExt,
11-
VersionedAttestation, TDX_QUOTE_REPORT_DATA_RANGE,
10+
AttestationV1, QuoteContentType, TdxAttestationExt, VersionedAttestation,
1211
};
1312
use std::fs;
1413
use tracing::warn;
@@ -89,52 +88,5 @@ fn maybe_patch_report_data(
8988
);
9089
return attestation.clone().into_v1();
9190
}
92-
patch_v1_report_data(attestation.clone().into_v1(), report_data)
93-
}
94-
95-
fn patch_v1_report_data(attestation: AttestationV1, report_data: [u8; 64]) -> AttestationV1 {
96-
let AttestationV1 {
97-
version,
98-
platform,
99-
stack,
100-
} = attestation;
101-
let platform = match platform {
102-
PlatformEvidence::Tdx {
103-
mut quote,
104-
event_log,
105-
} => {
106-
if quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
107-
quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
108-
}
109-
PlatformEvidence::Tdx { quote, event_log }
110-
}
111-
other => other,
112-
};
113-
let stack = match stack {
114-
StackEvidence::Dstack {
115-
runtime_events,
116-
config,
117-
..
118-
} => StackEvidence::Dstack {
119-
report_data: report_data.to_vec(),
120-
runtime_events,
121-
config,
122-
},
123-
StackEvidence::DstackPod {
124-
runtime_events,
125-
config,
126-
report_data_payload,
127-
..
128-
} => StackEvidence::DstackPod {
129-
report_data: report_data.to_vec(),
130-
runtime_events,
131-
config,
132-
report_data_payload,
133-
},
134-
};
135-
AttestationV1 {
136-
version,
137-
platform,
138-
stack,
139-
}
91+
attestation.clone().into_v1().with_report_data(report_data)
14092
}

guest-agent/src/rpc_service.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,7 @@ mod tests {
670670
Signature as Ed25519Signature, Verifier, VerifyingKey as Ed25519VerifyingKey,
671671
};
672672
use k256::ecdsa::{Signature as K256Signature, VerifyingKey};
673-
use ra_tls::attestation::{
674-
AttestationV1, PlatformEvidence, StackEvidence, VersionedAttestation,
675-
TDX_QUOTE_REPORT_DATA_RANGE,
676-
};
673+
use ra_tls::attestation::{AttestationV1, VersionedAttestation};
677674
use sha2::Sha256;
678675
use std::collections::HashSet;
679676
use std::convert::TryFrom;
@@ -817,51 +814,7 @@ pNs85uhOZE8z2jr8Pg==
817814
attestation: &VersionedAttestation,
818815
report_data: [u8; 64],
819816
) -> AttestationV1 {
820-
let attestation = attestation.clone().into_v1();
821-
let AttestationV1 {
822-
version,
823-
platform,
824-
stack,
825-
} = attestation;
826-
let platform = match platform {
827-
PlatformEvidence::Tdx {
828-
mut quote,
829-
event_log,
830-
} => {
831-
if quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
832-
quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
833-
}
834-
PlatformEvidence::Tdx { quote, event_log }
835-
}
836-
other => other,
837-
};
838-
let stack = match stack {
839-
StackEvidence::Dstack {
840-
runtime_events,
841-
config,
842-
..
843-
} => StackEvidence::Dstack {
844-
report_data: report_data.to_vec(),
845-
runtime_events,
846-
config,
847-
},
848-
StackEvidence::DstackPod {
849-
runtime_events,
850-
config,
851-
report_data_payload,
852-
..
853-
} => StackEvidence::DstackPod {
854-
report_data: report_data.to_vec(),
855-
runtime_events,
856-
config,
857-
report_data_payload,
858-
},
859-
};
860-
AttestationV1 {
861-
version,
862-
platform,
863-
stack,
864-
}
817+
attestation.clone().into_v1().with_report_data(report_data)
865818
}
866819

867820
impl PlatformBackend for TestSimulatorPlatform {

0 commit comments

Comments
 (0)