forked from confidential-containers/guest-components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc-cdh-tool.rs
144 lines (128 loc) · 4.79 KB
/
grpc-cdh-tool.rs
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
// Copyright (c) 2024 Alibaba Cloud
//
// SPDX-License-Identifier: Apache-2.0
//
//! This tool is to test gRPC Confidential Data Hub
#![allow(non_snake_case)]
use api::{
get_resource_service_client::GetResourceServiceClient,
key_provider_service_client::KeyProviderServiceClient,
sealed_secret_service_client::SealedSecretServiceClient,
secure_mount_service_client::SecureMountServiceClient, GetResourceRequest,
KeyProviderKeyWrapProtocolInput, SecureMountRequest, UnsealSecretInput,
};
use base64::{engine::general_purpose::STANDARD, Engine};
use clap::{Args, Parser, Subcommand};
use storage::volume_type::Storage;
mod api {
tonic::include_proto!("api");
tonic::include_proto!("keyprovider");
}
#[derive(Parser)]
#[command(name = "cdh_client_grpc")]
#[command(bin_name = "cdh_client_grpc")]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
operation: Operation,
/// gRPC socket
#[arg(short, long, default_value_t = String::from("http://127.0.0.1:50000"))]
socket: String,
}
#[derive(Subcommand)]
#[command(author, version, about, long_about = None)]
enum Operation {
/// Unseal the given sealed secret
UnsealSecret(UnsealSecretArgs),
/// Unwrap the image encryption key
UnwrapKey(UnwrapKeyArgs),
/// Get Resource from KBS
GetResource(GetResourceArgs),
/// Secure mount
SecureMount(SecureMountArgs),
}
#[derive(Args)]
#[command(author, version, about, long_about = None)]
struct UnsealSecretArgs {
/// path to the file which contains the sealed secret
#[arg(short, long)]
secret_path: String,
}
#[derive(Args)]
#[command(author, version, about, long_about = None)]
struct UnwrapKeyArgs {
/// path to the file which contains the AnnotationPacket
#[arg(short, long)]
annotation_path: String,
}
#[derive(Args)]
#[command(author, version, about, long_about = None)]
struct GetResourceArgs {
/// KBS Resource URI to the target resource
#[arg(short, long)]
resource_uri: String,
}
#[derive(Args)]
#[command(author, version, about, long_about = None)]
struct SecureMountArgs {
/// path to the file which contains the Storage object.
#[arg(short, long)]
storage_path: String,
}
#[tokio::main]
async fn main() {
let args = Cli::parse();
match args.operation {
Operation::UnsealSecret(arg) => {
let mut client = SealedSecretServiceClient::connect(args.socket)
.await
.expect("initialize client");
let secret = tokio::fs::read(arg.secret_path).await.expect("read file");
let req = tonic::Request::new(UnsealSecretInput { secret });
let res = client.unseal_secret(req).await.expect("request to CDH");
let res = STANDARD.encode(res.into_inner().plaintext);
println!("{res}");
}
Operation::UnwrapKey(arg) => {
let mut client = KeyProviderServiceClient::connect(args.socket)
.await
.expect("initialize client");
let key_provider_key_wrap_protocol_input = tokio::fs::read(arg.annotation_path)
.await
.expect("read file");
let req = tonic::Request::new(KeyProviderKeyWrapProtocolInput {
key_provider_key_wrap_protocol_input,
});
let res = client.un_wrap_key(req).await.expect("request to CDH");
let res = STANDARD.encode(res.into_inner().key_provider_key_wrap_protocol_output);
println!("{res}");
}
Operation::GetResource(arg) => {
let mut client = GetResourceServiceClient::connect(args.socket)
.await
.expect("initialize client");
let req = tonic::Request::new(GetResourceRequest {
resource_path: arg.resource_uri,
});
let res = client.get_resource(req).await.expect("request to CDH");
let res = STANDARD.encode(res.into_inner().resource);
println!("{res}");
}
Operation::SecureMount(arg) => {
let mut client = SecureMountServiceClient::connect(args.socket)
.await
.expect("initialize client");
let storage_manifest = tokio::fs::read(arg.storage_path).await.expect("read file");
let storage: Storage =
serde_json::from_slice(&storage_manifest).expect("deserialize Storage");
let req = tonic::Request::new(SecureMountRequest {
volume_type: storage.volume_type,
flags: storage.flags,
options: storage.options,
mount_point: storage.mount_point,
});
let res = client.secure_mount(req).await.expect("request to CDH");
println!("mount path: {}", res.into_inner().mount_path);
}
}
}