Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Envrionment Variables always blank #170

Open
austbot opened this issue Oct 26, 2022 · 6 comments
Open

Envrionment Variables always blank #170

austbot opened this issue Oct 26, 2022 · 6 comments

Comments

@austbot
Copy link

austbot commented Oct 26, 2022

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

@PiotrSikora
Copy link
Contributor

Are you building it against wasm32-wasi target?

@austbot
Copy link
Author

austbot commented Oct 27, 2022

let me check im using wasm-pack , after a little research it seems that im not using that rustwasm/wasm-pack#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

@PiotrSikora
Copy link
Contributor

Is there any reason for using wasm-pack in the first place? See the previous discussion: #149 (comment).

@Protryon
Copy link
Contributor

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

@johnkeates
Copy link

johnkeates commented Mar 8, 2023

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" 

@antonengelhardt
Copy link

antonengelhardt commented Jun 18, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants