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 parameters(&self) -> &'static [Parameter] {
21 &[Parameter {
22 keyword: "key",
23 kind: kind::BYTES,
24 required: true,
25 }]
26 }
27
28 fn examples(&self) -> &'static [Example] {
29 &[example!(
30 title: "Get the datadog api key",
31 source: r#"get_secret("datadog_api_key")"#,
32 result: Ok("secret value"),
33 )]
34 }
35
36 fn compile(
37 &self,
38 _state: &TypeState,
39 _ctx: &mut FunctionCompileContext,
40 arguments: ArgumentList,
41 ) -> Compiled {
42 let key = arguments.required("key");
43 Ok(GetSecretFn { key }.as_expr())
44 }
45}
46
47#[derive(Debug, Clone)]
48struct GetSecretFn {
49 key: Box<dyn Expression>,
50}
51
52impl FunctionExpression for GetSecretFn {
53 fn resolve(&self, ctx: &mut Context) -> Resolved {
54 let key = self.key.resolve(ctx)?;
55 get_secret(ctx, key)
56 }
57
58 fn type_def(&self, _: &TypeState) -> TypeDef {
59 TypeDef::bytes().add_null().infallible()
60 }
61}