1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use clap::Subcommand;
use cosmian_kmip::{
kmip_0::kmip_operations::DiscoverVersions,
kmip_2_1::{kmip_operations::Query, kmip_types::QueryFunction},
};
use cosmian_kms_client::{KmsClient, KmsClientConfig};
#[cfg(feature = "non-fips")]
use super::cover_crypt::CovercryptCommands;
#[cfg(feature = "non-fips")]
use super::pqc::PqcCommands;
use crate::{
actions::kms::{
access::AccessAction, attributes::AttributesCommands, aws::AwsCommands,
azure::AzureCommands, bench::BenchAction, certificates::CertificatesCommands,
console::Stdout, derive_key::DeriveKeyAction, elliptic_curves::EllipticCurveCommands,
google::GoogleCommands, hash::HashAction, login::LoginAction, mac::MacCommands,
opaque_object::OpaqueObjectCommands, rng::RngAction, rsa::RsaCommands,
secret_data::SecretDataCommands, shared::LocateObjectsAction, symmetric::SymmetricCommands,
version::ServerVersionAction,
},
error::result::KmsCliResult,
};
#[derive(Subcommand)]
pub enum ServerCommands {
/// Show server version information.
Version(ServerVersionAction),
/// Discover KMIP protocol versions supported by the server.
DiscoverVersions,
/// Query server capabilities and metadata (KMIP Query).
Query,
}
#[derive(Subcommand)]
pub enum KmsActions {
#[command(subcommand)]
AccessRights(AccessAction),
#[command(subcommand)]
Attributes(AttributesCommands),
#[command(subcommand)]
Azure(AzureCommands),
#[command(subcommand)]
Aws(AwsCommands),
Bench(BenchAction),
#[cfg(feature = "non-fips")]
#[command(subcommand)]
Cc(CovercryptCommands),
#[cfg(feature = "non-fips")]
#[command(subcommand)]
Pqc(PqcCommands),
#[command(subcommand)]
Certificates(CertificatesCommands),
DeriveKey(DeriveKeyAction),
#[command(subcommand)]
Ec(EllipticCurveCommands),
#[command(subcommand)]
Google(GoogleCommands),
Locate(LocateObjectsAction),
Login(LoginAction),
/// Logout from the Identity Provider.
///
/// The access token will be removed from the ckms configuration file.
Logout,
Hash(HashAction),
Mac(MacCommands),
/// RNG utilities: retrieve random bytes or seed RNG
Rng(RngAction),
/// Server-related commands.
#[command(subcommand)]
Server(ServerCommands),
#[command(subcommand)]
Rsa(RsaCommands),
#[command(subcommand)]
OpaqueObject(OpaqueObjectCommands),
#[command(subcommand)]
SecretData(SecretDataCommands),
#[command(subcommand)]
Sym(SymmetricCommands),
}
impl KmsActions {
/// Process the command line arguments
///
/// # Errors
/// - If the configuration file is not found or invalid
pub async fn process(&self, kms_rest_client: KmsClient) -> KmsCliResult<KmsClientConfig> {
let mut new_config = kms_rest_client.config.clone();
match self {
Self::AccessRights(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Attributes(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Aws(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Azure(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Bench(action) => Box::pin(action.process(kms_rest_client)).await?,
#[cfg(feature = "non-fips")]
Self::Cc(action) => Box::pin(action.process(kms_rest_client)).await?,
#[cfg(feature = "non-fips")]
Self::Pqc(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Certificates(action) => {
Box::pin(action.process(kms_rest_client)).await?;
}
Self::DeriveKey(action) => {
Box::pin(action.run(&kms_rest_client)).await?;
}
Self::Ec(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Google(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Locate(action) => {
Box::pin(action.run(kms_rest_client)).await?;
}
Self::Login(action) => {
let access_token = Box::pin(action.process(kms_rest_client.config)).await?;
new_config.http_config.access_token = Some(access_token);
}
Self::Logout => {
new_config.http_config.access_token = None;
}
Self::Hash(action) => Box::pin(action.run(kms_rest_client)).await?,
Self::Mac(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Rng(action) => Box::pin(action.run(kms_rest_client)).await?,
Self::Server(server_action) => match server_action {
ServerCommands::Version(action) => {
Box::pin(action.process(kms_rest_client)).await?;
}
ServerCommands::DiscoverVersions => {
Box::pin(async move {
let resp = kms_rest_client
.discover_versions(DiscoverVersions {
protocol_version: None,
})
.await?;
let versions = resp
.protocol_version
.unwrap_or_default()
.into_iter()
.map(|v| {
format!("{}.{}", v.protocol_version_major, v.protocol_version_minor)
})
.collect::<Vec<_>>()
.join(", ");
Stdout::new(&format!("Supported KMIP versions: {versions}")).write()?;
Ok::<(), crate::error::KmsCliError>(())
})
.await?;
}
ServerCommands::Query => {
Box::pin(async move {
// If query_function is None, ask all capabilities sequentially.
let all_funcs = [
QueryFunction::QueryOperations,
QueryFunction::QueryObjects,
QueryFunction::QueryServerInformation,
QueryFunction::QueryApplicationNamespaces,
QueryFunction::QueryExtensionList,
QueryFunction::QueryExtensionMap,
QueryFunction::QueryAttestationTypes,
QueryFunction::QueryRNGs,
QueryFunction::QueryValidations,
QueryFunction::QueryProfiles,
QueryFunction::QueryCapabilities,
QueryFunction::QueryClientRegistrationMethods,
QueryFunction::QueryDefaultsInformation,
QueryFunction::QueryStorageProtectionMasks,
];
for func in all_funcs {
let resp = kms_rest_client
.query(Query {
query_function: Some(vec![func]),
})
.await?;
match func {
QueryFunction::QueryOperations => {
let ops = resp
.operation
.unwrap_or_default()
.into_iter()
.map(|o| o.to_string())
.collect::<Vec<_>>()
.join(", ");
if !ops.is_empty() {
Stdout::new(&format!("Supported operations: {ops}"))
.write()?;
}
}
QueryFunction::QueryObjects => {
let objs = resp
.object_type
.unwrap_or_default()
.into_iter()
.map(|t| t.to_string())
.collect::<Vec<_>>()
.join(", ");
if !objs.is_empty() {
Stdout::new(&format!("Supported object types: {objs}"))
.write()?;
}
}
QueryFunction::QueryServerInformation => {
if let Some(vendor) = resp.vendor_identification {
Stdout::new(&format!("Vendor identification: {vendor}"))
.write()?;
}
if let Some(info) = resp.server_information {
Stdout::new(&format!("Server information: {info}"))
.write()?;
}
}
QueryFunction::QueryApplicationNamespaces => {
let namespaces =
resp.application_namespaces.unwrap_or_default().join(", ");
if !namespaces.is_empty() {
Stdout::new(&format!(
"Application namespaces: {namespaces}"
))
.write()?;
}
}
QueryFunction::QueryExtensionList
| QueryFunction::QueryExtensionMap => {
if let Some(exts) = resp.extension_information {
Stdout::new(&format!("Extensions: {} item(s)", exts.len()))
.write()?;
}
}
QueryFunction::QueryAttestationTypes => {
let types = resp
.attestation_types
.unwrap_or_default()
.into_iter()
.map(|t| t.to_string())
.collect::<Vec<_>>()
.join(", ");
if !types.is_empty() {
Stdout::new(&format!("Attestation types: {types}"))
.write()?;
}
}
QueryFunction::QueryRNGs => {
if let Some(params) = resp.rng_parameters {
Stdout::new(&format!(
"RNG parameters: {} item(s)",
params.len()
))
.write()?;
}
}
QueryFunction::QueryValidations => {
if let Some(vals) = resp.validation_information {
Stdout::new(&format!(
"Validation authorities: {} item(s)",
vals.len()
))
.write()?;
}
}
QueryFunction::QueryProfiles => {
if let Some(profiles) = resp.profiles_information {
Stdout::new(&format!(
"Profiles: {} item(s)",
profiles.len()
))
.write()?;
}
}
QueryFunction::QueryCapabilities
| QueryFunction::QueryClientRegistrationMethods => {
if let Some(caps) = resp.capability_information {
let caps_str = caps
.into_iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join("; ");
if !caps_str.is_empty() {
Stdout::new(&format!("Capabilities: {caps_str}"))
.write()?;
}
}
}
QueryFunction::QueryDefaultsInformation => {
if let Some(defs) = resp.defaults_information {
Stdout::new(&format!("Defaults information: {defs}"))
.write()?;
}
}
QueryFunction::QueryStorageProtectionMasks => {
if let Some(psm) = resp.protection_storage_masks {
Stdout::new(&format!("Protection storage masks: {psm}"))
.write()?;
}
}
}
}
Ok::<(), crate::error::KmsCliError>(())
})
.await?;
}
},
Self::Rsa(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::OpaqueObject(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::Sym(action) => Box::pin(action.process(kms_rest_client)).await?,
Self::SecretData(action) => Box::pin(action.process(kms_rest_client)).await?,
}
Ok(new_config)
}
}