proxy-wasm-rust-sdk icon indicating copy to clipboard operation
proxy-wasm-rust-sdk copied to clipboard

Envrionment Variables always blank

Open austbot opened this issue 3 years ago • 6 comments

Are Env vars suported in this?

here is my envoy yaml

...
http_filters:
  - name: envoy.filters.http.wasm
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
      config:
        name: "proxy"
        root_id: "proxy"
        vm_config:
          runtime: "envoy.wasm.runtime.v8"
          environment_variables:
            key_values:
              PROXY_AUTH: "value"
          code:
....

Here is the Rust code

//
fn call(service: &'static str, proxy: &mut RpcProxy, body: Bytes) -> Result<u32, Status> {
    let path = env::var("PROXY_AUTH").unwrap_or("".to_string());
    proxy.dispatch_http_call(
        service,
        vec![
            (":method", "POST"),
            (":path", "/"),
            (":authority", service),
            ("content-type", "application/json"),
            ("content-length", &body.len().to_string()),
        ],
        Some(&*body),
        vec![],
        Duration::from_secs(300),
    )
}

the variable is always none.

I am using precompiled wasm , meaning i use wasm pack and throw it into the envoy container

austbot avatar Oct 26 '22 18:10 austbot

Are you building it against wasm32-wasi target?

PiotrSikora avatar Oct 26 '22 23:10 PiotrSikora

let me check im using wasm-pack , after a little research it seems that im not using that https://github.com/rustwasm/wasm-pack/issues/654

Wasm pack does not use this target so this makes sense that the wasi_get_environ binding is not present. ill try to followup with some docs explaining this on this repo

austbot avatar Oct 27 '22 15:10 austbot

Is there any reason for using wasm-pack in the first place? See the previous discussion: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/issues/149#issuecomment-1200824021.

PiotrSikora avatar Oct 27 '22 22:10 PiotrSikora

To get this working without building with wasi, I just use the wasi crate via wasi::environ_get See working example: https://github.com/leaksignal/leaksignal/blob/master/leaksignal/src/env.rs

Protryon avatar Feb 25 '23 22:02 Protryon

This works fine as far as I can tell. Using Envoy 1.26 the following works as expected:

  vm_config:
    runtime: "envoy.wasm.runtime.v8"
    environment_variables:
      key_values:
        MY_ENVIRONMENT_THING: "There once was a pizza, but then I ate it" 

And this bit of code:

        let my_string = if let Ok(val) = env::var("MY_ENVIRONMENT_THING") {
            val
        } else {
            warn!("MY_ENVIRONMENT_THING not set");
            std::string::String::new()
        };

You will either get your string as expected, or a blank string and a nice warning in your WASM log.

This is using:

  • The main macro: proxy_wasm::main!
  • proxy-wasm = "0.2.1"
  • cargo build --target wasm32-wasi as build method

When implementing in Istio using WasmPlugin resources, it might look like this:

            apiVersion: extensions.istio.io/v1alpha1
            kind: WasmPlugin
            metadata:
              name: myCoolFilter
              namespace: istio-ingress
            spec:
              selector:
                matchLabels:
                  app: ingressgateway
                  istio: ingressgateway
              url: https://some.place.where.you.store.assets.com/your-filter.wasm            
              imagePullPolicy: Always
              phase: UNSPECIFIED_PHASE
              vmConfig:
                env:
                - name: MY_ENVIRONMENT_THING
                  value: "There once was a pizza, but then I ate it" 

johnkeates avatar Mar 08 '23 00:03 johnkeates

You can also load Configuration in the plugin insteaf of the VM:

 http_filters:
  - name: envoy.filters.http.wasm
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
      config:
        name: "oidc-wasm-plugin"
        configuration:
          "@type": "type.googleapis.com/google.protobuf.StringValue"
          value: |
            value1: 1

Then in the RootContext, you can use self.get_plugin_configuration to load these values. In my case i am using yaml but JSON is also fine. You just have to deserialize it differently.

antonengelhardt avatar Jun 18 '23 12:06 antonengelhardt