wasmcloud_provider_keyvalue_vault/
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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
pub(crate) mod config;

use core::str;
use core::time::Duration;

use std::collections::{hash_map, HashMap};
use std::string::ToString;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context as _};
use base64::Engine as _;
use bytes::Bytes;
use tokio::sync::{Mutex, RwLock};
use tokio::task::JoinHandle;
use tracing::{debug, error, info, instrument, warn};
use vaultrs::client::{Client as _, VaultClient, VaultClientSettings};
use wasmcloud_provider_sdk::{
    get_connection, load_host_data, propagate_trace_for_ctx, run_provider, Context, LinkConfig,
    LinkDeleteInfo, Provider,
};
use wasmcloud_provider_sdk::{initialize_observability, serve_provider_exports};

use crate::config::Config;

mod bindings {
    wit_bindgen_wrpc::generate!({
        with: {
            "wrpc:keyvalue/store@0.2.0-draft": generate,
        }
    });
}
use bindings::exports::wrpc::keyvalue;

type Result<T, E = keyvalue::store::Error> = core::result::Result<T, E>;

/// Vault HTTP api version. As of Vault 1.9.x (Feb 2022), all http api calls use version 1
const API_VERSION: u8 = 1;

/// Default TTL for tokens used by this provider. Defaults to 72 hours.
pub const TOKEN_INCREMENT_TTL: &str = "72h";
pub const TOKEN_REFRESH_INTERVAL: Duration = Duration::from_secs(60 * 60 * 12); // 12 hours

pub async fn run() -> anyhow::Result<()> {
    KvVaultProvider::run().await
}

/// Vault client connection information.
#[derive(Clone)]
pub struct Client {
    inner: Arc<vaultrs::client::VaultClient>,
    namespace: String,
    token_increment_ttl: String,
    token_refresh_interval: Duration,
    renew_task: Arc<Mutex<Option<JoinHandle<()>>>>,
}

impl Client {
    /// Creates a new Vault client. See [config](./config.rs) for explanation of parameters.
    ///
    /// Note that this constructor does not attempt to connect to the vault server,
    /// so the vault server does not need to be running at the time a `LinkDefinition` to this provider is created.
    pub fn new(config: Config) -> Result<Self, vaultrs::error::ClientError> {
        let client = VaultClient::new(VaultClientSettings {
            token: config.token,
            address: config.addr,
            ca_certs: config.certs,
            verify: false,
            version: API_VERSION,
            wrapping: false,
            timeout: None,
            namespace: None,
            identity: None,
        })?;
        Ok(Self {
            inner: Arc::new(client),
            namespace: config.mount,
            token_increment_ttl: config
                .token_increment_ttl
                .unwrap_or(TOKEN_INCREMENT_TTL.into()),
            token_refresh_interval: config
                .token_refresh_interval
                .unwrap_or(TOKEN_REFRESH_INTERVAL),
            renew_task: Arc::default(),
        })
    }

    /// Reads value of secret using namespace and key path
    pub async fn read_secret(&self, path: &str) -> Result<Option<HashMap<String, String>>> {
        match vaultrs::kv2::read(self.inner.as_ref(), &self.namespace, path).await {
            Err(vaultrs::error::ClientError::APIError {
                code: 404,
                errors: _,
            }) => Ok(None),
            Err(err) => {
                error!(error = %err, "failed to read secret");
                Err(keyvalue::store::Error::Other(format!(
                    "{:#}",
                    anyhow!(err).context("failed to read secret")
                )))
            }
            Ok(val) => Ok(val),
        }
    }

    /// Writes value of secret using namespace and key path
    pub async fn write_secret(&self, path: &str, data: &HashMap<String, String>) -> Result<()> {
        let md = vaultrs::kv2::set(self.inner.as_ref(), &self.namespace, path, data)
            .await
            .map_err(|err| {
                error!(error = %err, "failed to write secret");
                keyvalue::store::Error::Other(format!(
                    "{:#}",
                    anyhow!(err).context("failed to write secret")
                ))
            })?;
        debug!(?md, "set returned metadata");
        Ok(())
    }

    /// Sets up a background task to renew the token at the configured interval. This function
    /// attempts to lock the `renew_task` mutex and will deadlock if called without first ensuring
    /// the lock is available.
    pub async fn set_renewal(&self) {
        let mut renew_task = self.renew_task.lock().await;
        if let Some(handle) = renew_task.take() {
            handle.abort();
        }
        let client = self.inner.clone();
        let interval = self.token_refresh_interval;
        let ttl = self.token_increment_ttl.clone();

        *renew_task = Some(tokio::spawn(async move {
            let mut next_interval = tokio::time::interval(interval);
            loop {
                next_interval.tick().await;
                // NOTE(brooksmtownsend): Errors are appropriately logged in the function
                let _ = renew_self(&client, ttl.as_str()).await;
            }
        }));
    }
}

impl Drop for Client {
    fn drop(&mut self) {
        // NOTE(brooksmtownsend): We're trying to lock here so we don't deadlock on dropping.
        if let Ok(mut renew_task) = self.renew_task.try_lock() {
            if let Some(handle) = renew_task.take() {
                handle.abort();
            }
        }
    }
}

