zerokms_protocol/
lib.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use async_trait::async_trait;
use cipherstash_config::DatasetConfig;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, ops::Deref};
use uuid::Uuid;

mod base64_array;
mod base64_vec;
mod error;

pub use error::*;

/// Re-export config
pub use cipherstash_config;

pub mod testing;

#[async_trait]
pub trait ViturConnection {
    async fn send<Request: ViturRequest>(
        &self,
        request: Request,
        access_token: &str,
    ) -> Result<Request::Response, ViturRequestError>;
}

pub trait ViturResponse: Serialize + for<'de> Deserialize<'de> + Send {}

#[async_trait]
pub trait ViturRequest: Serialize + for<'de> Deserialize<'de> + Sized + Send {
    type Response: ViturResponse;

    const SCOPE: &'static str;
    const ENDPOINT: &'static str;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateDatasetRequest<'a> {
    pub name: Cow<'a, str>,
    pub description: Cow<'a, str>,
}

impl ViturRequest for CreateDatasetRequest<'_> {
    type Response = Dataset;

    const ENDPOINT: &'static str = "create-dataset";
    const SCOPE: &'static str = "dataset:create";
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct ListDatasetRequest {
    #[serde(default)]
    pub show_disabled: bool,
}

impl ViturRequest for ListDatasetRequest {
    type Response = Vec<Dataset>;

    const ENDPOINT: &'static str = "list-datasets";
    const SCOPE: &'static str = "dataset:list";
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Dataset {
    pub id: Uuid,
    pub name: String,
    pub description: String,
}

impl ViturResponse for Dataset {}

impl ViturResponse for Vec<Dataset> {}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct EmptyResponse {}

impl ViturResponse for EmptyResponse {}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateClientRequest<'a> {
    pub dataset_id: Uuid,
    pub name: Cow<'a, str>,
    pub description: Cow<'a, str>,
}

impl<'a> ViturRequest for CreateClientRequest<'a> {
    type Response = CreateClientResponse;

    const ENDPOINT: &'static str = "create-client";
    const SCOPE: &'static str = "client:create";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateClientResponse {
    pub id: Uuid,
    pub dataset_id: Uuid,
    pub name: String,
    pub description: String,

    #[serde(with = "base64_vec")]
    pub client_key: Vec<u8>,
}

impl ViturResponse for CreateClientResponse {}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListClientRequest;

impl ViturRequest for ListClientRequest {
    type Response = Vec<DatasetClient>;

