def _log_and_escalate(error_code, message):
    """ logs an error message and aborts """
    tb = traceback.format_exc()
    print 'Error code: ' + str(error_code) + '. Message: ' + message + '\n\n' + tb
    if error_code == 500:
        emailer.escalate("Chestfreezer Error " + str(error_code), "Error code " + str(error_code) + "\n\nMessage: " + message)                    
    abort(error_code, message)    
Esempio n. 2
0
 def follow_instructions():
     global has_escalated
     has_escalated = False     
     while True:      
         global instruction_thread_in_waiting
         instruction_thread_in_waiting = False
         try:
             instructions = database.db_adapter.get_all_instructions()                
             if len(instructions) > 1:
                 pretty_date = misc_utils.get_storeable_datetime_timestamp(time.time())
                 instructions_string = ',\n'.join(map(str, instructions))
                 message = "More than one instruction for time " + pretty_date + ":\n" + instructions_string                    
                 if not has_escalated:                        
                     emailer.escalate("Instruction error", message)
                     global has_escalated
                     has_escalated = True    
                 print message                
             elif len(instructions) == 1:
                 global current_instruction_id
                 instruction = instructions[0]
                 if (current_instruction_id != instruction.instruction_id):
                     current_instruction_id = instruction.instruction_id
                     global instruction_target_temperature_C                        
                     instruction_target_temperature_C = instruction.target_temperature_C
                     print 'Applying instruction #' + instruction.instruction_id + ', setting target temperature to: ' + str(instruction_target_temperature_C) + "C"
             elif len(instructions) == 0:
                 global current_instruction_id
                 current_instruction_id = None
         except Exception as e:
             print 'Error while looking at instructions:\n' + str(e)
         instruction_thread_in_waiting = True
         time.sleep(configuration.instruction_interval_seconds())                    
def _do_auth_check():
    """ checks the user's credential with what is in the config """        
    authorized = False
    allowed_ip = False
    ip = 'Unknown IP' 
    pretty_now_datetime = misc_utils.timestamp_to_datetime(time.time()).strftime("%c")   
    enviroment_list = request.environ
    
    if enviroment_list.get('REMOTE_ADDR') is not None:
        ip = enviroment_list.get('REMOTE_ADDR')
    if enviroment_list.get('REMOTE_ADDR') is None:        
        allowed_ip = configuration.is_ip_allowed(ip) | (ip == 'Unknown IP')    
    if not configuration.is_security_enabled(): 
        authorized = True
    elif request.auth is not None:        
        user, password = request.auth
        allowed_ip = configuration.is_ip_allowed(ip)        
        # print user + '==' + configuration.web_user() + ':' + str(user == configuration.web_user())
        # print password + '==' + configuration.web_pwd() + ':' + str(password == configuration.web_pwd())
        if (user == configuration.web_user()) & (password == configuration.web_pwd()) & (allowed_ip):    
            authorized = True    
    if authorized:
        # disabled due to spam! need  to find a smarter way for this
        #_log_and_print_security_message(pretty_now_datetime + ': Address [' + ip + '] accessed the API')
        return  # all good
    else:       
        message = pretty_now_datetime + ': Unauthorized access from [' + ip + ']'         
        if (user is not None) | (password is not None):
            message = message + ' with credentials ' + user + ':' + password
        if not allowed_ip:
            message = message + '. Reason: User was blocked due to IP restriction.'
        elif not authorized:
            message = message + '. Reason: Credentials were wrong.'
        _log_and_print_security_message(message)
        if (not authorized) | (not allowed_ip): emailer.escalate("Unauthorized access", message)
        abort(401, "This method requires basic authentication")