vector_vrl_functions/
remove_secret.rs

1use vector_vrl_category::Category;
2use vrl::prelude::*;
3
4fn remove_secret(ctx: &mut Context, key: Value) -> std::result::Result<Value, ExpressionError> {
5    let key_str = key.as_str().expect("argument must be a string");
6    ctx.target_mut().remove_secret(key_str.as_ref());
7    Ok(Value::Null)
8}
9
10#[derive(Clone, Copy, Debug)]
11pub struct RemoveSecret;
12
13impl Function for RemoveSecret {
14    fn identifier(&self) -> &'static str {
15        "remove_secret"
16    }
17
18    fn usage(&self) -> &'static str {
19        "Removes a secret from an event."
20    }
21
22    fn category(&self) -> &'static str {
23        Category::Event.as_ref()
24    }
25
26    fn return_kind(&self) -> u16 {
27        kind::NULL
28    }
29
30    fn parameters(&self) -> &'static [Parameter] {
31        const PARAMETERS: &[Parameter] = &[Parameter::required(
32            "key",
33            kind::BYTES,
34            "The name of the secret to remove.",
35        )];
36        PARAMETERS
37    }
38
39    fn examples(&self) -> &'static [Example] {
40        &[example!(
41            title: "Remove the datadog api key",
42            source: r#"remove_secret("datadog_api_key")"#,
43            result: Ok("null"),
44        )]
45    }
46
47    fn compile(
48        &self,
49        _state: &TypeState,
50        _ctx: &mut FunctionCompileContext,
51        arguments: ArgumentList,
52    ) -> Compiled {
53        let key = arguments.required("key");
54        Ok(RemoveSecretFn { key }.as_expr())
55    }
56}
57
58#[derive(Debug, Clone)]
59struct RemoveSecretFn {
60    key: Box<dyn Expression>,
61}
62
63impl FunctionExpression for RemoveSecretFn {
64    fn resolve(&self, ctx: &mut Context) -> Resolved {
65        let key = self.key.resolve(ctx)?;
66        remove_secret(ctx, key)
67    }
68
69    fn type_def(&self, _: &TypeState) -> TypeDef {
70        TypeDef::null().infallible().impure()
71    }
72}