Skip to content

Commit 081c25f

Browse files
authored
Merge pull request #81 from TencentBlueKing/develop
v1.3.1
2 parents a313b3a + 3678cde commit 081c25f

File tree

7 files changed

+75
-9
lines changed

7 files changed

+75
-9
lines changed

docs/usage.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,3 +874,11 @@ IAM(app_code, app_secret, bk_apigateway_url="http://bk-iam.{APIGATEWAY_DOMAIN}/p
874874

875875
- `BK_IAM_USE_APIGATEWAY = True`
876876
- `BK_IAM_APIGATEWAY_URL = "http://bk-iam.{APIGATEWAY_DOMAIN}/{env}"`
877+
878+
## 5. 使用 v1 鉴权 api
879+
880+
当前SDK默认使用 v2 鉴权 api, 如果开发者环境的权限中心后台版本小于 v1.2.6, 则不支持直接使用v2 api, 需要配置`api_version`指定使用v1 api
881+
882+
```python
883+
IAM(APP_CODE, APP_SECRET, BK_IAM_HOST, BK_PAAS_HOST, api_version="v1")
884+
```

iam/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22

3-
__version__ = "1.2.2"
3+
__version__ = "1.3.1"

iam/api/client.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ def __init__(self, app_code, app_secret, bk_iam_host=None, bk_paas_host=None, bk
5959
self._bk_paas_host = bk_paas_host
6060

6161
# will add ?debug=true in url, for debug api/policy, show the details
62-
is_api_debug_enabled = (os.environ.get("IAM_API_DEBUG") == "true"
63-
or os.environ.get("BKAPP_IAM_API_DEBUG") == "true")
62+
is_api_debug_enabled = (
63+
os.environ.get("IAM_API_DEBUG") == "true" or os.environ.get("BKAPP_IAM_API_DEBUG") == "true"
64+
)
6465
# will add ?force=true in url, for api/policy run without cache(all data from database)
65-
is_api_force_enabled = (os.environ.get("IAM_API_FORCE") == "true"
66-
or os.environ.get("BKAPP_IAM_API_FORCE") == "true")
66+
is_api_force_enabled = (
67+
os.environ.get("IAM_API_FORCE") == "true" or os.environ.get("BKAPP_IAM_API_FORCE") == "true"
68+
)
6769

6870
self._extra_url_params = {}
6971
if is_api_debug_enabled:
@@ -322,11 +324,22 @@ def policy_query(self, data):
322324
ok, message, data = self._call_iam_api(http_post, path, data)
323325
return ok, message, data
324326

327+
# --------- policy v2
328+
def v2_policy_query(self, system_id, data):
329+
path = f"/api/v2/policy/systems/{system_id}/query/"
330+
ok, message, data = self._call_iam_api(http_post, path, data)
331+
return ok, message, data
332+
325333
def policy_query_by_actions(self, data):
326334
path = "/api/v1/policy/query_by_actions"
327335
ok, message, data = self._call_iam_api(http_post, path, data)
328336
return ok, message, data
329337

338+
def v2_policy_query_by_actions(self, system_id, data):
339+
path = f"/api/v2/policy/systems/{system_id}/query_by_actions/"
340+
ok, message, data = self._call_iam_api(http_post, path, data)
341+
return ok, message, data
342+
330343
def get_token(self, system_id):
331344
path = "/api/v1/model/systems/{system_id}/token".format(system_id=system_id)
332345
ok, message, _data = self._call_iam_api(http_get, path, {})

iam/iam.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class IAM(object):
3737
input: object
3838
"""
3939

40-
def __init__(self, app_code, app_secret, bk_iam_host=None, bk_paas_host=None, bk_apigateway_url=None):
40+
def __init__(
41+
self, app_code, app_secret, bk_iam_host=None, bk_paas_host=None, bk_apigateway_url=None, api_version="v2"
42+
):
4143
"""
4244
如果有 APIGateway 且权限中心网关接入, 则可以统一API请求全部走APIGateway
4345
- 没有APIGateway的用法: IAM(app_code, app_secret, bk_iam_host, bk_paas_host)
@@ -48,6 +50,8 @@ def __init__(self, app_code, app_secret, bk_iam_host=None, bk_paas_host=None, bk
4850
"""
4951
self._client = Client(app_code, app_secret, bk_iam_host, bk_paas_host, bk_apigateway_url)
5052

53+
self._api_version = api_version
54+
5155
def _do_policy_query(self, request, with_resources=True):
5256
data = request.to_dict()
5357
logger.debug("the request: %s", data)
@@ -57,7 +61,10 @@ def _do_policy_query(self, request, with_resources=True):
5761
if not with_resources:
5862
data["resources"] = []
5963

60-
ok, message, policies = self._client.policy_query(data)
64+
if self._api_version == "v2":
65+
ok, message, policies = self._client.v2_policy_query(request.system, data)
66+
else:
67+
ok, message, policies = self._client.policy_query(data)
6168
if not ok:
6269
raise AuthAPIError(message)
6370
return policies
@@ -75,7 +82,10 @@ def _do_policy_query_by_actions(self, request, with_resources=True):
7582
if not with_resources:
7683
data["resources"] = []
7784

78-
ok, message, action_policies = self._client.policy_query_by_actions(data)
85+
if self._api_version == "v2":
86+
ok, message, action_policies = self._client.v2_policy_query_by_actions(request.system, data)
87+
else:
88+
ok, message, action_policies = self._client.policy_query_by_actions(data)
7989
if not ok:
8090
raise AuthAPIError(message)
8191
return action_policies
@@ -401,7 +411,7 @@ def make_filter(self, request, converter_class=DjangoQuerySetConverter, key_mapp
401411

402412
# TODO: add the register model apis
403413
def get_token(self, system):
404-
""" 获取token
414+
"""获取token
405415
return bool, message, token
406416
"""
407417
return self._client.get_token(system)

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ $ pip install bk-iam
4747
- [TencentBlueKing/iam-python-sdk](https://github.com/TencentBlueKing/iam-python-sdk)
4848
- [TencentBlueKing/iam-go-sdk](https://github.com/TencentBlueKing/iam-go-sdk)
4949
- [TencentBlueKing/iam-php-sdk](https://github.com/TencentBlueKing/iam-php-sdk)
50+
- [TencentBlueKing/iam-java-sdk](https://github.com/TencentBlueKing/iam-java-sdk)
5051

5152
## Support
5253

release.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
版本日志
22
===============
33

4+
# v1.3.1
5+
6+
- add: 支持权限中心后台v2鉴权api
7+
48
# v1.2.2
59

610
- add: fetch_instance_list/fetch_resource_type_schema in ResourceProvider

tests/api/test_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ def _test_ok_message_data(mock_request, call_func):
4141
assert data[1] == 1
4242

4343

44+
def _test_v2_ok_message_data(mock_request, call_func):
45+
# 1. request fail
46+
mock_request.return_value = (False, "error", {})
47+
ok, message, data = call_func("system", {})
48+
49+
assert not ok
50+
51+
# 2. request success, code not 0
52+
mock_request.return_value = (True, "error status_code != 200", {"code": 404, "message": "not found"})
53+
ok, message, data = call_func("system", {})
54+
assert not ok
55+
56+
# 3. request success, code 0
57+
mock_request.return_value = (True, "ok", {"code": 0, "message": "ok", "data": {1: 1}})
58+
ok, message, data = call_func("system", {})
59+
assert ok
60+
assert message == "ok"
61+
assert data
62+
assert data[1] == 1
63+
64+
4465
@patch("iam.api.client.http_post")
4566
def test_client_policy_query(mock_post):
4667
c = Client("bk_paas", "", "http://127.0.0.1:1234", "http://127.0.0.1:8000")
@@ -50,6 +71,15 @@ def test_client_policy_query(mock_post):
5071
_test_ok_message_data(mock_post, c.policy_query_by_actions)
5172

5273

74+
@patch("iam.api.client.http_post")
75+
def test_v2_client_policy_query(mock_post):
76+
c = Client("bk_paas", "", "http://127.0.0.1:1234", "http://127.0.0.1:8000")
77+
78+
_test_v2_ok_message_data(mock_post, c.v2_policy_query)
79+
80+
_test_v2_ok_message_data(mock_post, c.v2_policy_query_by_actions)
81+
82+
5383
def _test_ok_message(mock_request, call_func, kwargs):
5484
# 1. request fail
5585
mock_request.return_value = (False, "error", {})

0 commit comments

Comments
 (0)