Skip to content

Commit ba91ca4

Browse files
authored
feat: support multi tenant and [breakchange] remove direct calls and calls to IAM through ESB, allowing only apigateway call methods
1 parent 943ab4e commit ba91ca4

File tree

14 files changed

+231
-359
lines changed

14 files changed

+231
-359
lines changed

.github/workflows/flake8_and_black.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ on:
1212
jobs:
1313
build:
1414

15-
runs-on: ubuntu-latest
15+
runs-on: ubuntu-22.04
1616

1717
steps:
1818
- uses: actions/checkout@v2
19-
- name: Set up Python 3.6
19+
- name: Set up Python 3.11
2020
uses: actions/setup-python@v2
2121
with:
22-
python-version: 3.6.15
22+
python-version: 3.11.10
2323
- name: Install dependencies
2424
run: |
2525
python -m pip install --upgrade pip

.github/workflows/unittest.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ on:
1212
jobs:
1313
build:
1414

15-
runs-on: ubuntu-latest
15+
runs-on: ubuntu-22.04
1616

1717
steps:
1818
- uses: actions/checkout@v2
19-
- name: Set up Python 3.6
19+
- name: Set up Python 3.11
2020
uses: actions/setup-python@v2
2121
with:
22-
python-version: 3.6.15
22+
python-version: 3.11.10
2323
- name: Install dependencies
2424
run: |
2525
python -m pip install --upgrade pip

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
fail_fast: true
22
repos:
33
- repo: https://github.com/timothycrosley/isort
4-
rev: 5.7.0
4+
rev: 5.12.0
55
hooks:
66
- id: isort
77
additional_dependencies: [toml]
88
- repo: https://github.com/psf/black
9-
rev: 20.8b1
9+
rev: 22.3.0
1010
hooks:
1111
- id: black
1212
args: [--config=./pyproject.toml]

iam/__version__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# -*- coding: utf-8 -*-
2-
3-
__version__ = "1.3.6"
2+
__version__ = "2.0.0"

iam/api/client.py

Lines changed: 62 additions & 111 deletions
Large diffs are not rendered by default.

iam/contrib/iam_migration/migrator.py

Lines changed: 4 additions & 10 deletions
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
@@ -38,15 +38,9 @@ def __init__(self, migration_json):
3838
self._bk_app_secret = settings.SECRET_KEY
3939

4040
def migrate(self):
41-
iam_host = ""
42-
USE_APIGATEWAY = getattr(settings, "BK_IAM_USE_APIGATEWAY", False)
43-
if USE_APIGATEWAY:
44-
do_migrate.enable_use_apigateway()
45-
iam_host = getattr(settings, "BK_IAM_APIGATEWAY_URL", "")
46-
if iam_host == "":
47-
raise exceptions.MigrationFailError("settings.BK_IAM_APIGATEWAY_URL should be set")
48-
else:
49-
iam_host = settings.BK_IAM_INNER_HOST
41+
iam_host = getattr(settings, "BK_IAM_APIGATEWAY_URL", "")
42+
if iam_host == "":
43+
raise exceptions.MigrationFailError("settings.BK_IAM_APIGATEWAY_URL should be set")
5044

5145
# only trigger migrator at db migrate
5246
if "migrate" not in sys.argv:

iam/contrib/iam_migration/utils/do_migrate.py

Lines changed: 20 additions & 49 deletions
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
@@ -19,28 +19,21 @@
1919

2020
import requests
2121

22-
2322
# NOTE: the usage doc https://bk.tencent.com/docs/document/6.0/160/8388
2423

2524
__version__ = "1.0.0"
2625

27-
BK_IAM_HOST = os.getenv("BK_IAM_V3_INNER_HOST", "http://bkiam.service.consul:5001")
28-
USE_APIGATEWAY = os.getenv("BK_IAM_USE_APIGATEWAY", "false").lower() == "true"
26+
BK_APIGATEWAY_URL = os.getenv("BK_IAM_APIGATEWAY_URL", "https://bkapi.example.com/api/bk-iam/prod/")
2927

3028
APP_CODE = ""
3129
APP_SECRET = ""
3230
data_file = ""
3331

3432

3533
# =================== load json ===================
36-
def enable_use_apigateway():
37-
global USE_APIGATEWAY
38-
USE_APIGATEWAY = True
39-
40-
4134
def load_data(filename):
4235
"""
43-
解析JSON数据文件
36+
解析 JSON 数据文件
4437
"""
4538
data = {}
4639
try:
@@ -136,23 +129,21 @@ def http_delete(url, data, headers=None, verify=False, cert=None, timeout=None,
136129

137130

138131
class Client(object):
139-
def __init__(self, app_code, app_secret, bk_iam_host):
132+
def __init__(self, app_code, app_secret, bk_apigateway_url):
140133
self.app_code = app_code
141134
self.app_secret = app_secret
142-
self.bk_iam_host = bk_iam_host
135+
self.bk_apigateway_url = bk_apigateway_url.rstrip("/")
143136
self.system_id_set = set()
144137
self.resource_id_set = set()
145138
self.action_id_set = set()
146139

147140
# 调用权限中心方法
148141
def _call_iam_api(self, http_func, path, data):
149-
headers = {"X-BK-APP-CODE": self.app_code, "X-BK-APP-SECRET": self.app_secret}
150-
if USE_APIGATEWAY:
151-
headers = {
152-
"X-Bkapi-Authorization": json.dumps({"bk_app_code": self.app_code, "bk_app_secret": self.app_secret}),
153-
}
142+
headers = {
143+
"X-Bkapi-Authorization": json.dumps({"bk_app_code": self.app_code, "bk_app_secret": self.app_secret}),
144+
}
154145

155-
url = "{host}{path}".format(host=self.bk_iam_host, path=path)
146+
url = "{host}{path}".format(host=self.bk_apigateway_url, path=path)
156147
ok, _data = http_func(url, data, headers=headers)
157148
# TODO: add debug here
158149
if not ok:
@@ -209,7 +200,7 @@ def _call_iam_api(self, http_func, path, data):
209200
"upsert_feature_shield_rules": "update_feature_shield_rules",
210201
"add_custom_frontend_settings": "add_custom_frontend_settings",
211202
"update_custom_frontend_settings": "update_custom_frontend_settings",
212-
"upsert_custom_frontend_settings": "update_custom_frontend_settings"
203+
"upsert_custom_frontend_settings": "update_custom_frontend_settings",
213204
}
214205

