Skip to content

Commit 67dd553

Browse files
authored
feat(apigw_manager/config): add BK_APP_TENANT_ID (TencentBlueKing#227)
1 parent 7b35bb6 commit 67dd553

7 files changed

Lines changed: 105 additions & 45 deletions

File tree

sdks/apigw-manager/CHANGE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## Change logs
22

3+
### 4.1.0
4+
5+
- [breaking change] 做了多租户改造的全租户应用必须显式设置 `BK_APP_TENANT_ID=system` (环境变量或django settings)
6+
7+
### 4.0.1
8+
9+
- 支持多租户
10+
311
### 4.0.0
412

513
- [breaking change] drop support for python 3.6/3.7, request >=3.8 and < 3.13

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.4"
3+
version = "4.1.0"
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/utils.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
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 os
1213
import re
1314
import zipfile
1415

1516
import yaml
16-
from bkapi_client_core.config import SettingKeys, settings
17-
from packaging.version import Version as _Version, InvalidVersion
17+
from packaging.version import InvalidVersion
18+
from packaging.version import Version as _Version
1819

1920
from apigw_manager.core import configuration
21+
from bkapi_client_core.config import SettingKeys, settings
2022

2123

2224
def get_configuration(**kwargs):
@@ -27,6 +29,7 @@ def get_configuration(**kwargs):
2729
("BK_APP_CODE", "bk_app_code"),
2830
("BK_APP_SECRET", "bk_app_secret"),
2931
("BK_APIGW_JWT_PROVIDER_CLS", "jwt_provider_cls"),
32+
("BK_APP_TENANT_ID", "bk_app_tenant_id"),
3033
]
3134

3235
for attr, key in settings_mappings:
@@ -107,7 +110,7 @@ def _get_archived_files(cls, path):
107110
for root, _, files in os.walk(path):
108111
for name in files:
109112
file_path = os.path.join(root, name)
110-
path_to_name[file_path] = file_path[len(path):]
113+
path_to_name[file_path] = file_path[len(path) :]
111114

112115
return path_to_name
113116