    const ENDPOINT: &'static str = "list-clients";
    const SCOPE: &'static str = "client:list";
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum ClientDatasetId {
    Single(Uuid),
    Multiple(Vec<Uuid>),
}

/// A `Uuid` is comparable with `ClientDatasetId` if the `ClientDatasetId` is a `Single` variant.
impl PartialEq<Uuid> for ClientDatasetId {
    fn eq(&self, other: &Uuid) -> bool {
        if let ClientDatasetId::Single(id) = self {
            id == other
        } else {
            false
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DatasetClient {
    pub id: Uuid,
    pub dataset_id: ClientDatasetId,
    pub name: String,
    pub description: String,
}

impl ViturResponse for Vec<DatasetClient> {}

#[derive(Debug, Serialize, Deserialize)]
pub struct RevokeClientRequest {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
}

impl ViturRequest for RevokeClientRequest {
    type Response = RevokeClientResponse;

    const ENDPOINT: &'static str = "revoke-client";
    const SCOPE: &'static str = "client:revoke";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RevokeClientResponse {}

impl ViturResponse for RevokeClientResponse {}

#[derive(Debug, Serialize, Deserialize)]
pub struct ViturKeyMaterial(#[serde(with = "base64_vec")] Vec<u8>);

impl From<Vec<u8>> for ViturKeyMaterial {
    fn from(inner: Vec<u8>) -> Self {
        Self(inner)
    }
}

impl Deref for ViturKeyMaterial {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GeneratedKey {
    pub key_material: ViturKeyMaterial,
    #[serde(with = "base64_vec")]
    pub tag: Vec<u8>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GenerateKeyResponse {
    pub keys: Vec<GeneratedKey>,
}

impl ViturResponse for GenerateKeyResponse {}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GenerateKeySpec<'a> {
    #[serde(with = "base64_array")]
    pub iv: [u8; 16],
    pub descriptor: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GenerateKeyRequest<'a> {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
    pub keys: Cow<'a, [GenerateKeySpec<'a>]>,
}

impl ViturRequest for GenerateKeyRequest<'_> {
    type Response = GenerateKeyResponse;

    const ENDPOINT: &'static str = "generate-data-key";
    const SCOPE: &'static str = "data_key:generate";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RetrievedKey {
    pub key_material: ViturKeyMaterial,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RetrieveKeyResponse {
    pub keys: Vec<RetrievedKey>,
}

impl ViturResponse for RetrieveKeyResponse {}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RetrieveKeySpec<'a> {
    #[serde(with = "base64_array")]
    pub iv: [u8; 16],
    pub descriptor: Cow<'a, str>,
    pub tag: Cow<'a, [u8]>,

    // Since this field will be removed in the future allow older versions of Vitur to be able to
    // parse a RetrieveKeySpec that doesn't include the tag_version.
    #[serde(default)]
    pub tag_version: usize,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RetrieveKeyRequest<'a> {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
    pub keys: Cow<'a, [RetrieveKeySpec<'a>]>,
}

impl ViturRequest for RetrieveKeyRequest<'_> {
    type Response = RetrieveKeyResponse;

    const ENDPOINT: &'static str = "retrieve-data-key";
    const SCOPE: &'static str = "data_key:retrieve";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SaveConfigRequest<'a> {
    pub client_id: Uuid,
    #[serde(with = "base64_vec")]
    pub encrypted_index_root_key: Vec<u8>,
    pub dataset_config: Cow<'a, DatasetConfig>,
    pub dataset_id: Option<Uuid>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SaveConfigResponse {}

impl ViturResponse for SaveConfigResponse {}

impl ViturRequest for SaveConfigRequest<'_> {
    type Response = SaveConfigResponse;

    const ENDPOINT: &'static str = "save-config";
    const SCOPE: &'static str = "config:write";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoadConfigRequest {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoadConfigResponse {
    #[serde(with = "base64_vec")]
    pub encrypted_index_root_key: Vec<u8>,
    pub dataset_config: DatasetConfig,
}

impl ViturResponse for LoadConfigResponse {}

impl ViturRequest for LoadConfigRequest {
    type Response = LoadConfigResponse;

    const ENDPOINT: &'static str = "load-config";
    const SCOPE: &'static str = "config:read";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DisableDatasetRequest {
    pub dataset_id: Uuid,
}

impl ViturRequest for DisableDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "disable-dataset";
    const SCOPE: &'static str = "dataset:disable";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct EnableDatasetRequest {
    pub dataset_id: Uuid,
}

impl ViturRequest for EnableDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "enable-dataset";
    const SCOPE: &'static str = "dataset:enable";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ModifyDatasetRequest<'a> {
    pub dataset_id: Uuid,

    pub name: Option<Cow<'a, str>>,
    pub description: Option<Cow<'a, str>>,
}

impl ViturRequest for ModifyDatasetRequest<'_> {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "modify-dataset";
    const SCOPE: &'static str = "dataset:modify";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GrantDatasetRequest {
    pub client_id: Uuid,
    pub dataset_id: Uuid,
}

impl ViturRequest for GrantDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "grant-dataset";
    const SCOPE: &'static str = "dataset:grant";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RevokeDatasetRequest {
    pub client_id: Uuid,
    pub dataset_id: Uuid,
}

impl ViturRequest for RevokeDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "revoke-dataset";
    const SCOPE: &'static str = "dataset:revoke";
}