Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ jobs:
context: .
push: true
platforms: linux/amd64
build-args: |
CODE_VERSION=${{ github.sha }}
tags: ${{ vars.DOCKERHUB_REGISTRY }}/workspaces-backend-v2:dev

2 changes: 2 additions & 0 deletions .github/workflows/push-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ jobs:
context: .
push: true
platforms: linux/amd64
build-args: |
CODE_VERSION=${{ github.sha }}
tags: ${{ vars.DOCKERHUB_REGISTRY }}/workspaces-backend-v2:${{ needs.set-github-environment.outputs.environment }},
${{ vars.DOCKERHUB_REGISTRY }}/workspaces-backend-v2:${{github.sha}}
deploy:
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ RUN --mount=type=cache,target=/root/.cache/uv \
# set the environment variable for uvicorn to run the FastAPI app
ENV UV_PROJECT_ENVIRONMENT=.uvenv

# The commit this image was built from, reported to Sentry as the release.
# Set by the deploy workflow; declared last so a new commit doesn't
# invalidate the dependency layers above.
ARG CODE_VERSION=unknown
ENV CODE_VERSION=$CODE_VERSION

# Run with uvicorn
CMD ["uv", "run", "uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"]
74 changes: 62 additions & 12 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from fastapi.responses import RedirectResponse, StreamingResponse
from sqlmodel.ext.asyncio.session import AsyncSession
from starlette.background import BackgroundTask
from starlette.middleware.gzip import GZipMiddleware
from starlette.types import ASGIApp, Receive, Scope, Send

from api.core import config
from api.core.config import settings
Expand All @@ -38,22 +40,41 @@
from api.src.workspaces.routes import router as workspaces_router
from api.utils.migrations import run_migrations

sentry_sdk.init(
dsn=config.settings.SENTRY_DSN,
environment=os.getenv("ENV", "unknown"),
release=os.getenv("CODE_VERSION", "unknown"),
debug=settings.DEBUG,
)

# Kept alongside `release` for any dashboards that query the `version` tag.
sentry_sdk.set_tag("version", os.getenv("CODE_VERSION", "unknown"))

# Set up logging configuration
setup_logging()

# Set up logger for this module
logger = get_logger(__name__)


# @test: Test that a missing SENTRY_DSN logs a warning, and a set one logs the environment and release it reports under
def init_sentry() -> None:
# With no DSN the SDK quietly sends nothing, so say at startup which it
# is. The App Services once ran without SENTRY_DSN and nobody could tell.
environment = os.getenv("ENV", "unknown")
release = os.getenv("CODE_VERSION", "unknown")

sentry_sdk.init(
dsn=config.settings.SENTRY_DSN,
environment=environment,
release=release,
debug=settings.DEBUG,
)

# Kept alongside `release` for any dashboards that query the `version` tag.
sentry_sdk.set_tag("version", release)

if config.settings.SENTRY_DSN:
logger.info(
f"Reporting errors to Sentry: environment={environment!r}, "
f"release={release!r}"
)
else:
logger.warning("SENTRY_DSN is not set, so errors are not reported to Sentry")


init_sentry()

# Shared HTTP client for OSM proxy. Reuses connection pool across requests:
_osm_client: httpx.AsyncClient | None = None

Expand Down Expand Up @@ -108,6 +129,30 @@ async def lifespan(_app: FastAPI):
max_age=100,
)


# @test: Test that a large /api/v1 response is gzipped for a client that accepts it, and that proxied OSM paths are passed through uncompressed
class NativeApiGZipMiddleware:
"""Gzip responses from this service's own API (``/api/v1/``) only.

The OSM proxy (`catch_all`) is left alone: it already negotiates
Accept-Encoding with the upstream (see `_UPSTREAM_ACCEPT_ENCODING`) and
streams its responses through as received.
"""

def __init__(self, app: ASGIApp, prefix: str = "/api/v1/") -> None:
self.app = app
self.prefix = prefix
self.gzip = GZipMiddleware(app, minimum_size=1000)

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "http" and scope["path"].startswith(self.prefix):
await self.gzip(scope, receive, send)
else:
await self.app(scope, receive, send)


app.add_middleware(NativeApiGZipMiddleware)

# Never log these header values verbatim (tokens/session identifiers):
_SENSITIVE_LOG_HEADERS = frozenset({"authorization", "cookie", "set-cookie"})

Expand Down Expand Up @@ -136,10 +181,15 @@ async def log_request_headers(request: Request, call_next):
app.include_router(tasking_audit_router, prefix="/api/v1")


