Created
October 19, 2015 02:57
-
-
Save sujayy1983/0c18d0c212cbb2688c8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Description: | |
This application maybe imported or used as a command line interface. | |
A skeleton that ll interact with REST API, xml-rpc, cli and other possible interfaces of network devices. | |
Mappings: It pickles data mappings locally in the 'mapping' folder. | |
Configurations: All the configurations are read locally from 'confs' - TODO | |
Logs: All logs are stored and rotated from the folder 'logs' - TODO | |
Before starting this app. Create a directory tree structure as following: | |
|- <this-application.py> | |
| | |
|- mapping/<all-pickled-files-here> | |
| | |
|- logs/<all-logs-here> | |
| | |
|- confs/<all-confs-here> | |
""" | |
__author__ = 'Sujayyendhiren Srinivasamurthi' | |
__email__ = 'sujayy1983@gmail.com' | |
import os | |
import sys | |
import logging | |
import argparse | |
import traceback | |
import cPickle as pickle | |
#Libraries for REST interface | |
#import requests | |
#Libraries for verification interface | |
#import pyez | |
class SpaceInterface(object): | |
""" Interaface with space.""" | |
def __init__(self): | |
"""Rules addition and deletion.""" | |
try: | |
self.target = None | |
self.rulename = None | |
self.srcZone = None | |
self.dstZone = None | |
self.srcIp = None | |
self.dstIp = None | |
self.srcPorts = [] | |
self.dstPorts = [] | |
self.fwrule_map = 'mapping/fwrule.map' | |
self.address_map = 'mapping/address.map' | |
self.fwrulemap = None | |
#This ll be done first time this application runs on a VM. | |
if os.path.exists(self.fwrule_map) == False: | |
self.reinit_data_mappings() | |
except: | |
print traceback.print_exc() | |
def reinit_data_mappings(self): | |
""" Reinitialize the data mapping file """ | |
pickle.dump( {}, open( self.fwrule_map, "wb" ) ) | |
def retrieve_data_mappings(self): | |
""" Retrieve pickled data. """ | |
self.fwrulemap = pickle.load(open(self.fwrule_map, "rb")) | |
return self.fwrulemap | |
def add_mapping_fwrule_resourceid(self, rulename, resourceid): | |
""" Store fw rulename and associated might want a similar function for saving address book mappings. """ | |
self.fwrulemap = pickle.load(open(self.fwrule_map, "rb")) | |
self.fwrulemap[rulename] = resourceid | |
pickle.dump(self.fwrulemap, open(self.fwrule_map, "wb")) | |
def arguments_parser(self): | |
""" argparse based arguments parser. If this file is imported then we do not need to call this method. """ | |
textDescription = 'This application offers an interface with JunOS space firewall rules addition/deletion' | |
textDescription += 'Adding a rule: python filename.py --vsrx <vsrx-hostname> --add --dzone untrust --szone Trust' | |
textDescription += 'Deleting a rule: python filename.py --vsrx <vsrx-hostname> --add --dzone untrust --szone Trust' | |
parser = argparse.ArgumentParser(description= textDescription, prog='Scrubbed name') | |
parser.add_argument('--version', action='version', version='version 0.1') | |
parser.add_argument('--vsrx', action='store', dest='vsrx_hostname', help='Hostname of vSRX', required=True) | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('--add', action='store_true', default=True, help='Add firewall rule') | |
group.add_argument('--del', action='store_false', default=False, help='Delete firewall rule') | |
parser.add_argument('--rulename', action='store', dest='addrulnam', help='Rule name', required=True) | |
parser.add_argument('--szone', action='store', dest='szone_name', help='Store a source zone name', required=True) | |
parser.add_argument('--dzone', action='store', dest='dzone_name', help='Store a destination zone name', required=True) | |
parser.add_argument('--sip', action='store', default=False, dest='src_ip', help='Set a switch to true', required=True) | |
parser.add_argument('--dip', action='store', default=False, dest='dst_ip', help='Set a switch to false', required=True) | |
parser.add_argument('--sport', action='store', dest='src_port', default=[], help='A list of source ports', required=True, type=int) | |
parser.add_argument('--dport', action='store', dest='dst_port', default=[], help='A list of destination ports', required=True, type=int) | |
results = parser.parse_args() | |
def rest_add_rule(self): | |
""" Call this function to add a firewall rule.""" | |
pass | |
def rest_del_rule(self): | |
pass | |
if __name__ == '__main__': | |
space_interface = SpaceInterface() | |
#Reset data mapping | |
#space_interface.reinit_data_mappings() | |
#space_interface.arguments_parser() | |
space_interface.add_mapping_fwrule_resourceid("test-rule1", 123458) | |
print space_interface.retrieve_data_mappings() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment