diff --git a/build/test/test_conf.py b/build/test/test_conf.py deleted file mode 100644 index 4e13cdb..0000000 --- a/build/test/test_conf.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright (C) 2015 The Sipwise Team - http://sipwise.com -# -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by the Free -# Software Foundation, either version 3 of the License, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License along -# with this program. If not, see . -from django.test import SimpleTestCase - - -class TestBuildConf(SimpleTestCase): - def test_django_settings(self): - from django.conf import settings - - self.assertEqual(settings.BUILD_KEY_AUTH, True) - - def test_build_settings(self): - from build.conf import settings - - self.assertEqual(settings.BUILD_KEY_AUTH, True) diff --git a/build/views.py b/build/views.py index cb26318..fce132c 100644 --- a/build/views.py +++ b/build/views.py @@ -19,23 +19,14 @@ from rest_framework import generics from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView -from rest_framework_api_key.permissions import HasAPIAccess +from rest_framework_api_key.permissions import HasAPIKey from . import models from . import serializers from . import tasks -from .conf import settings from repoapi.serializers import JenkinsBuildInfoSerializer as JBISerializer -class BuildAccess(HasAPIAccess, IsAuthenticated): - def has_permission(self, request, view): - res = IsAuthenticated.has_permission(self, request, view) - if settings.BUILD_KEY_AUTH and not res: - res = HasAPIAccess.has_permission(self, request, view) - return res - - class BuildReleaseFilter(django_filters.FilterSet): class Meta: model = models.BuildRelease @@ -46,14 +37,14 @@ class BuildReleaseFilter(django_filters.FilterSet): class BuildReleaseList(generics.ListCreateAPIView): - permission_classes = (BuildAccess,) + permission_classes = [HasAPIKey | IsAuthenticated] queryset = models.BuildRelease.objects.all().order_by("id") serializer_class = serializers.BuildReleaseSerializer filter_class = BuildReleaseFilter class BuildReleaseDetail(generics.RetrieveDestroyAPIView): - permission_classes = (BuildAccess,) + permission_classes = [HasAPIKey | IsAuthenticated] queryset = models.BuildRelease.objects.all().order_by("id") serializer_class = serializers.BuildReleaseSerializer @@ -78,7 +69,7 @@ class BuildReleaseDetail(generics.RetrieveDestroyAPIView): class BuildProject(APIView): - permission_classes = (BuildAccess,) + permission_classes = [HasAPIKey | IsAuthenticated] def post(self, request, release_uuid, project): br = get_object_or_404(models.BuildRelease, uuid=release_uuid) diff --git a/release_dashboard/views/api.py b/release_dashboard/views/api.py index af6ad19..b04bede 100644 --- a/release_dashboard/views/api.py +++ b/release_dashboard/views/api.py @@ -1,4 +1,4 @@ -# Copyright (C) 2020 The Sipwise Team - http://sipwise.com +# Copyright (C) 2020-2022 The Sipwise Team - http://sipwise.com # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free @@ -13,14 +13,15 @@ # You should have received a copy of the GNU General Public License along # with this program. If not, see . from django.http import JsonResponse +from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView +from rest_framework_api_key.permissions import HasAPIKey from .. import tasks -from build.views import BuildAccess class RefreshGerritInfo(APIView): - permission_classes = (BuildAccess,) + permission_classes = [HasAPIKey | IsAuthenticated] def post(self, request): res = tasks.gerrit_fetch_all.delay() diff --git a/repoapi/settings/common.py b/repoapi/settings/common.py index c837bd9..f1626ad 100644 --- a/repoapi/settings/common.py +++ b/repoapi/settings/common.py @@ -182,3 +182,5 @@ CELERY_TASK_SERIALIZER = "json" CELERY_RESULT_SERIALIZER = "json" CELERY_ACCEPT_CONTENT = ["application/json"] CELERY_RESULT_BACKEND = "django-db" + +API_KEY_CUSTOM_HEADER = "HTTP_API_KEY" diff --git a/repoapi/settings/prod.py b/repoapi/settings/prod.py index 98bd238..8e423ce 100644 --- a/repoapi/settings/prod.py +++ b/repoapi/settings/prod.py @@ -93,7 +93,6 @@ WORKFRONT_CREDENTIALS = BASE_DIR / "/etc/jenkins_jobs/workfront.ini" WORKFRONT_NOTE = True # build app -BUILD_KEY_AUTH = True BUILD_REPOS_SCRIPTS_CONFIG_DIR = Path( "/usr/share/sipwise-repos-scripts/config" ) diff --git a/repoapi/settings/test.py b/repoapi/settings/test.py index ec980f0..a105ee4 100644 --- a/repoapi/settings/test.py +++ b/repoapi/settings/test.py @@ -94,7 +94,6 @@ RELEASE_DASHBOARD_DOCKER_IMAGES = { } # build app -BUILD_KEY_AUTH = True BUILD_REPOS_SCRIPTS_CONFIG_DIR = BASE_DIR.joinpath( "build", "fixtures", "config" ) diff --git a/repoapi/test/base.py b/repoapi/test/base.py index 0c66afc..a8eb338 100644 --- a/repoapi/test/base.py +++ b/repoapi/test/base.py @@ -20,7 +20,6 @@ from tempfile import mkdtemp from django.test import override_settings from django.test import TestCase from rest_framework.test import APITestCase -from rest_framework_api_key.helpers import generate_key from rest_framework_api_key.models import APIKey JBI_BASEDIR = Path(mkdtemp(dir=os.environ.get("RESULTS"))) @@ -49,7 +48,5 @@ class APIAuthenticatedTestCase(BaseTest, APITestCase): def setUp(self): super(APIAuthenticatedTestCase, self).setUp() - self.app_key = APIKey.objects.create( - name=self.APP_NAME, key=generate_key() - ) - self.client.credentials(HTTP_API_KEY=self.app_key.key) + self.app_key, key = APIKey.objects.create_key(name=self.APP_NAME) + self.client.credentials(HTTP_API_KEY=key) diff --git a/requirements/common.txt b/requirements/common.txt index 5219cdb..943bfbb 100644 --- a/requirements/common.txt +++ b/requirements/common.txt @@ -12,8 +12,8 @@ django-jsonify django-structlog django-timezone-field djangorestframework>=3.6 +djangorestframework-api-key==2.* drf-spectacular -drfapikey flower>=0.9.5 markdown natsort diff --git a/t/Dockerfile b/t/Dockerfile index 7cfccc8..9da10cc 100644 --- a/t/Dockerfile +++ b/t/Dockerfile @@ -5,7 +5,7 @@ FROM docker.mgm.sipwise.com/sipwise-bullseye:latest # is updated with the current date. It will force refresh of all # of the base images and things like `apt-get update` won't be using # old cached versions when the Dockerfile is built. -ENV REFRESHED_AT 2022-05-26 +ENV REFRESHED_AT 2022-05-27 RUN apt-get update && apt-get install --assume-yes python3 python3-dev \ python3-pytest python3-pytest-pep8 \