ockam_api 0.49.0

Ockam's request-response API
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::fmt::{Display, Formatter};
use std::sync::Arc;

use ockam::identity::models::{ChangeHistory, CredentialAndPurposeKey};
use ockam::identity::{
    AuthorityService, CredentialsMemoryRetriever, Identifier, Identity, RemoteCredentialsRetriever,
    RemoteCredentialsRetrieverInfo, SecureChannels, TrustContext,
};
use ockam_core::errcode::{Kind, Origin};
use ockam_core::Error;
use ockam_multiaddr::MultiAddr;
use ockam_transport_tcp::TcpTransport;

use crate::cli_state::CliState;
use crate::multiaddr_to_route;
use crate::nodes::service::default_address::DefaultAddress;

use super::Result;

impl CliState {
    pub async fn get_trust_context(&self, name: &str) -> Result<NamedTrustContext> {
        match self
            .trust_contexts_repository()
            .await?
            .get_trust_context(name)
            .await?
        {
            Some(trust_context) => Ok(trust_context),
            None => Err(Error::new(
                Origin::Api,
                Kind::NotFound,
                format!("there is no trust context with name {name}"),
            )
            .into()),
        }
    }

    pub async fn get_default_trust_context(&self) -> Result<NamedTrustContext> {
        match self
            .trust_contexts_repository()
            .await?
            .get_default_trust_context()
            .await?
        {
            Some(trust_context) => Ok(trust_context),
            None => Err(Error::new(
                Origin::Api,
                Kind::NotFound,
                "there is no default trust context",
            )
            .into()),
        }
    }

    pub async fn get_trust_context_or_default(
        &self,
        name: &Option<String>,
    ) -> Result<NamedTrustContext> {
        match name {
            Some(name) => self.get_trust_context(name).await,
            None => self.get_default_trust_context().await,
        }
    }

    pub async fn retrieve_trust_context(
        &self,
        trust_context_name: &Option<String>,
        project_name: &Option<String>,
        authority_identity: &Option<Identity>,
        credential_name: &Option<String>,
    ) -> Result<Option<NamedTrustContext>> {
        match trust_context_name {
            Some(name) => Ok(Some(self.get_trust_context(name).await?)),
            None => {
                let project = match project_name {
                    Some(name) => self.get_project_by_name(name).await.ok(),
                    None => self.get_default_project().await.ok(),
                };
                match project {
                    Some(project) => Ok(self.get_trust_context(&project.name).await.ok()),
                    None => match credential_name {
                        Some(credential_name) => Ok(Some(
                            self.create_trust_context(
                                None,
                                None,
                                Some(credential_name.clone()),
                                authority_identity.clone(),
                                None,
                            )
                            .await?,
                        )),
                        None => Ok(None),
                    },
                }
            }
        }
    }

    pub async fn get_trust_contexts(&self) -> Result<Vec<NamedTrustContext>> {
        Ok(self
            .trust_contexts_repository()
            .await?
            .get_trust_contexts()
            .await?)
    }

    pub async fn delete_trust_context(&self, name: &str) -> Result<()> {
        Ok(self
            .trust_contexts_repository()
            .await?
            .delete_trust_context(name)
            .await?)
    }

    pub async fn set_default_trust_context(&self, name: &str) -> Result<()> {
        Ok(self
            .trust_contexts_repository()
            .await?
            .set_default_trust_context(name)
            .await?)
    }

    pub async fn create_trust_context(
        &self,
        name: Option<String>,
        trust_context_id: Option<String>,
        credential_name: Option<String>,
        authority_identity: Option<Identity>,
        authority_route: Option<MultiAddr>,
    ) -> Result<NamedTrustContext> {
        let credential = match credential_name {
            Some(name) => self.get_credential_by_name(&name).await.ok(),
            None => None,
        };
        let name = name.unwrap_or("default".to_string());

        // if the authority identity is not defined use the
        // authority identity defined on the credential
        let authority_identity = match authority_identity.clone() {
            Some(identity) => Some(identity),
            None => match credential.clone() {
                None => None,
                Some(credential) => Some(credential.issuer_identity().await?),
            },
        };

        let trust_context_id = trust_context_id
            .or_else(|| {
                authority_identity
                    .clone()
                    .map(|i| i.identifier().to_string())
            })
            .unwrap_or("default".to_string());

        let trust_context = NamedTrustContext::new(
            &name,
            &trust_context_id,
            credential.map(|c| c.credential_and_purpose_key()),
            authority_identity.map(|i| i.change_history().clone()),
            authority_route,
        );

        let repository = self.trust_contexts_repository().await?;
        repository.store_trust_context(&trust_context).await?;

        // If there is no previous default trust_context set this trust_context as the default
        let default_trust_context = repository.get_default_trust_context().await?;
        if default_trust_context.is_none() {
            repository
                .set_default_trust_context(&trust_context.name())
                .await?
        };

        Ok(trust_context)
    }
}

