diff --git a/src/tools/genpolicy/.gitignore b/src/tools/genpolicy/.gitignore new file mode 100644 index 0000000000..4306af9266 --- /dev/null +++ b/src/tools/genpolicy/.gitignore @@ -0,0 +1 @@ +src/version.rs \ No newline at end of file diff --git a/src/tools/genpolicy/Cargo.toml b/src/tools/genpolicy/Cargo.toml index a8cf7f1276..a9f0162260 100644 --- a/src/tools/genpolicy/Cargo.toml +++ b/src/tools/genpolicy/Cargo.toml @@ -8,6 +8,7 @@ name = "genpolicy" version = "0.1.0" authors = ["The Kata Containers community "] edition = "2021" +license = "Apache-2.0" [dependencies] # Logging. diff --git a/src/tools/genpolicy/Makefile b/src/tools/genpolicy/Makefile index fe56fa0982..3143385700 100644 --- a/src/tools/genpolicy/Makefile +++ b/src/tools/genpolicy/Makefile @@ -1,4 +1,5 @@ # Copyright (c) 2020 Intel Corporation +# Portions Copyright (c) Microsoft Corporation. # # SPDX-License-Identifier: Apache-2.0 # @@ -9,10 +10,24 @@ ifeq ($(ARCH), ppc64le) override ARCH = powerpc64le endif +COMMIT_HASH := $(shell git rev-parse HEAD 2>/dev/null || true) +# appends '-dirty' to the commit hash if there are uncommitted changes +COMMIT_INFO := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),${COMMIT_HASH}-dirty,${COMMIT_HASH}) + +GENERATED_CODE = src/version.rs + +GENERATED_REPLACEMENTS= COMMIT_INFO +GENERATED_FILES := + +GENERATED_FILES += $(GENERATED_CODE) + +$(GENERATED_FILES): %: %.in + sed $(foreach r,$(GENERATED_REPLACEMENTS),-e 's|@$r@|$($r)|g') "$<" > "$@" + .DEFAULT_GOAL := default default: build -build: +build: $(GENERATED_FILES) @RUSTFLAGS="$(EXTRA_RUSTFLAGS) --deny warnings" cargo build --target $(TRIPLE) --$(BUILD_TYPE) static-checks-build: @@ -20,16 +35,17 @@ static-checks-build: clean: cargo clean + rm -f $(GENERATED_FILES) vendor: cargo vendor test: -install: +install: $(GENERATED_FILES) @RUSTFLAGS="$(EXTRA_RUSTFLAGS) --deny warnings" cargo install --locked --target $(TRIPLE) --path . -check: standard_rust_check +check: $(GENERATED_CODE) standard_rust_check .PHONY: \ build \ diff --git a/src/tools/genpolicy/src/main.rs b/src/tools/genpolicy/src/main.rs index 93c91d6020..29ca79ba84 100644 --- a/src/tools/genpolicy/src/main.rs +++ b/src/tools/genpolicy/src/main.rs @@ -27,6 +27,7 @@ mod settings; mod stateful_set; mod utils; mod verity; +mod version; mod volume; mod yaml; @@ -35,6 +36,16 @@ async fn main() { env_logger::init(); let config = utils::Config::new(); + if config.version { + println!( + "Kata Containers policy tool (Rust): id: {}, version: {}, commit: {}", + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION"), + version::COMMIT_INFO + ); + return; + } + debug!("Creating policy from yaml, settings, and rules.rego files..."); let mut policy = policy::AgentPolicy::from_files(&config).await.unwrap(); diff --git a/src/tools/genpolicy/src/utils.rs b/src/tools/genpolicy/src/utils.rs index b86963da34..f3a53b49dc 100644 --- a/src/tools/genpolicy/src/utils.rs +++ b/src/tools/genpolicy/src/utils.rs @@ -96,6 +96,8 @@ struct CommandLineOptions { require_equals = true )] layers_cache_file_path: Option, + #[clap(short, long, help = "Print version information and exit")] + version: bool, } /// Application configuration, derived from on command line parameters. @@ -115,6 +117,7 @@ pub struct Config { pub base64_out: bool, pub containerd_socket_path: Option, pub layers_cache_file_path: Option, + pub version: bool, } impl Config { @@ -153,6 +156,7 @@ impl Config { base64_out: args.base64_out, containerd_socket_path: args.containerd_socket_path, layers_cache_file_path, + version: args.version, } } } diff --git a/src/tools/genpolicy/src/version.rs.in b/src/tools/genpolicy/src/version.rs.in new file mode 100644 index 0000000000..bf6d9ae80e --- /dev/null +++ b/src/tools/genpolicy/src/version.rs.in @@ -0,0 +1,12 @@ +// Copyright (c) 2020 Intel Corporation +// Portions Copyright (c) Microsoft Corporation. +// +// SPDX-License-Identifier: Apache-2.0 +// + +// +// WARNING: This file is auto-generated - DO NOT EDIT! +// + +#![allow(dead_code)] +pub const COMMIT_INFO: &str = "@COMMIT_INFO@";