statusd.py: delete unused parts, make it pep8 conform
This commit is contained in:
parent
c60bb326cc
commit
1fbd95facf
1 changed files with 52 additions and 43 deletions
61
statusd.py
61
statusd.py
|
@ -12,7 +12,7 @@ import ssl
|
|||
import os
|
||||
import logging
|
||||
import json
|
||||
from time import time, ctime, sleep
|
||||
from time import time, sleep
|
||||
from sys import exit
|
||||
|
||||
|
||||
|
@ -35,7 +35,8 @@ def read_config(CONFIGFILE, CONFIG):
|
|||
if key in CONFIG.keys():
|
||||
value = strip_argument(value)
|
||||
CONFIG[key] = value
|
||||
else: pass
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
logging.error('Failed to read {}'.format(CONFIGFILE))
|
||||
logging.error('Using default values')
|
||||
|
@ -45,12 +46,14 @@ def read_config(CONFIGFILE, CONFIG):
|
|||
|
||||
def certs_readable(config):
|
||||
'''
|
||||
checks at start, if the needed certificates defined (no nullstring) and readable.
|
||||
checks at start, if the needed certificates defined (no nullstring) and
|
||||
readable.
|
||||
param 1: dictionary
|
||||
return: boolean
|
||||
'''
|
||||
for i in (config['SERVER_KEY'], config['SERVER_CERT'], config['CLIENT_CERT']):
|
||||
if i == '' or os.access(i, os.R_OK) == False:
|
||||
for i in (config['SERVER_KEY'], config['SERVER_CERT'],
|
||||
config['CLIENT_CERT']):
|
||||
if i == '' or os.access(i, os.R_OK) is False:
|
||||
logging.error('Cant read {}'.format(i))
|
||||
return False
|
||||
return True
|
||||
|
@ -135,11 +138,10 @@ def change_status(raw_data, api):
|
|||
param 2: string
|
||||
return: boolean
|
||||
'''
|
||||
edit = False
|
||||
|
||||
logging.debug('Change status API')
|
||||
data = read_api(api)
|
||||
if data != False:
|
||||
if data is not False:
|
||||
status, timestamp = set_values(raw_data)
|
||||
if os.access(api, os.W_OK):
|
||||
logging.debug('API file is writable')
|
||||
|
@ -196,7 +198,8 @@ def set_values(raw_data):
|
|||
status = "true"
|
||||
else:
|
||||
status = "false"
|
||||
logging.debug('Set values for timestamp: {} and status: {}'.format(timestamp, status))
|
||||
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
||||
timestamp, status))
|
||||
return (status, timestamp)
|
||||
|
||||
|
||||
|
@ -217,18 +220,19 @@ def read_loglevel(CONFIG):
|
|||
loglevel = logging.INFO
|
||||
elif CONFIG['VERBOSITY'] == 'debug':
|
||||
loglevel = logging.DEBUG
|
||||
else: loglevel = False
|
||||
else:
|
||||
loglevel = False
|
||||
return(loglevel)
|
||||
|
||||
|
||||
def main():
|
||||
'''
|
||||
The main function - opens a socket, create a ssl context, load certs and
|
||||
listen for connections. at ssl context we set some security options like
|
||||
OP_NO_SSLv2 (SSLv3): they are insecure
|
||||
PROTOCOL_TLS: only use tls
|
||||
listen for connections. at ssl context we set only one available cipher
|
||||
suite and disable compression.
|
||||
OP_NO_COMPRESSION: prevention against crime attack
|
||||
OP_DONT_ISERT_EMPTY_FRAGMENTS: prevention agains cbc 4 attack (cve-2011-3389)
|
||||
OP_DONT_ISERT_EMPTY_FRAGMENTS: prevention agains cbc 4 attack
|
||||
(cve-2011-3389)
|
||||
'''
|
||||
|
||||
loglevel = logging.WARNING
|
||||
|
@ -249,20 +253,20 @@ def main():
|
|||
CONFIG_FILE = './statusd.conf'
|
||||
read_config(CONFIG_FILE, CONFIG)
|
||||
loglevel = read_loglevel(CONFIG)
|
||||
if loglevel != False:
|
||||
if loglevel is not False:
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(loglevel)
|
||||
else:
|
||||
loglevel = logging.WARNING
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(loglevel)
|
||||
loggin.warning('Invalid value for loglevel. Set default value')
|
||||
logging.warning('Invalid value for loglevel. Set default value')
|
||||
|
||||
print_config(CONFIG)
|
||||
|
||||
# todo: zertifikate sollten nur lesbar sein!
|
||||
|
||||
if certs_readable(CONFIG) == False:
|
||||
if certs_readable(CONFIG) is False:
|
||||
logging.error('Cert check failed\nExit')
|
||||
exit()
|
||||
|
||||
|
@ -283,7 +287,8 @@ def main():
|
|||
try:
|
||||
mySocket.bind((CONFIG['HOST'], int(CONFIG['PORT'])))
|
||||
mySocket.listen(5)
|
||||
logging.info('Listen on {} at Port {}'.format(CONFIG['HOST'], CONFIG['PORT']))
|
||||
logging.info('Listen on {} at Port {}'.format(CONFIG['HOST'],
|
||||
CONFIG['PORT']))
|
||||
except Exception as e:
|
||||
logging.error('unable to bind and listen')
|
||||
logging.error('{}'.format(e))
|
||||
|
@ -291,12 +296,15 @@ def main():
|
|||
while True:
|
||||
try:
|
||||
fromSocket, fromAddr = mySocket.accept()
|
||||
logging.info('Client connected: {}:{}'.format(fromAddr[0], fromAddr[1]))
|
||||
logging.info('Client connected: {}:{}'.format(fromAddr[0],
|
||||
fromAddr[1]))
|
||||
try:
|
||||
fromSocket.settimeout(float(CONFIG['TIMEOUT']))
|
||||
logging.debug('Connection timeout set to {}'.format(CONFIG['TIMEOUT']))
|
||||
except Exception as e:
|
||||
logging.error('Canot set timeout to {}'.format(CONFIG['TIMEOUT']))
|
||||
logging.debug('Connection timeout set to {}'.format(
|
||||
CONFIG['TIMEOUT']))
|
||||
except Exception:
|
||||
logging.error('Canot set timeout to {}'.format(
|
||||
CONFIG['TIMEOUT']))
|
||||
logging.error('Use default value: 3.0')
|
||||
fromSocket.settimeout(3.0)
|
||||
try:
|
||||
|
@ -304,17 +312,17 @@ def main():
|
|||
conn.settimeout(3.0)
|
||||
# display_peercert(conn.getpeercert())
|
||||
logging.debug('Connection established')
|
||||
logging.debug('Peer certificate commonName: {}'.format \
|
||||
logging.debug('Peer certificate commonName: {}'.format
|
||||
(conn.getpeercert()['subject'][5][0][1]))
|
||||
logging.debug('Peer certificate serialNumber: {}'.format \
|
||||
logging.debug('Peer certificate serialNumber: {}'.format
|
||||
(conn.getpeercert()['serialNumber']))
|
||||
except socket.timeout:
|
||||
logging.error('Socket timeout')
|
||||
except Exception as e:
|
||||
logging.error('Connection failed: {}'.format(e))
|
||||
raw_data = conn.recv(1)
|
||||
if receive_buffer_is_valid(raw_data) == True:
|
||||
if change_status(raw_data, CONFIG['API']) == True:
|
||||
if receive_buffer_is_valid(raw_data) is True:
|
||||
if change_status(raw_data, CONFIG['API']) is True:
|
||||
logging.debug('Send {} back'.format(raw_data))
|
||||
conn.send(raw_data)
|
||||
# change_status returns false:
|
||||
|
@ -324,7 +332,8 @@ def main():
|
|||
conn.send(b'\x03')
|
||||
# receive_handle returns false:
|
||||
else:
|
||||
logging.info('Invalid argument recived: {}'.format(raw_data))
|
||||
logging.info('Invalid argument recived: {}'.format(
|
||||
raw_data))
|
||||
logging.debug('Send {} back'.format(b'\x03'))
|
||||
if conn:
|
||||
conn.send(b'\x03')
|
||||
|
|
Loading…
Reference in a new issue