vector_vrl_functions/
set_secret.rs

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