Exemple #1
0
    def punch_it_now(self, target):
        response = None
        failed = False
        trace = ''
        try:
            # parse out the target URL
            url = urlparse(target.url)
            kwargs = {TIMEOUT: target.timeout}
            location = url.netloc
            locations = location.split(':')

            # connect to the target
            if url.scheme == PROTOCOL_HTTP:
                conn = httplib.HTTPConnection(locations[0], url.port, **kwargs)
            elif url.scheme == PROTOCOL_HTTPS:
                conn = httplib.HTTPSConnection(locations[0], url.port,
                                               **kwargs)
            else:
                self.logger.warn('target protocol %s not supported for %s' %
                                 (url.scheme, target.name))
                raise SkyPunchInvalidProtocolError(
                    'target protocol %s not supported for %s' %
                    (url.scheme, target.name))
            trace += '%s Connecting to %s ....' % (datetime.datetime.now(),
                                                   target.url)
            conn.connect()
            trace += '\n%s Connection    [OK]' % datetime.datetime.now()
            trace += '\n%s Sending target request string ...' % datetime.datetime.now(
            )
            request = conn.putrequest(target.method, url.path)
            trace += '\n%s Sent [OK]' % datetime.datetime.now()

            # HTTP Basic authentication
            if target.authn == 'BASIC':
                try:
                    auth = get_basic_auth(target)
                    conn.putheader("Authorization", "Basic %s" % auth)
                    trace += '\n%s Using BASIC Authentication    [OK]' % datetime.datetime.now(
                    )
                except SkyPunchAuthParamError as spe:
                    # improperly defined target, log error but do not notify
                    self.process_error(
                        target,
                        'invalid BASIC authn params (user and password required)'
                    )
                    return

            # Openstack Keystone token based authentication
            elif target.authn == 'OPENSTACK':
                try:
                    trace += '\n%s Getting OpenStack Keystone Auth Token ....' % datetime.datetime.now(
                    )
                    auth = get_openstack_auth(target)
                    conn.putheader("X-Auth-Token", auth)
                    trace += '\n%s Openstack Keystone Token Obtained  [OK]' % datetime.datetime.now(
                    )
                except SkyPunchAuthParamError:
                    # improperly defined target params, log error but do not notify
                    self.process_error(
                        target,
                        'invalid Openstack / Keystone authn params (user,password,tenantid,osauthendpoint)'
                    )
                    return
                except SkyPunchInvalidProtocolError:
                    # improperly defined target params, log error but do not notify
                    self.process_error(
                        target,
                        'invalid Openstack / Keystone endpoint protocol)')
                    return
                except SkyPunchKeystoneAuthError as spe:
                    update_target_status(target, STATUS_FAIL, str(spe))
                    trace += '\n%s Using Openstack Keystone token authentication  [FAIL] reason: %s' % (
                        datetime.datetime.now(), str(spe))
                    failed = True

            if not failed:
                # add headers and issue request
                conn.endheaders()
                conn.send('')
                trace += '\n%s Sending %s request  [OK]' % (
                    datetime.datetime.now(), target.method)
                response = conn.getresponse()
                trace += '\n%s Reading response [OK]' % datetime.datetime.now()
                if response.status == target.pass_result:
                    trace += '\n%s Response [OK]' % datetime.datetime.now()
                else:
                    trace += '\n%s Response [Fail] reason: target status:%d != %d' % (
                        datetime.datetime.now(), response.status,
                        target.pass_result)
                update_target_status(
                    target, STATUS_PASS if response.status
                    == target.pass_result else STATUS_FAIL, response.reason
                    if response.status == target.pass_result else
                    ('target status:%d != %d' %
                     (response.status, target.pass_result)))
        except SystemExit, e:
            sys.exit(e)