215206
"""
@@ -561,13 +552,13 @@ def setup_models(self, system_id_set, resource_id_set, action_id_set, instance_s
561552
# ---------- ping
562553

563554

564-
def api_ping(bk_iam_host):
565-
url = "{host}{path}".format(host=bk_iam_host, path="/ping")
555+
def api_ping(bk_apigateway_url):
556+
url = "{host}{path}".format(host=bk_apigateway_url, path="/ping")
566557
ok, data = http_get(url, None, timeout=5)
567558
return ok, data
568559

569560

570-
def do_migrate(data, bk_iam_host=BK_IAM_HOST, app_code=APP_CODE, app_secret=APP_SECRET):
561+
def do_migrate(data, bk_apigateway_url=BK_APIGATEWAY_URL, app_code=APP_CODE, app_secret=APP_SECRET):
571562
system_id = data.get("system_id")
572563
if not system_id:
573564
print("invald json. [system_id] required, and should not be empty")
@@ -580,7 +571,7 @@ def do_migrate(data, bk_iam_host=BK_IAM_HOST, app_code=APP_CODE, app_secret=APP_
580571

581572
print("do migrate")
582573

583-
client = Client(app_code, app_secret, bk_iam_host)
574+
client = Client(app_code, app_secret, bk_apigateway_url)
584575

585576
# 1. query all data of the system
586577
system_ids, resource_type_ids, action_ids, instance_selection_ids = client.query_all_models(system_id)
@@ -619,11 +610,8 @@ def do_migrate(data, bk_iam_host=BK_IAM_HOST, app_code=APP_CODE, app_secret=APP_
619610
p.add_argument(
620611
"-t",
621612
action="store",
622-
dest="bk_iam_host",
623-
help=(
624-
"bk_iam_host, i.e: http://iam.service.consul;"
625-
"you can use bk_apigateway_url here, set with the '--apigateway' "
626-
),
613+
dest="bk_apigateway_url",
614+
help=("bk_apigateway_url, i.e: http://bkapi.example.com/api/bk-iam/prod/;"),
627615
required=True,
628616
)
629617
p.add_argument(
@@ -636,34 +624,17 @@ def do_migrate(data, bk_iam_host=BK_IAM_HOST, app_code=APP_CODE, app_secret=APP_
636624
p.add_argument("-a", action="store", dest="app_code", help="app code", required=True)
637625
p.add_argument("-s", action="store", dest="app_secret", help="app secret", required=True)
638626

639-
p.add_argument(
640-
"--apigateway",
641-
action="store_true",
642-
dest="use_apigateway",
643-
help="you can use bk_apigateway_url in '-t', should set this flag",
644-
)
645627
args = p.parse_args()
646628

647-
BK_IAM_HOST = args.bk_iam_host.rstrip("/")
648-
USE_APIGATEWAY = args.use_apigateway
649-
if USE_APIGATEWAY:
650-
print(
651-
"use apigateway:",
652-
args.use_apigateway,
653-
", please make sure '-t %s' is a valid bk_apigateway_url" % args.bk_iam_host,
654-
)
655-
656-
if not BK_IAM_HOST.startswith("http://"):
657-
BK_IAM_HOST = "http://%s" % BK_IAM_HOST
658-
659629
data_file = args.json_data_file
660630
APP_CODE = args.app_code
661631
APP_SECRET = args.app_secret
632+
BK_APIGATEWAY_URL = args.bk_apigateway_url.rstrip("/")
662633

663634
# test ping
664-
ok, _ = api_ping(BK_IAM_HOST)
635+
ok, _ = api_ping(BK_APIGATEWAY_URL)
665636
if not ok:
666-
print("iam service is not available: %s" % BK_IAM_HOST)
637+
print("iam service is not available: %s" % BK_APIGATEWAY_URL)
667638
exit(1)
668639

669640
print("start migrate [%s]" % data_file)
@@ -673,7 +644,7 @@ def do_migrate(data, bk_iam_host=BK_IAM_HOST, app_code=APP_CODE, app_secret=APP_
673644
if not data:
674645
exit(1)
675646

676-
ok = do_migrate(data, BK_IAM_HOST, APP_CODE, APP_SECRET)
647+
ok = do_migrate(data, BK_APIGATEWAY_URL, APP_CODE, APP_SECRET)
677648
if not ok:
678649
print("do migrate [%s] fail" % data_file)
679650
exit(1)

0 commit comments

Comments
 (0)