Skip to main content

nitro_cli/common/
json_output.rs

1// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3#![deny(missing_docs)]
4#![deny(warnings)]
5#![allow(clippy::too_many_arguments)]
6
7use aws_nitro_enclaves_image_format::defs::EifIdentityInfo;
8use aws_nitro_enclaves_image_format::utils::eif_reader::SignCertificateInfo;
9use serde::{Deserialize, Serialize};
10use std::collections::BTreeMap;
11
12/// The information to be provided for a `describe-enclaves` request.
13#[derive(Clone, Serialize, Deserialize)]
14pub struct EnclaveDescribeInfo {
15    /// Enclave name assigned by the user
16    #[serde(skip_serializing_if = "Option::is_none")]
17    #[serde(rename = "EnclaveName")]
18    pub enclave_name: Option<String>,
19    #[serde(rename = "EnclaveID")]
20    /// The full ID of the enclave.
21    pub enclave_id: String,
22    #[serde(rename = "ProcessID")]
23    /// The PID of the enclave process which manages the enclave.
24    pub process_id: u32,
25    #[serde(rename = "EnclaveCID")]
26    /// The enclave's CID.
27    pub enclave_cid: u64,
28    #[serde(rename = "NumberOfCPUs")]
29    /// The number of CPUs used by the enclave.
30    pub cpu_count: u64,
31    #[serde(rename = "CPUIDs")]
32    /// The IDs of the CPUs used by the enclave.
33    pub cpu_ids: Vec<u32>,
34    #[serde(rename = "MemoryMiB")]
35    /// The memory provided to the enclave (in MiB).
36    pub memory_mib: u64,
37    #[serde(rename = "State")]
38    /// The current state of the enclave.
39    pub state: String,
40    #[serde(rename = "Flags")]
41    /// The bit-mask which provides the enclave's launch flags.
42    pub flags: String,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[serde(flatten)]
45    /// Build measurements containing PCRs
46    pub build_info: Option<EnclaveBuildInfo>,
47    /// Assigned or default EIF name
48    #[serde(skip_serializing_if = "Option::is_none")]
49    #[serde(rename = "ImageName")]
50    pub img_name: Option<String>,
51    #[serde(rename = "ImageVersion")]
52    /// Assigned or default EIF version
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub img_version: Option<String>,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    #[serde(rename = "Metadata")]
57    /// EIF metadata
58    pub metadata: Option<MetadataDescribeInfo>,
59}
60
61impl EnclaveDescribeInfo {
62    /// Create a new `EnclaveDescribeInfo` instance from the given enclave information.
63    pub fn new(
64        enclave_name: Option<String>,
65        enclave_id: String,
66        enclave_cid: u64,
67        cpu_count: u64,
68        cpu_ids: Vec<u32>,
69        memory_mib: u64,
70        state: String,
71        flags: String,
72        build_info: Option<EnclaveBuildInfo>,
73        img_name: Option<String>,
74        img_version: Option<String>,
75        metadata: Option<MetadataDescribeInfo>,
76    ) -> Self {
77        EnclaveDescribeInfo {
78            enclave_name,
79            enclave_id,
80            process_id: std::process::id(),
81            enclave_cid,
82            cpu_count,
83            cpu_ids,
84            memory_mib,
85            state,
86            flags,
87            build_info,
88            img_name,
89            img_version,
90            metadata,
91        }
92    }
93}
94
95/// The information to be provided for a `run-enclave` request.
96#[derive(Clone, Serialize, Deserialize)]
97pub struct EnclaveRunInfo {
98    #[serde(rename = "EnclaveName")]
99    /// The name of the enclave.
100    pub enclave_name: String,
101    #[serde(rename = "EnclaveID")]
102    /// The full ID of the enclave.
103    pub enclave_id: String,
104    #[serde(rename = "ProcessID")]
105    /// The PID of the enclave process which manages the enclave.
106    pub process_id: u32,
107    #[serde(rename = "EnclaveCID")]
108    /// The enclave's CID.
109    pub enclave_cid: u64,
110    #[serde(rename = "NumberOfCPUs")]
111    /// The number of CPUs used by the enclave.
112    pub cpu_count: usize,
113    #[serde(rename = "CPUIDs")]
114    /// The IDs of the CPUs used by the enclave.
115    pub cpu_ids: Vec<u32>,
116    #[serde(rename = "MemoryMiB")]
117    /// The memory provided to the enclave (in MiB).
118    pub memory_mib: u64,
119}
120
121impl EnclaveRunInfo {
122    /// Create a new `EnclaveRunInfo` instance from the given enclave information.
123    pub fn new(
124        enclave_name: String,
125        enclave_id: String,
126        enclave_cid: u64,
127        cpu_count: usize,
128        cpu_ids: Vec<u32>,
129        memory_mib: u64,
130    ) -> Self {
131        EnclaveRunInfo {
132            enclave_name,
133            enclave_id,
134            process_id: std::process::id(),
135            enclave_cid,
136            cpu_count,
137            cpu_ids,
138            memory_mib,
139        }
140    }
141}
142
143/// The information to be provided for a `terminate-enclave` request.
144#[derive(Clone, Serialize, Deserialize)]
145pub struct EnclaveTerminateInfo {
146    #[serde(skip_serializing_if = "Option::is_none")]
147    #[serde(rename = "EnclaveName")]
148    /// The name of the enclave. Optional for older versions.
149    pub enclave_name: Option<String>,
150    #[serde(rename = "EnclaveID")]
151    /// The full ID of the enclave.
152    pub enclave_id: String,
153    #[serde(rename = "Terminated")]
154    /// A flag indicating if the enclave has terminated.
155    pub terminated: bool,
156}
157
158impl EnclaveTerminateInfo {
159    /// Create a new `EnclaveTerminateInfo` instance from the given enclave information.
160    pub fn new(enclave_name: Option<String>, enclave_id: String, terminated: bool) -> Self {
161        EnclaveTerminateInfo {
162            enclave_name,
163            enclave_id,
164            terminated,
165        }
166    }
167}
168
169/// The information to be provided for a `build-enclave` request.
170#[derive(Serialize, Clone, Deserialize, Debug)]
171pub struct EnclaveBuildInfo {
172    #[serde(rename = "Measurements")]
173    /// The measurement results (hashes) of various enclave properties.
174    pub measurements: BTreeMap<String, String>,
175}
176
177impl EnclaveBuildInfo {
178    /// Create a new `EnclaveBuildInfo` instance from the given measurements.
179    pub fn new(measurements: BTreeMap<String, String>) -> Self {
180        EnclaveBuildInfo { measurements }
181    }
182}
183
184/// The information to be provided for a `describe-eif` request.
185#[derive(Clone, Serialize, Deserialize)]
186pub struct EifDescribeInfo {
187    #[serde(rename = "EifVersion")]
188    /// EIF version.
189    pub version: u16,
190    #[serde(flatten)]
191    /// Contains the PCR values.
192    pub build_info: EnclaveBuildInfo,
193    #[serde(rename = "IsSigned")]
194    /// Specifies if the image is signed or not.
195    pub is_signed: bool,
196    #[serde(skip_serializing_if = "Option::is_none")]
197    #[serde(rename = "SigningCertificate")]
198    /// Certificate's signature algorithm
199    pub cert_info: Option<SignCertificateInfo>,
200    #[serde(rename = "CheckCRC")]
201    /// Specifies if the CRC check passed.
202    pub crc_check: bool,
203    #[serde(skip_serializing_if = "Option::is_none")]
204    #[serde(rename = "SignatureCheck")]
205    /// Specifies if the EIF signature check passed.
206    pub sign_check: Option<bool>,
207    #[serde(rename = "ImageName")]
208    /// Assigned or default EIF name
209    #[serde(skip_serializing_if = "Option::is_none")]
210    pub img_name: Option<String>,
211    #[serde(rename = "ImageVersion")]
212    /// Assigned or default EIF version
213    #[serde(skip_serializing_if = "Option::is_none")]
214    pub img_version: Option<String>,
215    #[serde(skip_serializing_if = "Option::is_none")]
216    #[serde(rename = "Metadata")]
217    /// EIF metadata
218    pub metadata: Option<MetadataDescribeInfo>,
219}
220
221/// Metadata to be included in the describe output
222#[derive(Clone, Serialize, Deserialize)]
223pub struct MetadataDescribeInfo {
224    #[serde(rename = "BuildTime")]
225    /// Time of the build
226    pub build_time: String,
227    #[serde(rename = "BuildTool")]
228    /// Tool used for EIF build
229    pub build_tool: String,
230    #[serde(rename = "BuildToolVersion")]
231    /// Version of the build tool
232    pub tool_version: String,
233    #[serde(rename = "OperatingSystem")]
234    /// Enclave OS
235    pub operating_system: String,
236    #[serde(rename = "KernelVersion")]
237    /// Enclave kernel version
238    pub kernel_version: String,
239    #[serde(rename = "DockerInfo")]
240    /// Docker image information
241    pub docker_info: serde_json::Value,
242    #[serde(skip_serializing_if = "serde_json::Value::is_null")]
243    #[serde(rename = "CustomMetadata")]
244    #[serde(flatten)]
245    /// Metadata added by the user as JSON
246    pub custom_metadata: serde_json::Value,
247}
248
249impl MetadataDescribeInfo {
250    /// Construct metadata output struct
251    pub fn new(eif_info: EifIdentityInfo) -> Self {
252        MetadataDescribeInfo {
253            build_time: eif_info.build_info.build_time,
254            build_tool: eif_info.build_info.build_tool,
255            tool_version: eif_info.build_info.build_tool_version,
256            operating_system: eif_info.build_info.img_os,
257            kernel_version: eif_info.build_info.img_kernel,
258            docker_info: eif_info.docker_info,
259            custom_metadata: eif_info.custom_info,
260        }
261    }
262}