diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml deleted file mode 100644 index 352a0a9..0000000 --- a/.forgejo/workflows/build.yaml +++ /dev/null @@ -1,93 +0,0 @@ -on: - push: - workflow_dispatch: - inputs: - verbose: - description: "Verbose" - required: false - default: false - type: boolean - -jobs: - build: - runs-on: container - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Install the latest version of uv - uses: astral-sh/setup-uv@v4 - with: - version: "0.5.16" - - - name: Install - run: uv sync - - - name: Run tests (API call) - run: .venv/bin/pytest -s tests/basic.py - - - name: Get version with git describe - id: version - run: | - echo "version=$(git describe)" >> $GITHUB_OUTPUT - echo "$VERSION" - - - name: Check if the container should be built - id: builder - env: - RUN: ${{ toJSON(inputs.build || !contains(steps.version.outputs.version, '-')) }} - run: | - echo "run=$RUN" >> $GITHUB_OUTPUT - echo "Run build: $RUN" - - - name: Set the version in pyproject.toml (workaround for uv not supporting dynamic version) - if: fromJSON(steps.builder.outputs.run) - env: - VERSION: ${{ steps.version.outputs.version }} - run: sed "s/0.0.0/$VERSION/" -i pyproject.toml - - - name: Workaround for bug of podman-login - if: fromJSON(steps.builder.outputs.run) - run: | - mkdir -p $HOME/.docker - echo "{ \"auths\": {} }" > $HOME/.docker/config.json - - - name: Log in to the container registry (with another workaround) - if: fromJSON(steps.builder.outputs.run) - uses: actions/podman-login@v1 - with: - registry: ${{ vars.REGISTRY }} - username: ${{ secrets.REGISTRY_USER }} - password: ${{ secrets.REGISTRY_PASSWORD }} - auth_file_path: /tmp/auth.json - - - name: Build the container image - if: fromJSON(steps.builder.outputs.run) - uses: actions/buildah-build@v1 - with: - image: oidc-fastapi-test - oci: true - labels: oidc-fastapi-test - tags: latest ${{ steps.version.outputs.version }} - containerfiles: | - ./Containerfile - - - name: Push the image to the registry - if: fromJSON(steps.builder.outputs.run) - uses: actions/push-to-registry@v2 - with: - registry: "docker://${{ vars.REGISTRY }}/${{ vars.ORGANISATION }}" - image: oidc-fastapi-test - tags: latest ${{ steps.version.outputs.version }} - - - name: Build wheel - if: fromJSON(steps.builder.outputs.run) - run: uv build --wheel - - - name: Publish Python package (home) - if: fromJSON(steps.builder.outputs.run) - env: - LOCAL_PYPI_TOKEN: ${{ secrets.LOCAL_PYPI_TOKEN }} - run: uv publish --publish-url https://code.philo.ydns.eu/api/packages/philorg/pypi --token $LOCAL_PYPI_TOKEN - continue-on-error: true diff --git a/.forgejo/workflows/test.yaml b/.forgejo/workflows/test.yaml deleted file mode 100644 index a56a9ce..0000000 --- a/.forgejo/workflows/test.yaml +++ /dev/null @@ -1,28 +0,0 @@ -on: - push: - workflow_dispatch: - inputs: - verbose: - description: "Verbose" - required: false - default: false - type: boolean - -jobs: - test: - runs-on: container - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Install the latest version of uv - uses: astral-sh/setup-uv@v4 - with: - version: "0.5.16" - - - name: Install - run: uv sync - - - name: Run tests (API call) - run: .venv/bin/pytest -s tests/basic.py diff --git a/.woodpecker/build.yaml b/.woodpecker/build.yaml new file mode 100644 index 0000000..3af8132 --- /dev/null +++ b/.woodpecker/build.yaml @@ -0,0 +1,63 @@ +when: + - event: manual + - event: tag + +depends_on: + - test + +steps: + python_sync: + image: code.philo.ydns.eu/philorg/uv + volumes: + - uv-cache:/uv-cache + environment: + UV_CACHE_DIR: /uv-cache + UV_LINK_MODE: copy + commands: + - uv sync + + python_build: + image: code.philo.ydns.eu/philorg/uv + volumes: + - uv-cache:/uv-cache + environment: + UV_CACHE_DIR: /uv-cache + UV_LINK_MODE: copy + commands: + - uv build --wheel + - uv cache prune --ci + + python_publish: + image: code.philo.ydns.eu/philorg/uv + environment: + OWNER: philorg + REGISTRY_URL: https://code.philo.ydns.eu + REGISTRY_TOKEN: + from_secret: registry_token + commands: + - uv publish --publish-url $REGISTRY_URL/api/packages/$OWNER/pypi --token $REGISTRY_TOKEN dist/*.whl + failure: ignore + + container_build_publish: + image: quay.io/podman/stable:latest + # Caution: This image is built daily. It might fill up your image store quickly. + #pull: true + volumes: + - containers:/var/lib/containers + - uv:/root/.cache/uv + # Fill in the trusted checkbox in Woodpecker's settings as well + privileged: true + environment: + registry: code.philo.ydns.eu + org: philorg + container_name: oidc-fastapi-test + registry_token: + from_secret: registry_token + commands: + # Login at the registry + - podman login -u __token__ --password $registry_token $registry + # Build the container image + - podman build --volume=/var/lib/containers:/var/lib/containers --tag $registry/$org/$container_name:latest --tag $registry/$org/$container_name:$CI_COMMIT_TAG . + # Push the image + - podman push $registry/$org/$container_name:latest + - podman push $registry/$org/$container_name:$CI_COMMIT_TAG diff --git a/.woodpecker/test.yaml b/.woodpecker/test.yaml new file mode 100644 index 0000000..d8816b2 --- /dev/null +++ b/.woodpecker/test.yaml @@ -0,0 +1,21 @@ +when: + - event: push + branch: main + - event: manual + - event: tag + +steps: + sync: + image: code.philo.ydns.eu/philorg/uv + volumes: + - uv-cache:/uv-cache + environment: + UV_CACHE_DIR: /uv-cache + UV_LINK_MODE: copy + commands: + - uv sync + + test: + image: code.philo.ydns.eu/philorg/uv + commands: + - .venv/bin/pytest -s tests/basic.py diff --git a/Containerfile b/Containerfile index 2e3fd28..357cc84 100644 --- a/Containerfile +++ b/Containerfile @@ -1,6 +1,7 @@ -FROM docker.io/library/python:alpine +FROM docker.io/library/python:3.13-slim COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /usr/local/bin/ +COPY --from=docker.io/python:3.13 /usr/bin/git /usr/local/bin/git COPY . /app diff --git a/README.md b/README.md index 68f335d..c7f8741 100644 --- a/README.md +++ b/README.md @@ -127,3 +127,5 @@ with the setting file in the current working directory: ```sh podman run -p 8000:80 --env OIDC_TEST_CONFIG_FILE=/app/settings.yaml --mount type=bind,source=settings.yaml,destination=/app/settings.yaml code.philo.ydns.eu/philorg/oidc-fastapi-test:latest ``` + +[](https://code.philo.ydns.eu/woodpecker/repos/22) diff --git a/pyproject.toml b/pyproject.toml index 9c205e7..c44e9f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,11 +24,7 @@ dependencies = [ oidc-test = "oidc_test.main:main" [dependency-groups] -dev = [ - "dunamai>=1.23.0", - "ipdb>=0.13.13", - "pytest>=8.3.4", -] +dev = ["dunamai>=1.23.0", "ipdb>=0.13.13", "pytest>=8.3.4"] [build-system] requires = ["hatchling", "uv-dynamic-versioning"] @@ -39,6 +35,10 @@ source = "uv-dynamic-versioning" [tool.hatch.build.targets.wheel] packages = ["src/oidc_test"] +package = true + +[tool.uv-dynamic-versioning] +style = "semver" [tool.uv] package = true diff --git a/src/oidc_test/__init__.py b/src/oidc_test/__init__.py index e69de29..f154022 100644 --- a/src/oidc_test/__init__.py +++ b/src/oidc_test/__init__.py @@ -0,0 +1,11 @@ +import importlib.metadata + +try: + from dunamai import Version, Style + + __version__ = Version.from_git().serialize(style=Style.SemVer, dirty=True) +except (ImportError, RuntimeError): + # __name__ could be used if the package name is the same + # as the directory - not the case here + # __version__ = importlib.metadata.version(__name__) + __version__ = importlib.metadata.version("oidc-fastapi-test") diff --git a/src/oidc_test/auth/provider.py b/src/oidc_test/auth/provider.py index c614805..ce288a6 100644 --- a/src/oidc_test/auth/provider.py +++ b/src/oidc_test/auth/provider.py @@ -61,28 +61,34 @@ class Provider(AuthProviderSettings): if self.info_url is not None: try: provider_info = await client.get(self.info_url) - except Exception: + except Exception as err: + logger.debug("Provider_info: cannot connect") + logger.exception(err) raise NoPublicKey try: self.info = provider_info.json() except JSONDecodeError: + logger.debug("Provider_info: cannot decode json response") raise NoPublicKey if "public_key" in self.info: # For Keycloak try: public_key = str(self.info["public_key"]) except KeyError: + logger.debug("Provider_info: cannot get public_key") raise NoPublicKey elif "keys" in self.info: # For Forgejo/Gitea try: public_key = str(self.info["keys"][0]["n"]) except KeyError: + logger.debug("Provider_info: cannot get key 0.n") raise NoPublicKey if self.public_key_url is not None: resp = await client.get(self.public_key_url) public_key = resp.text if public_key is None: + logger.debug("Provider_info: cannot determine public key") raise NoPublicKey self.public_key = "\n".join( ["-----BEGIN PUBLIC KEY-----", public_key, "-----END PUBLIC KEY-----"] diff --git a/src/oidc_test/main.py b/src/oidc_test/main.py index e5238c8..e882cda 100644 --- a/src/oidc_test/main.py +++ b/src/oidc_test/main.py @@ -29,6 +29,7 @@ from authlib.oauth2.rfc6749 import OAuth2Token # from fastapi.security import OpenIdConnect # from pkce import generate_code_verifier, generate_pkce_pair +from oidc_test import __version__ from oidc_test.registry import registry from oidc_test.auth.provider import NoPublicKey, Provider from oidc_test.auth.utils import ( @@ -108,6 +109,7 @@ async def home( "show_token": settings.show_token, "user": user, "now": datetime.now(), + "__version__": __version__, } if provider is None or token is None: context["providers"] = providers diff --git a/src/oidc_test/static/styles.css b/src/oidc_test/static/styles.css index 2baa748..1e8dc03 100644 --- a/src/oidc_test/static/styles.css +++ b/src/oidc_test/static/styles.css @@ -21,6 +21,12 @@ hr { .hidden { display: none; } +.version { + position: absolute; + font-size: 75%; + top: 0.3em; + right: 0.3em; +} .center { text-align: center; } diff --git a/src/oidc_test/templates/base.html b/src/oidc_test/templates/base.html index 4cb56f5..157e26f 100644 --- a/src/oidc_test/templates/base.html +++ b/src/oidc_test/templates/base.html @@ -5,6 +5,7 @@
+