wasmcloud_provider_wadm/
config.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
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::warn;
use wasmcloud_provider_sdk::{core::secrets::SecretValue, LinkConfig};

const DEFAULT_CTL_HOST: &str = "0.0.0.0";
const DEFAULT_CTL_PORT: u16 = 4222;
const DEFAULT_LATTICE: &str = "default";

// Configuration keys
const CONFIG_LATTICE: &str = "lattice";
const CONFIG_APP_NAME: &str = "app_name";
const CONFIG_CTL_HOST: &str = "ctl_host";
const CONFIG_CTL_PORT: &str = "ctl_port";
const CONFIG_CTL_JWT: &str = "ctl_jwt";
const CONFIG_CTL_SEED: &str = "ctl_seed";
const CONFIG_CTL_CREDSFILE: &str = "ctl_credsfile";
const CONFIG_CTL_TLS_CA_FILE: &str = "ctl_tls_ca_file";
const CONFIG_CTL_TLS_FIRST: &str = "ctl_tls_first";
const CONFIG_JS_DOMAIN: &str = "js_domain";

fn default_lattice() -> String {
    DEFAULT_LATTICE.to_string()
}

fn default_ctl_host() -> String {
    DEFAULT_CTL_HOST.to_string()
}

fn default_ctl_port() -> u16 {
    DEFAULT_CTL_PORT
}

/// Configuration when subscribing a component with the
/// WADM provider as a source along a link.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub(crate) struct ClientConfig {
    /// The lattice this subscription is on.
    #[serde(default = "default_lattice")]
    pub lattice: String,
    /// Application name to subscribe to updates for.
    /// Cannot be empty if this is a subscription config.
    #[serde(default)]
    pub app_name: Option<String>,
    /// Control host for connection
    #[serde(default = "default_ctl_host")]
    pub ctl_host: String,
    /// Control port for connection
    #[serde(default = "default_ctl_port")]
    pub ctl_port: u16,
    /// JWT file for authentication
    #[serde(default)]
    pub ctl_jwt: Option<String>,
    /// Seed file/literal for authentication
    #[serde(default)]
    pub ctl_seed: Option<String>,
    /// Credentials file combining seed and JWT
    #[serde(default)]
    pub ctl_credsfile: Option<String>,
    /// TLS CA certificate file
    #[serde(default)]
    pub ctl_tls_ca_file: Option<String>,
    /// Perform TLS handshake first
    #[serde(default)]
    pub ctl_tls_first: bool,
    /// JetStream domain
    #[serde(default)]
    pub js_domain: Option<String>,
}

impl Default for ClientConfig {
    fn default() -> Self {
        ClientConfig {
            lattice: default_lattice(),
            app_name: None,
            ctl_host: String::new(),
            ctl_port: 0,
            ctl_jwt: None,
            ctl_seed: None,
            ctl_credsfile: None,
            ctl_tls_ca_file: None,
            ctl_tls_first: false,
            js_domain: None,
        }
    }
}

impl TryFrom<HashMap<String, String>> for ClientConfig {
    type Error = anyhow::Error;

    fn try_from(values: HashMap<String, String>) -> Result<Self> {
        let mut config = ClientConfig::default();

        if let Some(ctl_host) = values.get(CONFIG_CTL_HOST) {
            config.ctl_host = ctl_host.clone();
        }
        if let Some(ctl_port) = values.get(CONFIG_CTL_PORT) {
            config.ctl_port = ctl_port.parse().map_err(|_| anyhow!("Invalid ctl_port"))?;
        }
        if let Some(ctl_jwt) = values.get(CONFIG_CTL_JWT) {
            config.ctl_jwt = Some(ctl_jwt.clone());
        }
        if let Some(ctl_seed) = values.get(CONFIG_CTL_SEED) {
            config.ctl_seed = Some(ctl_seed.clone());
        }
        if let Some(ctl_credsfile) = values.get(CONFIG_CTL_CREDSFILE) {
            config.ctl_credsfile = Some(ctl_credsfile.clone());
        }
        if let Some(ctl_tls_ca_file) = values.get(CONFIG_CTL_TLS_CA_FILE) {
            config.ctl_tls_ca_file = Some(ctl_tls_ca_file.clone());
        }
        if let Some(ctl_tls_first) = values.get(CONFIG_CTL_TLS_FIRST) {
            config.ctl_tls_first = ctl_tls_first
                .parse()
                .map_err(|_| anyhow!("Invalid ctl_tls_first value"))?;
        }
        if let Some(js_domain) = values.get(CONFIG_JS_DOMAIN) {
            config.js_domain = Some(js_domain.clone());
        }
        if let Some(lattice) = values.get(CONFIG_LATTICE) {
            config.lattice = lattice.clone();
        }
        if let Some(app_name) = values.get(CONFIG_APP_NAME) {
            config.app_name = Some(app_name.clone());
        }

        Ok(config)
    }
}