Exemple #2
0
    def punch_it_now(self,target):
        response = None
        failed = False
        trace = '' 
        try:
            # parse out the target URL
            url = urlparse(target.url)
            kwargs = {TIMEOUT: target.timeout}
            location = url.netloc
            locations = location.split(':')

            # connect to the target
            if url.scheme == PROTOCOL_HTTP:
                conn = httplib.HTTPConnection(locations[0],url.port,**kwargs)
            elif url.scheme == PROTOCOL_HTTPS:
                conn = httplib.HTTPSConnection(locations[0], url.port,**kwargs)
            else:
                self.logger.warn('target protocol %s not supported for %s' % (url.scheme,target.name))
                raise SkyPunchInvalidProtocolError('target protocol %s not supported for %s' % (url.scheme,target.name)) 
            trace += '%s Connecting to %s ....' % (datetime.datetime.now(),target.url)
            conn.connect()
            trace += '\n%s Connection    [OK]' % datetime.datetime.now()
            trace += '\n%s Sending target request string ...' % datetime.datetime.now()
            request = conn.putrequest(target.method, url.path)
            trace += '\n%s Sent [OK]' % datetime.datetime.now()
           
            # HTTP Basic authentication  
            if target.authn == 'BASIC':
                try:
                    auth = get_basic_auth(target)
                    conn.putheader("Authorization", "Basic %s" % auth)
                    trace += '\n%s Using BASIC Authentication    [OK]' % datetime.datetime.now()
                except SkyPunchAuthParamError as spe:
                    # improperly defined target, log error but do not notify
                    self.process_error(target,'invalid BASIC authn params (user and password required)')                
                    return
            
            # Openstack Keystone token based authentication
            elif target.authn == 'OPENSTACK':
                try:
                    trace += '\n%s Getting OpenStack Keystone Auth Token ....' % datetime.datetime.now()
                    auth = get_openstack_auth(target)
                    conn.putheader("X-Auth-Token",auth)
                    trace += '\n%s Openstack Keystone Token Obtained  [OK]' % datetime.datetime.now()
                except SkyPunchAuthParamError:
                    # improperly defined target params, log error but do not notify
                    self.process_error(target,'invalid Openstack / Keystone authn params (user,password,tenantid,osauthendpoint)')
                    return
                except SkyPunchInvalidProtocolError:
                    # improperly defined target params, log error but do not notify
                    self.process_error(target,'invalid Openstack / Keystone endpoint protocol)')
                    return
                except SkyPunchKeystoneAuthError as spe:
                    update_target_status(target,STATUS_FAIL, str(spe)) 
                    trace += '\n%s Using Openstack Keystone token authentication  [FAIL] reason: %s' % (datetime.datetime.now(),str(spe))
                    failed = True

            if not failed:
                # add headers and issue request
                conn.endheaders()
                conn.send('')
                trace += '\n%s Sending %s request  [OK]' % (datetime.datetime.now(),target.method)
                response = conn.getresponse()
                trace += '\n%s Reading response [OK]' % datetime.datetime.now()
                if response.status == target.pass_result:
                    trace += '\n%s Response [OK]' % datetime.datetime.now()
                else:
                    trace += '\n%s Response [Fail] reason: target status:%d != %d'% (datetime.datetime.now(),response.status,target.pass_result) 
                update_target_status(target,
                    STATUS_PASS if response.status == target.pass_result else STATUS_FAIL,
                    response.reason if response.status == target.pass_result else ('target status:%d != %d' % (response.status,target.pass_result))) 
        except SystemExit, e:
            sys.exit(e)
Exemple #3
0
                        datetime.datetime.now(), response.status,
                        target.pass_result)
                update_target_status(
                    target, STATUS_PASS if response.status
                    == target.pass_result else STATUS_FAIL, response.reason
                    if response.status == target.pass_result else
                    ('target status:%d != %d' %
                     (response.status, target.pass_result)))
        except SystemExit, e:
            sys.exit(e)
        except socket.error as se:
            trace += '\n%s [FAIL] reason: %s' % (datetime.datetime.now(),
                                                 str(se))
            update_target_status(target, STATUS_FAIL, str(se))
        except:
            update_target_status(target, STATUS_FAIL, str(sys.exc_info()[0]))

        self.log_result(target)

        # notify
        if self.notifiermodel != None:
            notifier = SkyPunchNotifier(self.logger)
            notifier.notify(target, self.notifiermodel, trace)
        else:
            print trace

        # update counters
        update_target_counters(target, response)

        # save back to db
        self.targetmodel.commit()
Exemple #4
0
                response = conn.getresponse()
                trace += '\n%s Reading response [OK]' % datetime.datetime.now()
                if response.status == target.pass_result:
                    trace += '\n%s Response [OK]' % datetime.datetime.now()
                else:
                    trace += '\n%s Response [Fail] reason: target status:%d != %d'% (datetime.datetime.now(),response.status,target.pass_result) 
                update_target_status(target,
                    STATUS_PASS if response.status == target.pass_result else STATUS_FAIL,
                    response.reason if response.status == target.pass_result else ('target status:%d != %d' % (response.status,target.pass_result))) 
        except SystemExit, e:
            sys.exit(e)
        except socket.error as se:
            trace += '\n%s [FAIL] reason: %s' % (datetime.datetime.now(),str(se))
            update_target_status(target,STATUS_FAIL,str(se))
        except:
            update_target_status(target,STATUS_FAIL,str(sys.exc_info()[0]))

        self.log_result(target)

        # notify
        if self.notifiermodel != None: 
            notifier = SkyPunchNotifier(self.logger)
            notifier.notify(target,self.notifiermodel,trace)
        else:
            print trace
        
        # update counters
        update_target_counters(target,response)
        
        # save back to db
        self.targetmodel.commit()