vector_vrl_functions/
get_secret.rs1use vrl::prelude::*;
2
3fn get_secret(ctx: &mut Context, key: Value) -> std::result::Result<Value, ExpressionError> {
4 let key_str = key.as_str().expect("argument must be a string");
5 let value = match ctx.target().get_secret(key_str.as_ref()) {
6 Some(secret) => secret.into(),
7 None => Value::Null,
8 };
9 Ok(value)
10}
11
12#[derive(Clone, Copy, Debug)]
13pub struct GetSecret;
14
15impl Function for GetSecret {
16 fn identifier(&self) -> &'static str {
17 "get_secret"
18 }
19
20 fn usage(&self) -> &'static str {
21 "Returns the value of the given secret from an event."
22 }
23
24 fn parameters(&self) -> &'static [Parameter] {
25 &[Parameter {
26 keyword: "key",
27 kind: kind::BYTES,
28 required: true,
29 }]
30 }
31
32 fn examples(&self) -> &'static [Example] {
33 &[example!(
34 title: "Get the datadog api key",
35 source: r#"get_secret("datadog_api_key")"#,
36 result: Ok("secret value"),
37 )]
38 }
39
40 fn compile(
41 &self,
42 _state: &TypeState,
43 _ctx: &mut FunctionCompileContext,
44 arguments: ArgumentList,
45 ) -> Compiled {
46 let key = arguments.required("key");
47 Ok(GetSecretFn { key }.as_expr())
48 }
49}
50
51#[derive(Debug, Clone)]
52struct GetSecretFn {
53 key: Box<dyn Expression>,
54}
55
56impl FunctionExpression for GetSecretFn {
57 fn resolve(&self, ctx: &mut Context) -> Resolved {
58 let key = self.key.resolve(ctx)?;
59 get_secret(ctx, key)
60 }
61
62 fn type_def(&self, _: &TypeState) -> TypeDef {
63 TypeDef::bytes().add_null().infallible()
64 }
65}