Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some advances #145

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import abc
from abc import ABC, abstractmethod
from typing import Any, Dict

class JSONSerializable(ABC, metaclass=abc.ABCMeta):
@staticmethod
@abstractmethod
def from_dict(d: Dict[str, Any]) -> JSONSerializable: ...
@abstractmethod
def to_dict(self) -> Dict[str, Any]: ...
@staticmethod
@abstractmethod
def from_json_file(json_file_path: str) -> JSONSerializable: ...
def to_json_str(self) -> str: ...
def to_json_file(self, json_file_path: str) -> None: ...
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Dict, Any
from typing import List, Dict, Any, Union, Iterable
import time
from csle_common.dao.emulation_action.defender.emulation_defender_action_type import EmulationDefenderActionType
from csle_common.dao.emulation_action.defender.emulation_defender_action_id import EmulationDefenderActionId
Expand All @@ -11,11 +11,11 @@ class EmulationDefenderAction(JSONSerializable):
Class representing an action of the defender in the environment
"""

def __init__(self, id: EmulationDefenderActionId, name: str, cmds: List[str],
def __init__(self, id: EmulationDefenderActionId, name: str, cmds: Iterable[str],
type: EmulationDefenderActionType, descr: str,
ips: List[str], index: int,
ips: Iterable[str], index: int,
action_outcome: EmulationDefenderActionOutcome = EmulationDefenderActionOutcome.GAME_END,
alt_cmds: List[str] = None, execution_time: float = 0.0, ts: float = 0.0):
alt_cmds: Iterable[str] = None, execution_time: float = 0.0, ts: float = 0.0):
"""
Class constructor

Expand All @@ -37,7 +37,7 @@ def __init__(self, id: EmulationDefenderActionId, name: str, cmds: List[str],
if self.cmds is None:
self.cmds = []
self.type = type
self.descr = descr
self.descr : str = descr
self.ips = ips
if self.ips is None:
self.ips = []
Expand Down Expand Up @@ -70,9 +70,9 @@ def from_dict(d: Dict[str, Any]) -> "EmulationDefenderAction":
ts=d["ts"], execution_time=d["execution_time"])
return obj

def to_dict(self) -> Dict[str, Any]:
def to_dict(self): # -> Dict[str, Any]:
"""
:return: a dicr representation of the object
:return: a dict representation of the object
"""
d = {}
d["id"] = self.id
Expand All @@ -95,9 +95,10 @@ def ips_match(self, ips: List[str]) -> bool:
:param ips: the list of ips to check
:return: True if they match, False otherwise
"""
for ip in self.ips:
if ip in ips:
return True
if self.ips is not None:
for ip in self.ips:
if ip in ips:
return True
return False

def to_kafka_record(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, Dict, Any
from typing import Union, Dict, Any, Optional
import csle_common.constants.constants as constants
from csle_common.dao.emulation_config.emulation_env_config import EmulationEnvConfig
from csle_common.dao.emulation_observation.attacker.emulation_attacker_observation_state \
Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, emulation_env_config: EmulationEnvConfig):
self.os_lookup = constants.OS.os_lookup
self.os_lookup_inv = {v: k for k, v in self.os_lookup.items()}
self.attacker_obs_state: Union[EmulationAttackerObservationState, None] = None
self.defender_obs_state: Union[EmulationDefenderObservationState, None] = None
self.defender_obs_state: Optional[EmulationDefenderObservationState] = None
self.attacker_cached_ssh_connections = {}
self.attacker_cached_telnet_connections = {}
self.attacker_cached_ftp_connections = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ def __init__(self, emulation_name: str, descr: str = ""):
EmulationStatistics.initialize_counters(d={},
agg_labels=collector_constants.KAFKA_CONFIG.ALL_DELTA_AGG_LABELS)
self.id = -1
self.means = {}
self.stds = {}
self.mins = {}
self.maxs = {}
self.conditionals_probs = {}
self.initial_distributions_probs = {}
self.initial_means = {}
self.initial_stds = {}
self.initial_mins = {}
self.initial_maxs = {}
self.conditionals_kl_divergences = {}
self.num_metrics = 0
self.num_measurements = 0
self.num_conditions = 0
self.conditions = []
self.metrics = []
means: Dict[Any, Any] = {}
self.stds: Dict[Any, Any] = {}
self.mins: Dict[Any, Any] = {}
self.maxs: Dict[Any, Any] = {}
self.conditionals_probs: Dict[Any, Any] = {}
self.initial_distributions_probs: Dict[Any, Any] = {}
self.initial_means: Dict[Any, Any] = {}
self.initial_stds: Dict[Any, Any] = {}
self.initial_mins: Dict[Any, Any] = {}
self.initial_maxs: Dict[Any, Any]= {}
self.conditionals_kl_divergences: Dict[Any, Any] = {}
self.num_metrics: int = 0
self.num_measurements: int = 0
self.num_conditions: int = 0
self.conditions: List[Any] = []
self.metrics: List[Any] = []

@staticmethod
def initialize_counters(d: Dict[str, Dict[int, int]], agg_labels: List[str]) \
Expand Down Expand Up @@ -465,7 +465,7 @@ def from_dict(d: Dict[str, Any]) -> "EmulationStatistics":
obj.conditionals_kl_divergences = d["conditionals_kl_divergences"]
return obj

def to_dict(self) -> Dict[str, Any]:
def to_dict(self):
"""
Converts the object to a dict representation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from flask import request
from flask_socketio import ConnectionRefusedError
from flask import Blueprint
import logging

