wasmcloud_provider_messaging_nats/
connection.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
use std::collections::HashMap;

use tracing::warn;
use wasmcloud_core::messaging::{
    ConnectionConfig, CONFIG_NATS_CLIENT_JWT, CONFIG_NATS_CLIENT_SEED,
};
use wasmcloud_provider_sdk::{core::secrets::SecretValue, LinkConfig};

/// Create a [`ConnectionConfig`] from a given [`LinkConfig`]
pub fn from_link_config(
    LinkConfig {
        secrets, config, ..
    }: &LinkConfig,
) -> anyhow::Result<ConnectionConfig> {
    let mut map = HashMap::clone(config);

    if let Some(jwt) = secrets
        .get(CONFIG_NATS_CLIENT_JWT)
        .and_then(SecretValue::as_string)
        .or_else(|| {
            warn!("secret value [{CONFIG_NATS_CLIENT_JWT}] was found not found in secrets. Prefer using secrets for sensitive values.");
            config.get(CONFIG_NATS_CLIENT_JWT).map(String::as_str)
        })
    {
        map.insert(CONFIG_NATS_CLIENT_JWT.into(), jwt.to_string());
    }

    if let Some(seed) = secrets
        .get(CONFIG_NATS_CLIENT_SEED)
        .and_then(SecretValue::as_string)
        .or_else(|| {
            warn!("secret value [{CONFIG_NATS_CLIENT_SEED}] was found not found in secrets. Prefer using secrets for sensitive values.");
            config.get(CONFIG_NATS_CLIENT_SEED).map(String::as_str)
        })
    {
        map.insert(CONFIG_NATS_CLIENT_SEED.into(), seed.to_string());
    }

    ConnectionConfig::from_map(&map)
}