这里介绍一下编写调试 Applet 脚本时,如何获取到当前 JumpServer 版本中资产授权的 base64_json_data 参数。
JumpServer 当前 v3 版本连接资产时,会生成一个 connection_token, 包含了资产的授权认证数据,包含 asset、account、platform 等信息。
Tinker 传递给 applet 脚本的参数,就是这个 connection_token 的 JSON 数据 base64 编码后的字符串。
前提条件:
-
需要 JumpServer 的管理员权限
-
创建管理员的 API key,用户的个人信息 - API Key 页面创建,并生成 Access Key 和 Secret Key
通过以下脚本,创建一个资产授权的 connection_token, 并生成相应的 base64 字符串参数
相应的替换 JumpServer 地址、access_key 和 access_secret,
再根据资产授权,替换 data 数据的 user、asset 等数据。
import base64
import json
import requests
from httpsig.requests_auth import HTTPSignatureAuth
from datetime import datetime
def get_auth(key_id, key_secret):
signature_headers = ['(request-target)', 'accept', 'date']
auth = HTTPSignatureAuth(key_id=key_id, secret=key_secret, algorithm='hmac-sha256',
headers=signature_headers)
return auth
def get_header():
gmt_form = '%a, %d %b %Y %H:%M:%S GMT'
headers = {
'Accept': 'application/json',
'X-JMS-ORG': '00000000-0000-0000-0000-000000000002',
'Date': datetime.utcnow().strftime(gmt_form)
}
return headers
class UserClient(object):
def __init__(self, base_url, access_key, access_secret):
self.base_url = base_url
self.access_key = access_key
self.access_secret = access_secret
def create_token(self, **data):
url = f"{self.base_url}/api/v1/authentication/super-connection-token/"
auth = get_auth(self.access_key, self.access_secret)
headers = get_header()
resp = requests.post(url, headers=headers, auth=auth, json=data)
return resp.json()
def get_connect_token_auth_info(self, token):
data = {
"id": token,
"expire_now": False,
}
url = f"{self.base_url}/api/v1/authentication/super-connection-token/secret/"
access_key = self.access_key
access_secret = self.access_secret
auth = get_auth(access_key, access_secret)
res = requests.post(url, headers=get_header(), auth=auth, json=data)
return res.json()
def main():
base_url = 'http://localhost:8080'
access_key = 'c3f42485-e437-43a4-ae3b-492840b1806e'
access_secret = 'f2e0dcbd-94ee-4be7-9227-687f3f2d9312'
user_client = UserClient(base_url, access_key, access_secret)
data = {
"user": "d94e9617-9b3d-4768-9afc-16c20db29b57",
"asset": "20724755-6862-41da-8e25-38a0fc949482",
"account": "root",
"protocol": "rdp",
"connect_method": "rdp",
}
token = user_client.create_token(**data)
print("create token: ", token['id'])
print("================== get token detail ==================")
detail = user_client.get_connect_token_auth_info(token['id'])
print(json.dumps(detail, indent=2, ensure_ascii=False))
print("================== get token base64 ==================")
print(base64.b64encode(json.dumps(detail).encode()).decode())
if __name__ == '__main__':
main()