Skip to content

Commit 100b22d

Browse files
authored
fix(apigw/authentication.py): fix missing tenant_id in UserBackend (TencentBlueKing#221)
1 parent 3a5cc46 commit 100b22d

4 files changed

Lines changed: 47 additions & 22 deletions

File tree

.github/workflows/apigw-manager.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
22-
os: [ubuntu-20.04, macos-13, windows-latest]
22+
os: [ubuntu-22.04, macos-13, windows-latest]
2323
steps:
2424
- uses: actions/checkout@v4
2525

sdks/apigw-manager/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "apigw-manager"
3-
version = "4.0.2"
3+
version = "4.0.3"
44
description = "The SDK for managing blueking gateway resource."
55
readme = "README.md"
66
authors = ["blueking <blueking@tencent.com>"]

sdks/apigw-manager/src/apigw_manager/apigw/authentication.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4-
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5-
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
7-
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8-
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9-
* specific language governing permissions and limitations under the License.
3+
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4+
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5+
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
1010
"""
11+
1112
import logging
1213
from collections import namedtuple
1314
from typing import ClassVar, Type
@@ -162,7 +163,7 @@ def make_anonymous_user(self, bk_username=None):
162163
user.tenant_id = "" # type: ignore
163164
return user
164165

165-
def authenticate(self, request, gateway_name, bk_username, tenant_id, verified, **credentials):
166+
def authenticate(self, request, gateway_name, bk_username, verified, tenant_id="", **credentials):
166167
if not verified:
167168
return self.make_anonymous_user(bk_username=bk_username)
168169

sdks/apigw-manager/tests/apigw_manager/apigw/test_authentication.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4-
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5-
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
7-
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8-
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9-
* specific language governing permissions and limitations under the License.
3+
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4+
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5+
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
1010
"""
11+
1112
import pytest
1213
from django.contrib.auth.models import AnonymousUser
1314
from django.core.cache import caches
@@ -16,6 +17,7 @@
1617
from apigw_manager.apigw import authentication, providers
1718
from apigw_manager.apigw.providers import CachePublicKeyProvider, DefaultJWTProvider, SettingsPublicKeyProvider
1819

20+
1921
@pytest.fixture()
2022
def mock_response(mocker):
2123
return mocker.MagicMock()
@@ -70,9 +72,9 @@ def jwt_request(fake_gateway_name, jwt_decoded, mock_request):
7072

7173
@pytest.fixture()
7274
def invalid_apigw_request(mock_request):
73-
mock_request.META[
74-
"HTTP_X_BKAPI_JWT"
75-
] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibHljIn0.iHy-g0R-q3sVnO16gTHV0FAIViEuKMGCtNLNVYSJX5c"
75+
mock_request.META["HTTP_X_BKAPI_JWT"] = (
76+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibHljIn0.iHy-g0R-q3sVnO16gTHV0FAIViEuKMGCtNLNVYSJX5c"
77+
)
7678

7779
return mock_request
7880

@@ -243,16 +245,38 @@ def _setup_backend(self):
243245

244246
def test_authenticate_user(self, mock_request):
245247
user = self.backend.authenticate(
246-
mock_request, gateway_name="test", bk_username="admin", tenant_id="system", verified=True
248+
mock_request,
249+
gateway_name="test",
250+
bk_username="admin",
251+
verified=True,
252+
tenant_id="system",
253+
)
254+
assert not isinstance(user, AnonymousUser)
255+
assert user.username == "admin"
256+
assert user.is_authenticated is True
257+
assert user.tenant_id == "system"
258+
259+
def test_authenticate_user_no_tenant_id(self, mock_request):
260+
user = self.backend.authenticate(
261+
mock_request,
262+
gateway_name="test",
263+
bk_username="admin",
264+
verified=True,
247265
)
248266
assert not isinstance(user, AnonymousUser)
249267
assert user.username == "admin"
250268
assert user.is_authenticated is True
269+
assert user.tenant_id == ""
251270

252271
def test_authenticate_anonymous_user(self, mock_request):
253272
user = self.backend.authenticate(
254-
mock_request, gateway_name="test", bk_username="admin", tenant_id="system", verified=False
273+
mock_request,
274+
gateway_name="test",
275+
bk_username="admin",
276+
verified=False,
277+
tenant_id="system",
255278
)
256279
assert isinstance(user, AnonymousUser)
257280
assert user.username == "admin"
258281
assert user.is_authenticated is False
282+
assert user.tenant_id == ""

0 commit comments

Comments
 (0)