Skip to content

Commit cb23fbb

Browse files
authored
feat: paas-service support py3.11 (TencentBlueKing#193)
1 parent 0197e91 commit cb23fbb

12 files changed

Lines changed: 955 additions & 617 deletions

File tree

sdks/paas-service/CHANGES.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# 版本历史
2-
# 1.1.6
2+
3+
## 2.0.0
4+
- 支持 Python 3.11,移除对 Python 3.6, 3.7 的支持
5+
6+
## 1.1.6
37
- Loosen the version restriction on sub-dep `pyjwt`
48

5-
# 1.1.5
9+
## 1.1.5
610
- 支持通过 name 查询 service instance
711

8-
# 1.1.4
12+
## 1.1.4
913
- 版权申明格式修改
1014

1115
## 1.1.3

sdks/paas-service/paas_service/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
We undertake not to change the open source license (MIT license) applicable
1717
to the current version of the project delivered to anyone in the future.
1818
"""
19-
__version__ = '1.1.6'
19+
__version__ = '2.0.0'
2020

2121
default_app_config = 'paas_service.apps.PaaSServiceConfig'

sdks/paas-service/paas_service/auth/backends.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
class AuthFailedError(Exception):
34-
"""Raised when one authenticater fails"""
34+
"""Raised when one authenticator fails"""
3535

3636

3737
class InstanceAuthFailed(Exception):
@@ -66,8 +66,8 @@ def __init__(self, client: Client, extra_payload: Dict):
6666
self.extra_payload = extra_payload
6767

6868

69-
class JWTClientAuthenticater:
70-
"""Authenticater using JWT clients"""
69+
class JWTClientAuthenticator:
70+
"""Authenticator using JWT clients"""
7171

7272
def __init__(self):
7373
try:
@@ -112,7 +112,7 @@ def parse(self, token: str) -> Tuple[Dict, Client]:
112112

113113
@staticmethod
114114
def _validate_payload(client: Dict, payload: Dict) -> bool:
115-
"""Validates given JWT payload, a valid payload must contains at least 2 fields:
115+
"""Validates given JWT payload, a valid payload must contain at least 2 fields:
116116
'iss' and 'expires_at'.
117117
"""
118118
expires_at = payload.get('expires_at')
@@ -136,21 +136,21 @@ class InstanceAuthBackend:
136136
TOKEN_KEY = 'token'
137137

138138
def __init__(self):
139-
self.authenticaters = [JWTClientAuthenticater()]
139+
self.authenticators = [JWTClientAuthenticator()]
140140

141141
def get_token(self, request: WSGIRequest):
142142
return request.GET.get(self.TOKEN_KEY, None)
143143

144144
def invoke(self, request: WSGIRequest) -> ServiceInstance:
145-
"""Invoke currrent bakend
145+
"""Invoke current backend
146146
147147
:raises: InstanceAuthFailed
148148
"""
149149
token = self.get_token(request)
150150
if not token:
151151
raise InstanceAuthFailed('token parameter is missing')
152152

153-
for auth in self.authenticaters:
153+
for auth in self.authenticators:
154154
try:
155155
result = auth.authenticate(token)
156156
break
@@ -189,7 +189,7 @@ def sign_role_aware_token(client_name: str, role_name: str) -> str:
189189

190190

191191
def _sign_token(client_name: str, additional_payload: Dict) -> str:
192-
"""Sign a new JWT token using given client's crendentials, this token can be used
192+
"""Sign a new JWT token using given client's credentials, this token can be used
193193
for an InstanceAuthBackend's authentication process.
194194
195195
:returns: a JWT token which will expires after 3600 seconds
@@ -199,6 +199,6 @@ def _sign_token(client_name: str, additional_payload: Dict) -> str:
199199
if client_name == client_data['iss']:
200200
payload = {'iss': client_name, 'expires_at': int(time.time()) + 3600}
201201
payload.update(additional_payload)
202-
return jwt.encode(payload, key=client_data['key'], algorithm=client_data['algorithm']).decode()
202+
return jwt.encode(payload, key=client_data['key'], algorithm=client_data['algorithm'])
203203
else:
204204
raise ValueError(f'client name {client_name} is invalid')

sdks/paas-service/paas_service/auth/middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import logging
2020
from typing import Optional
2121

22-
from .backends import AuthFailedError, JWTClientAuthenticater
22+
from .backends import AuthFailedError, JWTClientAuthenticator
2323

2424
logger = logging.getLogger(__name__)
2525

2626

2727
class VerifiedClientMiddleware(object):
28-
"""This middleware will inject request.verifed_client if current request has a valid
28+
"""This middleware will inject request.verified_client if current request has a valid
2929
signed JWT token in request headers
3030
"""
3131

@@ -52,7 +52,7 @@ def __call__(self, request):
5252
request.client = None
5353
if token:
5454
try:
55-
ret = JWTClientAuthenticater().authenticate(token=token)
55+
ret = JWTClientAuthenticator().authenticate(token=token)
5656
request.client = ret.client
5757
except AuthFailedError:
5858
pass

sdks/paas-service/paas_service/urls.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,65 @@
1616
We undertake not to change the open source license (MIT license) applicable
1717
to the current version of the project delivered to anyone in the future.
1818
"""
19-
from django.conf.urls import url
19+
from django.urls import re_path
2020

2121
from . import views
2222

23-
urlpatterns = [url(r"^authenticate/$", views.AuthenticationViewSet.as_view(), name='authenticate')]
23+
urlpatterns = [re_path(r"^authenticate/$", views.AuthenticationViewSet.as_view(), name='authenticate')]
2424

2525
urlpatterns += [
26-
url(r'^meta_info/$', views.SvcBasicViewSet.as_view({'get': 'get_meta_info'}), name='api.services.get_meta_info'),
27-
url(
26+
re_path(r'^meta_info/$', views.SvcBasicViewSet.as_view({'get': 'get_meta_info'}), name='api.services.get_meta_info'),
27+
re_path(
2828
r'^services/$',
2929
views.ServiceManageViewSet.as_view({'get': 'list', 'post': 'create'}),
3030
name='api.services.index',
3131
),
32-
url(
32+
re_path(
3333
r'^services/(?P<service_id>[0-9a-f-]{32,36})/$',
3434
views.ServiceManageViewSet.as_view({'put': 'update'}),
3535
name='api.services.update',
3636
),
37-
url(
37+
re_path(
3838
r'^services/(?P<service_id>[0-9a-f-]{32,36})/instances/(?P<instance_id>[0-9a-f-]{32,36})/$',
3939
views.SvcInstanceViewSet.as_view({'post': 'provision'}),
4040
name='api.services.instances_creation',
4141
),
42-
url(
42+
re_path(
4343
r'^services/(?P<service_id>[0-9a-f-]{32,36})/instances/$',
4444
views.SvcInstanceViewSet.as_view({'get': 'retrieve_by_fields'}),
4545
name='api.services.instances_retrieve_by_fields',
4646
),
47-
url(
47+
re_path(
4848
r'^services/(?P<service_id>[0-9a-f-]{32,36})/client-side-instances/(?P<instance_id>[0-9a-f-]{32,36})/$',
4949
views.ClientSideSvcInstanceViewSet.as_view({'post': 'create'}),
5050
name='api.services.client_side_instances_creation',
5151
),
52-
url(
52+
re_path(
5353
r'^plans/$',
5454
views.PlanManageViewSet.as_view({'post': 'create'}),
5555
name='api.plans.index',
5656
),
57-
url(
57+
re_path(
5858
r'^plans/(?P<plan_id>[0-9a-f-]{32,36})/$',
5959
views.PlanManageViewSet.as_view({'put': 'update'}),
6060
name='api.plans.update',
6161
),
62-
url(
62+
re_path(
6363
r'^instances/(?P<instance_id>[0-9a-f-]{32,36})/$',
6464
views.SvcInstanceViewSet.as_view({'get': 'retrieve', 'patch': 'update', 'delete': 'destroy'}),
6565
name='api.services.instance',
6666
),
67-
url(
67+
re_path(
6868
r'^instances/(?P<instance_id>[0-9a-f-]{32,36})/async_delete$',
6969
views.SvcInstanceViewSet.as_view({'delete': 'async_destroy'}),
7070
name='api.services.instance.async_destroy',
7171
),
72-
url(
72+
re_path(
7373
r'^client-side-instances/(?P<instance_id>[0-9a-f-]{32,36})/',
7474
views.ClientSideSvcInstanceViewSet.as_view({'delete': 'destroy'}),
7575
name='api.services.client_side_instance.destroy',
7676
),
77-
url(
77+
re_path(
7878
r'^instances/(?P<instance_id>[0-9a-f-]{32,36})/config/$',
7979
views.SvcInstanceConfigViewSet.as_view({'get': 'retrieve', 'put': 'update'}),
8080
name='api.services.instance_config',

sdks/paas-service/paas_service/views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def list(self, request):
8080
def update(self, request, service_id):
8181
service = get_object_or_404(Service, pk=service_id)
8282
slz = serializers.ServiceUpsertSLZ(data=request.data, instance=service, partial=True)
83-
slz.is_valid(True)
83+
slz.is_valid(raise_exception=True)
8484
slz.save()
8585
return Response(status=status.HTTP_204_NO_CONTENT)
8686

8787
@verified_client_role_require('internal_platform')
8888
def create(self, request):
8989
slz = serializers.ServiceUpsertSLZ(data=request.data)
90-
slz.is_valid(True)
90+
slz.is_valid(raise_exception=True)
9191
slz.save()
9292
return Response(status=status.HTTP_201_CREATED)
9393

@@ -104,16 +104,16 @@ class PlanManageViewSet(viewsets.ViewSet):
104104
@verified_client_role_require('internal_platform')
105105
def create(self, request):
106106
slz = serializers.PlanUpsertSLZ(data=request.data)
107-
slz.is_valid(True)
107+
slz.is_valid(raise_exception=True)
108108
slz.save()
109109
return Response(status=status.HTTP_201_CREATED)
110110

111111
@verified_client_role_require('internal_platform')
112112
def update(self, request, plan_id):
113113
plan = get_object_or_404(Plan, pk=plan_id)
114114
slz = serializers.PlanUpsertSLZ(data=request.data, instance=plan, partial=True)
115-
slz.is_valid(True)
116-
plan = slz.save()
115+
slz.is_valid(raise_exception=True)
116+
slz.save()
117117
return Response(status=status.HTTP_204_NO_CONTENT)
118118

119119
@verified_client_role_require('internal_platform')

0 commit comments

Comments
 (0)