/// A NamedTrustContext collects all the data necessary to create a TrustContext
/// under a specific name:
///
/// Either we can
///   - retrieve a fixed credential
///   - access an authority node to retrieve credentials
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamedTrustContext {
    name: String,
    trust_context_id: String,
    credential: Option<CredentialAndPurposeKey>,
    authority_change_history: Option<ChangeHistory>,
    authority_route: Option<MultiAddr>,
}

impl NamedTrustContext {
    pub fn new(
        name: &str,
        trust_context_id: &str,
        credential: Option<CredentialAndPurposeKey>,
        authority_identity: Option<ChangeHistory>,
        authority_route: Option<MultiAddr>,
    ) -> Self {
        Self {
            name: name.to_string(),
            trust_context_id: trust_context_id.to_string(),
            credential,
            authority_change_history: authority_identity,
            authority_route,
        }
    }
}

impl Display for NamedTrustContext {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Name: {}", self.name())?;
        Ok(())
    }
}

impl NamedTrustContext {
    pub fn name(&self) -> String {
        self.name.clone()
    }

    pub fn trust_context_id(&self) -> String {
        self.trust_context_id.to_string()
    }

    pub fn credential(&self) -> Option<CredentialAndPurposeKey> {
        self.credential.clone()
    }

    /// Return the route to the trust context authority if configured
    pub fn authority_route(&self) -> Option<MultiAddr> {
        self.authority_route.clone()
    }

    /// Return the change history of the trust context authority if configured
    pub fn authority_change_history(&self) -> Option<ChangeHistory> {
        self.authority_change_history.clone()
    }

    /// Return the identity of the trust context authority if configured
    pub async fn authority_identity(&self) -> Result<Option<Identity>> {
        match &self.authority_change_history {
            Some(change_history) => Ok(Some(
                Identity::create_from_change_history(change_history).await?,
            )),
            None => Ok(None),
        }
    }

    /// Return the identifier of the trust context authority if configured
    pub async fn authority_identifier(&self) -> Result<Option<Identifier>> {
        Ok(self
            .authority_identity()
            .await?
            .map(|i| i.identifier().clone()))
    }

    /// Make a TrustContext
    /// This requires a transport and secure channels if we need to communicate with an Authority node
    pub async fn trust_context(
        &self,
        tcp_transport: &TcpTransport,
        secure_channels: Arc<SecureChannels>,
    ) -> Result<TrustContext> {
        let authority_identifier = self.authority_identifier().await?;
        let authority_service = match (
            self.credential.clone(),
            authority_identifier,
            self.authority_route.clone(),
        ) {
            (Some(credential), Some(identifier), _) => {
                let credential_retriever = CredentialsMemoryRetriever::new(credential);
                Some(AuthorityService::new(
                    secure_channels.identities().credentials(),
                    identifier,
                    Some(Arc::new(credential_retriever)),
                ))
            }
            (None, Some(identifier), Some(route)) => {
                let credential_retriever = RemoteCredentialsRetriever::new(
                    secure_channels.clone(),
                    RemoteCredentialsRetrieverInfo::new(
                        identifier.clone(),
                        multiaddr_to_route(&route, tcp_transport)
                            .await
                            .ok_or_else(|| {
                                Error::new(
                                    Origin::Api,
                                    Kind::Internal,
                                    format!("cannot create a route from the address {route}"),
                                )
                            })?
                            .route,
                        DefaultAddress::CREDENTIAL_ISSUER.into(),
                    ),
                );
                Some(AuthorityService::new(
                    secure_channels.identities().credentials(),
                    identifier,
                    Some(Arc::new(credential_retriever)),
                ))
            }
            (None, Some(identifier), None) => Some(AuthorityService::new(
                secure_channels.identities().credentials(),
                identifier.clone(),
                None,
            )),
            _ => None,
        };
        Ok(TrustContext::new(
            self.trust_context_id.clone(),
            authority_service,
        ))
    }

