Skip to content

Commit

Permalink
Merge pull request #409 from Limmen/cli_command
Browse files Browse the repository at this point in the history
Ossec and ryu manager CLI start/stop added
  • Loading branch information
Limmen authored Aug 3, 2024
2 parents f76022a + b79aa2d commit 4d80989
Showing 1 changed file with 236 additions and 3 deletions.
239 changes: 236 additions & 3 deletions simulation-system/libs/csle-cli/src/csle_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
"| snortmanager | elkmanager | trafficmanagers | trafficmanager | kafkamanager")
"| snortmanager | elkmanager | trafficmanagers | trafficmanager | kafkamanager "
"| ossecmanagers | ossecmanager | ryumanager")
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
"""
Stops an entity
Expand Down Expand Up @@ -747,6 +748,12 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
stop_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "kafkamanager":
stop_kafka_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "ossecmanagers":
stop_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "ossecmanager":
stop_ossec_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "ryumanager":
stop_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
else:
container_stopped = False
for node in config.cluster_config.cluster_nodes:
Expand Down Expand Up @@ -941,6 +948,54 @@ def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
bold=False)


def stop_ryu_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the ryu manager
:param ip: the ip of the node to stop the ryu manger
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_ryu_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if stopped.outcome:
click.secho(f"Stopping host managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Host managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def stop_ossec_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the ossec managers
:param ip: the ip of the node to stop the ossec mangers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_ossec_ids_managers(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if stopped.outcome:
click.secho(f"Stopping ossec managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Ossec managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the host manager
Expand Down Expand Up @@ -968,6 +1023,34 @@ def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet
bold=False)


def stop_ossec_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the ossec manager
:param ip: the ip of the node to stop the ossec manager
:param container_ip: the ip of the host to be stopped
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_ossec_ids_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet, container_ip=container_ip)
if stopped.outcome:
click.secho(
f"Stopping ossec manager with ip {container_ip} on port:"
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Ossec manager with ip {container_ip} is not "
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def stop_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the client manager
Expand Down Expand Up @@ -1333,7 +1416,8 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
"container-name | emulation-name | all | statsmanager | training_job "
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
"| trafficmanagers | trafficmanager | kafkamanager")
"| trafficmanagers | trafficmanager | kafkamanager | ossecmanagers | ossecmanager "
"| ryumanager")
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
container_ip: str, no_beats: bool) -> None:
"""
Expand Down Expand Up @@ -1406,6 +1490,12 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
start_traffic_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "kafkamanager":
start_kafka_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "ossecmanagers":
start_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "ossecmanager":
start_ossec_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "ryumanager":
start_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
else:
container_started = False
for node in config.cluster_config.cluster_nodes:
Expand Down Expand Up @@ -1604,6 +1694,30 @@ def start_host_managers(ip: str, emulation: str, ip_first_octet: int):
bold=False)


def start_ryu_manager(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting ryu manager
:param ip: the ip of the node to start ryu manager
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_ryu_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if operation_outcome.outcome:
click.secho(f"Starting ryu managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Ryu managers are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting host manager
Expand Down Expand Up @@ -1726,6 +1840,57 @@ def start_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first
bold=False)


def start_ossec_ids_managers(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting ossec managers
:param ip: the ip of the node to start ossec managers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_ossec_ids_managers(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if operation_outcome.outcome:
click.secho(f"Starting ossec ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Ossec ids managers are not started:"
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}", bold=False)


def start_ossec_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting ossec ids manager
:param ip: the ip of the node to start ossec ids manager
:param container_ip: the ip of the host to start
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_ossec_ids_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet, container_ip=container_ip)
if operation_outcome.outcome:
click.secho(f"Started ossec ids manager with ip {container_ip} on "
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Ossec ids manager with ip {container_ip} is not "
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def start_elk_manager(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting elk manager
Expand Down Expand Up @@ -2112,7 +2277,8 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
"clientmanager | snortmanagers | elkmanager | trafficmanagers | kafkamanager")
"clientmanager | snortmanagers | elkmanager | trafficmanagers | kafkamanager | "
"ossecmanagers | ryumanager")
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
@click.option('--all', is_flag=True, help='list all')
@click.option('--running', is_flag=True, help='list running only (default)')
Expand Down Expand Up @@ -2189,6 +2355,10 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
list_traffic_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "kafkamanager":
list_kafka_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "ossecmanagers":
list_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "ryumanager":
list_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
else:
container = get_running_container(name=entity)
if container is not None:
Expand Down Expand Up @@ -2223,6 +2393,32 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
click.secho(f"entity: {entity} is not recognized", fg="red", bold=True)


def list_ryu_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing ryu manager
:param ip: the ip of the node to list ryu manager
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
emulation_config = MetastoreFacade.get_emulation_by_name(name=emulation)
has_sdn = emulation_config.sdn_controller_config is not None
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
if has_sdn:
ryu_manager_info = ClusterController.get_ryu_managers_info(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
click.secho(ryu_manager_info)
else:
click.secho(f"Emulation with name: {emulation} does not have SDN.", fg="red", bold=True)


def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing host managers
Expand Down Expand Up @@ -2430,6 +2626,43 @@ def list_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> Non
click.secho('+' + '-' * 60 + '+', fg='white')


def list_ossec_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing ossec ids managers
:param ip: the ip of the node to list ossec ids managers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
ossec_manager_info = ClusterController.get_ossec_ids_managers_info(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)

click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{"Ossec ids manager IP":^30}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{"Ossec ids status":^29}', nl=False, fg='white')
click.secho('|', fg='white')

for i in range(len(ossec_manager_info.ips)):
status = "Running" if ossec_manager_info.ossecIdsManagersRunning[i] else "Stopped"
status_color = 'green' if ossec_manager_info.ossecIdsManagersRunning[i] else 'red'

click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{ossec_manager_info.ips[i]:^30}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{status:^29}', nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')


def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing client managers
Expand Down

0 comments on commit 4d80989

Please sign in to comment.