API 文档

API 文档默认已经集成在代码里面, 部署完成后可以通过下面的方式进行访问

API 访问

VersionAccess methodexample
< 2.0.0http://<url>/docshttp://192.168.244.144/docs
>=2.0.0http://<url>/api/docs/http://192.168.244.144/api/docs/
>=2.6.0http://<url>/api/docs/http://192.168.244.144/api/docs/

版本小于 v2.6 需要打开 debug 模式

  1. vi config.yml
  1. ...
  2. # 如果版本更低的话,配置文件是 config.py
  3. # Debug = true
  4. DEBUG: true

api_swagger

API 认证

JumpServer API 支持的认证有以下几种方式

  1. Session 登录后可以直接使用 session_id 作为认证方式
  2. Token 获取一次性 Token,该 Token 有有效期, 过期作废
  3. Private Token 永久 Token
  4. Access Key Http Header 进行签名

Session

用户通过页面后登录,cookie 中会存在 sessionid, 请求时同样把 sessionid 放到 cookie 中

Token

  1. curl -X POST http://localhost/api/v1/authentication/auth/ \
  2. -H 'Content-Type: application/json' \
  3. -d '{"username": "admin", "password": "admin"}'
  1. # pip install requests
  2. import requests, json
  3. jms_url = 'https://demo.jumpserver.org'
  4. username = 'admin'
  5. password = 'admin'
  6. def get_token():
  7. url = jms_url + '/api/v1/authentication/auth/'
  8. query_args = {
  9. "username": username,
  10. "password": password
  11. }
  12. response = requests.post(url, data=query_args)
  13. return json.loads(response.text)['token']
  14. def get_user_info():
  15. url = jms_url + '/api/v1/users/users/'
  16. token = get_token()
  17. headers = { "Authorization": 'Bearer ' + token }
  18. response = requests.get(url, headers=headers)
  19. print(json.loads(response.text))
  20. get_user_info()

Private Token

  1. docker exec -it jms_core /bin/bash
  2. cd /opt/jumpserver/apps
  3. python manage.py shell
  4. from users.models import User
  5. u = User.objects.get(username='admin')
  6. u.create_private_token()

已经存在 private_token, 可以直接获取即可

  1. u.private_token

以 PrivateToken: 937b38011acf499eb474e2fecb424ab3 为例:

  1. curl -H 'Authorization: Token 937b38011acf499eb474e2fecb424ab3' \
  2. -H "Content-Type:application/json" http://demo.jumpserver.org/api/v1/users/users/
  1. # pip install requests
  2. import requests, json
  3. jms_url = 'https://demo.jumpserver.org'
  4. jms_token = '937b38011acf499eb474e2fecb424ab3'
  5. def get_user_info():
  6. url = jms_url + '/api/v1/users/users/'
  7. headers = { "Authorization": 'Token ' + jms_token }
  8. response = requests.get(url, headers=headers)
  9. print(json.loads(response.text))
  10. get_user_info()

Access Key

在 Web 页面 API Key 列表创建或获取 AccessKeyID AccessKeySecret

  1. # pip install requests drf-httpsig
  2. import requests, datetime, json
  3. from httpsig.requests_auth import HTTPSignatureAuth
  4. jms_url = 'https://demo.jumpserver.org'
  5. AccessKeyID = 'AccessKeyID'
  6. AccessKeySecret = 'AccessKeySecret'
  7. GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
  8. def get_auth():
  9. signature_headers = ['(request-target)', 'accept', 'date']
  10. auth = HTTPSignatureAuth(key_id=AccessKeyID, secret=AccessKeySecret, algorithm='hmac-sha256', headers=signature_headers)
  11. return auth
  12. def get_user_info():
  13. url = jms_url + '/api/v1/users/users/'
  14. auth = get_auth()
  15. headers = {
  16. 'Accept': 'application/json',
  17. 'Date': datetime.datetime.utcnow().strftime(GMT_FORMAT)
  18. }
  19. response = requests.get(url, auth=auth, headers=headers)
  20. print(json.loads(response.text))
  21. get_user_info()

示例

