wasmcloud_core/
link.rs

1//! Core reusable types related to [links on wasmCloud lattices][docs-links]
2//!
3//! [docs-links]: <https://wasmcloud.com/docs/concepts/linking-components>
4
5use std::collections::HashMap;
6
7use secrecy::zeroize::{Zeroize, ZeroizeOnDrop};
8use serde::{Deserialize, Serialize};
9
10use crate::{ComponentId, LatticeTarget, WitInterface, WitNamespace, WitPackage};
11
12/// Name of a link on the wasmCloud lattice
13pub type LinkName = String;
14
15/// A link definition between a source and target component (component or provider) on a given
16/// interface. An [`InterfaceLinkDefinition`] connects one component's import to another
17/// component's export, specifying the configuration each component needs in order to execute
18/// the request, and represents an operator's intent to allow the source to invoke the target.
19#[derive(Clone, Debug, Default, Deserialize, Serialize)]
20pub struct InterfaceLinkDefinition {
21    /// Source identifier for the link
22    pub source_id: ComponentId,
23    /// Target for the link, which can be a unique identifier or (future) a routing group
24    pub target: LatticeTarget,
25    /// Name of the link. Not providing this is equivalent to specifying "default"
26    #[serde(default = "default_link_name")]
27    pub name: LinkName,
28    /// WIT namespace of the link operation, e.g. `wasi` in `wasi:keyvalue/readwrite.get`
29    pub wit_namespace: WitNamespace,
30    /// WIT package of the link operation, e.g. `keyvalue` in `wasi:keyvalue/readwrite.get`
31    pub wit_package: WitPackage,
32    /// WIT Interfaces to be used for the link, e.g. `readwrite`, `atomic`, etc.
33    pub interfaces: Vec<WitInterface>,
34    /// The configuration to give to the source for this link
35    #[serde(default)]
36    pub source_config: HashMap<String, String>,
37    /// The configuration to give to the target for this link
38    #[serde(default)]
39    pub target_config: HashMap<String, String>,
40    /// The secrets to give to the source of this link
41    /// Should decrypt as a [`HashMap<String, SecretValue>`]
42    #[serde(default)]
43    pub source_secrets: Option<Vec<u8>>,
44    /// The secrets to give to the target of this link
45    /// Should decrypt as a [`HashMap<String, SecretValue>`]
46    #[serde(default)]
47    pub target_secrets: Option<Vec<u8>>,
48}
49
50// Trait implementations that ensure we zeroize secrets when they are dropped
51impl ZeroizeOnDrop for InterfaceLinkDefinition {}
52impl Zeroize for InterfaceLinkDefinition {
53    fn zeroize(&mut self) {
54        self.source_secrets.zeroize();
55        self.target_secrets.zeroize();
56    }
57}
58
59/// Helper function to provide a default link name
60pub(crate) fn default_link_name() -> LinkName {
61    "default".to_string()
62}