Exemple #1
0
def Main():

    # Connect and Authenticate
    conn = grpc.insecure_channel('%s:%d' % (options.HOST, int(options.PORT)))
    auth_stub = authentication_service_pb2_grpc.LoginStub(conn)
    login = auth_stub.LoginCheck(
            auth_svc.LoginRequest(user_name=options.USER,
                                  password=options.PASSWORD,
                                  client_id=options.CLIENT_ID), 
            TIMEOUT)
    if login:
        print ("Successfully connected to server %s:%s as %s" % (
            options.HOST, options.PORT, options.USER))
    else:
        print ("ERROR: Login to server server %s:%s as %s FAILED" % (
            options.HOST, options.b, routes_req))
        sys.exit(1)

    # Create the RIB service stub
    rib = rib_service_pb2_grpc.RibStub(conn)

    # Read routes
    print ('Reading JSON input file...')
    f = open(options.FILE, "rb")
    routes_req = jsonf.Parse(f.read(), rib_svc.RouteUpdateRequest())
    print (routes_req)

    # Do stuff
    if options.OPER == 'add':
            add_routes(rib, routes_req)
    elif options.OPER == 'del':
            del_routes(rib, routes_req)
    else:
            print ('Invalid operation')
def Main():

    # Connect and Authenticate
    conn = grpc.insecure_channel('%s:%d' % (options.HOST, int(options.PORT)))
    auth_stub = authentication_service_pb2_grpc.LoginStub(conn)
    login = auth_stub.LoginCheck(
        auth_svc.LoginRequest(user_name=options.USER,
                              password=options.PASSWORD,
                              client_id=options.CLIENT_ID), TIMEOUT)
    if login:
        print "Successfully connected to server %s:%s as %s" % (
            options.HOST, options.PORT, options.USER)
    else:
        print "ERROR: Login to server server %s:%s as %s FAILED" % (
            options.HOST, options.PORT, options.USER)
        sys.exit(1)

    # Create the RIB service stub
    rib = rib_service_pb2_grpc.RibStub(conn)

    # Open the output file
    f = open(options.FILE, "wb")

    # Make the request for all IPv4 routes in the given table

    match = rib_svc.RouteMatchFields(
        dest_prefix=prpd.NetworkAddress(inet=jnx_addr.IpAddress(
            addr_string="0.0.0.0")),
        dest_prefix_len=0,
        table=prpd.RouteTable(rtt_name=prpd.RouteTableName(
            name=options.TABLE)))
    if (options.TAG):
        match_tags = {}
        match_tags[0] = rib_svc.RouteAttributeUint32(value=int(options.TAG))
        ext_match = rib_svc.RouteExtendedMatchFields(tags=match_tags)
    else:
        ext_match = None
    this_req = rib_svc.RouteGetRequest(key=match,
                                       extended_match=ext_match,
                                       match_type=rib_svc.EXACT_OR_LONGER,
                                       route_count=1000)
    getStream = rib.RouteGet(this_req, TIMEOUT)

    rt_count = 0
    cummulative_reply = rib_svc.RouteGetReply()
    for i in getStream:
        #print "Read reply with %d routes" % len(i.routes)
        #print i
        cummulative_reply.status = i.status
        cummulative_reply.routes.extend(i.routes)
        rt_count += len(i.routes)

    f.write(jsonf.MessageToJson(cummulative_reply))
    f.close()
    print "Wrote %d JSON routes to %s" % (rt_count, options.FILE)
Exemple #3
0
def add_firewall_filter(intf):
    channel = grpc.insecure_channel('vqfx:32767')
    auth_stub = authentication_service_pb2_grpc.LoginStub(channel)
    response = auth_stub.LoginCheck(
        auth.LoginRequest(
            user_name='antidote',
            password='******',
            client_id='jet',
        ))

    fw_stub = firewall_service_pb2_grpc.AclServiceStub(channel)
    filter = fw.AccessList(
        acl_name='filter-by-jet',
        acl_type=fw.ACL_TYPE_CLASSIC,
        acl_family=fw.ACL_FAMILY_INET,
        acl_flag=fw.ACL_FLAGS_NONE,
        ace_list=[
            fw.AclEntry(inet_entry=fw.AclInetEntry(
                ace_name='t1',
                ace_op=fw.ACL_ENTRY_OPERATION_ADD,
                adjacency=fw.AclAdjacency(type=fw.ACL_ADJACENCY_AFTER),
                matches=fw.AclEntryMatchInet(match_protocols=[
                    fw.AclMatchProtocol(
                        min=1, max=1, match_op=fw.ACL_MATCH_OP_EQUAL)
                ]),
                actions=fw.AclEntryInetAction(
                    action_t=fw.AclEntryInetTerminatingAction(action_accept=1),
                    actions_nt=fw.AclEntryInetNonTerminatingAction(
                        action_log=1)))),
            fw.AclEntry(inet_entry=fw.AclInetEntry(
                ace_name='t2',
                ace_op=fw.ACL_ENTRY_OPERATION_ADD,
                adjacency=fw.AclAdjacency(type=fw.ACL_ADJACENCY_AFTER),
                actions=fw.AclEntryInetAction(
                    action_t=fw.AclEntryInetTerminatingAction(
                        action_discard=1),
                    actions_nt=fw.AclEntryInetNonTerminatingAction(
                        action_log=1)),
            ))
        ])
    fw_stub.AccessListAdd(filter)

    fw_stub.AccessListBindAdd(
        fw.AccessListObjBind(acl=filter,
                             obj_type=fw.ACL_BIND_OBJ_TYPE_INTERFACE,
                             bind_object=fw.AccessListBindObjPoint(intf=intf),
                             bind_direction=fw.ACL_BIND_DIRECTION_INPUT,
                             bind_family=fw.ACL_FAMILY_INET))
