Skip to content

Commit e33980b

Browse files
committed
Merge branch 'main' into feature/authentication-extension
2 parents 71ac590 + 45b4e38 commit e33980b

File tree

11 files changed

+172
-124
lines changed

11 files changed

+172
-124
lines changed

.github/workflows/cicd.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: CI/CD
22

33
on:
4-
- push
4+
push:
5+
release:
56

67
jobs:
78
lint:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Publish Docker image
2+
3+
on:
4+
workflow_run:
5+
workflows: ["CI/CD"]
6+
types:
7+
- completed
8+
branches:
9+
- main
10+
11+
env:
12+
REGISTRY: ghcr.io
13+
IMAGE_NAME: ${{ github.repository }}
14+
15+
jobs:
16+
build-and-push:
17+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: read
21+
packages: write
22+
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
27+
- name: Set up Docker Buildx
28+
uses: docker/setup-buildx-action@v3
29+
30+
- name: Log in to the Container registry
31+
uses: docker/login-action@v3
32+
with:
33+
registry: ${{ env.REGISTRY }}
34+
username: ${{ github.actor }}
35+
password: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Extract metadata (tags, labels) for Docker
38+
id: meta
39+
uses: docker/metadata-action@v5
40+
with:
41+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
42+
tags: |
43+
type=raw,value=latest,enable={{is_default_branch}}
44+
type=semver,pattern={{version}}
45+
type=sha,format=long
46+
type=ref,event=branch
47+
type=ref,event=tag
48+
49+
- name: Build and push Docker image
50+
uses: docker/build-push-action@v5
51+
with:
52+
context: .
53+
push: true
54+
platforms: linux/amd64,linux/arm64
55+
tags: ${{ steps.meta.outputs.tags }}
56+
labels: ${{ steps.meta.outputs.labels }}
57+
cache-from: type=gha
58+
cache-to: type=gha,mode=max

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ STAC Auth Proxy is a proxy API that mediates between the client and your interna
1818

1919
## Usage
2020

