def show_gcp_session_detail(self): msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = GcpMsgType.ShowGcpSessionDetail rspData = self.cliEntry(msg) if rspData is None: self.cli.log.error("recv Msg with None string data") return rsp = t_CliMessage() rsp.ParseFromString(rspData) if rsp.CliMsgType != GcpMsgType.ShowGcpSessionDetail: self.cli.log.error("recv Msg with incorrect type") return if rsp.CliDataResult != rsp.CLI_RESULT_OK: self.cli.log.error("recv Msg with respond result %s" % rsp.CliDataResult) return if not rsp.HasField("CliGcp"): self.cli.log.error("recv Msg without respond data") return if len(rsp.CliGcp.ShowGcpStats) == 0: self.cli.log.error("recv Msg without GCP session statistics data") return print "GCP session statistics:" for session_stats in rsp.CliGcp.ShowGcpStats: print "\n" + session_stats.sessions.session session_stats.ClearField("sessions") for (field, value) in session_stats.ListFields(): print "{}:{}".format(field.name, value)
def show_gcp_session(self): msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = GcpMsgType.ShowGcpSession rspData = self.cliEntry(msg) if rspData is None: self.cli.log.error("recv Msg with None string data") return rsp = t_CliMessage() rsp.ParseFromString(rspData) if rsp.CliMsgType != GcpMsgType.ShowGcpSession: self.cli.log.error("recv Msg with incorrect type") return if rsp.CliDataResult != rsp.CLI_RESULT_OK: self.cli.log.error("recv Msg with respond result %s" % rsp.CliDataResult) return if not rsp.HasField("CliGcp"): self.cli.log.error("recv Msg without respond data") return if not rsp.CliGcp.HasField("ShowGcpSession"): self.cli.log.error("recv Msg without GCP session data") return gs = rsp.CliGcp.ShowGcpSession print "GCP session information" print "\nActive sessions:" for session in gs.ActiveSessions: print session.session print "\nPrincipal session:" for session in gs.PrincipalSessions: print session.session if len(gs.PrincipalSessions) == 0: print "None" print "\nPrincipal candidate session" print gs.PrincipalCandidateSession.session print "\nNon Principal sessions:" for session in gs.NonPrincipalSessions: print session.session if len(gs.NonPrincipalSessions) == 0: print "None" print "\nFailed sessions:" for session in gs.FailedSessions: print session.session if len(gs.FailedSessions) == 0: print "None" print "\n"
def show_8021x_detail(self): """'show 8021x cabllback.""" msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = MacsecMsgType.Show8021xDetail rspData = self.cliEntry(msg) if rspData is None: self.cli.log.error("recv Msg with None string data") return rsp = t_CliMessage() rsp.ParseFromString(rspData) if rsp is None: self.cli.log.error("recv Msg with None data") return if rsp.CliMsgType != MacsecMsgType.Show8021xDetail: self.cli.log.error("recv Msg with incorrect type") return if rsp.CliDataResult != rsp.CLI_RESULT_OK: self.cli.log.error("recv Msg with respond result %s" % rsp.CliDataResult) return if not rsp.HasField("Cli8021x"): self.cli.log.error("recv Msg without respond data") return para = json.loads(rsp.Cli8021x.Show8021xStatus.status) name = "%-17s%-20s%-17s" % ("Interface", "EAP_Received", "Status") print_list = list() print_list.append(name) for dic in para: try: summary_info = "%-17s%-20s%-17s" % (dic["Interface"], dic["eap"], dic["Status"]) print_list.append(summary_info) print_list.append(" ") print_list.append(dic["Details"]) print_list.append(" ") except KeyError as e: self.cli.log.error("can't get key[%s] from msg" % str(e)) return for field in print_list: print field
def process_cli_action(self, msg): """Process the request from the CLI module. :param msg: message from CLI module :type: t_CliMessage :return: """ self.logger.debug("Receive an CLI message:%s", msg) rsp_msg = t_CliMessage() rsp_msg.CliMsgType = msg.CliMsgType rsp_msg.CliDataOperation = msg.CliDataOperation if msg.CliMsgType in self.CliMsgsHandler: handler = self.CliMsgsHandler[msg.CliMsgType] ret, value = handler() if ret: rsp_msg.CliDataResult = rsp_msg.CLI_RESULT_OK else: rsp_msg.CliDataResult = rsp_msg.CLI_RESULT_FAIL rsp_msg.CliIkev2.ShowIkev2Status.status = json.dumps(value) else: self.logger.debug("Receive a fake CLI message:%s" % str(msg)) rsp_msg.CliDataResult = rsp_msg.CLI_RESULT_NONE self.send_cli_rsp(rsp_msg)
def show_ikev2_status(self): """'show ikev2' cabllback.""" msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = Ikev2MsgType.ShowIkev2Session rspData = self.cliEntry(msg) if rspData is None: self.cli.log.error("recv Msg with None string data") return rsp = t_CliMessage() rsp.ParseFromString(rspData) if rsp is None: self.cli.log.error("recv Msg with None data") return if rsp.CliMsgType != Ikev2MsgType.ShowIkev2Session: self.cli.log.error("recv Msg with incorrect type") return if rsp.CliDataResult != rsp.CLI_RESULT_OK: self.cli.log.error("recv Msg with respond result %s" % rsp.CliDataResult) return if not rsp.HasField("CliIkev2"): self.cli.log.error("recv Msg without respond data") return para = json.loads(rsp.CliIkev2.ShowIkev2Status.status) name = ("Core-id", "Local", "Remote", "Status") print_list = list() print_list.append(name) for dic in para: try: print_list.append((dic["Core-id"], dic["Local"], dic["Remote"], dic["Status"])) except KeyError as e: self.cli.log.error("can't get key[%s] from msg" % str(e)) return for field in print_list: print "%-17s%-17s%-17s%-17s" % field
def test_process_cli_action(self): self.agent.status_8021x['eth0'] = { "status": self.agent.DOWN, "lastChangeTime": time.time(), "ccap_core_id": ['CORE-1234567890'], "count": 0, "eap_received": False, } msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = MacsecMsgType.Show8021xSummary self.agent.process_cli_action(msg) msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = MacsecMsgType.Show8021xDetail self.agent.process_cli_action(msg) msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = 3 self.agent.process_cli_action(msg)
def _cli_msg_cb(self, fd, eventmask): # pragma: no cover del eventmask try: while self.cli_sock.getsockopt(zmq.EVENTS) and zmq.POLLIN: zmq_msg = self.cli_sock.recv(flags=zmq.NOBLOCK) self.logger.debug("GCP CLI message received, len[%d]", len(zmq_msg)) if len(zmq_msg) > 0: msg = t_CliMessage() msg.ParseFromString(zmq_msg) self.logger.debug("Receive an GCP CLI message:%s", msg) rsp_msg = t_CliMessage() rsp_msg.CliMsgType = msg.CliMsgType rsp_msg.CliDataOperation = msg.CliDataOperation if msg.CliMsgType in self.CliMsgsHandler: handler = self.CliMsgsHandler[msg.CliMsgType] ret = handler(msg, rsp_msg) if ret: rsp_msg.CliDataResult = rsp_msg.CLI_RESULT_OK else: rsp_msg.CliDataResult = rsp_msg.CLI_RESULT_FAIL else: self.logger.debug("Receive a fake CLI message:%s" % str(msg)) rsp_msg.CliDataResult = rsp_msg.CLI_RESULT_NONE self.cli_sock.send(rsp_msg.SerializeToString(), flags=zmq.NOBLOCK) except zmq.Again: # Ignore ... retry handled by dispatcher return except DecodeError as exception: self.logger.error("Malformed CLI message, dropping ...: %s", exception.message) return
def gcp_logging_entry(self, module, parameters): msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_WRITE msg.CliMsgType = GcpMsgType.ChangeGcpLoggingLevel if parameters[0] not in [ 'debug', 'info', 'warning', 'error', 'critical' ]: print "invalid logging level {}".format(parameters[0]) msg.CliGcp.GcpLogging.module = module msg.CliGcp.GcpLogging.level = parameters[0] rspData = self.cliEntry(msg) if rspData is None: self.cli.log.error("recv Msg with None string data") return rsp = t_CliMessage() rsp.ParseFromString(rspData) if rsp.CliDataResult == t_CliMessage.CLI_RESULT_OK: print "set gcp debug level({}) successfully".format(parameters[0]) else: print "set gcp debug level({}) failed".format(parameters[0])
def test_cli(self): self.process.orchestrator.sessions_active_fd[ self.desc.get_uniq_id()] = self.session self.process.orchestrator.sessions_failed[ self.desc.get_uniq_id()] = self.session self.process.orchestrator.principal.append(self.session) self.process.orchestrator.non_principals.append(self.session) msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = GcpMsgType.ShowGcpSession rsp_msg = t_CliMessage() rsp_msg.CliMsgType = msg.CliMsgType rsp_msg.CliDataOperation = msg.CliDataOperation self.process.show_gcp_session(msg, rsp_msg) print rsp_msg msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = GcpMsgType.ShowGcpSessionDetail rsp_msg = t_CliMessage() rsp_msg.CliMsgType = msg.CliMsgType rsp_msg.CliDataOperation = msg.CliDataOperation self.process.show_gcp_session_detail(msg, rsp_msg) print rsp_msg msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_WRITE msg.CliMsgType = GcpMsgType.ChangeGcpLoggingLevel msg.CliGcp.GcpLogging.module = GcpMsgType.GcpGDM msg.CliGcp.GcpLogging.level = 'debug' rsp_msg = t_CliMessage() rsp_msg.CliMsgType = msg.CliMsgType rsp_msg.CliDataOperation = msg.CliDataOperation ret = self.process.change_gcp_logging_level(msg, rsp_msg) self.assertTrue(ret) print rsp_msg msg.CliGcp.GcpLogging.level = 'notsupported' ret = self.process.change_gcp_logging_level(msg, rsp_msg) self.assertFalse(ret) msg.CliGcp.GcpLogging.module = GcpMsgType.GcpAll msg.CliGcp.GcpLogging.level = 'debug' ret = self.process.change_gcp_logging_level(msg, rsp_msg) self.assertFalse(ret)
def _process_cli_callback(self, fd, eventmask): """Callback function for API socket. :param fd: passed from the dispatcher, the fd should contains the transport information :param eventmask: passed from the dispatcher, which indicates the event type. :return: None """ # Receive the msg from the remote if eventmask == 0 or self.cli_transport.sock != fd: self.logger.warn("Got a fake cli message, ignore it") return # FixMe: may need more action if eventmask & self.dispatcher.EV_FD_ERR: self.logger.error("Got an error when receiving the msg.") return if self.cli_transport.sock.getsockopt(zmq.EVENTS) != zmq.POLLIN: self.logger.debug( "Got a fake cli message, the receive is not ready!") return try: data = self.cli_transport.sock.recv(flags=zmq.NOBLOCK) msg = t_CliMessage() msg.ParseFromString(data) self.logger.debug("Receive an CLI message: %s" % str(msg)) self.process_cli_action(msg) except zmq.Again as e: pass except Exception as e: self.logger.error("Cannot process the CLI message, reason:%s" % str(e))
def show_ikev2_status_detail(self): """'show ikev2' cabllback.""" msg = t_CliMessage() msg.CliDataOperation = t_CliMessage.CLI_CFG_READ msg.CliMsgType = Ikev2MsgType.ShowIkev2SessionDetail rspData = self.cliEntry(msg) if rspData is None: self.cli.log.error("recv Msg with None string data") return rsp = t_CliMessage() rsp.ParseFromString(rspData) if rsp is None: self.cli.log.error("recv Msg with None data") return if rsp.CliMsgType != Ikev2MsgType.ShowIkev2SessionDetail: self.cli.log.error("recv Msg with incorrect type") return if rsp.CliDataResult != rsp.CLI_RESULT_OK: self.cli.log.error("recv Msg with respond result %s" % rsp.CliDataResult) return if not rsp.HasField("CliIkev2"): self.cli.log.error("recv Msg without respond data") return para = json.loads(rsp.CliIkev2.ShowIkev2Status.status) print para name = "%-17s%-20s%-20s%-17s" % ("Core-id", "Local", "Remote", "Status") print_list = list() print_list.append(name) for ike_conn in para: for key in ike_conn.keys(): core_id = key value = ike_conn[key] try: ip_info = "%-17s%-20s%-20s%-17s" % (core_id, value["local-host"] + "/" + value["local-port"], value["remote-host"] + "/" + value["remote-port"], value["state"]) print_list.append(ip_info) print_list.append(" ") print_list.append((" Encr:" + value["encr-alg"] + "," + "keysize:" + value["encr-keysize"] + "," + "PRF:" + value["prf-alg"] + "," + "DH Grp:" + value["dh-group"] + "," + "Hash:" + value["integ-alg"])) # no this field if the node is a responder if "reauth-time" not in value: value["reauth-time"] = "unknown" print_list.append((" Active time:" + value["established"] + "," + "Reauth time:" + value["reauth-time"])) # no this field if the node is a responder if "initiator" not in value: value["initiator"] = "No" print_list.append((" Local spi:" + value["initiator-spi"] + " " "Remote spi:" + value["responder-spi"] + ", " + "Initiator:" + value["initiator"])) print_list.append((" Local id:" + value["local-id"])) print_list.append((" Remote id:" + value["remote-id"])) # print the child sa info print_list.append(" ") print_list.append("Child sa:") for child_sa in value["child-sas"]: child_value = value["child-sas"][child_sa] local_ts = "" remote_ts = "" for item in child_value["local-ts"]: local_ts += item + " " for item in child_value["remote-ts"]: remote_ts += item + " " print_list.append((" " + child_sa + ":")) print_list.append((" Local Selector:" + local_ts)) print_list.append((" Remote Selector:" + remote_ts)) print_list.append((" Protocol:" + child_value["protocol"] + "," + "mode:" + child_value["mode"])) print_list.append((" spi in/out:" + child_value["spi-in"] + "/" + child_value["spi-out"])) # no encr-keysize if use null encryption if "encr-keysize" not in child_value: child_value["encr-keysize"] = "Null" print_list.append((" Encr:" + child_value["encr-alg"] + "," + "keysize:" + child_value["encr-keysize"] + ",Hash:" + child_value["integ-alg"])) print_list.append((" Bytes-in/out:" + child_value["bytes-in"] + "/" + child_value["bytes-out"])) print_list.append((" Packets-in/out:" + child_value["packets-in"] + "/" + child_value["packets-out"])) print_list.append((" Active time:" + child_value["install-time"] + "s," + "life time:" + child_value["life-time"] + "s," + "rekey time:" + child_value["rekey-time"] + "s")) print_list.append(" ") except KeyError as e: self.cli.log.error("can't get key[%s] from msg" % str(e)) return for field in print_list: print field