Exemple #4
0
def stubAuth(DEFAULT_JSD_HOST, DEFAULT_JSD_PORT, USER, PASSWORD, DEFAULT_CLIENT_ID, JET_TIMEOUT):
    channel = grpc.insecure_channel('%s:%d' %(DEFAULT_JSD_HOST,DEFAULT_JSD_PORT))
    stub = authentication_service_pb2_grpc.LoginStub(channel)
    login_response = stub.LoginCheck(authentication_service_pb2.LoginRequest(
                                                                user_name=USER, 
                                                                password=PASSWORD,
                                                                client_id=DEFAULT_CLIENT_ID
                                                                ),
                                     JET_TIMEOUT
                                    )
    print(login_response.result)
    bfd = bfd_service_pb2_grpc.BFDStub(channel)
    try:
        result = bfd.Initialize(InitializeRequest())
        print(result.status)
        logging.info(result.status)
    except:
        print("Initialization Failed: ")
        logging.error("BFD JET Initialization failed")
    return bfd, channel
def Main():

    # Connect and Authenticate
    conn = grpc.insecure_channel('%s:%d' % (options.HOST, int(options.PORT)))
    auth_stub = authentication_service_pb2_grpc.LoginStub(conn)
    login = auth_stub.LoginCheck(
        auth_svc.LoginRequest(user_name=options.USER,
                              password=options.PASSWORD,
                              client_id=options.CLIENT_ID), TIMEOUT)
    if login:
        print "Successfully connected to server %s:%s as %s" % (
            options.HOST, options.PORT, options.USER)
    else:
        print "ERROR: Login to server server %s:%s as %s FAILED" % (
            options.HOST, options.PORT, options.USER)
        sys.exit(1)

    # Create the RIB service stub
    ft = flexible_tunnel_service_pb2_grpc.FlexibleTunnelStub(conn)

    # Read routes
    print 'Reading JSON input file...'
    f = open(options.FILE, "rb")

    # Do stuff
    if options.OPER == 'add':
        ft_req = jsonf.Parse(f.read(), ft_svc.FlexibleTunnelAddRequest())
        add_profiles(ft, ft_req)
    elif options.OPER == 'upd':
        ft_req = jsonf.Parse(f.read(), ft_svc.FlexibleTunnelUpdateRequest())
        add_profiles(ft, ft_req)
    elif options.OPER == 'del':
        ft_req = jsonf.Parse(f.read(), ft_svc.FlexibleTunnelDeleteRequest())
        del_profiles(ft, ft_req)
    elif options.OPER == 'sample':
        show_sample()
Exemple #6
0
    def open(self):
        """
        Opens a grpc channel

        :return: True, upon successful creation & authentication of channel,
                 False, in case of failure

        """

        timeout = self.timeout

        import grpc  # pylint: disable=import-error
        import authentication_service_pb2  # pylint: disable=import-error
        import authentication_service_pb2_grpc  # pylint: disable=import-error
        from grpc.beta import implementations  # pylint: disable=import-error

        try:
            host_port = self.host + ':' + str(self.port)

            if self.bypass_jsd is True:
                logging.info('Using implementations.insecure_channel on %s',
                             self.host)

                channel = implementations.insecure_channel(host=self.host,
                                                           port=self.port)
            else:
                #                 channel = grpc.insecure_channel(
                #                     target=host_port,
                #                 )
                # use_local_subchannel_pool is supported from grpcio=1.19.0
                # Helps retain the multi channel functionality of gRPC
                # Automatically ignored for unsupported versions by gRPC (backwards compatible)

                channel = grpc.insecure_channel(
                    host_port, options=[('grpc.use_local_subchannel_pool', 1)])

            # Generate random client id
            client_id_list = []
            #client_id = str(random.randint(100000, 1000000))
            #Making the client_id same as channel_id

            client_id = self.channel_id
            client_id_list.append(client_id)
            self.client_id = client_id
            # while True:
            #     if client_id not in client_id_list:
            #         client_id_list.append(client_id)
            #         self.client_id = client_id
            #         break
            #     else:
            #         client_id = str(random.randint(100000, 1000000))
            if self.bypass_jsd is not True:
                stub = authentication_service_pb2_grpc.LoginStub(channel)
                logging.info("Channel created via JSD")

                try:
                    login_response = \
                        stub.LoginCheck(
                            authentication_service_pb2.LoginRequest(
                                user_name=self.user,
                                password=self.password,
                                client_id=client_id
                            ),
                            timeout
                        )

                    if login_response.result:
                        logging.info(
                            'GRPC client-server connect is successful')
                    else:
                        logging.error('GRPC client-server connect has failed')
                        return False

                except Exception as exp:
                    logging.error(exp)
                    return False

            self.channel = channel
            logging.info('GRPC channel created successfully for %s', self.host)
            return True

        except Exception as exp:
            logging.error(exp)
            return False