From 8f44776745e454233295a7325a9adbd318444954 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 16 Jul 2021 06:11:51 -0700 Subject: [PATCH] Linting with Clippy (#69) * Dockerfile: add test stage with cargo test entrypoint * Workflows: merge all in docker.yml - Run cargo test in docker.yml workflow - Manage PRs, releases, branches and latest altogether with variables * Workflows: update dockerhub description * Add clippy linting --- .github/workflows/build.yml | 28 ----- .github/workflows/buildx-branch.yml | 42 ------- .github/workflows/buildx-latest.yml | 39 ------- .github/workflows/buildx-release.yml | 39 ------- .github/workflows/docker.yml | 89 ++++++++++++++ .github/workflows/dockerhub-description.yml | 12 +- .github/workflows/rust.yml | 19 --- Cargo.lock | 123 ++++++++++++++++++++ Cargo.toml | 3 + Dockerfile | 18 ++- clippy.toml | 1 + src/main.rs | 2 +- src/wireguard.rs | 11 +- 13 files changed, 244 insertions(+), 182 deletions(-) delete mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/buildx-branch.yml delete mode 100644 .github/workflows/buildx-latest.yml delete mode 100644 .github/workflows/buildx-release.yml create mode 100644 .github/workflows/docker.yml delete mode 100644 .github/workflows/rust.yml create mode 100644 clippy.toml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index fdb0fed..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Docker build -on: - pull_request: - branches: [master] - paths-ignore: - - .github/workflows/buildx-branch.yml - - .github/workflows/buildx-latest.yml - - .github/workflows/buildx-release.yml - - .github/workflows/dockerhub-description.yml - - .github/workflows/rust.yml - - extra - - _config.yml - - .gitignore - - .rustfmt.toml - - .gitignore - - example.json - - LICENSE - - README.md -jobs: - build: - runs-on: ubuntu-latest - env: - DOCKER_BUILDKIT: "1" - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Build image - run: docker build . diff --git a/.github/workflows/buildx-branch.yml b/.github/workflows/buildx-branch.yml deleted file mode 100644 index 897f5ff..0000000 --- a/.github/workflows/buildx-branch.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: Buildx branch -on: - push: - branches: - - '*' - - '*/*' - - '!master' - paths-ignore: - - .github/workflows/build.yml - - .github/workflows/buildx-latest.yml - - .github/workflows/buildx-release.yml - - .github/workflows/dockerhub-description.yml - - .github/workflows/rust.yml - - extra - - _config.yml - - .gitignore - - .rustfmt.toml - - .gitignore - - example.json - - LICENSE - - README.md -jobs: - buildx: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Buildx setup - uses: crazy-max/ghaction-docker-buildx@v1 - - name: Dockerhub login - run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u mindflavor --password-stdin 2>&1 - - name: Run Buildx - run: | - docker buildx build \ - --progress plain \ - --platform=linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7 \ - --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - --build-arg COMMIT=`git rev-parse --short HEAD` \ - --build-arg VERSION=${GITHUB_REF##*/} \ - -t mindflavor/prometheus-wireguard-exporter:${GITHUB_REF##*/} \ - --push \ - . - - run: curl -X POST https://hooks.microbadger.com/images/mindflavor/prometheus-wireguard-exporter/TODO || exit 0 diff --git a/.github/workflows/buildx-latest.yml b/.github/workflows/buildx-latest.yml deleted file mode 100644 index 8610c3b..0000000 --- a/.github/workflows/buildx-latest.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Buildx latest -on: - release: - branches: [master] - paths-ignore: - - .github/workflows/build.yml - - .github/workflows/buildx-branch.yml - - .github/workflows/buildx-release.yml - - .github/workflows/dockerhub-description.yml - - .github/workflows/rust.yml - - extra - - _config.yml - - .gitignore - - .rustfmt.toml - - .gitignore - - example.json - - LICENSE - - README.md -jobs: - buildx: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Buildx setup - uses: crazy-max/ghaction-docker-buildx@v1 - - name: Dockerhub login - run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u mindflavor --password-stdin 2>&1 - - name: Run Buildx - run: | - docker buildx build \ - --progress plain \ - --platform=linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7 \ - --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - --build-arg COMMIT=`git rev-parse --short HEAD` \ - --build-arg VERSION=latest \ - -t mindflavor/prometheus-wireguard-exporter:latest \ - --push \ - . - - run: curl -X POST https://hooks.microbadger.com/images/mindflavor/prometheus-wireguard-exporter/TODO || exit 0 diff --git a/.github/workflows/buildx-release.yml b/.github/workflows/buildx-release.yml deleted file mode 100644 index ec64897..0000000 --- a/.github/workflows/buildx-release.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Buildx release -on: - release: - types: [published] - paths-ignore: - - .github/workflows/build.yml - - .github/workflows/buildx-branch.yml - - .github/workflows/buildx-latest.yml - - .github/workflows/dockerhub-description.yml - - .github/workflows/rust.yml - - extra - - _config.yml - - .gitignore - - .rustfmt.toml - - .gitignore - - example.json - - LICENSE - - README.md -jobs: - buildx: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Buildx setup - uses: crazy-max/ghaction-docker-buildx@v1 - - name: Dockerhub login - run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u mindflavor --password-stdin 2>&1 - - name: Run Buildx - run: | - docker buildx build \ - --progress plain \ - --platform=linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7 \ - --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - --build-arg COMMIT=`git rev-parse --short HEAD` \ - --build-arg VERSION=${GITHUB_REF##*/} \ - -t mindflavor/prometheus-wireguard-exporter:${GITHUB_REF##*/} \ - --push \ - . - - run: curl -X POST https://hooks.microbadger.com/images/mindflavor/prometheus-wireguard-exporter/TODO || exit 0 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..4a40ab5 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,89 @@ +name: CI +on: + push: + paths: + - .github/workflows/docker.yml + - src/** + - .dockerignore + - Cargo.lock + - Cargo.toml + - Dockerfile + pull_request: + paths: + - .github/workflows/docker.yml + - src/** + - .dockerignore + - Cargo.lock + - Cargo.toml + - Dockerfile + +jobs: + verify: + runs-on: ubuntu-latest + env: + DOCKER_BUILDKIT: "1" + steps: + - uses: actions/checkout@v2.3.4 + + - name: Lint + run: docker build --target lint . + + - name: Build test image + run: docker build --target test -t test-container . + + - name: Run tests in test container + run: | + docker run --rm test-container + + # We run this using the caching from the previous steps + - name: Build final image + run: docker build . + + publish: + needs: [verify] + if: github.event_name == 'push' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2.3.4 + + - uses: docker/setup-qemu-action@v1 + - uses: docker/setup-buildx-action@v1 + + - uses: docker/login-action@v1.9.0 + with: + username: mindflavor + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Set variables + id: vars + run: | + BRANCH=${GITHUB_REF#refs/heads/} + TAG=${GITHUB_REF#refs/tags/} + echo ::set-output name=commit::$(git rev-parse --short HEAD) + echo ::set-output name=build_date::$(date -u +%Y-%m-%dT%H:%M:%SZ) + if [ "$TAG" != "$GITHUB_REF" ]; then + echo ::set-output name=version::$TAG + echo ::set-output name=platforms::linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7 + elif [ "$BRANCH" = "master" ]; then + echo ::set-output name=version::latest + echo ::set-output name=platforms::linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7 + else + echo ::set-output name=version::$BRANCH + echo ::set-output name=platforms::linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7 + fi + + - name: Build and push final image + uses: docker/build-push-action@v2.4.0 + with: + platforms: ${{ steps.vars.outputs.platforms }} + build-args: | + BUILD_DATE=${{ steps.vars.outputs.build_date }} + COMMIT=${{ steps.vars.outputs.commit }} + VERSION=${{ steps.vars.outputs.version }} + tags: mindflavor/prometheus-wireguard-exporter:${{ steps.vars.outputs.version }} + push: true + + - if: github.event.ref == 'refs/heads/master' + name: Microbadger hook + run: curl -X POST https://hooks.microbadger.com/images/mindflavor/prometheus-wireguard-exporter/TODO + continue-on-error: true diff --git a/.github/workflows/dockerhub-description.yml b/.github/workflows/dockerhub-description.yml index c8c3fb2..d537bed 100644 --- a/.github/workflows/dockerhub-description.yml +++ b/.github/workflows/dockerhub-description.yml @@ -10,10 +10,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v2.3.4 - name: Docker Hub Description - uses: peter-evans/dockerhub-description@v2.1.0 - env: - DOCKERHUB_USERNAME: mindflavor - DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} - DOCKERHUB_REPOSITORY: mindflavor/prometheus-wireguard-exporter + uses: peter-evans/dockerhub-description@2.4.2 + with: + username: mindflavor + password: ${{ secrets.DOCKERHUB_PASSWORD }} + repository: mindflavor/prometheus-wireguard-exporter diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 6738b0b..0000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: Rust - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose diff --git a/Cargo.lock b/Cargo.lock index 4cec1a7..f537790 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "atty" version = "0.2.14" @@ -49,12 +61,29 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + [[package]] name = "bumpalo" version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.0.1" @@ -88,6 +117,21 @@ dependencies = [ "vec_map", ] +[[package]] +name = "clippy" +version = "0.0.302" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d911ee15579a3f50880d8c1d59ef6e79f9533127a3bd342462f5d584f5e8c294" +dependencies = [ + "term", +] + +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + [[package]] name = "core-foundation" version = "0.9.1" @@ -104,6 +148,16 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" +[[package]] +name = "crossbeam-utils" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" +dependencies = [ + "cfg-if", + "lazy_static", +] + [[package]] name = "ct-logs" version = "0.8.0" @@ -113,6 +167,17 @@ dependencies = [ "sct", ] +[[package]] +name = "dirs" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "env_logger" version = "0.7.1" @@ -196,6 +261,17 @@ dependencies = [ "slab", ] +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -512,6 +588,7 @@ name = "prometheus_wireguard_exporter" version = "3.5.1" dependencies = [ "clap", + "clippy", "env_logger 0.8.4", "http", "hyper", @@ -540,6 +617,23 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "redox_users" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + [[package]] name = "regex" version = "1.5.4" @@ -572,6 +666,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "rust-argon2" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + [[package]] name = "rustls" version = "0.19.1" @@ -713,6 +819,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "term" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" +dependencies = [ + "byteorder", + "dirs", + "winapi", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -853,6 +970,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasm-bindgen" version = "0.2.74" diff --git a/Cargo.toml b/Cargo.toml index c0fc622..5f18277 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,6 @@ http = "0.2" tokio = { version = "1.0", features = ["macros", "rt"] } prometheus_exporter_base = { version = "1.2", features = ["hyper_server"] } regex = "1.5.4" + +[dev-dependencies] +clippy = "*" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2d6d7f4..0c8e522 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ ARG BUILDPLATFORM=linux/amd64 ARG ALPINE_VERSION=3.12 ARG RUST_VERSION=1-slim-bullseye -FROM --platform=${BUILDPLATFORM} rust:${RUST_VERSION} AS build +FROM --platform=${BUILDPLATFORM} rust:${RUST_VERSION} AS base WORKDIR /usr/src/prometheus_wireguard_exporter # Setup @@ -90,8 +90,22 @@ RUN rm -r \ target/*-linux-*/release/prometheus_wireguard_exporter* \ src/main.rs -# Build static binary with musl built-in COPY . . + +FROM base AS lint +ENTRYPOINT \ + RUSTFLAGS="$(cat /tmp/rustflags)" \ + CC="$(cat /tmp/musl)-gcc" \ + cargo clippy --target "$(cat /tmp/rusttarget)" + +FROM base AS test +ENTRYPOINT \ + RUSTFLAGS="$(cat /tmp/rustflags)" \ + CC="$(cat /tmp/musl)-gcc" \ + cargo test --target "$(cat /tmp/rusttarget)" + +FROM base AS build +# Build static binary with musl built-in RUN RUSTFLAGS="$(cat /tmp/rustflags)" \ CC="$(cat /tmp/musl)-gcc" \ cargo build --target "$(cat /tmp/rusttarget)" --release && \ diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..5bf98d8 --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +# see https://rust-lang.github.io/rust-clippy/master/index.html diff --git a/src/main.rs b/src/main.rs index f77bc1b..e9b986e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -194,7 +194,7 @@ async fn main() -> Result<(), Box> { info!("using options: {:?}", options); let bind = matches.value_of("port").unwrap(); - let bind = u16::from_str_radix(&bind, 10).expect("port must be a valid number"); + let bind = (&bind).parse::().expect("port must be a valid number"); let ip = matches.value_of("addr").unwrap().parse::().unwrap(); let addr = (ip, bind).into(); diff --git a/src/wireguard.rs b/src/wireguard.rs index f5648b1..0df43a1 100644 --- a/src/wireguard.rs +++ b/src/wireguard.rs @@ -139,8 +139,7 @@ impl TryFrom<&str> for WireGuard { if let Some(endpoints) = wg.interfaces.get_mut(v[0]) { endpoints.push(endpoint); } else { - let mut new_vec = Vec::new(); - new_vec.push(endpoint); + let new_vec = vec![endpoint]; wg.interfaces.insert(v[0].to_owned(), new_vec); } } @@ -212,10 +211,10 @@ impl WireGuard { // store in attibutes their references. attributes_owned is onyl // needed for separate ip+subnet let mut attributes_owned: Vec<(String, String)> = Vec::new(); - let mut attributes: Vec<(&str, &str)> = Vec::new(); - - attributes.push(("interface", interface)); - attributes.push(("public_key", &ep.public_key)); + let mut attributes: Vec<(&str, &str)> = vec![ + ("interface", interface), + ("public_key", &ep.public_key), + ]; if split_allowed_ips { let v_ip_and_subnet: Vec<(&str, &str)> = ep