    /// Return access data for an authority in order to be able to create
    /// a RPC client to that authority and obtain credentials
    pub async fn authority(&self) -> Result<Option<Authority>> {
        match (
            self.authority_identifier().await?,
            self.authority_route.clone(),
        ) {
            (Some(identifier), Some(route)) => Ok(Some(Authority::new(identifier, route))),
            _ => Ok(None),
        }
    }
}

/// Configuration of an authority node
#[derive(Clone)]
pub struct Authority {
    identifier: Identifier,
    route: MultiAddr,
}

impl Authority {
    pub fn new(identifier: Identifier, route: MultiAddr) -> Self {
        Self { identifier, route }
    }

    pub fn identifier(&self) -> Identifier {
        self.identifier.clone()
    }

    pub fn route(&self) -> MultiAddr {
        self.route.clone()
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::time::Duration;

    use ockam::identity::models::CredentialAndPurposeKey;
    use ockam::identity::models::CredentialSchemaIdentifier;
    use ockam::identity::utils::AttributesBuilder;
    use ockam::identity::{identities, Identifier, Identities};
    use ockam_core::env::FromString;

    use super::*;

    // There are 3 ways to create a trust context
    //  - with only an id
    //  - with a credential
    //  - with an authority identity + route
    #[tokio::test]
    async fn test_create_trust_context() -> Result<()> {
        let cli = CliState::test().await?;

        // 1. with only an id
        let result = cli
            .create_trust_context(
                Some("trust-context".into()),
                Some("1".into()),
                None,
                None,
                None,
            )
            .await?;
        let expected = NamedTrustContext::new("trust-context", "1", None, None, None);
        assert_eq!(result, expected);

        // that trust context is the default one because it is the first created trust context
        let result = cli.get_default_trust_context().await?;
        assert_eq!(result, expected);

        // 2. with a credential
        let identities = identities().await?;
        let authority_identifier = identities.identities_creation().create_identity().await?;
        let authority = identities.get_identity(&authority_identifier).await?;
        let credential = create_credential(identities, &authority_identifier).await?;
        cli.store_credential("credential-name", &authority, credential.clone())
            .await?;
        let result = cli
            .create_trust_context(
                Some("trust-context".into()),
                None,
                Some("credential-name".into()),
                None,
                None,
            )
            .await?;
        let expected = NamedTrustContext::new(
            "trust-context",
            authority.identifier().to_string().as_str(),
            Some(credential),
            Some(authority.change_history().clone()),
            None,
        );
        assert_eq!(result, expected);

        // 3. with an authority
        let authority_route = MultiAddr::from_string("/dnsaddr/127.0.0.1/tcp/5000/service/api")?;
        let result = cli
            .create_trust_context(
                Some("trust-context".into()),
                None,
                None,
                Some(authority.clone()),
                Some(authority_route.clone()),
            )
            .await?;
        let expected = NamedTrustContext::new(
            "trust-context",
            authority.identifier().to_string().as_str(),
            None,
            Some(authority.change_history().clone()),
            Some(authority_route),
        );
        assert_eq!(result, expected);
        Ok(())
    }

    /// HELPERS
    pub async fn create_credential(
        identities: Arc<Identities>,
        issuer: &Identifier,
    ) -> Result<CredentialAndPurposeKey> {
        let subject = identities.identities_creation().create_identity().await?;

        let attributes = AttributesBuilder::with_schema(CredentialSchemaIdentifier(1))
            .with_attribute("name".as_bytes().to_vec(), b"value".to_vec())
            .build();

        Ok(identities
            .credentials()
            .credentials_creation()
            .issue_credential(issuer, &subject, attributes, Duration::from_secs(1))
            .await?)
    }
}