vector_vrl_functions/
get_secret.rs1use vector_vrl_category::Category;
2use vrl::prelude::*;
3
4fn get_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 let value = match ctx.target().get_secret(key_str.as_ref()) {
7 Some(secret) => secret.into(),
8 None => Value::Null,
9 };
10 Ok(value)
11}
12
13#[derive(Clone, Copy, Debug)]
14pub struct GetSecret;
15
16impl Function for GetSecret {
17 fn identifier(&self) -> &'static str {
18 "get_secret"
19 }
20
21 fn usage(&self) -> &'static str {
22 "Returns the value of the given secret from an event."
23 }
24
25 fn category(&self) -> &'static str {
26 Category::Event.as_ref()
27 }
28
29 fn return_kind(&self) -> u16 {
30 kind::BYTES | kind::NULL
31 }
32
33 fn parameters(&self) -> &'static [Parameter] {
34 const PARAMETERS: &[Parameter] = &[Parameter::required(
35 "key",
36 kind::BYTES,
37 "The name of the secret.",
38 )];
39 PARAMETERS
40 }
41
42 fn examples(&self) -> &'static [Example] {
43 &[
44 example! {
45 title: "Get the Datadog API key from the event metadata",
46 source: r#"get_secret("datadog_api_key")"#,
47 result: Ok("secret value"),
48 },
49 example! {
50 title: "Get a non existent secret",
51 source: r#"get_secret("i_dont_exist")"#,
52 result: Ok("null"),
53 },
54 ]
55 }
56
57 fn compile(
58 &self,
59 _state: &TypeState,
60 _ctx: &mut FunctionCompileContext,
61 arguments: ArgumentList,
62 ) -> Compiled {
63 let key = arguments.required("key");
64 Ok(GetSecretFn { key }.as_expr())
65 }
66}
67
68#[derive(Debug, Clone)]
69struct GetSecretFn {
70 key: Box<dyn Expression>,
71}
72
73impl FunctionExpression for GetSecretFn {
74 fn resolve(&self, ctx: &mut Context) -> Resolved {
75 let key = self.key.resolve(ctx)?;
76 get_secret(ctx, key)
77 }
78
79 fn type_def(&self, _: &TypeState) -> TypeDef {
80 TypeDef::bytes().add_null().infallible()
81 }
82}