# @test: Test that /health reports the CODE_VERSION the image was built with, or "unknown"
@app.get("/health")
async def health_check():
"""Health check endpoint. Used for Docker."""
return {"status": "ok"}
"""Health check endpoint. Used for Docker.

Also says which commit is running (`CODE_VERSION`, baked into the image by
the deploy workflow), so a deploy can be confirmed from outside.
"""
return {"status": "ok", "version": os.getenv("CODE_VERSION", "unknown")}


@app.get("/")
Expand Down
33 changes: 30 additions & 3 deletions api/src/workspaces/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from sqlalchemy import delete, func, select, update
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import defer, noload
from sqlmodel.ext.asyncio.session import AsyncSession

from api.core.exceptions import (
Expand All @@ -12,6 +13,7 @@
from api.core.json_schema import get_http_client
from api.core.security import UserInfo, fetch_tdei_dataset
from api.src.workspaces.schemas import (
ExternalAppsDefinitionType,
ImagerySettingsPatch,
QuestDefinitionType,
QuestSettingsPatch,
Expand Down Expand Up @@ -75,10 +77,35 @@ async def getById(self, current_user: UserInfo, workspace_id: int) -> Workspace:
raise NotFoundException(f"Workspace with id {workspace_id} not found")
return workspace

async def getAll(self, current_user: UserInfo) -> list[Workspace]:
query = select(Workspace).where(
Workspace.tdeiProjectGroupId.in_(current_user.getProjectGroupIds()) # type: ignore[attr-defined]
# @test: Test that gig_only limits the list to OSW workspaces with external app access (public or project group), and that without it every type and access level is returned
async def getAll(
self, current_user: UserInfo, gig_only: bool = False
) -> list[Workspace]:
query = (
select(Workspace).where(
Workspace.tdeiProjectGroupId.in_(current_user.getProjectGroupIds()) # type: ignore[attr-defined]
)
# The list response sends none of these, so don't load them: the
# geometry can be a large multipolygon, and the quest and imagery
# definitions are otherwise joined in for every workspace.
.options(
defer(Workspace.geometry), # pyright: ignore[reportArgumentType]
noload(
Workspace.longFormQuestDef # pyright: ignore[reportArgumentType]
),
noload(Workspace.imageryListDef), # pyright: ignore[reportArgumentType]
)
)
# The mobile apps (ScoutRoute) only show OSW workspaces opened to
# external apps, so filter here instead of sending the rest to discard.
if gig_only:
is_osw = Workspace.type == WorkspaceType.OSW
open_to_apps = (
Workspace.externalAppAccess != ExternalAppsDefinitionType.NONE
)
query = query.where(
is_osw, open_to_apps # pyright: ignore[reportArgumentType]
)
result = await self.session.execute(query)
return list(result.scalars().all())

Expand Down
13 changes: 12 additions & 1 deletion api/src/workspaces/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
File,
Form,
HTTPException,
Query,
Response,
UploadFile,
status,
Expand Down Expand Up @@ -109,18 +110,28 @@ async def save_uploaded_dataset(workspace_id: int, upload: UploadFile) -> Path:
# @test: Test that this method properly handles the case where the workspace does not exist and doesn't include it
# @test: Test that this method properly handles inputs that match the schema in WorkspaceResponse
# @test: Test that this method's results match the values of the fetch-by-workspace-id method below; all workspaces in this list are retrievable via that method
# @test: Test that gig_only=true returns only OSW workspaces with external app access, and that lat, lon and radius are accepted and ignored


# Returns list of workspaces user has access to as JSON payload on success--returns empty JSON list if none
#
# The mobile apps also send lat, lon and radius. Those are deliberately not
# applied: the apps get every workspace in the user's project groups, wherever
# it is. FastAPI ignores undeclared query parameters, so they need no handling.
@router.get("/mine", response_model=list[WorkspaceResponse])
async def get_my_workspaces(
gig_only: bool = Query(
False,
description="Only OSW workspaces open to external apps, the ones the "
"mobile apps (ScoutRoute) can use.",
),
repository: WorkspaceRepository = Depends(get_workspace_repository),
user_repo: UserRepository = Depends(get_user_repository),
project_repo: TaskingProjectRepository = Depends(get_project_repository),
current_user: UserInfo = Depends(validate_token),
) -> list[WorkspaceResponse]:
try:
workspaces = await repository.getAll(current_user)
workspaces = await repository.getAll(current_user, gig_only=gig_only)
workspace_ids = [ws.id for ws in workspaces if ws.id is not None]

projects_counts = await project_repo.get_projects_counts(workspace_ids)
Expand Down
12 changes: 10 additions & 2 deletions tests/integration/test_health.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
"""Integration smoke tests for unauthenticated endpoints."""


async def test_health_check(client):
async def test_health_check(client, monkeypatch):
monkeypatch.setenv("CODE_VERSION", "abc123")
response = await client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
assert response.json() == {"status": "ok", "version": "abc123"}


async def test_health_check_without_code_version(client, monkeypatch):
monkeypatch.delenv("CODE_VERSION", raising=False)
response = await client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok", "version": "unknown"}


async def test_root_redirects_to_docs(client):
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ async def test_response_body_is_streamed_unmodified(client, login, monkeypatch):
assert response.headers["content-type"] == "application/xml"


async def test_proxied_response_is_not_recompressed(client, login, monkeypatch):
# NativeApiGZipMiddleware covers /api/v1/ only; the proxy streams the
# upstream's bytes through as received, even large ones.
body = b"<osm>" + b"<node id='1'/>" * 200 + b"</osm>"
install_osm(
monkeypatch, lambda req: (200, {"content-type": "application/xml"}, body)
)
login(factories.make_user_info(accessible_workspace_ids={"pg": [1]}))

response = await client.get(
"/api/0.6/map", headers={"X-Workspace": "1", "Accept-Encoding": "gzip"}
)

assert response.status_code == 200
assert "content-encoding" not in response.headers
assert response.content == body


# --- /workspace/{id}/ path prefix ------------------------------------------
#
# Workspace selection for third-party clients that cannot set an X-Workspace
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/test_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ async def test_list_my_workspaces(client, login, task_session, osm_session):
assert "updatedAt" in body[0]


async def test_list_my_workspaces_accepts_the_mobile_apps_query(
client, login, task_session
):
# ScoutRoute sends exactly this; lat/lon/radius are deliberately ignored.
login()
task_session.queue(fakes.rows())
response = await client.get(
f"{API}/mine",
params={"lat": 47.6, "lon": -122.3, "radius": 20000, "gig_only": "true"},
)
assert response.status_code == 200
assert response.json() == []


async def test_list_my_workspaces_is_gzipped_when_accepted(
client, login, task_session, osm_session
):
login()
task_session.queue(
fakes.rows(
*(factories.make_workspace(id=i, title=f"Workspace {i}") for i in range(10))
)
)
osm_session.queue(fakes.rows(), fakes.rows())

response = await client.get(f"{API}/mine", headers={"Accept-Encoding": "gzip"})

assert response.status_code == 200
assert response.headers["content-encoding"] == "gzip"
assert len(response.json()) == 10 # httpx decompresses transparently


async def test_list_my_workspaces_rejects_a_non_boolean_gig_only(client, login):
login()
response = await client.get(f"{API}/mine", params={"gig_only": "maybe"})
assert response.status_code == 422


async def test_list_my_workspaces_empty(client, login, task_session):
login()
task_session.queue(fakes.rows())
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/test_sentry_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Tests for `init_sentry` in api/main.py: it must say at startup whether
errors reach Sentry, since the SDK sends nothing, silently, without a DSN."""

import logging

import pytest

import api.main as main


@pytest.fixture
def sentry_calls(monkeypatch):
# Record init/set_tag instead of reconfiguring the real SDK.
calls: dict = {}
monkeypatch.setattr(
main.sentry_sdk, "init", lambda **kwargs: calls.update(init=kwargs)
)
monkeypatch.setattr(
main.sentry_sdk, "set_tag", lambda key, value: calls.update(tag=(key, value))
)
return calls


def test_missing_dsn_logs_a_warning(monkeypatch, caplog, sentry_calls):
monkeypatch.setattr(main.config.settings, "SENTRY_DSN", "")

with caplog.at_level(logging.INFO, logger=main.logger.name):
main.init_sentry()

assert sentry_calls["init"]["dsn"] == ""
warnings = [r for r in caplog.records if r.levelno == logging.WARNING]
assert ["SENTRY_DSN is not set" in r.getMessage() for r in warnings] == [True]


def test_set_dsn_logs_environment_and_release(monkeypatch, caplog, sentry_calls):
monkeypatch.setattr(
main.config.settings, "SENTRY_DSN", "https://key@sentry.example/1"
)
monkeypatch.setenv("ENV", "production")
monkeypatch.setenv("CODE_VERSION", "abc123")

with caplog.at_level(logging.INFO, logger=main.logger.name):
main.init_sentry()

assert sentry_calls["init"]["environment"] == "production"
assert sentry_calls["init"]["release"] == "abc123"
assert sentry_calls["tag"] == ("version", "abc123")
assert not [r for r in caplog.records if r.levelno >= logging.WARNING]
assert "environment='production', release='abc123'" in caplog.text
Loading
Loading