import csle_common.constants.constants as constants
import paramiko
import csle_rest_api.util.rest_api_util as rest_api_util
from flask import Blueprint, request
from flask_socketio import ConnectionRefusedError

import csle_rest_api.constants.constants as api_constants
import csle_common.constants.constants as constants
import csle_rest_api.util.rest_api_util as rest_api_util
from csle_rest_api import socketio

logger = logging.getLogger()


def get_container_terminal_bp(app):
"""
Expand All @@ -15,7 +19,9 @@ def get_container_terminal_bp(app):
:param app: the Flask app
:return: the blue print
"""

logger.info(dir(app.test_client()))
logger.info(dir(app))
# logger.info(request)
def set_container_terminal_winsize(ssh_channel, row: int, col: int, xpix: int = 0, ypix: int = 0) -> None:
"""
Set shell window size of the host terminal
Expand All @@ -27,6 +33,7 @@ def set_container_terminal_winsize(ssh_channel, row: int, col: int, xpix: int =
:param ypix: the number of y pixels of the new size
:return:
"""
logger.info("kommer jag hit?")
ssh_channel.resize_pty(width=col, height=row, width_pixels=xpix, height_pixels=ypix)

def ssh_connect(ip: str) -> paramiko.SSHClient:
Expand All @@ -36,6 +43,7 @@ def ssh_connect(ip: str) -> paramiko.SSHClient:
:param ip: the IP to connect to
:return: the created ssh connection
"""
logger.info("kommer jag hit?")
conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn.connect(ip, username=constants.CSLE_ADMIN.SSH_USER, password=constants.CSLE_ADMIN.SSH_PW)
Expand All @@ -52,6 +60,7 @@ def read_and_forward_container_terminal_output() -> None:

:return: None
"""
logger.info("kommer jag hit?")
max_read_bytes = 1024 * 20
while True:
socketio.sleep(0.01)
Expand All @@ -76,6 +85,7 @@ def container_terminal_input(data) -> None:
:param data: the input data to write
:return: None
"""
logger.info("kommer jag hit?")
cmd = data[api_constants.MGMT_WEBAPP.INPUT_PROPERTY].encode()
ssh_channel = app.config[api_constants.MGMT_WEBAPP.CONTAINER_TERMINAL_SSH_SHELL]
ssh_channel.send(cmd)
Expand Down Expand Up @@ -106,6 +116,7 @@ def container_terminal_connect() -> None:

:return: None
"""
logger.info("kommer jag hit?")
authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=True)
if authorized is not None or (constants.CONFIG_FILE.PARSED_CONFIG is None):
raise ConnectionRefusedError()
Expand All @@ -122,6 +133,6 @@ def container_terminal_connect() -> None:
socketio.start_background_task(target=read_and_forward_container_terminal_output)
else:
ConnectionRefusedError()

logger.info("vad returneras här?")
container_terminal_bp = Blueprint(api_constants.MGMT_WEBAPP.WS_CONTAINER_TERMINAL_NAMESPACE, __name__)
return container_terminal_bp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import json
import logging

import csle_common.constants.constants as constants
import pytest
import pytest_mock
from csle_cluster.cluster_manager.cluster_manager_pb2 import NodeStatusDTO
from csle_common.dao.emulation_config.config import Config

import csle_rest_api.constants.constants as api_constants
from csle_rest_api.rest_api import create_app

logger = logging.getLogger()


class TestResourcesFlaskSuite:
"""
Expand Down Expand Up @@ -126,6 +131,7 @@ def test_flask_get(self, flask_app, mocker: pytest_mock.MockFixture, logged_in_a
gpus = config.cluster_config.cluster_nodes[0].gpus
mocker.patch("csle_cluster.cluster_manager.cluster_controller.ClusterController.get_node_status",
side_effect=node_status_flask_running)
logger.info("hej")
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=not_logged_in)
response = flask_app.test_client().get(api_constants.MGMT_WEBAPP.FLASK_RESOURCE)
response_data = response.data.decode('utf-8')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging

import csle_common.constants.constants as constants
import paramiko
import pytest
import pytest_mock
from flask import Blueprint, Flask
from flask_socketio import ConnectionRefusedError, SocketIO

import csle_rest_api.constants.constants as api_constants
import csle_rest_api.web_sockets.container_terminal.container_terminal as container_terminal
from csle_rest_api.rest_api import create_app

# from csle_rest_api import socketio

logger = logging.getLogger()


class TestWebsocketSuite:
# app = Flask(__name__)
# app.config['SECRET_KEY'] = 'secret!'
# socketio = SocketIO(app)
@pytest.fixture
def flask_app(self):
"""
Gets the Flask app

:return: the flask app fixture representing the webserver
"""
return create_app(static_folder="../../../../../management-system/csle-mgmt-webapp/build")

def test_web_socket_ct(self, mocker: pytest_mock.MockFixture, flask_app) -> None:
logger.info("Hej")
# socketio = SocketIO(flask_app)
response = flask_app.test_client().on(api_constants.MGMT_WEBAPP.WS_CONTAINER_TERMINAL_INPUT_MSG,
namespace=f"{constants.COMMANDS.SLASH_DELIM}"
f"{api_constants.MGMT_WEBAPP.WS_CONTAINER_TERMINAL_NAMESPACE}")
logger.info(response)
Loading