diff --git a/.github/workflows/build-image.yaml b/.github/workflows/build-image.yaml index 8013e2a5..3871a69e 100644 --- a/.github/workflows/build-image.yaml +++ b/.github/workflows/build-image.yaml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Check out code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Add latest tag if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }} id: add-latest-tag @@ -30,6 +30,8 @@ jobs: with: image: wasm-shim tags: ${{ env.IMG_TAGS }} + build-args: | + GITHUB_SHA=${{ github.sha }} dockerfiles: | ./Dockerfile - name: Push Image diff --git a/Cargo.lock b/Cargo.lock index a5ab73cb..43aa8154 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,6 +164,26 @@ dependencies = [ "vec_map", ] +[[package]] +name = "const_format" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "cpp_demangle" version = "0.3.5" @@ -1670,8 +1690,9 @@ dependencies = [ [[package]] name = "wasm-shim" -version = "0.1.0" +version = "0.5.0-dev" dependencies = [ + "const_format", "log", "prost", "prost-types", diff --git a/Cargo.toml b/Cargo.toml index a06794c1..0419d62b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,16 @@ [package] name = "wasm-shim" -version = "0.1.0" +version = "0.5.0-dev" edition = "2021" -authors = ["Rahul Anand "] -description = "shim connecting envoy and authorino/limitador" +authors = ["Alex Snaps ", "Eguzki Astiz Lezaun ", "Rahul Anand "] +description = "Wasm module connecting envoy and authorino/limitador" license = "Apache-2.0" +keywords = ["rate-limiting", "rate", "limiter", "envoy", "rls"] +categories = ["web-programming"] +homepage = "https://kuadrant.io" +repository = "https://github.com/Kuadrant/wasm-shim" +documentation = "https://kuadrant.io" +readme = "README.md" [lib] crate-type = ["cdylib"] @@ -24,6 +30,7 @@ protobuf = { version = "2.27", features = ["with-serde"] } thiserror = "1.0" regex = "1" radix_trie = "0.2.1" +const_format = "0.2.31" [dev-dependencies] proxy-wasm-test-framework = { git = "https://github.com/Kuadrant/wasm-test-framework.git", branch = "kuadrant" } diff --git a/Dockerfile b/Dockerfile index 16efc078..0f385409 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,9 @@ FROM alpine:3.16 as wasm-shim-build +ARG GITHUB_SHA +ENV GITHUB_SHA=${GITHUB_SHA:-unknown} + ARG RUSTC_VERSION=1.69.0 RUN apk update \ && apk upgrade \ diff --git a/build.rs b/build.rs index bd0a966b..6be8f5c1 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,57 @@ use std::error::Error; +use std::process::Command; fn main() -> Result<(), Box> { + set_git_hash("WASM_SHIM_GIT_HASH"); + set_profile("WASM_SHIM_PROFILE"); + set_features("WASM_SHIM_FEATURES"); generate_protobuf() } +fn set_profile(env: &str) { + if let Ok(profile) = std::env::var("PROFILE") { + println!("cargo:rustc-env={env}={profile}"); + } +} + +fn set_features(env: &str) { + let mut features = vec![]; + if cfg!(feature = "with-serde") { + features.push("+with-serde"); + } + println!("cargo:rustc-env={env}={features:?}"); +} + +fn set_git_hash(env: &str) { + let git_sha = Command::new("/usr/bin/git") + .args(["rev-parse", "HEAD"]) + .output() + .ok() + .filter(|output| output.status.success()) + .and_then(|x| String::from_utf8(x.stdout).ok()) + .map(|sha| sha[..8].to_owned()); + + if let Some(sha) = git_sha { + let dirty = Command::new("/usr/bin/git") + .args(["diff", "--stat"]) + .output() + .ok() + .filter(|output| output.status.success()) + .map(|output| !matches!(output.stdout.len(), 0)); + + match dirty { + Some(true) => println!("cargo:rustc-env={env}={sha}-dirty"), + Some(false) => println!("cargo:rustc-env={env}={sha}"), + _ => unreachable!("How can we have a git hash, yet not know if the tree is dirty?"), + } + } else { + let fallback = option_env!("GITHUB_SHA") + .map(|sha| if sha.len() > 8 { &sha[..8] } else { sha }) + .unwrap_or("NO_SHA"); + println!("cargo:rustc-env={env}={fallback}"); + } +} + fn generate_protobuf() -> Result<(), Box> { let custom = protoc_rust::Customize { serde_derive: Some(true), diff --git a/src/filter/root_context.rs b/src/filter/root_context.rs index 1302d15f..ac43c8b1 100644 --- a/src/filter/root_context.rs +++ b/src/filter/root_context.rs @@ -1,10 +1,17 @@ use crate::configuration::{FilterConfig, PluginConfiguration}; use crate::filter::http_context::Filter; +use const_format::formatcp; use log::{info, warn}; use proxy_wasm::traits::{Context, HttpContext, RootContext}; use proxy_wasm::types::ContextType; use std::rc::Rc; +const WASM_SHIM_VERSION: &str = env!("CARGO_PKG_VERSION"); +const WASM_SHIM_PROFILE: &str = env!("WASM_SHIM_PROFILE"); +const WASM_SHIM_FEATURES: &str = env!("WASM_SHIM_FEATURES"); +const WASM_SHIM_GIT_HASH: &str = env!("WASM_SHIM_GIT_HASH"); +const WASM_SHIM_HEADER: &str = "Kuadrant wasm module"; + pub struct FilterRoot { pub context_id: u32, pub config: Rc, @@ -12,7 +19,18 @@ pub struct FilterRoot { impl RootContext for FilterRoot { fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool { - info!("root-context #{}: VM started", self.context_id); + let full_version: &'static str = formatcp!( + "v{} ({}) {} {}", + WASM_SHIM_VERSION, + WASM_SHIM_GIT_HASH, + WASM_SHIM_FEATURES, + WASM_SHIM_PROFILE, + ); + + info!( + "{} {} root-context #{}: VM started", + WASM_SHIM_HEADER, full_version, self.context_id + ); true }