11# -*- coding: utf-8 -*-
22"""
33TencentBlueKing is pleased to support the open source community by making
4- 蓝鲸智云-权限中心Python SDK(iam-python-sdk) available.
4+ 蓝鲸智云 - 权限中心 Python SDK(iam-python-sdk) available.
55Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
66Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
77You may obtain a copy of the License at http://opensource.org/licenses/MIT
1919
2020import 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
3028APP_CODE = ""
3129APP_SECRET = ""
3230data_file = ""
3331
3432
3533# =================== load json ===================
36- def enable_use_apigateway ():
37- global USE_APIGATEWAY
38- USE_APIGATEWAY = True
39-
40-
4134def 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
138131class 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