diff --git a/entrypoint.sh b/entrypoint.sh index fb94869..23d21dc 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -16,16 +16,35 @@ if [ ! -f "$DB_PATH" ] && [ -f "$LEGACY_DB_PATH" ]; then cp "$LEGACY_DB_PATH" "$DB_PATH" fi -if [ ! -f "$DB_PATH" ] && [ "${LOAD_FIXTURE_ON_EMPTY_DB:-true}" = "true" ]; then - python manage.py migrate - if [ -f "$DUMP_SRC" ]; then - TMP_DUMP="$(mktemp)" - python /app/scripts/rewrite_dumps.py "$DUMP_SRC" "$TMP_DUMP" - python manage.py loaddata "$TMP_DUMP" - rm -f "$TMP_DUMP" +python manage.py migrate + +should_load_fixture=0 +if [ "${LOAD_FIXTURE_ON_EMPTY_DB:-true}" = "true" ] && [ -f "$DUMP_SRC" ]; then + if [ ! -s "$DB_PATH" ]; then + should_load_fixture=1 + else + entity_conf_count="$(python manage.py shell -c ' +from django.db import connection +table = "spid_cie_oidc_entity_federationentityconfiguration" +with connection.cursor() as cursor: + tables = connection.introspection.table_names(cursor) + if table not in tables: + print(0) + else: + cursor.execute(f"SELECT COUNT(*) FROM {table}") + print(cursor.fetchone()[0]) +')" + if [ "${entity_conf_count:-0}" = "0" ]; then + should_load_fixture=1 + fi fi -else - python manage.py migrate +fi + +if [ "$should_load_fixture" = "1" ]; then + TMP_DUMP="$(mktemp)" + python /app/scripts/rewrite_dumps.py "$DUMP_SRC" "$TMP_DUMP" + python manage.py loaddata "$TMP_DUMP" + rm -f "$TMP_DUMP" fi python /app/scripts/normalize_db_urls.py "$DB_PATH" diff --git a/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/http_client.py b/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/http_client.py index 152fa0d..c012d54 100644 --- a/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/http_client.py +++ b/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/http_client.py @@ -2,12 +2,26 @@ import aiohttp import asyncio +def _body_preview(body: str, limit: int = 256) -> str: + if not body: + return "" + preview = body[:limit].strip() + if len(body) > limit: + return f"{preview}..." + return preview + + async def fetch(session, url, httpc_params: dict = {}): async with session.get(url, **httpc_params.get("connection", {})) as response: + body = await response.text() if response.status != 200: # pragma: no cover - # response.raise_for_status() - return "" - return await response.text() + raise RuntimeError( + "HTTP GET failed for " + f"{url}: status={response.status} " + f"content_type={response.headers.get('Content-Type', '')} " + f"body_preview={_body_preview(body)}" + ) + return body async def fetch_all(session, urls, httpc_params): diff --git a/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/statements.py b/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/statements.py index 0ccc4d5..aa6cb55 100644 --- a/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/statements.py +++ b/vendor/spid-cie-oidc-django/spid_cie_oidc/entity/statements.py @@ -39,7 +39,15 @@ def get_http_url(urls: list, httpc_params: dict = {}) -> list: responses = [] for i in urls: res = requests.get(i, **httpc_params) # nosec - B113 - responses.append(res.content.decode()) + body = res.content.decode() + if res.status_code != 200: + raise RuntimeError( + "HTTP GET failed for " + f"{i}: status={res.status_code} " + f"content_type={res.headers.get('Content-Type', '')} " + f"body_preview={body[:256].strip()}" + ) + responses.append(body) else: responses = asyncio.run(http_get(urls, httpc_params)) # pragma: no cover return responses