From e807f9f12c6662ee21fc647f3d8630920dc15116 Mon Sep 17 00:00:00 2001 From: Ken Sternberg <133134217+kensternberg-authentik@users.noreply.github.com> Date: Tue, 26 Sep 2023 03:10:18 -0700 Subject: [PATCH] root: make postgres connection in makefile customizable (#6977) * core/allow alternative postgres credentials This commit allows the `dev-reset` command in the Makefile to pick up and use credentials from the `.env` file if they are present, or fallback to the defaults provided if they are not. This is the only place in the Makefile where the database credentials are used directly against postgresql binaries. The syntax was tested with bash, zsh, and csh, and did not fail under those. The `$${:-}` syntax is a combination of a Makefile idiom for "Pass a single `$` to the environment where this command will be executed," and the shell expresion `${VARIABLE:-default}` means "dereference the environment variable; if it is undefined, used the default value provided." * Re-arrange sequence to avoid recursive make. Nothing wrong with recursive make; it just wasn't essential here. `migrate` is just a build target, not a task. * Cleanup according to the Usage: checkmake [options] ... checkmake -h | --help checkmake --version checkmake --list-rules Makefile linting tool. * core: added 'help' to the Makefile * get postgres config from authentik config loader Signed-off-by: Jens Langhammer * don't set -x by default Signed-off-by: Jens Langhammer * sort help Signed-off-by: Jens Langhammer * update help strings Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer Co-authored-by: Jens Langhammer --- Makefile | 87 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index f04e8dac8..a6c4b3e69 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,16 @@ -.SHELLFLAGS += -x -e +.PHONY: gen dev-reset all clean test web website + +.SHELLFLAGS += ${SHELLFLAGS} -e PWD = $(shell pwd) UID = $(shell id -u) GID = $(shell id -g) NPM_VERSION = $(shell python -m scripts.npm_version) PY_SOURCES = authentik tests scripts lifecycle +DOCKER_IMAGE ?= "authentik:test" + +pg_user := $(shell python -m authentik.lib.config postgresql.user 2>/dev/null) +pg_host := $(shell python -m authentik.lib.config postgresql.host 2>/dev/null) +pg_name := $(shell python -m authentik.lib.config postgresql.name 2>/dev/null) CODESPELL_ARGS = -D - -D .github/codespell-dictionary.txt \ -I .github/codespell-words.txt \ @@ -19,57 +26,78 @@ CODESPELL_ARGS = -D - -D .github/codespell-dictionary.txt \ website/integrations \ website/src -all: lint-fix lint test gen web +all: lint-fix lint test gen web ## Lint, build, and test everything + +help: ## Show this help + @echo "\nSpecify a command. The choices are:\n" + @grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ + awk 'BEGIN {FS = ":.*?## "}; {printf " \033[0;36m%-24s\033[m %s\n", $$1, $$2}' | \ + sort + @echo "" test-go: go test -timeout 0 -v -race -cover ./... -test-docker: +test-docker: ## Run all tests in a docker-compose echo "PG_PASS=$(openssl rand -base64 32)" >> .env echo "AUTHENTIK_SECRET_KEY=$(openssl rand -base64 32)" >> .env docker-compose pull -q docker-compose up --no-start docker-compose start postgresql redis - docker-compose run -u root server test + docker-compose run -u root server test-all rm -f .env -test: +test: ## Run the server tests and produce a coverage report (locally) coverage run manage.py test --keepdb authentik coverage html coverage report -lint-fix: +lint-fix: ## Lint and automatically fix errors in the python source code. Reports spelling errors. isort authentik $(PY_SOURCES) black authentik $(PY_SOURCES) ruff authentik $(PY_SOURCES) codespell -w $(CODESPELL_ARGS) -lint: +lint: ## Lint the python and golang sources pylint $(PY_SOURCES) bandit -r $(PY_SOURCES) -x node_modules golangci-lint run -v -migrate: +migrate: ## Run the Authentik Django server's migrations python -m lifecycle.migrate -i18n-extract: i18n-extract-core web-i18n-extract +i18n-extract: i18n-extract-core web-i18n-extract ## Extract strings that require translation into files to send to a translation service i18n-extract-core: ak makemessages --ignore web --ignore internal --ignore web --ignore web-api --ignore website -l en +install: web-install website-install ## Install all requires dependencies for `web`, `website` and `core` + poetry install + +dev-drop-db: + echo dropdb -U ${pg_user} -h ${pg_host} ${pg_name} + # Also remove the test-db if it exists + dropdb -U ${pg_user} -h ${pg_host} test_${pg_name} || true + echo redis-cli -n 0 flushall + +dev-create-db: + createdb -U ${pg_user} -h ${pg_host} ${pg_name} + +dev-reset: dev-drop-db dev-create-db migrate ## Drop and restore the Authentik PostgreSQL instance to a "fresh install" state. + ######################### ## API Schema ######################### -gen-build: +gen-build: ## Extract the schema from the database AUTHENTIK_DEBUG=true ak make_blueprint_schema > blueprints/schema.json AUTHENTIK_DEBUG=true ak spectacular --file schema.yml -gen-changelog: +gen-changelog: ## (Release) generate the changelog based from the commits since the last tag git log --pretty=format:" - %s" $(shell git describe --tags $(shell git rev-list --tags --max-count=1))...$(shell git branch --show-current) | sort > changelog.md npx prettier --write changelog.md -gen-diff: +gen-diff: ## (Release) generate the changelog diff between the current schema and the last tag git show $(shell git describe --tags $(shell git rev-list --tags --max-count=1)):schema.yml > old_schema.yml docker run \ --rm -v ${PWD}:/local \ @@ -84,7 +112,7 @@ gen-clean: rm -rf web/api/src/ rm -rf api/ -gen-client-ts: +gen-client-ts: ## Build and install the authentik API for Typescript into the authentik UI Application docker run \ --rm -v ${PWD}:/local \ --user ${UID}:${GID} \ @@ -100,7 +128,7 @@ gen-client-ts: cd gen-ts-api && npm i \cp -rfv gen-ts-api/* web/node_modules/@goauthentik/api -gen-client-go: +gen-client-go: ## Build and install the authentik API for Golang mkdir -p ./gen-go-api ./gen-go-api/templates wget https://raw.githubusercontent.com/goauthentik/client-go/main/config.yaml -O ./gen-go-api/config.yaml wget https://raw.githubusercontent.com/goauthentik/client-go/main/templates/README.mustache -O ./gen-go-api/templates/README.mustache @@ -117,7 +145,7 @@ gen-client-go: go mod edit -replace goauthentik.io/api/v3=./gen-go-api rm -rf ./gen-go-api/config.yaml ./gen-go-api/templates/ -gen-dev-config: +gen-dev-config: ## Generate a local development config file python -m scripts.generate_config gen: gen-build gen-clean gen-client-ts @@ -126,21 +154,21 @@ gen: gen-build gen-clean gen-client-ts ## Web ######################### -web-build: web-install +web-build: web-install ## Build the Authentik UI cd web && npm run build -web: web-lint-fix web-lint web-check-compile web-i18n-extract +web: web-lint-fix web-lint web-check-compile web-i18n-extract ## Automatically fix formatting issues in the Authentik UI source code, lint the code, and compile it -web-install: +web-install: ## Install the necessary libraries to build the Authentik UI cd web && npm ci -web-watch: +web-watch: ## Build and watch the Authentik UI for changes, updating automatically rm -rf web/dist/ mkdir web/dist/ touch web/dist/.gitkeep cd web && npm run watch -web-storybook-watch: +web-storybook-watch: ## Build and run the storybook documentation server cd web && npm run storybook web-lint-fix: @@ -160,7 +188,7 @@ web-i18n-extract: ## Website ######################### -website: website-lint-fix website-build +website: website-lint-fix website-build ## Automatically fix formatting issues in the Authentik website/docs source code, lint the code, and compile it website-install: cd website && npm ci @@ -171,15 +199,15 @@ website-lint-fix: website-build: cd website && npm run build -website-watch: +website-watch: ## Build and watch the documentation website, updating automatically cd website && npm run watch ######################### ## Docker ######################### -docker: - DOCKER_BUILDKIT=1 docker build . --progress plain --tag authentik:test +docker: ## Build a docker image of the current source tree + DOCKER_BUILDKIT=1 docker build . --progress plain --tag ${DOCKER_IMAGE} ######################### ## CI @@ -214,14 +242,3 @@ ci-pyright: ci--meta-debug ci-pending-migrations: ci--meta-debug ak makemigrations --check - -install: web-install website-install - poetry install - -dev-reset: - dropdb -U postgres -h localhost authentik - # Also remove the test-db if it exists - dropdb -U postgres -h localhost test_authentik || true - createdb -U postgres -h localhost authentik - redis-cli -n 0 flushall - make migrate