Beispiel #1
0
    def onOpen(self):
        print("WebSocket connection open.")

        # create a fabric port
        node = Node("PORT_TEST", "host-test",
                    metadata={"Name": "Test port !", "Type": "fabric"})
        msg = WSMessage("Graph", NodeAddedMsgType, node)
        self.sendWSMessage(msg)

        # get the TOR id
        restclient = RESTClient("localhost:8082")
        nodes = restclient.lookup_nodes("G.V().Has('Name', 'TOR2')")
        if len(nodes) != 1:
            print("More than one node found")
            sys.exit(-1)
        tor_id = nodes[0].id

        # create a ownership + layer2 link
        edge = Edge(tor_id + node.id + "ownership", "host-test",
                    tor_id, node.id,
                    metadata={"RelationType": "ownership"})
        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
        self.sendWSMessage(msg)

        edge = Edge(tor_id + node.id + "layer2", "",
                    tor_id, node.id,
                    metadata={"RelationType": "layer2", "Type": "fabric"})
        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
        self.sendWSMessage(msg)

        print("Success!")
        self.sendClose()
Beispiel #2
0
    def run(self):
        logging.info("thread threadGetSkydiveFlows starttime %d",
                     int(time.time() * 1000.0))
        err = ""
        restclient = RESTClient(SKYDIVE_IP + ":" + SKYDIVE_PORT)
        gremlinFlow = "G.Flows().Has('Application', 'TCP')"
        flows = restclient.lookup(gremlinFlow)
        dfOldFlows = json_normalize(flows)
        frames = []
        time_out = time.time() + TIME_OUT
        while (self.collectFlows) & (time.time() < time_out):
            flows = restclient.lookup(gremlinFlow)
            df = json_normalize(flows)
            if (not df.empty) & (not dfOldFlows.empty):
                cond = df['UUID'].isin(dfOldFlows['UUID']) == True
                df.drop(df[cond].index, inplace=True)
            if not df.empty:
                frames.append(df)
            sleep(1)
        if time.time() >= time_out:
            err = "Error: skydive time out"
            logging.info(err)

        df = pd.DataFrame()
        if len(frames) > 0:
            df = pd.concat(frames, sort=False)
            df = df.drop_duplicates()
            df = df.sort_values("Metric.Last", ascending=True)
            df = df.drop_duplicates(subset="UUID", keep='last')
        self._return = (df, err)
Beispiel #3
0
    def onOpen(self):
        print("WebSocket connection open.")

        # create a fabric port
        node = Node("PORT_TEST", "host-test",
                    metadata={"Name": "Test port !", "Type": "fabric"})
        msg = WSMessage("Graph", NodeAddedMsgType, node)
        self.sendWSMessage(msg)

        # get the TOR id
        restclient = RESTClient("localhost:8082")
        nodes = restclient.lookup_nodes("G.V().Has('Name', 'TOR2')")
        if len(nodes) != 1:
            print("More than one node found")
            sys.exit(-1)
        tor_id = nodes[0].id

        # create a ownership + layer2 link
        edge = Edge(tor_id + node.id + "ownership", "host-test",
                    tor_id, node.id,
                    metadata={"RelationType": "ownership"})
        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
        self.sendWSMessage(msg)

        edge = Edge(tor_id + node.id + "layer2", "",
                    tor_id, node.id,
                    metadata={"RelationType": "layer2", "Type": "fabric"})
        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
        self.sendWSMessage(msg)

        print("Success!")
        self.sendClose()
Beispiel #4
0
    def test_injections(self):
        restclient = RESTClient("localhost:8082",
                                scheme=self.schemeHTTP,
                                username=self.username,
                                password=self.password,
                                insecure=True)

        nodes = restclient.lookup("G.V().Has('Name', 'eth0')")

        testnode = nodes[0]["Metadata"]["TID"]

        query = "G.V().Has('TID', '" + testnode + "')"
        num_injections_before = len(restclient.injection_list())

        injection_response = restclient.injection_create(query,
                                                         query,
                                                         count=1000)

        num_injections_after = len(restclient.injection_list())

        self.assertEqual(num_injections_after, num_injections_before + 1,
                         "injection creation didn't succeed")

        restclient.injection_delete(injection_response.uuid)

        num_injections_after_deletion = len(restclient.injection_list())

        self.assertEqual(num_injections_after_deletion, num_injections_before,
                         "injection deletion didn't succeed")
