monkey/envs/monkey_zoo/blackbox/island_client/monkey_island_requests.py

117 lines
3.7 KiB
Python
Raw Normal View History

import functools
import logging
from typing import Dict
2019-09-13 21:12:58 +08:00
import requests
ISLAND_USERNAME = "test"
ISLAND_PASSWORD = "test"
2019-10-01 21:11:53 +08:00
LOGGER = logging.getLogger(__name__)
2019-09-13 21:12:58 +08:00
class AuthenticationFailedError(Exception):
pass
2021-09-02 22:36:19 +08:00
class InvalidRegistrationCredentialsError(Exception):
pass
2019-10-30 20:38:17 +08:00
# noinspection PyArgumentList
2019-09-13 21:12:58 +08:00
class MonkeyIslandRequests(object):
def __init__(self, server_address):
self.addr = "https://{IP}/".format(IP=server_address)
self.token = self.try_get_jwt_from_server()
def try_get_jwt_from_server(self):
try:
return self.get_jwt_from_server()
except AuthenticationFailedError:
2021-09-02 22:36:19 +08:00
self.try_set_island_to_credentials()
return self.get_jwt_from_server()
2021-09-02 22:36:19 +08:00
except (requests.ConnectionError, InvalidRegistrationCredentialsError) as err:
LOGGER.error(
2021-04-06 21:19:27 +08:00
"Unable to connect to island, aborting! Error information: {}. Server: {}".format(
err, self.addr
)
)
2019-09-13 21:12:58 +08:00
assert False
def get_jwt_from_server(self):
resp = requests.post( # noqa: DUO123
self.addr + "api/authenticate",
2021-09-02 22:36:19 +08:00
json={"username": ISLAND_USERNAME, "password": ISLAND_PASSWORD},
verify=False,
)
if resp.status_code == 401:
raise AuthenticationFailedError
return resp.json()["access_token"]
2021-09-02 22:36:19 +08:00
def try_set_island_to_credentials(self):
resp = requests.post( # noqa: DUO123
self.addr + "api/register",
2021-10-14 20:52:13 +08:00
json={"username": ISLAND_USERNAME, "password": ISLAND_PASSWORD},
2021-09-02 22:36:19 +08:00
verify=False,
)
2021-09-02 22:36:19 +08:00
if resp.status_code == 400:
raise InvalidRegistrationCredentialsError("Missing part of the credentials")
2019-10-30 20:38:17 +08:00
class _Decorators:
@classmethod
def refresh_jwt_token(cls, request_function):
@functools.wraps(request_function)
2019-11-27 19:39:47 +08:00
def request_function_wrapper(self, *args, **kwargs):
2019-10-30 20:38:17 +08:00
self.token = self.try_get_jwt_from_server()
# noinspection PyArgumentList
return request_function(self, *args, **kwargs)
2019-11-27 19:39:47 +08:00
2019-10-30 20:38:17 +08:00
return request_function_wrapper
@_Decorators.refresh_jwt_token
2019-09-13 21:12:58 +08:00
def get(self, url, data=None):
2021-04-22 19:09:19 +08:00
return requests.get( # noqa: DUO123
self.addr + url,
2021-04-06 21:19:27 +08:00
headers=self.get_jwt_header(),
params=data,
verify=False,
)
2019-09-13 21:12:58 +08:00
2019-10-30 20:38:17 +08:00
@_Decorators.refresh_jwt_token
2019-09-13 21:12:58 +08:00
def post(self, url, data):
2021-04-22 19:09:19 +08:00
return requests.post( # noqa: DUO123
self.addr + url, data=data, headers=self.get_jwt_header(), verify=False
2021-04-06 21:19:27 +08:00
)
2019-09-13 21:12:58 +08:00
@_Decorators.refresh_jwt_token
def put(self, url, data):
return requests.put( # noqa: DUO123
self.addr + url, data=data, headers=self.get_jwt_header(), verify=False
)
2022-08-05 21:20:52 +08:00
@_Decorators.refresh_jwt_token
def put_json(self, url, json: Dict):
return requests.put( # noqa: DUO123
self.addr + url, json=json, headers=self.get_jwt_header(), verify=False
)
2019-10-30 20:38:17 +08:00
@_Decorators.refresh_jwt_token
def post_json(self, url, json: Dict):
2021-04-22 19:09:19 +08:00
return requests.post( # noqa: DUO123
self.addr + url, json=json, headers=self.get_jwt_header(), verify=False
2021-04-06 21:19:27 +08:00
)
@_Decorators.refresh_jwt_token
def patch(self, url, data: Dict):
2021-04-22 19:09:19 +08:00
return requests.patch( # noqa: DUO123
self.addr + url, data=data, headers=self.get_jwt_header(), verify=False
2021-04-06 21:19:27 +08:00
)
2019-09-13 21:12:58 +08:00
@_Decorators.refresh_jwt_token
def delete(self, url):
2021-04-22 19:09:19 +08:00
return requests.delete( # noqa: DUO123
2021-04-06 21:19:27 +08:00
self.addr + url, headers=self.get_jwt_header(), verify=False
)
2019-09-13 21:12:58 +08:00
def get_jwt_header(self):
return {"Authorization": "Bearer " + self.token}