Swagger file update

This commit is contained in:
Dominik Chilla 2018-12-23 22:56:16 +01:00
parent 76acc94391
commit 937b805779
2 changed files with 65 additions and 40 deletions

View File

@ -1,4 +1,4 @@
import json,sys,os,logging,re,magic import json, sys,os,logging,re,magic
import email,email.header,email.message import email,email.header,email.message
from GulagDB import GulagDB,GulagDBException from GulagDB import GulagDB,GulagDBException
from GulagMailbox import IMAPmailbox,IMAPmailboxException from GulagMailbox import IMAPmailbox,IMAPmailboxException
@ -25,10 +25,10 @@ class Gulag:
raise GulagException(whoami(self) + str(sys.exc_info())) raise GulagException(whoami(self) + str(sys.exc_info()))
# logging # logging
# logging_level = logging.INFO # logging_level = logging.INFO
# if 'level' in self.config['logging']: # if 'level' in self.config['logging']:
if 'logging' not in self.config: if 'logging' not in self.config:
raise GulagException(whoami(self) + "Logging not configured!") raise GulagException(whoami(self) + "Logging not configured!")
if('filename' in self.config['logging'] and if('filename' in self.config['logging'] and
len(self.config['logging']['filename']) > 0): len(self.config['logging']['filename']) > 0):
# TODO: Exception handling # TODO: Exception handling
logging.basicConfig( logging.basicConfig(
@ -50,20 +50,20 @@ class Gulag:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
logging.info('Gulag core initialized by ' + os.path.basename(__file__)) logging.info('Gulag core initialized by ' + os.path.basename(__file__))
def check_fields(self,fields_target,args): def check_fields(self,fields_target,args):
if fields_target not in self.fields: if fields_target not in self.fields:
raise GulagException( raise GulagException(
whoami(self) + fields_target + " not found in Gulag.fields!" whoami(self) + fields_target + " not found in Gulag.fields!"
) )
for arg in args: for arg in args:
if(arg == 'query_offset' or arg == 'query_limit' if(arg == 'query_offset' or arg == 'query_limit'
or arg == 'sort_index' or arg == 'sort_order' or arg == 'sort_index' or arg == 'sort_order'
or arg == 'rfc822_message'): or arg == 'rfc822_message'):
continue continue
if arg not in self.fields[fields_target]: if arg not in self.fields[fields_target]:
raise GulagException( raise GulagException(
whoami(self) + arg + " is not a valid field of " whoami(self) + arg + " is not a valid field of "
+ fields_target + "!" + fields_target + "!"
) )
@ -90,9 +90,10 @@ class Gulag:
r5321_from = r5321_from.replace(">","") r5321_from = r5321_from.replace(">","")
r5321_rcpts = None r5321_rcpts = None
try: try:
r5321_rcpts = email.header.decode_header(msg['X-Envelope-To-Blocked'])[0][0] r5321_rcpts = email.header.decode_header(
msg['X-Envelope-To-Blocked'])[0][0]
except: except:
logging.warning(whoami(self) + logging.warning(whoami(self) +
"Failed to extract envelope recipients! Skipping mail" "Failed to extract envelope recipients! Skipping mail"
) )
continue continue
@ -100,7 +101,7 @@ class Gulag:
try: try:
r5322_from = email.header.decode_header(msg['From'])[0][0] r5322_from = email.header.decode_header(msg['From'])[0][0]
except: except:
logging.warning(whoami(self) + logging.warning(whoami(self) +
"Failed to extract from header! Skipping mail" "Failed to extract from header! Skipping mail"
) )
continue continue
@ -130,10 +131,12 @@ class Gulag:
# über die mailbox_id sowie die imap_uid mehrfach referenziert. # über die mailbox_id sowie die imap_uid mehrfach referenziert.
for r5321_rcpt in r5321_rcpts.split(","): for r5321_rcpt in r5321_rcpts.split(","):
quarmail_id = self.db.add_quarmail({ quarmail_id = self.db.add_quarmail({
'mx_queue_id': mx_queue_id, 'env_from': r5321_from, 'env_rcpt': r5321_rcpt, 'mx_queue_id': mx_queue_id, 'env_from': r5321_from,
'hdr_cf': x_spam_status, 'hdr_from': r5322_from, 'hdr_subject': subject, 'env_rcpt': r5321_rcpt, 'hdr_cf': x_spam_status,
'hdr_msgid': msg_id, 'hdr_date': date, 'cf_meta': 'cf_meta', 'hdr_from': r5322_from, 'hdr_subject': subject,'hdr_msgid': msg_id,
'mailbox_id': 'quarantine@zwackl.de', 'imap_uid': uid, 'msg_size': msg_size 'hdr_date': date, 'cf_meta': 'cf_meta',
'mailbox_id': 'quarantine@zwackl.de', 'imap_uid': uid,
'msg_size': msg_size
}) })
logging.info(whoami(self) + "QuarMail (%s) imported" % (quarmail_id)) logging.info(whoami(self) + "QuarMail (%s) imported" % (quarmail_id))
quarmail_ids.append(quarmail_id) quarmail_ids.append(quarmail_id)
@ -167,7 +170,9 @@ class Gulag:
ctype = part.get_content_type() ctype = part.get_content_type()
if(ctype == 'text/plain' or ctype == 'text/html'): if(ctype == 'text/plain' or ctype == 'text/html'):
curis = {} curis = {}
curis = extract_uris(part.get_payload(decode=True).decode("utf-8","replace")) curis = extract_uris(
part.get_payload(decode=True).decode("utf-8","replace")
)
if(len(curis) > 0): if(len(curis) > 0):
logging.info(whoami(self) + "CURIS: " + str(curis)) logging.info(whoami(self) + "CURIS: " + str(curis))
uris = {**uris, **curis} uris = {**uris, **curis}
@ -177,8 +182,9 @@ class Gulag:
for quarmail_id in quarmail_ids: for quarmail_id in quarmail_ids:
for attachment_id in attachments: for attachment_id in attachments:
self.db.quarmail2attachment(str(quarmail_id), str(attachment_id)) self.db.quarmail2attachment(str(quarmail_id), str(attachment_id))
logging.info(whoami(self) + logging.info(whoami(self) +
"Attachment("+str(attachment_id)+")@QuarMail("+str(quarmail_id)+") imported" "Attachment("+str(attachment_id)+")@QuarMail("+
str(quarmail_id)+") imported"
) )
# link message with uris # link message with uris
if(len(uris) > 0): if(len(uris) > 0):
@ -190,7 +196,7 @@ class Gulag:
"fqdn": extract_fqdn(uri) "fqdn": extract_fqdn(uri)
}) })
self.db.quarmail2uri(str(quarmail_id), str(uri_id)) self.db.quarmail2uri(str(quarmail_id), str(uri_id))
logging.info(whoami(self) + logging.info(whoami(self) +
"URI("+str(uri_id)+")@QuarMail("+str(quarmail_id)+") imported" "URI("+str(uri_id)+")@QuarMail("+str(quarmail_id)+") imported"
) )
except GulagDBException as e: except GulagDBException as e:
@ -203,7 +209,7 @@ class Gulag:
logging.info(whoami(self) + "QuarMails to purge: " + str(len( logging.info(whoami(self) + "QuarMails to purge: " + str(len(
self.db.get_deprecated_mails(self.config['cleaner']['retention_period']) self.db.get_deprecated_mails(self.config['cleaner']['retention_period'])
))) )))
def get_mailboxes(self): def get_mailboxes(self):
try: try:
return self.db.get_mailboxes() return self.db.get_mailboxes()
@ -234,7 +240,7 @@ class Gulag:
mailbox = self.db.get_mailbox(mailbox_id) mailbox = self.db.get_mailbox(mailbox_id)
except GulagDBException as e: except GulagDBException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
imap_mb = None imap_mb = None
try: try:
imap_mb = IMAPmailbox(mailbox) imap_mb = IMAPmailbox(mailbox)
@ -250,7 +256,7 @@ class Gulag:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
return qms_db return qms_db
def get_quarmail(self,args): def get_quarmail(self,args):
qm_db = None qm_db = None
try: try:
@ -266,11 +272,13 @@ class Gulag:
mailbox = self.db.get_mailbox(qm_db['mailbox_id']) mailbox = self.db.get_mailbox(qm_db['mailbox_id'])
except GulagDBException as e: except GulagDBException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
imap_mb = None imap_mb = None
try: try:
imap_mb = IMAPmailbox(mailbox) imap_mb = IMAPmailbox(mailbox)
qm_db['rfc822_message'] = imap_mb.get_message(qm_db['imap_uid']).decode("utf-8") qm_db['rfc822_message'] = imap_mb.get_message(
qm_db['imap_uid']
).decode("utf-8")
return qm_db return qm_db
except IMAPmailboxException as e: except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
@ -288,7 +296,7 @@ class Gulag:
mailbox = self.db.get_mailbox(qm_db['mailbox_id']) mailbox = self.db.get_mailbox(qm_db['mailbox_id'])
except GulagDBException as e: except GulagDBException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
# Delete QuarMail from IMAP mailbox # Delete QuarMail from IMAP mailbox
imap_mb = None imap_mb = None
try: try:
@ -322,7 +330,7 @@ class Gulag:
except GulagDBException as e: except GulagDBException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
def get_quarmail_attachment(self,args): def get_quarmail_attachment(self,args):
qmat_db = None qmat_db = None
try: try:
@ -340,7 +348,7 @@ class Gulag:
mailbox = self.db.get_mailbox(qmat_db['mailbox_id']) mailbox = self.db.get_mailbox(qmat_db['mailbox_id'])
except GulagDBException as e: except GulagDBException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
imap_mb = None imap_mb = None
try: try:
imap_mb = IMAPmailbox(mailbox) imap_mb = IMAPmailbox(mailbox)
@ -379,7 +387,7 @@ class Gulag:
mailbox = self.db.get_mailbox(qm_db['mailbox_id']) mailbox = self.db.get_mailbox(qm_db['mailbox_id'])
except GulagDBException as e: except GulagDBException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
imap_mb = None imap_mb = None
try: try:
imap_mb = IMAPmailbox(mailbox) imap_mb = IMAPmailbox(mailbox)
@ -394,7 +402,7 @@ class Gulag:
except IMAPmailboxException as e: except IMAPmailboxException as e:
logging.warning(whoami(self) + e.message) logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
def rspamd_http2imap(self,args): def rspamd_http2imap(self,args):
mailbox = None mailbox = None
try: try:
@ -404,41 +412,41 @@ class Gulag:
# check if the request comes really from rspamd´s metadata_exporter # check if the request comes really from rspamd´s metadata_exporter
# default metadata_header prefix 'X-Rspamd' will be expected # default metadata_header prefix 'X-Rspamd' will be expected
if('X-Rspamd-From' not in args['req_headers']): if('X-Rspamd-From' not in args['req_headers']):
err = str(whoami(self) err = str(whoami(self)
+ "Missing Rspamd-specific request header X-Rspamd-From!" + "Missing Rspamd-specific request header X-Rspamd-From!"
) )
logging.error(err) logging.error(err)
raise GulagException(err) raise GulagException(err)
# Prepend Gulag-specific headers to rejected mail # Prepend Gulag-specific headers to rejected mail
# before pushing into quarantine mailbox # before pushing into quarantine mailbox
msg = None msg = None
try: try:
if('X-Rspamd-From' not in args['req_headers']): if('X-Rspamd-From' not in args['req_headers']):
err = str(whoami(self) err = str(whoami(self)
+ "Missing Rspamd-specific request header X-Rspamd-From!" + "Missing Rspamd-specific request header X-Rspamd-From!"
) )
logging.error(err) logging.error(err)
raise GulagException(err) raise GulagException(err)
if('X-Rspamd-Rcpt' not in args['req_headers']): if('X-Rspamd-Rcpt' not in args['req_headers']):
err = str(whoami(self) err = str(whoami(self)
+ "Missing Rspamd-specific request header X-Rspamd-Rcpt!" + "Missing Rspamd-specific request header X-Rspamd-Rcpt!"
) )
logging.error(err) logging.error(err)
raise GulagException(err) raise GulagException(err)
if('X-Rspamd-Symbols' not in args['req_headers']): if('X-Rspamd-Symbols' not in args['req_headers']):
err = str(whoami(self) err = str(whoami(self)
+ "Missing Rspamd-specific request header X-Rspamd-Symbols!" + "Missing Rspamd-specific request header X-Rspamd-Symbols!"
) )
logging.error(err) logging.error(err)
raise GulagException(err) raise GulagException(err)
if('X-Rspamd-Qid' not in args['req_headers']): if('X-Rspamd-Qid' not in args['req_headers']):
err = str(whoami(self) err = str(whoami(self)
+ "Missing Rspamd-specific request header X-Rspamd-Qid!" + "Missing Rspamd-specific request header X-Rspamd-Qid!"
) )
logging.error(err) logging.error(err)
raise GulagException(err) raise GulagException(err)
if('rfc822_message' not in args): if('rfc822_message' not in args):
err = str(whoami(self) err = str(whoami(self)
+ "Missing rfc822_message!" + "Missing rfc822_message!"
) )
logging.error(err) logging.error(err)
@ -449,9 +457,9 @@ class Gulag:
if(len(rcpts_hdr) > 0): if(len(rcpts_hdr) > 0):
rcpts_hdr += "," + rcpt rcpts_hdr += "," + rcpt
else: else:
rcpts_hdr = rcpt rcpts_hdr = rcpt
msg = "Return-Path: <" + args['req_headers']['X-Rspamd-From'] + ">\r\n" msg = "Return-Path: <" + args['req_headers']['X-Rspamd-From'] + ">\r\n"
msg += "Received: from rspamd_http2imap relay by gulag-mailbox " msg += "Received: from rspamd_http2imap relay by gulag-mailbox "
msg += args['mailbox_id'] + "\r\n" msg += args['mailbox_id'] + "\r\n"
msg += "X-Envelope-To-Blocked: " + rcpts_hdr + "\r\n" msg += "X-Envelope-To-Blocked: " + rcpts_hdr + "\r\n"
msg += "X-Spam-Status: " + args['req_headers']['X-Rspamd-Symbols'] + "\r\n" msg += "X-Spam-Status: " + args['req_headers']['X-Rspamd-Symbols'] + "\r\n"
@ -468,4 +476,3 @@ class Gulag:
imap_mb.append_message(msg) imap_mb.append_message(msg)
except IMAPmailboxException as e: except IMAPmailboxException as e:
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e