impl ClientConfig {
    pub fn merge(&self, extra: &ClientConfig) -> ClientConfig {
        let mut out = self.clone();

        if !extra.ctl_host.is_empty() {
            out.ctl_host = extra.ctl_host.clone();
        }
        if extra.ctl_port != 0 {
            out.ctl_port = extra.ctl_port;
        }
        if extra.ctl_jwt.is_some() {
            out.ctl_jwt = extra.ctl_jwt.clone();
        }
        if extra.ctl_seed.is_some() {
            out.ctl_seed = extra.ctl_seed.clone();
        }
        if extra.ctl_credsfile.is_some() {
            out.ctl_credsfile = extra.ctl_credsfile.clone();
        }
        if extra.ctl_tls_ca_file.is_some() {
            out.ctl_tls_ca_file = extra.ctl_tls_ca_file.clone();
        }
        if extra.ctl_tls_first {
            out.ctl_tls_first = extra.ctl_tls_first;
        }
        if extra.js_domain.is_some() {
            out.js_domain = extra.js_domain.clone();
        }
        if !extra.lattice.is_empty() {
            out.lattice = extra.lattice.clone();
        }
        if extra.app_name.is_some() {
            out.app_name = extra.app_name.clone();
        }

        out
    }
}

pub(crate) fn extract_wadm_config(
    link_config: &LinkConfig,
    is_subscription: bool,
) -> Option<ClientConfig> {
    let LinkConfig {
        config, secrets, ..
    } = link_config;
    let mut client_config = ClientConfig::default();

    // For subscriptions we need app_name
    if is_subscription {
        let app_name = config.get(CONFIG_APP_NAME);
        if app_name.is_none() {
            warn!("Subscription config missing required app_name field");
            return None;
        }
        client_config.app_name = app_name.cloned();
    }

    if let Some(host) = config.get(CONFIG_CTL_HOST) {
        client_config.ctl_host = host.clone();
    }
    if let Some(port) = config.get(CONFIG_CTL_PORT) {
        if let Ok(port_num) = port.parse() {
            client_config.ctl_port = port_num;
        }
    }

    if let Some(jwt_val) = config.get(CONFIG_CTL_JWT) {
        client_config.ctl_jwt = Some(jwt_val.clone());
    }

    // Handle seed (prefer secrets)
    if let Some(seed_secret) = secrets
        .get(CONFIG_CTL_SEED)
        .and_then(SecretValue::as_string)
    {
        client_config.ctl_seed = Some(seed_secret.to_string());
    } else if let Some(seed_val) = config.get(CONFIG_CTL_SEED) {
        warn!("Seed found in config instead of secrets - consider moving to secrets");
        client_config.ctl_seed = Some(seed_val.clone());
    }

    if let Some(lattice) = config.get(CONFIG_LATTICE) {
        client_config.lattice = lattice.clone();
    }
    if let Some(credsfile) = config.get(CONFIG_CTL_CREDSFILE) {
        client_config.ctl_credsfile = Some(credsfile.clone());
    }
    if let Some(tls_ca_file) = config.get(CONFIG_CTL_TLS_CA_FILE) {
        client_config.ctl_tls_ca_file = Some(tls_ca_file.clone());
    }
    if let Some(tls_first) = config.get(CONFIG_CTL_TLS_FIRST) {
        client_config.ctl_tls_first = matches!(tls_first.to_lowercase().as_str(), "true" | "yes");
    }
    if let Some(js_domain) = config.get(CONFIG_JS_DOMAIN) {
        client_config.js_domain = Some(js_domain.clone());
    }

    Some(client_config)
}