<commit_before>import requests
import json
import os
from configlib import getConfig, OptionParser
from requests_jwt import JWTAuth


class message(object):
    def __init__(self):
        '''
        takes an incoming alert
        and triggers the block ip rest api endpoint
        '''

        self.configfile = os.path.join(os.path.dirname(__file__), 'block_ip.conf')
        self.options = None
        if os.path.exists(self.configfile):
            self.initConfiguration()
        if self.options.jwt_token is not None:
            self._restapi_jwt = JWTAuth(self.options.jwt_token)
            self._restapi_jwt.set_header_format('Bearer %s')
        else:
            self._restapi_jwt = None

        self.registration = self.options.keywords.split(" ")
        # Block for 1 day
        self.DEFAULT_BLOCK_LENGTH = "1d"
        self.priority = 1

    def initConfiguration(self):
        myparser = OptionParser()
        (self.options, args) = myparser.parse_args([])

        self.options.keywords = getConfig('alert_names', [], self.configfile)
        self.options.restapi_url = getConfig('restapi_url', '', self.configfile)
        self.options.jwt_token = getConfig('jwt_token', None, self.configfile)

    def onMessage(self, alert):
        message = alert['_source']
        post_data = [
            {'ipaddress': "{0}/32".format(message['details']['sourceipaddress'])},
            {'duration': self.DEFAULT_BLOCK_LENGTH},
            {'comment': 'MozDef Automatic Block'},
            {'referenceid': ''},
            {'IPBlockList': True},
            {'userid': "<EMAIL>"}
        ]
        headers = {'Content-type': 'application/json'}
        resp = requests.post(url=self.options.restapi_url + "/blockip", data=json.dumps(post_data), auth=self._restapi_jwt, headers=headers)<commit_msg>Update block ip comment and userid<commit_after>import requests
import json
import os
from configlib import getConfig, OptionParser
from requests_jwt import JWTAuth


class message(object):
    def __init__(self):
        '''
        takes an incoming alert
        and triggers the block ip rest api endpoint
        '''

        self.configfile = os.path.join(os.path.dirname(__file__), 'block_ip.conf')
        self.options = None
        if os.path.exists(self.configfile):
            self.initConfiguration()
        if self.options.jwt_token is not None:
            self._restapi_jwt = JWTAuth(self.options.jwt_token)
            self._restapi_jwt.set_header_format('Bearer %s')
        else:
            self._restapi_jwt = None

        self.registration = self.options.keywords.split(" ")
        # Block for 1 day
        self.DEFAULT_BLOCK_LENGTH = "1d"
        self.priority = 1

    def initConfiguration(self):
        myparser = OptionParser()
        (self.options, args) = myparser.parse_args([])

        self.options.keywords = getConfig('alert_names', [], self.configfile)
        self.options.restapi_url = getConfig('restapi_url', '', self.configfile)
        self.options.jwt_token = getConfig('jwt_token', None, self.configfile)

    def onMessage(self, alert):
        message = alert['_source']
        post_data = [
            {'ipaddress': "{0}/32".format(message['details']['sourceipaddress'])},
            {'duration': self.DEFAULT_BLOCK_LENGTH},
            {'comment': 'Automatic Block'},
            {'referenceid': ''},
            {'IPBlockList': True},
            {'userid': "MozDef"}
        ]
        headers = {'Content-type': 'application/json'}
        resp = requests.post(url=self.options.restapi_url + "/blockip", data=json.dumps(post_data), auth=self._restapi_jwt, headers=headers)
{'fraction_non_alphanumeric': 0.12496574403946287, 'fraction_numerical': 0.003288572211564812, 'mean_word_length': 2.624627606752731, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7360008', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1047, 'n_words': 215}