Skip to main content

Read secret environment variables

Read values from FastEdge-defined secret environment variable in CDB Edge Compute programs.

Compute

Use this solution in your CDB Edge Compute service:

use fastedge::proxywasm::secret;

use proxy_wasm::traits::*;
use proxy_wasm::types::*;


proxy_wasm::main! {{
    proxy_wasm::set_log_level(LogLevel::Trace);
    proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpHeadersRoot) });
}}

struct HttpHeadersRoot;

impl Context for HttpHeadersRoot {}

impl RootContext for HttpHeadersRoot {
    fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
        Some(Box::new(HttpHeaders {}))
    }

    fn get_type(&self) -> Option<ContextType> {
        Some(ContextType::HttpContext)
    }
}

struct HttpHeaders {}

impl Context for HttpHeaders {}

const INTERNAL_SERVER_ERROR: u32 = 500;

impl HttpContext for HttpHeaders {
    fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
        let Ok(Some(_secret)) = secret::get("SECRET") else {
            self.send_http_response(INTERNAL_SERVER_ERROR, vec![], Some(b"App misconfigured"));
            println!("App misconfigured");
            return Action::Pause;
        };
        println!("Secret retrieve");
        Action::Continue
    }
}
[package]
name = "read-env-secret"
version = "1.0.0"
edition = "2021"
authors = ["John Doe <john.doe@orange.com>"]
description = "Proxy-Wasm example: Read an environment variable that is store in vault"


[lib]
crate-type = ["cdylib"]

[dependencies]
fastedge = { git ="https://github.com/G-Core/FastEdgeSDK.git", version = "0.1.10", features = ["json"]}
proxy-wasm = "0.2"