sdks/apigw-manager/src/apigw_manager/core/configuration.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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
"""
1111

1212

@@ -23,6 +23,7 @@ def __init__(
2323
api_cache=None,
2424
access_token=None,
2525
jwt_provider_cls=None,
26+
bk_app_tenant_id=None,
2627
*args,
2728
**kwargs,
2829
):
@@ -34,3 +35,4 @@ def __init__(
3435
self.api_cache = api_cache
3536
self.access_token = access_token
3637
self.jwt_provider_cls = jwt_provider_cls
38+
self.bk_app_tenant_id = bk_app_tenant_id

sdks/apigw-manager/src/apigw_manager/core/handler.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
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 json
1213
import logging
14+
import os
1315
import typing
1416

1517
from bkapi.bk_apigateway.client import Client as BKAPIGatewayClient
16-
from bkapi_client_core.exceptions import ResponseError
1718

1819
from apigw_manager.core.exceptions import ApiException, ApiResponseError, ApiResultError
20+
from bkapi_client_core.exceptions import ResponseError
1921

2022
if typing.TYPE_CHECKING:
2123
from apigw_manager.core import configuration # noqa
@@ -75,6 +77,28 @@ def _call_with_cache(self, operation, **kwargs):
7577

7678
return result
7779

80+
def _get_tenant_id(self):
81+
"""
82+
获取应用所属的租户 ID
83+
Note: BKPAAS_APP_TENANT_ID 和 BK_APP_TENANT_ID 的含义不一样
84+
BKPAAS_APP_TENANT_ID 是应用的租户模式标识,表示应用是全租户还是单租户
85+
BK_APP_TENANT_ID 是应用所属的租户 ID,表示应用是属于哪个租户的,即由哪个租户产生的
86+
"""
87+
# [大多数是外部 SaaS 场景] PaaS 平台上部署运行的应用,会自动内置 BKPAAS_APP_TENANT_ID 环境变量,表示应用是全租户的还是单租户的
88+
paas_app_tenant_id = os.environ.get("BKPAAS_APP_TENANT_ID")
89+
if paas_app_tenant_id is not None:
90+
# 空字符串表示全租户应用,则返回 system,因为全租户应用只能在运营租户 (system) 下创建
91+
return paas_app_tenant_id or "system"
92+
93+
# 对于单租户应用,BK_APP_TENANT_ID 可以不设置; 对于全租户应用,BK_APP_TENANT_ID 必须设置,建议设置为 system
94+
bk_app_tenant_id = self.config.bk_app_tenant_id or ""
95+
logger.warning(
96+
"the [X-Bk-Tenant-Id=%s], if the syncing to apigateway failed, and your app(%s) is a global tenant app, please set the environment variable BK_APP_TENANT_ID to `system` (or set django settings.BK_APP_TENANT_ID to `system`) and try again",
97+
bk_app_tenant_id,
98+
self.config.bk_app_code,
99+
)
100+
return bk_app_tenant_id
101+
78102
def _call(self, operation, files=None, **kwargs):
79103
"""Call the API instance"""
80104
data = {
@@ -84,7 +108,7 @@ def _call(self, operation, files=None, **kwargs):
84108
"X-Bkapi-Authorization": kwargs.pop("x_bkapi_authorization", self._get_bkapi_authorization()),
85109
# the header is required by the API gateway plugin bk-tenant-validate, for global tenant app!
86110
# so we set it to system, it would not be used in the gateway
87-
"X-Bk-Tenant-Id": "system",
111+
"X-Bk-Tenant-Id": self._get_tenant_id(),
88112
},
89113
"files": files,
90114
}

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

Lines changed: 20 additions & 10 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 os
1213
import tempfile
1314
import zipfile
@@ -48,6 +49,17 @@ def test_jwt_provider_cls(self, settings, faker):
4849

4950
assert configuration.jwt_provider_cls == settings.BK_APIGW_JWT_PROVIDER_CLS
5051

52+
def test_bk_app_tenant_id(self, settings, faker):
53+
settings.BK_APP_TENANT_ID = faker.color()
54+
configuration = get_configuration()
55+
assert configuration.bk_app_tenant_id == settings.BK_APP_TENANT_ID
56+
57+
def test_bk_app_tenant_id_by_env(self, settings, faker, monkeypatch):
58+
tenant_id = faker.color()
59+
monkeypatch.setenv("BK_APP_TENANT_ID", tenant_id)
60+
configuration = get_configuration()
61+
assert configuration.bk_app_tenant_id == tenant_id
62+
5163
@pytest.mark.parametrize(
5264
("kwargs", "expected"),
5365
[
@@ -144,7 +156,6 @@ def test_get_archived_files(self):
144156

145157

146158
class TestVersion:
147-
148159
def test_valid_versions(self):
149160
valid_versions = [
150161
"1.0.0",
@@ -155,11 +166,10 @@ def test_valid_versions(self):
155166
"6.0.0-alpha+build.1",
156167
"7.0.0-feature-layered-alpha2",
157168
"8.0.0-feature-layered-alpha.2",
158-
"3.14.1-feature-layered-alpha2"
169+
"3.14.1-feature-layered-alpha2",
159170
]
160171
for version in valid_versions:
161172
try:
162173
parse_version(version)
163174
except InvalidVersion:
164175
pytest.fail(f"Valid version '{version}' raised InvalidVersion")
165-

sdks/apigw-manager/tests/apigw_manager/core/test_hander.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
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
12-
from bkapi_client_core.exceptions import HTTPResponseError
1313
from faker import Faker
1414

1515
from apigw_manager.core.exceptions import ApiResponseError
1616
from apigw_manager.core.handler import Handler
17+
from bkapi_client_core.exceptions import HTTPResponseError
1718

1819

1920
@pytest.fixture()
@@ -119,3 +120,15 @@ def test_call_request_error_with_no_json(self, handler: Handler, operation, mock
119120

120121
with pytest.raises(ApiResponseError):
121122
handler._call(operation)
123+
124+
def test_get_tenant_id_from_config(self, handler: Handler, mocker):
125+
handler.config.bk_app_tenant_id = "123"
126+
assert handler._get_tenant_id() == "123"
127+
128+
def test_get_tenant_id_from_env(self, handler: Handler, monkeypatch):
129+
monkeypatch.setenv("BKPAAS_APP_TENANT_ID", "123")
130+
assert handler._get_tenant_id() == "123"
131+
132+
def test_get_tenant_id_from_env_default_system(self, handler: Handler, monkeypatch):
133+
monkeypatch.setenv("BKPAAS_APP_TENANT_ID", "")
134+
assert handler._get_tenant_id() == "system"

0 commit comments

Comments
 (0)