Skip to content

Commit c265f65

Browse files
authored
Merge pull request #4777 from easyops-cn/janice/feat/brick_next_v3/token
feat(): brick_next_v3 组件支持服务鉴权
2 parents 689c4d3 + 279771d commit c265f65

File tree

10 files changed

+97
-7
lines changed

10 files changed

+97
-7
lines changed

.pkgbuild/@easyops/PKGBUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ check() {
2525

2626
package() {
2727
cd next-core
28+
cp -arf conf ${pkgdir}
2829
cp -arf packages/brick-container/deploy ${pkgdir}
2930
cp -arf --parents packages/brick-container/dist ${pkgdir}
3031
cp -arf --parents packages/brick-container/conf ${pkgdir}

conf/config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
appId: brick_next_v3
2+
exampleFile: "examples/application.properties"
3+
4+
namespaces: ~
5+
6+
relatedNamespaces: ~

conf/examples/application.properties

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# easyops 安装根目录
4+
install_base="/usr/local/easyops"
5+
6+
plugin_name='brick_next_v3'
7+
8+
install_path="${install_base}/${plugin_name}"
9+
10+
deploy_init_path="/usr/local/easyops/deploy_init"
11+
config_tool="${deploy_init_path}/tools/config_tool"
12+
13+
export LD_LIBRARY_PATH=/usr/local/easyops/ens_client/sdk:${LD_LIBRARY_PATH}
14+
15+
16+
value=$(${config_tool} get --appID "deploy_init" --namespaceName "common" --key "check_auth_token.enable")
17+
if [[ ${value} == "true" ]]; then
18+
# 初始化默认命名空间,并生成 clientId 和 secret
19+
${config_tool} init -f "${install_path}/conf/config.yaml"
20+
if [[ $? -ne 0 ]];then
21+
echo "init brick_next_v3 config namespace error, exit"
22+
exit 1
23+
fi
24+
fi
25+
26+

packages/brick-container/deploy/update_postscript.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ plugin_name='brick_next_v3'
77

88
install_path="${install_base}/${plugin_name}"
99

10+
deploy_init_path="/usr/local/easyops/deploy_init"
11+
config_tool="${deploy_init_path}/tools/config_tool"
12+
13+
1014
function check_service_availability() {
1115
services=$1
1216
for service in ${services[*]}; do
@@ -45,12 +49,26 @@ function report_package() {
4549
fi
4650
}
4751

52+
export LD_LIBRARY_PATH=/usr/local/easyops/ens_client/sdk:${LD_LIBRARY_PATH}
53+
4854
# 优先取环境变量里面的org
4955
if [[ ${org}X == X ]]; then
5056
org=$(/usr/local/easyops/deploy_init/tools/get_env.py common org)
5157
[[ $? -ne 0 ]] && echo "get org error, exit" && exit 1
5258
fi
5359

60+
61+
value=$(${config_tool} get --appID "deploy_init" --namespaceName "common" --key "check_auth_token.enable")
62+
if [[ ${value} == "true" ]]; then
63+
# 初始化默认命名空间,并生成 clientId 和 secret
64+
${config_tool} init -f "${install_path}/conf/config.yaml"
65+
if [[ $? -ne 0 ]];then
66+
echo "import brick_next_v3 config namespace error, exit"
67+
exit 1
68+
fi
69+
fi
70+
71+
5472
# 上报当前安装小产品
5573
check_service
5674
report_package ${install_base} ${org} ${install_path}

packages/brick-container/tools/package-union/report_union_micro_app.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
reload(sys)
1212
sys.setdefaultencoding("utf-8")
1313

14+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # 将 tools 目录加入 path
15+
from utils.get_headers import get_headers
16+
1417
# 公共路径
1518
_INSTALL_BASE_PATH = "/usr/local/easyops"
1619
_APPLICATIONS_SA_FOLDER = "applications_sa"
@@ -50,7 +53,7 @@ def get_version(install_path):
5053
def collect_app_info(app_path, report_app_id, version):
5154
if not os.path.exists(app_path):
5255
print u"could not find app path {}".format(app_path)
53-
return
56+
return
5457
bootstrap_file_name = ""
5558
for f in os.listdir(app_path):
5659
if f.startswith("bootstrap-mini.") and f.endswith(".json"):
@@ -99,7 +102,7 @@ def report(org, app):
99102

100103

101104
def create_or_update_micro_app_sa(org, app):
102-
headers = {"org": str(org), "user": "defaultUser"}
105+
headers = get_headers(org)
103106
url = "http://{}/api/v1/micro_app_standalone/report".format(MICRO_APP_SA_ADDR)
104107
rsp = requests.post(url, json=app, headers=headers)
105108
rsp.raise_for_status()
@@ -115,7 +118,7 @@ def import_micro_app_permissions(org, permission_path):
115118
if not os.path.exists(permission_path):
116119
print "permission path {} does not exist, return...".format(permission_path)
117120
return
118-
headers = {"org": str(org), "user": "defaultUser"}
121+
headers = get_headers(org)
119122
url = "http://{}/api/micro_app/v1/permission/import".format(MICRO_APP_ADDR)
120123

121124
print "permission path is {}, will start import permissions".format(permission_path)

packages/brick-container/tools/report_installed_brick_next_package.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
from copy import deepcopy
1212

13+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
14+
from utils.get_headers import get_headers
15+
1316
reload(sys)
1417
sys.setdefaultencoding("utf-8")
1518

@@ -239,7 +242,7 @@ def collect(install_path):
239242

240243
def report_bricks_atom(org, nb_targz_path, package_name, package_version, bricks_content, stories_content,
241244
snippets_content):
242-
headers = {"org": str(org), "user": "defaultUser"}
245+
headers = get_headers(org)
243246
# report atom
244247
atom_url = "http://{}/api/v1/brick/atom/import".format(MICRO_APP_ADDR)
245248
data_dict = {"stories": stories_content, "bricks": bricks_content}
@@ -258,7 +261,8 @@ def report_bricks_atom(org, nb_targz_path, package_name, package_version, bricks
258261

259262
def report_brick_next_package(org, brick_targz_path, package_name, package_version):
260263
# report brick_next or NT
261-
headers = {"org": str(org), "user": "defaultUser"}
264+
headers = get_headers(org)
265+
262266
# report atom
263267
atom_url = "http://{}/api/v1/brick_next/report".format(MICRO_APP_ADDR)
264268
data = {"packageName": package_name, "packageVersion": package_version}
@@ -268,7 +272,8 @@ def report_brick_next_package(org, brick_targz_path, package_name, package_versi
268272

269273

270274
def report_provider_into_contract(org, package_name, contract_content):
271-
headers = {"org": str(org), "user": "defaultUser"}
275+
headers = get_headers(org)
276+
272277
# report contract
273278
url = "http://{}/api/v1/brick/provider/import_into_contract".format(MICRO_APP_ADDR)
274279
param = {"packageName": package_name, "data": {

packages/brick-container/tools/report_installed_theme_template.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import requests
66
import ens_api
77

8+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
9+
from utils.get_headers import get_headers
10+
11+
812
def collect(install_path):
913
if not os.path.exists(install_path):
1014
raise Exception("could not find install path {}".format(install_path))
@@ -22,7 +26,7 @@ def create_or_update_theme_template_data(data, org):
2226
if session_id <= 0:
2327
raise Exception("get nameservice logic.micro_app_service error, session_id={}".format(session_id))
2428
address = "{}:{}".format(ip, port)
25-
headers = {"org": str(org), "user": "defaultUser"}
29+
headers = get_headers(org)
2630
url = "http://{}/api/v1/next-builder/theme-data-import".format(address)
2731
param = {"themeData": data}
2832
rsp = requests.post(url, json=param, headers=headers)

packages/brick-container/tools/utils/__init__.py

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import sys
4+
import os
5+
6+
TOOLS_DIR = "/usr/local/easyops/deploy_init/tools"
7+
sys.path.append(TOOLS_DIR)
8+
get_token = None
9+
token_util_path = os.path.join(TOOLS_DIR, "get_token_util.py")
10+
if os.path.exists(token_util_path):
11+
from get_token_util import get_token
12+
13+
APP_ID = "brick_next_v3"
14+
DEFAULT_USER = "defaultUser"
15+
16+
17+
def get_headers(org, user=DEFAULT_USER):
18+
headers = {"org": str(org), "user": user}
19+
if get_token is None:
20+
return headers
21+
22+
token = get_token(user=user, org=int(org), app_id=APP_ID)
23+
if not token:
24+
return headers
25+
26+
headers["Authorization"] = "Bearer {}".format(token)
27+
return headers

0 commit comments

Comments
 (0)