/// Helper function to renew a client's token, incrementing the validity by `increment`
async fn renew_self(
    client: &VaultClient,
    increment: &str,
) -> Result<(), vaultrs::error::ClientError> {
    debug!("renewing token");
    client.renew(Some(increment)).await.map_err(|e| {
        error!("error renewing self token: {}", e);
        e
    })?;

    let info = client.lookup().await.map_err(|e| {
        error!("error looking up self token: {}", e);
        e
    })?;

    let expire_time = info.expire_time.unwrap_or_else(|| "None".to_string());
    info!(%expire_time, accessor = %info.accessor, "renewed token");
    Ok(())
}

/// Redis KV provider implementation which utilizes [Hashicorp Vault](https://developer.hashicorp.com/vault/docs)
#[derive(Default, Clone)]
pub struct KvVaultProvider {
    // store vault connection per component
    components: Arc<RwLock<HashMap<String, Arc<Client>>>>,
}

impl KvVaultProvider {
    pub fn name() -> &'static str {
        "keyvalue-vault-provider"
    }

    pub async fn run() -> anyhow::Result<()> {
        let host_data = load_host_data().context("failed to load host data")?;
        let flamegraph_path = host_data
            .config
            .get("FLAMEGRAPH_PATH")
            .map(String::from)
            .or_else(|| std::env::var("PROVIDER_KEYVALUE_VAULT_FLAMEGRAPH_PATH").ok());
        initialize_observability!(Self::name(), flamegraph_path);
        let provider = Self::default();
        let shutdown = run_provider(provider.clone(), KvVaultProvider::name())
            .await
            .context("failed to run provider")?;
        let connection = get_connection();
        let wrpc = connection
            .get_wrpc_client(connection.provider_key())
            .await?;
        serve_provider_exports(&wrpc, provider, shutdown, bindings::serve)
            .await
            .context("failed to serve provider exports")
    }

    /// Retrieve a client for a given context (determined by `source_id`)
    async fn get_client(&self, ctx: Option<Context>) -> Result<Arc<Client>> {
        let ctx = ctx.ok_or_else(|| {
            warn!("invocation context missing");
            keyvalue::store::Error::Other("invocation context missing".into())
        })?;
        let source_id = ctx.component.as_ref().ok_or_else(|| {
            warn!("source ID missing");
            keyvalue::store::Error::Other("source ID missing".into())
        })?;
        let links = self.components.read().await;
        links.get(source_id).cloned().ok_or_else(|| {
            warn!(source_id, "source ID not linked");
            keyvalue::store::Error::Other("source ID not linked".into())
        })
    }

    /// Gets a value for a specified key. Deserialize the value as json
    /// If it's any other map, the entire map is returned as a serialized json string
    /// If the stored value is a plain string, returns the plain value
    /// All other values are returned as serialized json
    #[instrument(level = "debug", skip(ctx, self))]
    async fn get(&self, ctx: Option<Context>, path: String, key: String) -> Result<Option<Bytes>> {
        propagate_trace_for_ctx!(ctx);
        let client = self.get_client(ctx).await?;
        if let Some(mut secret) = client.read_secret(&path).await? {
            match secret.remove(&key) {
                Some(value) => {
                    let value = base64::engine::general_purpose::STANDARD_NO_PAD
                        .decode(value)
                        .map_err(|err| {
                            error!(?err, "failed to decode secret value");
                            keyvalue::store::Error::Other(format!(
                                "{:#}",
                                anyhow!(err).context("failed to decode secret value")
                            ))
                        })?;
                    Ok(Some(value.into()))
                }
                None => Ok(None),
            }
        } else {
            Ok(None)
        }
    }

    /// Returns true if the store contains the key
    #[instrument(level = "debug", skip(ctx, self))]
    async fn contains(&self, ctx: Option<Context>, path: String, key: String) -> Result<bool> {
        propagate_trace_for_ctx!(ctx);
        let client = self.get_client(ctx).await?;
        let secret = client.read_secret(&path).await?;
        Ok(secret.is_some_and(|secret| secret.contains_key(&key)))
    }

    /// Deletes a key from a secret
    #[instrument(level = "debug", skip(ctx, self))]
    async fn del(&self, ctx: Option<Context>, path: String, key: String) -> Result<()> {
        propagate_trace_for_ctx!(ctx);
        let client = self.get_client(ctx).await?;
        let secret = client.read_secret(&path).await?;
        let secret = if let Some(mut secret) = secret {
            if secret.remove(&key).is_none() {
                debug!("key does not exist in the secret");
                return Ok(());
            }
            secret
        } else {
            debug!("secret not found");
            return Ok(());
        };
        client.write_secret(&path, &secret).await
    }

    /// Sets the value of a key.
    #[instrument(level = "debug", skip(ctx, self))]
    async fn set(
        &self,
        ctx: Option<Context>,
        path: String,
        key: String,
        value: Bytes,
    ) -> Result<()> {
        propagate_trace_for_ctx!(ctx);
        let client = self.get_client(ctx).await?;
        let value = base64::engine::general_purpose::STANDARD_NO_PAD.encode(value);
        let secret = client.read_secret(&path).await?;
        let secret = if let Some(mut secret) = secret {
            match secret.entry(key) {
                hash_map::Entry::Vacant(e) => {
                    e.insert(value);
                }
                hash_map::Entry::Occupied(mut e) => {
                    if *e.get() == value {
                        return Ok(());
                    }
                    e.insert(value);
                }
            }
            secret
        } else {
            HashMap::from([(key, value)])
        };
        client.write_secret(&path, &secret).await
    }

    #[instrument(level = "debug", skip(ctx, self))]
    async fn list_keys(
        &self,
        ctx: Option<Context>,
        path: String,
        skip: u64,
    ) -> Result<keyvalue::store::KeyResponse> {
        propagate_trace_for_ctx!(ctx);
        let client = self.get_client(ctx).await?;
        let secret = client.read_secret(&path).await?;
        Ok(keyvalue::store::KeyResponse {
            cursor: None,
            keys: secret
                .map(|secret| {
                    secret
                        .into_keys()
                        .skip(skip.try_into().unwrap_or(usize::MAX))
                        .collect()
                })
                .unwrap_or_default(),
        })
    }
}