View File

@ -129,7 +129,7 @@ paths:
description: bad input parameter description: bad input parameter
500: 500:
description: server error description: server error
/quarmails/{quarmail_id}: /quarmails/{quarmail_id}:
get: get:
summary: retrieves a quarantined email summary: retrieves a quarantined email
@ -156,6 +156,24 @@ paths:
description: bad input parameter description: bad input parameter
500: 500:
description: server error description: server error
delete:
summary: deletes a quarantined email
operationId: delete_quarmail
produces:
- application/json
parameters:
- in: path
name: quarmail_id
description: unique id of quarantined email
required: true
type: integer
responses:
200:
description: quarantined email deleted
400:
description: bad input parameter
500:
description: server error
/quarmails/{quarmail_id}/attachments: /quarmails/{quarmail_id}/attachments:
get: get:
@ -180,7 +198,7 @@ paths:
description: bad input parameter description: bad input parameter
500: 500:
description: server error description: server error
/quarmails/{quarmail_id}/attachments/{attachment_id}: /quarmails/{quarmail_id}/attachments/{attachment_id}:
get: get:
summary: retrieves a quarantined email summary: retrieves a quarantined email
@ -212,7 +230,7 @@ paths:
description: bad input parameter description: bad input parameter
500: 500:
description: server error description: server error
/quarmails/{quarmail_id}/uris: /quarmails/{quarmail_id}/uris:
get: get:
summary: "retrieves all URIS from any main MIME part (text/plain,text/html)" summary: "retrieves all URIS from any main MIME part (text/plain,text/html)"