Merge pull request #1452 from guardicore/1418/bb-to-use-credentials

Zoo: Change island to use credentials
This commit is contained in:
VakarisZ 2021-09-06 10:28:39 +03:00 committed by GitHub
commit 57908b94eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 9 deletions

View File

@ -7,8 +7,8 @@ import requests
from envs.monkey_zoo.blackbox.island_client.supported_request_method import SupportedRequestMethod from envs.monkey_zoo.blackbox.island_client.supported_request_method import SupportedRequestMethod
# SHA3-512 of '1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()' ISLAND_USERNAME = "m0nk3y"
NO_AUTH_CREDS = "1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()" ISLAND_PASSWORD = "Passw0rd!"
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -16,6 +16,10 @@ class AuthenticationFailedError(Exception):
pass pass
class InvalidRegistrationCredentialsError(Exception):
pass
# noinspection PyArgumentList # noinspection PyArgumentList
class MonkeyIslandRequests(object): class MonkeyIslandRequests(object):
def __init__(self, server_address): def __init__(self, server_address):
@ -48,9 +52,9 @@ class MonkeyIslandRequests(object):
try: try:
return self.get_jwt_from_server() return self.get_jwt_from_server()
except AuthenticationFailedError: except AuthenticationFailedError:
self.try_set_island_to_no_password() self.try_set_island_to_credentials()
return self.get_jwt_from_server() return self.get_jwt_from_server()
except requests.ConnectionError as err: except (requests.ConnectionError, InvalidRegistrationCredentialsError) as err:
LOGGER.error( LOGGER.error(
"Unable to connect to island, aborting! Error information: {}. Server: {}".format( "Unable to connect to island, aborting! Error information: {}. Server: {}".format(
err, self.addr err, self.addr
@ -61,17 +65,21 @@ class MonkeyIslandRequests(object):
def get_jwt_from_server(self): def get_jwt_from_server(self):
resp = requests.post( # noqa: DUO123 resp = requests.post( # noqa: DUO123
self.addr + "api/auth", self.addr + "api/auth",
json={"username": NO_AUTH_CREDS, "password": NO_AUTH_CREDS}, json={"username": ISLAND_USERNAME, "password": ISLAND_PASSWORD},
verify=False, verify=False,
) )
if resp.status_code == 401: if resp.status_code == 401:
raise AuthenticationFailedError raise AuthenticationFailedError
return resp.json()["access_token"] return resp.json()["access_token"]
def try_set_island_to_no_password(self): def try_set_island_to_credentials(self):
requests.patch( # noqa: DUO123 resp = requests.post( # noqa: DUO123
self.addr + "api/environment", json={"server_config": "standard"}, verify=False self.addr + "api/registration",
json={"user": ISLAND_USERNAME, "password": ISLAND_PASSWORD},
verify=False,
) )
if resp.status_code == 400:
raise InvalidRegistrationCredentialsError("Missing part of the credentials")
class _Decorators: class _Decorators:
@classmethod @classmethod
@ -117,6 +125,5 @@ class MonkeyIslandRequests(object):
self.addr + url, headers=self.get_jwt_header(), verify=False self.addr + url, headers=self.get_jwt_header(), verify=False
) )
@_Decorators.refresh_jwt_token
def get_jwt_header(self): def get_jwt_header(self):
return {"Authorization": "Bearer " + self.token} return {"Authorization": "Bearer " + self.token}