diff --git a/.dockerignore b/.dockerignore index e15b65b..98acbe7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,3 @@ README.md target/ +scripts/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..6972f29 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,22 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.1.0 + hooks: + - id: check-added-large-files + - id: check-yaml + - id: check-toml + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-merge-conflict + - repo: https://github.com/doublify/pre-commit-rust + rev: 14b3e118cfc36fb87d8d9cbd1305a2238fd85868 + hooks: + - id: fmt + - id: cargo-check + - id: clippy + - repo: https://github.com/IamTheFij/docker-pre-commit + rev: v2.0.0 + hooks: + - id: docker-compose-check + - id: hadolint diff --git a/Dockerfile b/Dockerfile index e805936..9e14ab4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,15 +5,13 @@ RUN USER=root cargo new --bin bitwarden_rs_ldap WORKDIR /usr/src/bitwarden_rs_ldap # Compile dependencies -COPY ./Cargo.toml ./Cargo.toml -COPY ./Cargo.lock ./Cargo.lock -RUN cargo build --release -# Remove temp src -RUN rm src/*.rs +COPY Cargo.toml Cargo.lock ./ +RUN cargo build --locked --release -# Copy source and install -COPY ./src ./src +# Remove bins to make sure we rebuild RUN rm ./target/release/deps/bitwarden_rs_ldap* +# Copy source and install +COPY src ./src RUN cargo install --path . CMD ["bitwarden_rs_ldap"] diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 408fa36..3ec1749 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,4 +1,4 @@ -FROM ekidd/rust-musl-builder AS builder +FROM ekidd/rust-musl-builder:1.33.0 AS builder RUN USER=rust cargo init @@ -10,15 +10,15 @@ RUN cargo build --release RUN rm src/*.rs COPY ./src ./src # Fix permissions on source -RUN sudo chown -R rust:rust /home/rust/src/src +RUN USER=root chown -R rust:rust /home/rust/src/src RUN touch ./src/main.rs RUN cargo build --release -FROM alpine:latest -RUN apk --no-cache add ca-certificates +FROM alpine:3 +RUN apk --no-cache add ca-certificates=20191127-r4 COPY --from=builder \ /home/rust/src/target/x86_64-unknown-linux-musl/release/bitwarden_rs_ldap \ /usr/local/bin/ -CMD /usr/local/bin/bitwarden_rs_ldap +CMD ["/usr/local/bin/bitwarden_rs_ldap"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b00c445 --- /dev/null +++ b/Makefile @@ -0,0 +1,54 @@ +DOCKER_TAG ?= bitwarden_rs_ldap_${USER} + +.PHONY: all +all: test check release + +# Default make target will run tests +.DEFAULT_GOAL = test + +# Build debug version +target/debug/bitwarden_rs_ldap: src/ + cargo build + +# Build release version +target/release/bitwarden_rs_ldap: src/ + cargo build --locked --release + +.PHONY: debug +debug: target/debug/bitwarden_rs_ldap + +.PHONY: release +release: target/release/bitwarden_rs_ldap + +# Run debug version +.PHONY: run-debug +run-debug: target/debug/bitwarden_rs_ldap + target/debug/bitwarden_rs_ldap + +# Run all tests +.PHONY: test +test: + cargo test + +# Installs pre-commit hooks +.PHONY: install-hooks +install-hooks: + pre-commit install --install-hooks + +# Checks files for encryption +.PHONY: check +check: + pre-commit run --all-files + +# Checks that version matches the current tag +.PHONY: check-version +check-version: + ./scripts/check-version.sh + +.PHONY: clean +clean: + rm -f ./target + +.PHONY: docker-build +docker-build: + docker build -f ./Dockerfile -t $(DOCKER_TAG) . diff --git a/README.md b/README.md index ee54c08..fd99a7c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # bitwarden_rs_ldap A simple LDAP connector for [bitwarden_rs](https://github.com/dani-garcia/bitwarden_rs) -After configuring, simply run `bitwarden_rs_ldap` and it will invite any users it finds in LDAP to your `bitwarden_rs` instance. +After configuring, run `bitwarden_rs_ldap` and it will invite any users it finds in LDAP to your `bitwarden_rs` instance. ## Deploying @@ -31,6 +31,12 @@ Configuration values are as follows: |`ldap_sync_interval_seconds`|Integer|Optional|Number of seconds to wait between each LDAP request. Defaults to `60`| |`ldap_sync_loop`|Boolean|Optional|Indicates whether or not syncing should be polled in a loop or done once. Defaults to `true`| +## Development + +This repo has a predefined set of [pre-commit](https://pre-commit.com) rules. You can install pre-commit via any means you'd like. Once your system has `pre-commit` installed, you can run `make install-hooks` to ensure the hooks will run with every commit. You can also force running all hooks with `make check`. + +For those less familiar with `cargo`, you can use the `make` targets that have been included for common tasks like running a debug version. `make run-debug` or building a release version `make release`. + ## Testing All testing is manual right now. First step is to set up Bitwarden and the LDAP server. diff --git a/docker-compose.yml b/docker-compose.yml index 365440c..e604252 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,4 @@ +--- version: '3' services: ldap_sync: diff --git a/scripts/check-version.sh b/scripts/check-version.sh new file mode 100755 index 0000000..69d0fd5 --- /dev/null +++ b/scripts/check-version.sh @@ -0,0 +1,8 @@ +#! /bin/sh + +CARGO_VERSION=$(cargo pkgid --offline | sed 's/.*#//') +GIT_VERSION=${GIT_VERSION:-$(git describe --tags --exact-match)} +if ! [ "v$CARGO_VERSION" = "$GIT_VERSION" ]; then + echo "ERROR: Cargo version (v$CARGO_VERSION) and git version ($GIT_VERSION) do not match" + exit 1 +fi