21-
> [!NOTE]
22-
> Currently, the project can only be installed by downloading the repository. It will eventually be available on Docker ([#5](https://github.com/developmentseed/stac-auth-proxy/issues/5)) and PyPi ([#30](https://github.com/developmentseed/stac-auth-proxy/issues/30)).
23-
2421
### Installation
2522

2623
For local development, we use [`uv`](https://docs.astral.sh/uv/) to manage project dependencies and environment.
@@ -35,15 +32,27 @@ Otherwise, the application can be installed as a standard Python module:
3532
pip install -e .
3633
```
3734

35+
> [!NOTE]
36+
> This project will be available on PyPi in the near future ([#30](https://github.com/developmentseed/stac-auth-proxy/issues/30)).
37+
3838
### Running
3939

40-
The simplest way to run the project is by calling the module directly:
40+
The simplest way to run the project is by invoking the application via Docker:
4141

4242
```sh
43-
python -m stac_auth_proxy
43+
docker run \
44+
-it --rm \
45+
-p 8000:8000 \
46+
-e UPSTREAM_URL=https://google.com \
47+
-e OIDC_DISCOVERY_URL=https://auth.openveda.cloud/realms/veda/.well-known/openid-configuration \
48+
ghcr.io/developmentseed/stac-auth-proxy:latest
4449
```
4550

46-
Alternatively, the application's factory can be passed to Uvicorn:
51+
Alternatively, the module can be invoked directly or the application's factory can be passed to Uvicorn:
52+
53+
```sh
54+
python -m stac_auth_proxy
55+
```
4756

4857
```sh
4958
uvicorn --factory stac_auth_proxy:create_app

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[project]
2-
authors = [{name = "Your Name", email = "your.email@example.com"}]
2+
authors = [{name = "Anthony Lukach", email = "anthonylukach@gmail.com"}]
33
classifiers = [
44
"Programming Language :: Python :: 3",
55
"Programming Language :: Python :: 3.8",
@@ -10,7 +10,7 @@ dependencies = [
1010
"brotli>=1.1.0",
1111
"cql2>=0.3.5",
1212
"fastapi>=0.115.5",
13-
"httpx>=0.28.0",
13+
"httpx[http2]>=0.28.0",
1414
"jinja2>=3.1.4",
1515
"pydantic-settings>=2.6.1",
1616
"pyjwt>=2.10.1",

src/stac_auth_proxy/app.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"""
77

88
import logging
9+
from contextlib import asynccontextmanager
910
from typing import Optional
1011

1112
from fastapi import FastAPI
1213
from starlette_cramjam.middleware import CompressionMiddleware
1314

1415
from .config import Settings
15-
from .handlers import HealthzHandler, ReverseProxyHandler, S3AssetSigner
16-
from .lifespan import LifespanManager, ServerHealthCheck
16+
from .handlers import HealthzHandler, ReverseProxyHandler
1717
from .middleware import (
1818
AddProcessTimeHeaderMiddleware,
1919
ApplyCql2FilterMiddleware,
@@ -22,6 +22,7 @@
2222
EnforceAuthMiddleware,
2323
OpenApiMiddleware,
2424
)
25+
from .utils.lifespan import check_server_health
2526

2627
logger = logging.getLogger(__name__)
2728

@@ -33,14 +34,17 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
3334
#
3435
# Application
3536
#
36-
upstream_urls = (
37-
[settings.upstream_url, settings.oidc_discovery_internal_url]
38-
if settings.wait_for_upstream
39-
else []
40-
)
41-
lifespan = LifespanManager(
42-
on_startup=([ServerHealthCheck(url=url) for url in upstream_urls])
43-
)
37+
38+
@asynccontextmanager
39+
async def lifespan(app: FastAPI):
40+
assert settings
41+
42+
# Wait for upstream servers to become available
43+
if settings.wait_for_upstream:
44+
for url in [settings.upstream_url, settings.oidc_discovery_internal_url]:
45+
await check_server_health(url=url)
46+
47+
yield
4448

4549
app = FastAPI(
4650
openapi_url=None, # Disable OpenAPI schema endpoint, we want to serve upstream's schema

src/stac_auth_proxy/handlers/reverse_proxy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __post_init__(self):
2525
self.client = self.client or httpx.AsyncClient(
2626
base_url=self.upstream,
2727
timeout=self.timeout,
28+
http2=True,
2829
)
2930

3031
async def proxy_request(self, request: Request) -> Response:

src/stac_auth_proxy/lifespan/LifespanManager.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/stac_auth_proxy/lifespan/ServerHealthCheck.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/stac_auth_proxy/lifespan/__init__.py

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Health check implementations for lifespan events."""
2+
3+
import asyncio
4+
import logging
5+
6+
import httpx
7+
from pydantic import HttpUrl
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
async def check_server_health(
13+
url: str | HttpUrl,
14+
max_retries: int = 10,
15+
retry_delay: float = 1.0,
16+
retry_delay_max: float = 5.0,
17+
timeout: float = 5.0,
18+
) -> None:
19+
"""Wait for upstream API to become available."""
20+
# Convert url to string if it's a HttpUrl
21+
if isinstance(url, HttpUrl):
22+
url = str(url)
23+
24+
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
25+
for attempt in range(max_retries):
26+
try:
27+
response = await client.get(url)
28+
response.raise_for_status()
29+
logger.info(f"Upstream API {url!r} is healthy")
30+
return
31+
except Exception as e:
32+
logger.warning(f"Upstream health check for {url!r} failed: {e}")
33+
retry_in = min(retry_delay * (2**attempt), retry_delay_max)
34+
logger.warning(
35+
f"Upstream API {url!r} not healthy, retrying in {retry_in:.1f}s "
36+
f"(attempt {attempt + 1}/{max_retries})"
37+
)
38+
await asyncio.sleep(retry_in)
39+
40+
raise RuntimeError(
41+
f"Upstream API {url!r} failed to respond after {max_retries} attempts"
42+
)

0 commit comments

Comments
 (0)