Skip to content

Commit 8359740

Browse files
authored
feat: support IAMMigrator config tenant_id and disable urllib3 warn
1 parent 0bf9b33 commit 8359740

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

iam/api/http.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""
33
TencentBlueKing is pleased to support the open source community by making
4-
蓝鲸智云-权限中心Python SDK(iam-python-sdk) available.
4+
蓝鲸智云 - 权限中心 Python SDK(iam-python-sdk) available.
55
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
66
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
77
You may obtain a copy of the License at http://opensource.org/licenses/MIT
@@ -15,6 +15,10 @@
1515

1616
import curlify
1717
import requests
18+
import urllib3
19+
20+
# no more useless warning
21+
urllib3.disable_warnings()
1822

1923
logger = logging.getLogger("iam")
2024

iam/contrib/iam_migration/migrator.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ def __init__(self, migration_json):
3636
self.migration_json = migration_json
3737
self._bk_app_code = getattr(settings, "APP_CODE", "")
3838
self._bk_app_secret = settings.SECRET_KEY
39+
self._bk_app_tenant_id = self.get_tenant_id()
40+
41+
@staticmethod
42+
def get_tenant_id():
43+
"""
44+
获取应用所属的租户 ID
45+
Note: BKPAAS_APP_TENANT_ID 和 BK_APP_TENANT_ID 的含义不一样
46+
BKPAAS_APP_TENANT_ID 是应用的租户模式标识,表示应用是全租户还是单租户
47+
BK_APP_TENANT_ID 是应用所属的租户 ID,表示应用是属于哪个租户的,即由哪个租户产生的
48+
"""
49+
# PaaS 平台上部署运行的应用,会自动内置 BKPAAS_APP_TENANT_ID 环境变量,表示应用是全租户的还是单租户的
50+
tenant_id = os.environ.get("BKPAAS_APP_TENANT_ID")
51+
if tenant_id is not None:
52+
# 空字符串表示全租户应用,则返回 system,因为全租户应用只能在运营租户 (system) 下创建
53+
return tenant_id or "system"
54+
55+
# 如果从环境变量获取不到,即非 PaaS 平台上运行,则需要从配置中获取
56+
# 注意:对于单租户应用,BK_APP_TENANT_ID 可以不设置
57+
# 对于全租户应用,BK_APP_TENANT_ID 必须设置,建议设置为 system
58+
return getattr(settings, "BK_APP_TENANT_ID", "")
3959

4060
def migrate(self):
4161
iam_host = getattr(settings, "BK_IAM_APIGATEWAY_URL", "")
@@ -64,6 +84,6 @@ def migrate(self):
6484
if not ok:
6585
raise exceptions.NetworkUnreachableError("bk iam ping error")
6686

67-
ok = do_migrate.do_migrate(data, iam_host, self._bk_app_code, self._bk_app_secret)
87+
ok = do_migrate.do_migrate(data, iam_host, self._bk_app_code, self._bk_app_secret, self._bk_app_tenant_id)
6888
if not ok:
6989
raise exceptions.MigrationFailError("iam migrate fail")

iam/contrib/iam_migration/utils/do_migrate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import os
1919

2020
import requests
21+
import urllib3
2122

2223
# NOTE: the usage doc https://bk.tencent.com/docs/document/6.0/160/8388
2324

2425
__version__ = "1.0.0"
26+
# no more useless warning
27+
urllib3.disable_warnings()
2528

2629
BK_APIGATEWAY_URL = os.getenv("BK_IAM_APIGATEWAY_URL", "https://bkapi.example.com/api/bk-iam/prod/")
2730

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
requires = [
1010
"six>=1.11.0",
1111
"cachetools>=3.1.1,<6.0",
12-
"requests",
12+
"requests>=2.16.0",
1313
"curlify==2.2.1",
1414
]
1515

0 commit comments

Comments
 (0)