impl keyvalue::store::Handler<Option<Context>> for KvVaultProvider {
    #[instrument(level = "debug", skip(self))]
    async fn delete(
        &self,
        context: Option<Context>,
        bucket: String,
        key: String,
    ) -> anyhow::Result<Result<()>> {
        propagate_trace_for_ctx!(context);
        Ok(self.del(context, bucket, key).await)
    }

    #[instrument(level = "debug", skip(self))]
    async fn exists(
        &self,
        context: Option<Context>,
        bucket: String,
        key: String,
    ) -> anyhow::Result<Result<bool>> {
        propagate_trace_for_ctx!(context);
        Ok(self.contains(context, bucket, key).await)
    }

    #[instrument(level = "debug", skip(self))]
    async fn get(
        &self,
        context: Option<Context>,
        bucket: String,
        key: String,
    ) -> anyhow::Result<Result<Option<Bytes>>> {
        propagate_trace_for_ctx!(context);
        Ok(self.get(context, bucket, key).await)
    }

    #[instrument(level = "debug", skip(self))]
    async fn set(
        &self,
        context: Option<Context>,
        bucket: String,
        key: String,
        value: Bytes,
    ) -> anyhow::Result<Result<()>> {
        propagate_trace_for_ctx!(context);
        Ok(self.set(context, bucket, key, value).await)
    }

    #[instrument(level = "debug", skip(self))]
    async fn list_keys(
        &self,
        context: Option<Context>,
        bucket: String,
        cursor: Option<u64>,
    ) -> anyhow::Result<Result<keyvalue::store::KeyResponse>> {
        propagate_trace_for_ctx!(context);
        Ok(self
            .list_keys(context, bucket, cursor.unwrap_or_default())
            .await)
    }
}

/// Handle provider control commands, the minimum required of any provider on
/// a wasmcloud lattice
impl Provider for KvVaultProvider {
    /// Provider should perform any operations needed for a new link,
    /// including setting up per-component resources, and checking authorization.
    /// If the link is allowed, return true, otherwise return false to deny the link.
    #[instrument(level = "debug", skip_all, fields(source_id))]
    async fn receive_link_config_as_target(
        &self,
        link_config: LinkConfig<'_>,
    ) -> anyhow::Result<()> {
        let LinkConfig {
            source_id,
            link_name,
            ..
        } = link_config;
        debug!(
           %source_id,
           %link_name,
            "adding link for component",
        );

        let config = match Config::from_link_config(&link_config) {
            Ok(config) => config,
            Err(e) => {
                error!(
                    %source_id,
                    %link_name,
                    "failed to parse config: {e}",
                );
                bail!(anyhow!(e).context("failed to parse config"))
            }
        };

        let client = match Client::new(config.clone()) {
            Ok(client) => client,
            Err(e) => {
                error!(
                    %source_id,
                    %link_name,
                    "failed to create new client config: {e}",
                );
                return Err(anyhow!(e).context("failed to create new client config"));
            }
        };
        client.set_renewal().await;

        let mut update_map = self.components.write().await;
        update_map.insert(source_id.to_string(), Arc::new(client));

        Ok(())
    }

    /// Handle notification that a link is dropped - close the connection
    #[instrument(level = "info", skip_all, fields(source_id = info.get_source_id()))]
    async fn delete_link_as_target(&self, info: impl LinkDeleteInfo) -> anyhow::Result<()> {
        let component_id = info.get_source_id();
        let mut aw = self.components.write().await;
        if let Some(client) = aw.remove(component_id) {
            debug!(component_id, "deleting link for component");
            drop(client);
        }
        Ok(())
    }

    /// Handle shutdown request by closing all connections
    async fn shutdown(&self) -> anyhow::Result<()> {
        let mut aw = self.components.write().await;
        // Empty the component link data and stop all servers
        for (_, client) in aw.drain() {
            drop(client);
        }
        Ok(())
    }
}