Token

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import sys, requests, time
  4. class HTTP:
  5. server = None
  6. token = None
  7. @classmethod
  8. def get_token(cls, username, password):
  9. data = {'username': username, 'password': password}
  10. url = "/api/v1/authentication/auth/"
  11. res = requests.post(cls.server + url, data)
  12. res_data = res.json()
  13. if res.status_code in [200, 201] and res_data:
  14. token = res_data.get('token')
  15. cls.token = token
  16. else:
  17. print("获取 token 错误, 请检查输入项是否正确")
  18. sys.exit()
  19. @classmethod
  20. def get(cls, url, params=None, **kwargs):
  21. url = cls.server + url
  22. headers = {
  23. 'Authorization': "Bearer {}".format(cls.token)
  24. }
  25. kwargs['headers'] = headers
  26. res = requests.get(url, params, **kwargs)
  27. return res
  28. @classmethod
  29. def post(cls, url, data=None, json=None, **kwargs):
  30. url = cls.server + url
  31. headers = {
  32. 'Authorization': "Bearer {}".format(cls.token)
  33. }
  34. kwargs['headers'] = headers
  35. res = requests.post(url, data, json, **kwargs)
  36. return res
  37. class User(object):
  38. def __init__(self):
  39. self.id = None
  40. self.name = user_name
  41. self.username = user_username
  42. self.email = user_email
  43. def exist(self):
  44. url = '/api/v1/users/users/'
  45. params = {'username': self.username}
  46. res = HTTP.get(url, params=params)
  47. res_data = res.json()
  48. if res.status_code in [200, 201] and res_data:
  49. self.id = res_data[0].get('id')
  50. else:
  51. self.create()
  52. def create(self):
  53. print("创建用户 {}".format(self.username))
  54. url = '/api/v1/users/users/'
  55. data = {
  56. 'name': self.name,
  57. 'username': self.username,
  58. 'email': self.email,
  59. 'is_active': True
  60. }
  61. res = HTTP.post(url, json=data)
  62. self.id = res.json().get('id')
  63. def perform(self):
  64. self.exist()
  65. class Node(object):
  66. def __init__(self):
  67. self.id = None
  68. self.name = asset_node_name
  69. def exist(self):
  70. url = '/api/v1/assets/nodes/'
  71. params = {'value': self.name}
  72. res = HTTP.get(url, params=params)
  73. res_data = res.json()
  74. if res.status_code in [200, 201] and res_data:
  75. self.id = res_data[0].get('id')
  76. else:
  77. self.create()
  78. def create(self):
  79. print("创建资产节点 {}".format(self.name))
  80. url = '/api/v1/assets/nodes/'
  81. data = {
  82. 'value': self.name
  83. }
  84. res = HTTP.post(url, json=data)
  85. self.id = res.json().get('id')
  86. def perform(self):
  87. self.exist()
  88. class AdminUser(object):
  89. def __init__(self):
  90. self.id = None
  91. self.name = assets_admin_name
  92. self.username = assets_admin_username
  93. self.password = assets_admin_password
  94. def exist(self):
  95. url = '/api/v1/assets/admin-user/'
  96. params = {'username': self.name}
  97. res = HTTP.get(url, params=params)
  98. res_data = res.json()
  99. if res.status_code in [200, 201] and res_data:
  100. self.id = res_data[0].get('id')
  101. else:
  102. self.create()
  103. def create(self):
  104. print("创建管理用户 {}".format(self.name))
  105. url = '/api/v1/assets/admin-users/'
  106. data = {
  107. 'name': self.name,
  108. 'username': self.username,
  109. 'password': self.password
  110. }
  111. res = HTTP.post(url, json=data)
  112. self.id = res.json().get('id')
  113. def perform(self):
  114. self.exist()
  115. class Asset(object):
  116. def __init__(self):
  117. self.id = None
  118. self.name = asset_name
  119. self.ip = asset_ip
  120. self.platform = asset_platform
  121. self.protocols = asset_protocols
  122. self.admin_user = AdminUser()
  123. self.node = Node()
  124. def exist(self):
  125. url = '/api/v1/assets/assets/'
  126. params = {
  127. 'hostname': self.name
  128. }
  129. res = HTTP.get(url, params)
  130. res_data = res.json()
  131. if res.status_code in [200, 201] and res_data:
  132. self.id = res_data[0].get('id')
  133. else:
  134. self.create()
  135. def create(self):
  136. print("创建资产 {}".format(self.ip))
  137. self.admin_user.perform()
  138. self.node.perform()
  139. url = '/api/v1/assets/assets/'
  140. data = {
  141. 'hostname': self.ip,
  142. 'ip': self.ip,
  143. 'platform': self.platform,
  144. 'protocols': self.protocols,
  145. 'admin_user': self.admin_user.id,
  146. 'nodes': [self.node.id],
  147. 'is_active': True
  148. }
  149. res = HTTP.post(url, json=data)
  150. self.id = res.json().get('id')
  151. def perform(self):
  152. self.exist()
  153. class SystemUser(object):
  154. def __init__(self):
  155. self.id = None
  156. self.name = assets_system_name
  157. self.username = assets_system_username
  158. def exist(self):
  159. url = '/api/v1/assets/system-users/'
  160. params = {'name': self.name}
  161. res = HTTP.get(url, params)
  162. res_data = res.json()
  163. if res.status_code in [200, 201] and res_data:
  164. self.id = res_data[0].get('id')
  165. else:
  166. self.create()
  167. def create(self):
  168. print("创建系统用户 {}".format(self.name))
  169. url = '/api/v1/assets/system-users/'
  170. data = {
  171. 'name': self.name,
  172. 'username': self.username,
  173. 'login_mode': 'auto',
  174. 'protocol': 'ssh',
  175. 'auto_push': True,
  176. 'sudo': 'All',
  177. 'shell': '/bin/bash',
  178. 'auto_generate_key': True,
  179. 'is_active': True
  180. }
  181. res = HTTP.post(url, json=data)
  182. self.id = res.json().get('id')
  183. def perform(self):
  184. self.exist()
  185. class AssetPermission(object):
  186. def __init__(self):
  187. self.name = perm_name
  188. self.user = User()
  189. self.asset = Asset()
  190. self.system_user = SystemUser()
  191. def create(self):
  192. print("创建资产授权名称 {}".format(self.name))
  193. url = '/api/v1/perms/asset-permissions/'
  194. data = {
  195. 'name': self.name,
  196. 'users': [self.user.id],
  197. 'assets': [self.asset.id],
  198. 'system_users': [self.system_user.id],
  199. 'actions': ['all'],
  200. 'is_active': True,
  201. 'date_start': perm_date_start,
  202. 'date_expired': perm_date_expired
  203. }
  204. res = HTTP.post(url, json=data)
  205. res_data = res.json()
  206. if res.status_code in [200, 201] and res_data:
  207. print("创建资产授权规则成功: ", res_data)
  208. else:
  209. print("创建授权规则失败: ", res_data)
  210. def perform(self):
  211. self.user.perform()
  212. self.asset.perform()
  213. self.system_user.perform()
  214. self.create()
  215. class APICreateAssetPermission(object):
  216. def __init__(self):
  217. self.jms_url = jms_url
  218. self.username = jms_username
  219. self.password = jms_password
  220. self.token = None
  221. self.server = None
  222. def init_http(self):
  223. HTTP.server = self.jms_url
  224. HTTP.get_token(self.username, self.password)
  225. def perform(self):
  226. self.init_http()
  227. self.perm = AssetPermission()
  228. self.perm.perform()
  229. if __name__ == '__main__':
  230. # jumpserver url 地址
  231. jms_url = 'http://192.168.100.244'
  232. # 管理员账户
  233. jms_username = 'admin'
  234. jms_password = 'admin'
  235. # 资产节点
  236. asset_node_name = 'test'
  237. # 资产信息
  238. asset_name = '192.168.100.1'
  239. asset_ip = '192.168.100.1'
  240. asset_platform = 'Linux'
  241. asset_protocols = ['ssh/22']
  242. # 资产管理用户
  243. assets_admin_name = 'test_root'
  244. assets_admin_username = 'root'
  245. assets_admin_password = 'test123456'
  246. # 资产系统用户
  247. assets_system_name = 'test'
  248. assets_system_username = 'test'
  249. # 用户用户名
  250. user_name = '测试用户'
  251. user_username = 'test'
  252. user_email = 'test@jumpserver.org'
  253. # 资产授权
  254. perm_name = 'AutoPerm' +'_'+ (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  255. perm_date_start = '2021-05-01 14:25:47 +0800'
  256. perm_date_expired = '2021-06-01 14:25:47 +0800'
  257. api = APICreateAssetPermission()
  258. api.perform()

Access Key

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import sys, requests, time, datetime
  4. from httpsig.requests_auth import HTTPSignatureAuth
  5. class HTTP:
  6. server = None
  7. auth = None
  8. @classmethod
  9. def get_auth(cls, accesskeyid, accesskeysecret):
  10. signature_headers = ['(request-target)', 'accept', 'date']
  11. auth = HTTPSignatureAuth(key_id=accesskeyid, secret=accesskeysecret, algorithm='hmac-sha256', headers=signature_headers)
  12. cls.auth = auth
  13. @classmethod
  14. def get(cls, url, params=None, **kwargs):
  15. url = cls.server + url
  16. GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
  17. headers = {
  18. 'Accept': 'application/json',
  19. 'Date': datetime.datetime.utcnow().strftime(GMT_FORMAT)
  20. }
  21. kwargs['auth'] = cls.auth
  22. kwargs['headers'] = headers
  23. res = requests.get(url, params, **kwargs)
  24. return res
  25. @classmethod
  26. def post(cls, url, data=None, json=None, **kwargs):
  27. url = cls.server + url
  28. GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
  29. headers = {
  30. 'Accept': 'application/json',
  31. 'Date': datetime.datetime.utcnow().strftime(GMT_FORMAT)
  32. }
  33. kwargs['auth'] = cls.auth
  34. kwargs['headers'] = headers
  35. res = requests.post(url, data, json, **kwargs)
  36. return res
  37. class User(object):
  38. def __init__(self):
  39. self.id = None
  40. self.name = user_name
  41. self.username = user_username
  42. self.email = user_email
  43. def exist(self):
  44. url = '/api/v1/users/users/'
  45. params = {'username': self.username}
  46. res = HTTP.get(url, params=params)
  47. res_data = res.json()
  48. if res.status_code in [200, 201] and res_data:
  49. self.id = res_data[0].get('id')
  50. else:
  51. self.create()
  52. def create(self):
  53. print("创建用户 {}".format(self.username))
  54. url = '/api/v1/users/users/'
  55. data = {
  56. 'name': self.name,
  57. 'username': self.username,
  58. 'email': self.email,
  59. 'is_active': True
  60. }
  61. res = HTTP.post(url, json=data)
  62. self.id = res.json().get('id')
  63. def perform(self):
  64. self.exist()
  65. class Node(object):
  66. def __init__(self):
  67. self.id = None
  68. self.name = asset_node_name
  69. def exist(self):
  70. url = '/api/v1/assets/nodes/'
  71. params = {'value': self.name}
  72. res = HTTP.get(url, params=params)
  73. res_data = res.json()
  74. if res.status_code in [200, 201] and res_data:
  75. self.id = res_data[0].get('id')
  76. else:
  77. self.create()
  78. def create(self):
  79. print("创建资产节点 {}".format(self.name))
  80. url = '/api/v1/assets/nodes/'
  81. data = {
  82. 'value': self.name
  83. }
  84. res = HTTP.post(url, json=data)
  85. self.id = res.json().get('id')
  86. def perform(self):
  87. self.exist()
  88. class AdminUser(object):
  89. def __init__(self):
  90. self.id = None
  91. self.name = assets_admin_name
  92. self.username = assets_admin_username
  93. self.password = assets_admin_password
  94. def exist(self):
  95. url = '/api/v1/assets/admin-user/'
  96. params = {'username': self.name}
  97. res = HTTP.get(url, params=params)
  98. res_data = res.json()
  99. if res.status_code in [200, 201] and res_data:
  100. self.id = res_data[0].get('id')
  101. else:
  102. self.create()
  103. def create(self):
  104. print("创建管理用户 {}".format(self.name))
  105. url = '/api/v1/assets/admin-users/'
  106. data = {
  107. 'name': self.name,
  108. 'username': self.username,
  109. 'password': self.password
  110. }
  111. res = HTTP.post(url, json=data)
  112. self.id = res.json().get('id')
  113. def perform(self):
  114. self.exist()
  115. class Asset(object):
  116. def __init__(self):
  117. self.id = None
  118. self.name = asset_name
  119. self.ip = asset_ip
  120. self.platform = asset_platform
  121. self.protocols = asset_protocols
  122. self.admin_user = AdminUser()
  123. self.node = Node()
  124. def exist(self):
  125. url = '/api/v1/assets/assets/'
  126. params = {
  127. 'hostname': self.name
  128. }
  129. res = HTTP.get(url, params)
  130. res_data = res.json()
  131. if res.status_code in [200, 201] and res_data:
  132. self.id = res_data[0].get('id')
  133. else:
  134. self.create()
  135. def create(self):
  136. print("创建资产 {}".format(self.ip))
  137. self.admin_user.perform()
  138. self.node.perform()
  139. url = '/api/v1/assets/assets/'
  140. data = {
  141. 'hostname': self.ip,
  142. 'ip': self.ip,
  143. 'platform': self.platform,
  144. 'protocols': self.protocols,
  145. 'admin_user': self.admin_user.id,
  146. 'nodes': [self.node.id],
  147. 'is_active': True
  148. }
  149. res = HTTP.post(url, json=data)
  150. self.id = res.json().get('id')
  151. def perform(self):
  152. self.exist()
  153. class SystemUser(object):
  154. def __init__(self):
  155. self.id = None
  156. self.name = assets_system_name
  157. self.username = assets_system_username
  158. def exist(self):
  159. url = '/api/v1/assets/system-users/'
  160. params = {'name': self.name}
  161. res = HTTP.get(url, params)
  162. res_data = res.json()
  163. if res.status_code in [200, 201] and res_data:
  164. self.id = res_data[0].get('id')
  165. else:
  166. self.create()
  167. def create(self):
  168. print("创建系统用户 {}".format(self.name))
  169. url = '/api/v1/assets/system-users/'
  170. data = {
  171. 'name': self.name,
  172. 'username': self.username,
  173. 'login_mode': 'auto',
  174. 'protocol': 'ssh',
  175. 'auto_push': True,
  176. 'sudo': 'All',
  177. 'shell': '/bin/bash',
  178. 'auto_generate_key': True,
  179. 'is_active': True
  180. }
  181. res = HTTP.post(url, json=data)
  182. self.id = res.json().get('id')
  183. def perform(self):
  184. self.exist()
  185. class AssetPermission(object):
  186. def __init__(self):
  187. self.name = perm_name
  188. self.user = User()
  189. self.asset = Asset()
  190. self.system_user = SystemUser()
  191. def create(self):
  192. print("创建资产授权名称 {}".format(self.name))
  193. url = '/api/v1/perms/asset-permissions/'
  194. data = {
  195. 'name': self.name,
  196. 'users': [self.user.id],
  197. 'assets': [self.asset.id],
  198. 'system_users': [self.system_user.id],
  199. 'actions': ['all'],
  200. 'is_active': True,
  201. 'date_start': perm_date_start,
  202. 'date_expired': perm_date_expired
  203. }
  204. res = HTTP.post(url, json=data)
  205. res_data = res.json()
  206. if res.status_code in [200, 201] and res_data:
  207. print("创建资产授权规则成功: ", res_data)
  208. else:
  209. print("创建授权规则失败: ", res_data)
  210. def perform(self):
  211. self.user.perform()
  212. self.asset.perform()
  213. self.system_user.perform()
  214. self.create()
  215. class APICreateAssetPermission(object):
  216. def __init__(self):
  217. self.jms_url = jms_url
  218. self.accesskeyid = jms_accesskeyid
  219. self.accesskeysecret = jms_accesskeysecret
  220. self.auth = None
  221. self.server = None
  222. def init_http(self):
  223. HTTP.server = self.jms_url
  224. HTTP.get_auth(self.accesskeyid, self.accesskeysecret)
  225. def perform(self):
  226. self.init_http()
  227. self.perm = AssetPermission()
  228. self.perm.perform()
  229. if __name__ == '__main__':
  230. # jumpserver url 地址
  231. jms_url = 'http://192.168.100.244'
  232. # 管理员 AK SK
  233. jms_accesskeyid = ''
  234. jms_accesskeysecret = ''
  235. # 资产节点
  236. asset_node_name = 'test'
  237. # 资产信息
  238. asset_name = '192.168.100.1'
  239. asset_ip = '192.168.100.1'
  240. asset_platform = 'Linux'
  241. asset_protocols = ['ssh/22']
  242. # 资产管理用户
  243. assets_admin_name = 'test_root'
  244. assets_admin_username = 'root'
  245. assets_admin_password = 'test123456'
  246. # 资产系统用户
  247. assets_system_name = 'test'
  248. assets_system_username = 'test'
  249. # 用户用户名
  250. user_name = '测试用户'
  251. user_username = 'test'
  252. user_email = 'test@jumpserver.org'
  253. # 资产授权
  254. perm_name = 'AutoPerm' +'_'+ (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  255. perm_date_start = '2021-05-01 14:25:47 +0800'
  256. perm_date_expired = '2021-06-01 14:25:47 +0800'
  257. api = APICreateAssetPermission()
  258. api.perform()