Пример #1
0
    def rule_add(self):
        print self.TEXT_HELP_RULE_ADD
        buf_in = raw_input(self.PROMPT_RULE_ADD)
        items = buf_in.split(" ")

        if len(items) != 6:
            print "Expected 6 arguments, " + str(len(items)) + " given."
            return
        items[2] = items[2].lower()
        errors = rule_syntax.check_rule(items[0], items[1], items[2],
                                        items[3], items[4])
        if len(errors) != 0 :
            print "Invalid rule provided:"
            for e in errors:
                print "\t" + e
            return
        add_req = self.rule_to_json(items[0], items[1], items[2],
                                    items[3], items[4], items[5])
        try:
            resp = requests.post(self.URL_ACLSWITCH_RULE, data=add_req,
                                headers = {"Content-type": "application/json"})
            print("adding request" + add_req)
        except:
            print self.TEXT_ERROR_CONNECTION
            return
        if resp.status_code != 200:
            print("Error creating resource, HTTP " + str(resp.status_code))
        print resp.text
Пример #2
0
 def rule_add(self):
     print self.TEXT_HELP_RULE_ADD
     buf_in = raw_input(self.PROMPT_RULE_ADD)
     items = buf_in.split(" ")
     if len(items) != 6:
         print "Expected 6 arguments, " + str(len(items)) + " given."
         return
     items[2] = items[2].lower()
     errors = rule_syntax.check_rule(items[0], items[1], items[2],
                                     items[3], items[4])
     if len(errors) != 0 :
         print "Invalid rule provided:"
         for e in errors:
             print "\t" + e
         return
     add_req = self.rule_to_json(items[0], items[1], items[2],
                                 items[3], items[4], items[5])
     try:
         resp = requests.post(self.URL_ACLSWITCH_RULE, data=add_req,
                             headers = {"Content-type": "application/json"})
     except:
         print self.TEXT_ERROR_CONNECTION
         return
     if resp.status_code != 200:
         print("Error creating resource, HTTP " + str(resp.status_code))
     print resp.text
Пример #3
0
 def _acl_rule_syntax_check(self, ip_src, ip_dst, tp_proto, port_src,
                            port_dst):
     errors = rule_syntax.check_rule(ip_src, ip_dst, tp_proto, port_src,
                                     port_dst)
     error_msg = "Provided rule has invalid syntax:"
     if len(errors) != 0:
         for e in errors:
             error_msg = error_msg + "\n\t" + e
         return (False, error_msg)
     return (True, "Rule syntax is valid.")
Пример #4
0
 def _acl_rule_syntax_check(self, ip_src, ip_dst, tp_proto,
                            port_src, port_dst):
     errors = rule_syntax.check_rule(ip_src, ip_dst, tp_proto,
                                     port_src, port_dst)
     error_msg = "Provided rule has invalid syntax:"
     if len(errors) != 0:
         for e in errors:
             error_msg = error_msg + "\n\t" + e
         return (False, error_msg)
     return (True, "Rule syntax is valid.")
Пример #5
0
 def rule_time(self):
     print self.TEXT_HELP_RULE_TIME
     buf_in = raw_input(self.PROMPT_RULE_TIME)
     items = buf_in.split(" ")
     if len(items) != 8:
         print "Expected 8 arguments, " + str(len(items)) + " given."
         return
     items[2] = items[2].lower()
     errors = rule_syntax.check_rule(items[0], items[1], items[2],
                                     items[3], items[4])
     if len(errors) != 0 :
         print "Invalid rule provided:"
         for e in errors:
             print "\t" + e
         return
     # Check that the given time is valid
     try:
         datetime.strptime(items[6], "%H:%M")
     except:
         print self.TEXT_ERROR_SYNTAX_TIME_START        
         return
     # Check that the duration for the rule is valid
     try:
         duration = int(items[7])
         if (duration > self.TIME_MAX_MINUTES
             or duration < self.TIME_MIN_MINUTES):
             raise
     except:
         print self.TEXT_ERROR_SYNTAX_TIME_DURATION
         return
     add_req = self.rule_time_to_json(items[0], items[1], items[2],
                                      items[3], items[4], items[5],
                                      items[6], str(duration*60))
     try:
         resp = requests.post(self.URL_ACLSWITCH_TIME, data=add_req,
                             headers = {"Content-type": "application/json"})
     except:
         print self.TEXT_ERROR_CONNECTION
         return
     if resp.status_code != 200:
         print("Error creating resource, HTTP " + str(resp.status_code))
     print resp.text
Пример #6
0
 def rule_time(self):
     print self.TEXT_HELP_RULE_TIME
     buf_in = raw_input(self.PROMPT_RULE_TIME)
     items = buf_in.split(" ")
     if len(items) != 8:
         print "Expected 8 arguments, " + str(len(items)) + " given."
         return
     items[2] = items[2].lower()
     errors = rule_syntax.check_rule(items[0], items[1], items[2],
                                     items[3], items[4])
     if len(errors) != 0 :
         print "Invalid rule provided:"
         for e in errors:
             print "\t" + e
         return
     # Check that the given time is valid
     try:
         datetime.strptime(items[6], "%H:%M")
     except:
         print self.TEXT_ERROR_SYNTAX_TIME_START        
         return
     # Check that the duration for the rule is valid
     try:
         duration = int(items[7])
         if (duration > self.TIME_MAX_MINUTES
             or duration < self.TIME_MIN_MINUTES):
             raise
     except:
         print self.TEXT_ERROR_SYNTAX_TIME_DURATION
         return
     add_req = self.rule_time_to_json(items[0], items[1], items[2],
                                      items[3], items[4], items[5],
                                      items[6], str(duration*60))
     try:
         resp = requests.post(self.URL_ACLSWITCH_TIME, data=add_req,
                             headers = {"Content-type": "application/json"})
     except:
         print self.TEXT_ERROR_CONNECTION
         return
     if resp.status_code != 200:
         print("Error creating resource, HTTP " + str(resp.status_code))
     print resp.text
Пример #7
0
    def rule_dst_list(self):
        print self.TEX_HELP_RULE_LIST
        buf_in = raw_input(self.PROMPT_RULE_LIST)
        items = buf_in.split(" ")

        if len(items) != 7:
            print "Expected 7 arguments, " + str(len(items)) + " given."
            return
        items[2] = items[2].lower()
        errors = rule_syntax.check_rule(items[0], items[1], items[2],
                                        items[3], items[4])
        DSTLIST = 0
        if (items[6] == "whitelist"):
            items[6] = TABLE_ID_WHITELIST
        elif (items[6] == "blacklist"):
            items[6] = TABLE_ID_BLACKLIST
        else:
            print("Invalid list specified")
            return

        if len(errors) != 0 :
            print "Invalid rule provided:"
            for e in errors:
                print "\t" + e
            return
        add_req = self.rule_to_json(items[0], items[1], items[2],
                                    items[3], items[4], items[5],
                                    items[6])
        try:
            resp = requests.post(self.URL_ACLSWITCH_RULE, data=add_req,
                                headers = {"Content-type": "application/json"})
        except:
            print self.TEXT_ERROR_CONNECTION
            return
        if resp.status_code != 200:
            print("Error creating resource, HTTP " + str(resp.status_code))
        print resp.text