-
Notifications
You must be signed in to change notification settings - Fork 97
/
cdh-oneshot.rs
185 lines (165 loc) · 5.64 KB
/
cdh-oneshot.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
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
// Copyright (c) 2024 Alibaba Cloud
//
// SPDX-License-Identifier: Apache-2.0
//
//! This is a one-shot version of CDH
#![allow(non_snake_case)]
use base64::{engine::general_purpose::STANDARD, Engine};
use clap::{Args, Parser, Subcommand};
use confidential_data_hub::{hub::Hub, storage::volume_type::Storage, CdhConfig, DataHub};
use log::warn;
#[derive(Parser)]
#[command(name = "cdh_oneshot")]
#[command(bin_name = "cdh_oneshot")]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
operation: Operation,
/// CDH's config path
#[arg(short, long)]
config: Option<String>,
/// Retries times
#[arg(short, long, default_value = "3")]
retry: u32,
}
#[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),
/// Pull image
PullImage(PullImageArgs),
}
#[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,
}
#[derive(Args)]
#[command(author, version, about, long_about = None)]
struct PullImageArgs {
/// URL of the image to be pulled
#[arg(short, long)]
image_url: String,
/// Path to store the bundle
#[arg(short, long)]
bundle_path: String,
}
#[tokio::main]
async fn main() {
let args = Cli::parse();
let config = CdhConfig::new(args.config).expect("failed to initialize cdh config");
config.set_configuration_envs();
let cdh = Hub::new(config).await.expect("failed to start CDH");
let mut tried = 1;
match args.operation {
Operation::UnsealSecret(op_args) => {
let secret = tokio::fs::read(op_args.secret_path)
.await
.expect("read secret file");
loop {
match cdh.unseal_secret(secret.clone()).await {
Ok(secret) => {
let res = STANDARD.encode(secret);
println!("{res}");
break;
}
Err(e) => {
if tried > args.retry {
let error = format!("failed to unseal secret, {:?}", e);
panic!("{error}");
}
warn!("Tried {tried} times... failed to unseal secret: {e}.");
tried += 1;
}
}
}
}
Operation::UnwrapKey(op_args) => {
let KeyProviderKeyWrapProtocolInput = tokio::fs::read(op_args.annotation_path)
.await
.expect("read annotation packet file");
loop {
match cdh.unwrap_key(&KeyProviderKeyWrapProtocolInput).await {
Ok(KeyProviderKeyWrapProtocolOutput) => {
let res = STANDARD.encode(KeyProviderKeyWrapProtocolOutput);
println!("{res}");
break;
}
Err(e) => {
if tried > args.retry {
panic!("failed to unwrap key");
}
warn!("Tried {tried} times... failed to unwrap key: {e}.");
tried += 1;
}
}
}
}
Operation::GetResource(op_args) => loop {
match cdh.get_resource(op_args.resource_uri.clone()).await {
Ok(resource) => {
let res = STANDARD.encode(resource);
println!("{res}");
break;
}
Err(e) => {
if tried > args.retry {
let error = format!("failed to get resource, {:?}", e);
panic!("{error}");
}
warn!("Tried {tried} times... failed to get resource: {e}.");
tried += 1;
}
}
},
Operation::SecureMount(op_args) => {
let storage_manifest = tokio::fs::read(op_args.storage_path)
.await
.expect("read file");
let storage: Storage =
serde_json::from_slice(&storage_manifest).expect("deserialize Storage");
let res = cdh
.secure_mount(storage)
.await
.expect("failed to secure mount");
println!("mount path: {res}");
}
Operation::PullImage(op_args) => {
let manifest_digest = cdh
.pull_image(&op_args.image_url, &op_args.bundle_path)
.await
.expect("failed to pull image");
println!("image digest: {manifest_digest}");
}
}
}