Beispiel #5
0
    def test_injection(self):
        def create_node(protocol):
            node = Node("TOR_TEST",
                        "",
                        metadata={
                            "Name": "Test TOR",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            node = Node("PORT_TEST",
                        "",
                        metadata={
                            "Name": "Test port",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            edge = Edge("TOR_L2LINK",
                        "",
                        "TOR_TEST",
                        "PORT_TEST",
                        metadata={"RelationType": "layer2"})
            msg = WSMessage("Graph", EdgeAddedMsgType, edge)
            protocol.sendWSMessage(msg)

        self.wsclient = WSClient("host-test2",
                                 self.schemeWS +
                                 "://localhost:8082/ws/publisher",
                                 protocol=WSTestClient,
                                 test=create_node,
                                 username=self.username,
                                 password=self.password,
                                 insecure=True)
        self.wsclient.connect()
        self.wsclient.start()

        time.sleep(1)

        restclient = RESTClient("localhost:8082",
                                scheme=self.schemeHTTP,
                                username=self.username,
                                password=self.password,
                                insecure=True)
        nodes = restclient.lookup_nodes("G.V().Has('Name', 'Test port')")
        self.assertEqual(len(nodes), 1, "should find one an only one node")

        tor_id = nodes[0].id
        self.assertEqual(tor_id, nodes[0].id, "wrong id for node")

        edges = restclient.lookup_edges("G.E().Has('RelationType', 'layer2')")
        self.assertEqual(len(edges), 1, "should find one an only one edge")
Beispiel #6
0
def make_query(params):
    """Performs the query with the rest client

       :param params: the parameters of the ansible module implemented.
       :returns: a list of nodes coded as dictionary (JSON representation
            of skydive node).
    """
    scheme = "https" if params["ssl"] else "http"
    restclient = RESTClient(params["analyzer"],
                            scheme=scheme,
                            insecure=params["insecure"],
                            username=params["username"],
                            password=params["password"])
    nodes = restclient.lookup_nodes(params["query"])
    return [node.repr_json() for node in nodes]
Beispiel #7
0
 def clear_dragonflow_items(self):
     """Delete all the items created by DragonFlow"""
     restclient = RESTClient(cfg.CONF.df_skydive.analyzer_endpoint,
                             username=cfg.CONF.df_skydive.user,
                             password=cfg.CONF.df_skydive.password)
     items = restclient.lookup_edges("G.E().Has('source', 'dragonflow')")
     for edge in items:
         edge_del_msg = skydive_client.WSMessage(
             "Graph", skydive_client.EdgeDeletedMsgType, edge)
         self.protocol.sendWSMessage(edge_del_msg)
     items = restclient.lookup_nodes("G.V().Has('source', 'dragonflow')")
     for node in items:
         node_del_msg = skydive_client.WSMessage(
             "Graph", skydive_client.NodeDeletedMsgType, node)
         self.protocol.sendWSMessage(node_del_msg)
    def __init__(self, **kwargs):
        if not HAS_SKYDIVE_CLIENT:
            raise Exception('skydive-client is required but does not appear '
                            'to be installed.  It can be installed using the '
                            'command `pip install skydive-client`')

        if not set(kwargs.keys()).issubset(SKYDIVE_PROVIDER_SPEC.keys()):
            raise Exception(
                'invalid or unsupported keyword argument for skydive_restclient connection.'
            )
        for key, value in iteritems(SKYDIVE_PROVIDER_SPEC):
            if key not in kwargs:
                # apply default values from SKYDIVE_PROVIDER_SPEC since we cannot just
                # assume the provider values are coming from AnsibleModule
                if 'default' in value:
                    kwargs[key] = value['default']
                # override any values with env variables unless they were
                # explicitly set
                env = ('SKYDIVE_%s' % key).upper()
                if env in os.environ:
                    kwargs[key] = os.environ.get(env)
        kwargs['scheme'] = "http"
        if 'ssl' in kwargs:
            if kwargs['ssl']:
                kwargs['scheme'] = "https"
        if 'insecure' not in kwargs:
            kwargs['insecure'] = False
        self.restclient_object = RESTClient(kwargs['endpoint'],
                                            scheme=kwargs['scheme'],
                                            insecure=kwargs['insecure'],
                                            username=kwargs['username'],
                                            password=kwargs['password'])
Beispiel #9
0
def main():

    parser=argparse.ArgumentParser()
    parser.add_argument('--analyzer', type=str, required=True,
                        dest='analyzer',
                        help='address of the Skydive analyzer')
    parser.add_argument('--username', type=str, default="",
                        dest='username',
                        help='client username')
    parser.add_argument('--password', type=str, default="",
                        dest='password',
                        help='client password')
    parser.add_argument('--ssl', default=False,
                        dest='ssl',
                        action="store_true",
                        help='use a secure SSL/TLS connection')
    parser.add_argument('--insecure', default=False,
                        dest='insecure',
                        action="store_true",
                        help="insecure connection, don't verify certificate")
    parser.add_argument('--use-flows', default=False,
                        dest='use_flows',
                        action="store_true",
                        help="leverages flows for matrix computing")
    parser.add_argument('--at', type=str, default="",
                        dest='at',
                        help='gremlin time at context')
    parser.add_argument('--duration', type=int, default=0,
                        dest='duration',
                        help='gremlin time duration context')
    parser.add_argument('--list-no-peers', default=False,
                        dest='list_no_peers',
                        action="store_true",
                        help="list connection peer not found/tracked by skydive")
    parser.add_argument('--format', default="csv",
                        dest='format', choices=['csv', 'dot', 'render', 'skydive'],
                        help="specify the output format")
    parser.add_argument('--engine', default="dot",
                        dest='engine', choices=['dot', 'circo', 'neato'],
                        help="specify the rendering engine")

    args=parser.parse_args()

    scheme="http"
    if args.ssl:
        scheme="https"

    restclient=RESTClient(args.analyzer, scheme=scheme, insecure=args.insecure,
                            username=args.username, password=args.password)
    matrix=Matrix(restclient, args.use_flows, args.at, args.duration)

    if args.format == "dot" or args.format == "render":
        dot_output(matrix, args.engine, args.format == "render")
    elif args.format == "skydive":
        skydive_output(matrix)
    else:
        csv_output(matrix, args.list_no_peers)
Beispiel #10
0
 def new_rest_client(self):
     return RESTClient("localhost:8082",
                       scheme=self.schemeHTTP,
                       username=self.username,
                       password=self.password,
                       insecure=self.insecure,
                       cafile=self.cafile,
                       certfile=self.certfile,
                       keyfile=self.keyfile)
Beispiel #11
0
    def test_injection(self):
        def create_node(protocol):
            node = Node("TOR_TEST", "",
                        metadata={"Name": "Test TOR", "Type": "fabric"})
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            node = Node("PORT_TEST", "",
                        metadata={"Name": "Test port", "Type": "fabric"})
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            edge = Edge("TOR_L2LINK", "",
                        "TOR_TEST", "PORT_TEST",
                        metadata={"RelationType": "layer2"})
            msg = WSMessage("Graph", EdgeAddedMsgType, edge)
            protocol.sendWSMessage(msg)

        self.wsclient = WSClient("host-test2",
                                 self.schemeWS +
                                 "://localhost:8082/ws/publisher",
                                 protocol=WSTestClient, test=create_node,
                                 username=self.username,
                                 password=self.password,
                                 insecure=True)
        self.wsclient.connect()
        self.wsclient.start()

        time.sleep(1)

        restclient = RESTClient("localhost:8082",
                                scheme=self.schemeHTTP,
                                username=self.username,
                                password=self.password,
                                insecure=True)
        nodes = restclient.lookup_nodes("G.V().Has('Name', 'Test port')")
        self.assertEqual(len(nodes), 1, "should find one an only one node")

        tor_id = nodes[0].id
        self.assertEqual(tor_id, nodes[0].id, "wrong id for node")

        edges = restclient.lookup_edges(
            "G.E().Has('RelationType', 'layer2')")
        self.assertEqual(len(edges), 1, "should find one an only one edge")
Beispiel #12
0
def get_node_id(module, node_selector):
    scheme = "http"
    if module.params["ssl"]:
        scheme = "https"

    if node_selector.startswith("G.") or node_selector.startswith("g."):
        restclient = RESTClient(module.params["analyzer"],
                                scheme=scheme,
                                insecure=module.params["insecure"],
                                username=module.params["username"],
                                password=module.params["password"])
        nodes = restclient.lookup_nodes(node_selector)
        if len(nodes) == 0:
            raise Exception("Node not found: %s" % node_selector)
        elif len(nodes) > 1:
            raise Exception("Node selection should return only one node: %s" %
                            node_selector)

        return str(nodes[0].id)

    return node_selector
Beispiel #13
0
 def __init__(self, **kwargs):
     super(skydive_restclient, self).__init__(**kwargs)
     kwargs['scheme'] = "http"
     if 'ssl' in kwargs:
         if kwargs['ssl']:
             kwargs['scheme'] = "https"
     if 'insecure' not in kwargs:
         kwargs['insecure'] = False
     self.restclient_object = RESTClient(kwargs['endpoint'],
                                         scheme=kwargs['scheme'],
                                         insecure=kwargs['insecure'],
                                         username=kwargs['username'],
                                         password=kwargs['password'])
Beispiel #14
0
        def create_node(protocol):
            node = Node("TOR_TEST",
                        "",
                        metadata={
                            "Name": "Test TOR",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            node = Node("PORT_TEST",
                        "",
                        metadata={
                            "Name": "Test port",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            edge = Edge("TOR_L2LINK",
                        "",
                        "TOR_TEST",
                        "PORT_TEST",
                        metadata={"RelationType": "layer2"})
            msg = WSMessage("Graph", EdgeAddedMsgType, edge)
            protocol.sendWSMessage(msg)

            time.sleep(1)

            restclient = RESTClient("localhost:8082")
            nodes = restclient.lookup_nodes("G.V().Has('Name', 'Test port')")
            self.assertEqual(len(nodes), 1, "should find one an only one node")

            tor_id = nodes[0].id
            self.assertEqual(tor_id, nodes[0].id, "wrong id for node")

            edges = restclient.lookup_edges(
                "G.E().Has('RelationType', 'layer2')")
            self.assertEqual(len(edges), 1, "should find one an only one edge")
Beispiel #15
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--analyzer',
                        type=str,
                        required=True,
                        dest='analyzer',
                        help='address of the Skydive analyzer')
    parser.add_argument('--username',
                        type=str,
                        default="",
                        dest='username',
                        help='client username')
    parser.add_argument('--password',
                        type=str,
                        default="",
                        dest='password',
                        help='client password')
    parser.add_argument('--ssl',
                        default=False,
                        dest='ssl',
                        action="store_true",
                        help='use a secure SSL/TLS connection')
    parser.add_argument('--insecure',
                        default=False,
                        dest='insecure',
                        action="store_true",
                        help="insecure connection, don't verify certificate")
    parser.add_argument('--use-flows',
                        default=False,
                        dest='use_flows',
                        action="store_true",
                        help="leverages flows for matrix computing")

    args = parser.parse_args()

    scheme = "http"
    if args.ssl:
        scheme = "https"

    restclient = RESTClient(args.analyzer,
                            scheme=scheme,
                            insecure=args.insecure,
                            username=args.username,
                            password=args.password)
    result = matrix(restclient, args.use_flows)

    print("protocol,server,server_ip,port,server_proc,server_procname,"
          "client,client_ip,client_proc,client_procname")
    for e in result:
        print(e)
Beispiel #16
0
 def __init__(self, **kwargs):
     super(skydive_restclient, self).__init__(**kwargs)
     kwargs["scheme"] = "http"
     if "ssl" in kwargs:
         if kwargs["ssl"]:
             kwargs["scheme"] = "https"
     if "insecure" not in kwargs:
         kwargs["insecure"] = False
     self.restclient_object = RESTClient(
         kwargs["endpoint"],
         scheme=kwargs["scheme"],
         insecure=kwargs["insecure"],
         username=kwargs["username"],
         password=kwargs["password"],
     )
Beispiel #17
0
    def test_capture(self):
        restclient = RESTClient("localhost:8082",
                                scheme=self.schemeHTTP,
                                username=self.username,
                                password=self.password,
                                insecure=True)

        capture1 = restclient.capture_create(
            "G.V().Has('Name', 'test', 'Type', 'netns')")

        captures = restclient.capture_list()
        self.assertGreaterEqual(len(captures), 1, "no capture found")

        for capture in captures:
            if (capture.uuid == capture1.uuid):
                found = True
                break
        self.assertTrue(found, "created capture not found")

        restclient.capture_delete(capture1.uuid)
Beispiel #18
0
    def test_alert(self):
        restclient = RESTClient("localhost:8082",
                                scheme=self.schemeHTTP,
                                username=self.username,
                                password=self.password,
                                insecure=True)

        alert1 = restclient.alert_create(
            "https://localhost:8081",
            "G.V().Has('Name', 'alert-ns-webhook', 'Type', 'netns')")

        alerts = restclient.alert_list()
        self.assertGreaterEqual(len(alerts), 1, "no alerts found")
        for alert in alerts:
            if (alert.uuid == alert1.uuid):
                found = True
                break

        self.assertTrue(found, "created alert not found")

        restclient.alert_delete(alert1.uuid)
Beispiel #19
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--analyzer',
                        type=str,
                        required=True,
                        dest='analyzer',
                        help='address of the Skydive analyzer')
    parser.add_argument('--username',
                        type=str,
                        default="",
                        dest='username',
                        help='client username')
    parser.add_argument('--password',
                        type=str,
                        default="",
                        dest='password',
                        help='client password')
    parser.add_argument('--ssl',
                        default=False,
                        dest='ssl',
                        action="store_true",
                        help='use a secure SSL/TLS connection')
    parser.add_argument('--insecure',
                        default=False,
                        dest='insecure',
                        action="store_true",
                        help="insecure connection, don't verify certificate")
    parser.add_argument('--use-flows',
                        default=False,
                        dest='use_flows',
                        action="store_true",
                        help="leverages flows for matrix computing")
    parser.add_argument('--at',
                        type=str,
                        default="",
                        dest='at',
                        help='gremlin time at context')
    parser.add_argument('--duration',
                        type=int,
                        default=0,
                        dest='duration',
                        help='gremlin time duration context')
    parser.add_argument(
        '--list-no-peers',
        default=False,
        dest='list_no_peers',
        action="store_true",
        help="list connection peer not found/tracked by skydive")

    args = parser.parse_args()

    scheme = "http"
    if args.ssl:
        scheme = "https"

    restclient = RESTClient(args.analyzer,
                            scheme=scheme,
                            insecure=args.insecure,
                            username=args.username,
                            password=args.password)
    matrix = Matrix(restclient, args.use_flows, args.at, args.duration)
    result = matrix.get_matrix()

    print("protocol,server,server_ip,port,server_proc,server_procname,"
          "client,client_ip,client_proc,client_procname")
    for e in result:
        print(e)

    if args.list_no_peers:
        print("peer not found for connection:")
        for cnx in matrix.unknown_cnx:
            socket = cnx["socket"]
            print(socket["Protocol"], cnx["node"].host, socket["RemotePort"],
                  socket)
Beispiel #20
0
    def test_topology_rules(self):
        restclient = RESTClient("localhost:8082",
                                scheme=self.schemeHTTP,
                                username=self.username,
                                password=self.password,
                                insecure=True)

        noderule1 = restclient.noderule_create(
            "create", metadata={"Name": "node1", "Type": "fabric"})
        noderule2 = restclient.noderule_create(
            "create", metadata={"Name": "node2", "Type": "fabric"})

        time.sleep(1)

        edgerule = restclient.edgerule_create(
                "G.V().Has('Name', 'node1')", "G.V().Has('Name', 'node2')",
                {"RelationType": "layer2", "EdgeName": "my_edge"})

        time.sleep(1)

        node1 = restclient.lookup_nodes("G.V().Has('Name', 'node1')")
        self.assertEqual(len(node1), 1, "should find only one node as node1")

        node2 = restclient.lookup_nodes("G.V().Has('Name', 'node2')")
        self.assertEqual(len(node2), 1, "should find only one node as node2")

        edge = restclient.lookup_edges(
                "G.E().Has('RelationType', 'layer2', 'EdgeName', 'my_edge')")
        self.assertEqual(len(edge), 1, "should find only one edge")

        restclient.edgerule_delete(edgerule.uuid)
        restclient.noderule_delete(noderule1.uuid)
        restclient.noderule_delete(noderule2.uuid)
Beispiel #21
0
from skydive.rest.client import RESTClient
import yaml
conf_vars = yaml.load(open('tests_conf.yaml'))
SKYDIVE_IP = conf_vars.get('skydive_ip', '9.148.244.26')
SKYDIVE_PORT = conf_vars.get('skydive_port', '30777')
restclient = RESTClient(SKYDIVE_IP + ":" + SKYDIVE_PORT)
captures = restclient.capture_list()
print("capture before")
for capture in captures:
    print("capture", capture)

restclient.capture_create(
    "G.V().Has('Manager', NE('k8s'),'Docker.Labels.app', Regex('.*wordpress.*'),'Docker.Labels.tier', Regex('frontend')).Both().Out('Name','eth0')"
)
print("captures after")
captures = restclient.capture_list()
for capture in captures:
    print("capture", capture)