diff --git a/gateway/api/v1/views.py b/gateway/api/v1/views.py index d7ca1ce05..7cb014dec 100644 --- a/gateway/api/v1/views.py +++ b/gateway/api/v1/views.py @@ -82,6 +82,9 @@ def get_serializer_class(self): return v1_serializers.RuntimeJobSerializer def get_queryset(self): + # Allow unauthenticated users to read the swagger documentation + if self.request.user is None or not self.request.user.is_authenticated: + return RuntimeJob.objects.none() return RuntimeJob.objects.all().filter(job__author=self.request.user) diff --git a/gateway/api/views.py b/gateway/api/views.py index 5d7ce40d7..e0f330b9c 100644 --- a/gateway/api/views.py +++ b/gateway/api/views.py @@ -5,6 +5,7 @@ Version views inherit from the different views. """ + import glob import json import logging @@ -132,6 +133,9 @@ def get_serializer_class(self): return self.serializer_class def get_queryset(self): + # Allow unauthenticated users to read the swagger documentation + if self.request.user is None or not self.request.user.is_authenticated: + return Program.objects.none() return ( Program.objects.all().filter(author=self.request.user).order_by("-created") ) @@ -322,7 +326,10 @@ def get_serializer_class(self): return self.serializer_class def get_queryset(self): - return Job.objects.all().filter(author=self.request.user).order_by("-created") + # Allow unauthenticated users to read the swagger documentation + if self.request.user is None or not self.request.user.is_authenticated: + return Job.objects.none() + return (Job.objects.all()).filter(author=self.request.user).order_by("-created") def perform_create(self, serializer): serializer.save(author=self.request.user) diff --git a/gateway/main/settings.py b/gateway/main/settings.py index bf08efb38..2589da95b 100644 --- a/gateway/main/settings.py +++ b/gateway/main/settings.py @@ -9,6 +9,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.1/ref/settings/ """ + import os import os.path import sys @@ -59,6 +60,7 @@ "allauth", "api", "psycopg2", + "drf_yasg", ] MIDDLEWARE = [ @@ -242,6 +244,17 @@ # 'JWT_AUTH_REFRESH_COOKIE': 'gateway-refresh-token', } +SWAGGER_SETTINGS = { + "SECURITY_DEFINITIONS": { + "Bearer Token": { + "type": "apiKey", + "name": "Authorization", + "in": "header", + }, + }, + "USE_SESSION_AUTH": False, +} + SITE_ID = 1 SITE_HOST = os.environ.get("SITE_HOST", "http://localhost:8000") @@ -346,3 +359,8 @@ CSP_SCRIPT_SRC = "'none'" CSP_FRAME_ANCESTORS = "'self'" CSP_OBJECT_SRC = "'self'" +CSP_IMG_SRC = ("'self'", "data:", "https://cdn.redoc.ly") +CSP_STYLE_SRC_ELEM = ("'self'", "'unsafe-inline'") +CSP_SCRIPT_SRC_ELEM = "'self'" +CSP_CONNECT_SRC = "'self'" +CSP_WORKER_SRC = ("'self'", "blob:") diff --git a/gateway/main/urls.py b/gateway/main/urls.py index 52a8cdee3..3180e81b4 100644 --- a/gateway/main/urls.py +++ b/gateway/main/urls.py @@ -13,14 +13,31 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ + from django.conf import settings from django.conf.urls.static import static from django.urls import path, include, re_path from django.views.generic import TemplateView -from rest_framework import routers - +from rest_framework import routers, permissions +from drf_yasg.views import get_schema_view +from drf_yasg import openapi import probes.views +schema = get_schema_view( # pylint: disable=invalid-name + openapi.Info( + title="Gateway API", + default_version="v1", + description="List of available API endpoint for gateway.", + ), + public=True, + permission_classes=[permissions.AllowAny], + # Patterns to be included in the Swagger documentation + patterns=[ + re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")), + # Add other included patterns if necessary + ], +) + router = routers.DefaultRouter() @@ -30,6 +47,18 @@ path("liveness/", probes.views.liveness, name="liveness"), path("", include("django_prometheus.urls")), re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")), + # docs + re_path( + r"^swagger(?P\.json|\.yaml)$", + schema.without_ui(cache_timeout=0), + name="schema-json", + ), + re_path( + r"^swagger/$", + schema.with_ui("swagger", cache_timeout=0), + name="schema-swagger-ui", + ), + re_path(r"^redoc/$", schema.with_ui("redoc", cache_timeout=0), name="schema-redoc"), path( "DomainVerification.html", TemplateView.as_view(template_name="DomainVerification.html"), diff --git a/gateway/requirements.txt b/gateway/requirements.txt index 0e7e134fc..ca988cb66 100644 --- a/gateway/requirements.txt +++ b/gateway/requirements.txt @@ -15,3 +15,4 @@ kubernetes>=26.1.0 opentelemetry-distro>=0.40b0 opentelemetry-exporter-otlp>=1.19.0 django-concurrency>=2.4 +drf-yasg>=1.21.7