modify attachment/uri

This commit is contained in:
Dominik Chilla 2019-01-21 22:27:19 +01:00
parent f4b4d65a0d
commit 25dbedc06c
5 changed files with 143 additions and 9 deletions

View File

@ -20,7 +20,7 @@ curl -v -s http://127.0.0.1:9090/api/v1/quarmails?rfc822_message=1 | jq
## update a QuarMail´s metadata (e.g. sandbox_results) by ID ## update a QuarMail´s metadata (e.g. sandbox_results) by ID
``` ```
curl -v -s -X PATCH -d '{"sandbox_results":"12345abc"}' http://127.0.0.1:9090/api/v1/quarmails/311|jq curl -v -s -X PATCH -d '{"cf_meta":"12345abc"}' http://127.0.0.1:9090/api/v1/quarmails/311|jq
``` ```
## delete a QuarMail by ID ## delete a QuarMail by ID

View File

@ -61,6 +61,7 @@ class Gulag:
self.fields['Mailrelays'] = self.db.get_fields('Mailrelays') self.fields['Mailrelays'] = self.db.get_fields('Mailrelays')
self.fields['QuarMails'] = self.db.get_fields('QuarMails') self.fields['QuarMails'] = self.db.get_fields('QuarMails')
self.fields['Attachments'] = self.db.get_fields('Attachments') self.fields['Attachments'] = self.db.get_fields('Attachments')
self.fields['URIs'] = self.db.get_fields('URIs')
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
@ -178,9 +179,9 @@ class Gulag:
"QuarMail(%s)@Mailbox(%s) imported" % (quarmail_id,mailbox['id']) "QuarMail(%s)@Mailbox(%s) imported" % (quarmail_id,mailbox['id'])
) )
quarmail_ids.append(quarmail_id) quarmail_ids.append(quarmail_id)
# Ende for rcpts # End for rcpts
# Alle MIME-Parts durchiterieren und Attachments # Iterate through all MIME-parts and extract all
# (MIME-Parts mit name/filename Attribut) extrahieren # attachments (parts with a name/filename attribute)
for part in msg.walk(): for part in msg.walk():
if part.get_filename(): if part.get_filename():
# ist ein Attachment # ist ein Attachment
@ -533,6 +534,24 @@ class Gulag:
if 'data' not in args: if 'data' not in args:
return at_db return at_db
def modify_attachment(self, attachment):
try:
if 'id' not in attachment:
raise GulagBadInputException(whoami(self) + "'id' is mandatory!")
for field in attachment:
if field not in self.fields['Attachments']:
raise GulagBadInputException(whoami(self) +
"Unknown Attachment field: " + field
)
self.db.modify_attachment(attachment)
except GulagDBBadInputException as e:
raise GulagBadInputException(whoami(self) + e.message) from e
except GulagDBNotFoundException as e:
raise GulagNotFoundException(whoami(self) + e.message) from e
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
def get_quarmail_uris(self,args): def get_quarmail_uris(self,args):
if('from_rfc822_message' not in args): if('from_rfc822_message' not in args):
try: try:
@ -580,6 +599,24 @@ class Gulag:
except GulagDBException as e: except GulagDBException as e:
raise GulagException(whoami(self) + e.message) from e raise GulagException(whoami(self) + e.message) from e
def modify_uri(self, uri):
try:
if 'id' not in uri:
raise GulagBadInputException(whoami(self) + "'id' is mandatory!")
for field in uri:
if field not in self.fields['URIs']:
raise GulagBadInputException(whoami(self) +
"Unknown URI field: " + field
)
self.db.modify_uri(uri)
except GulagDBBadInputException as e:
raise GulagBadInputException(whoami(self) + e.message) from e
except GulagDBNotFoundException as e:
raise GulagNotFoundException(whoami(self) + e.message) from e
except GulagDBException as e:
logging.warning(whoami(self) + e.message)
raise GulagException(whoami(self) + e.message) from e
def rspamd2mailbox(self,args): def rspamd2mailbox(self,args):
mailbox = None mailbox = None
try: try:

View File

@ -415,6 +415,31 @@ class GulagDB:
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e.msg)) from e raise GulagDBException(whoami(self) + str(e.msg)) from e
def modify_attachment(self, attachment):
try:
cursor = self.conn.cursor()
mod_fields = ""
if 'id' not in attachment:
raise GulagDBBadInputException("Missing Attachment-ID!")
if len(attachment) < 2:
raise GulagDBBadInputException("No fields specified to modify!")
for field in attachment:
if field == 'id':
continue
mod_fields += " " + field + "='" + attachment[field] + "',"
mod_fields = str(mod_fields).rstrip(',')
cursor.execute(
"update Attachment set "+mod_fields+" where id="+str(attachment['id'])
)
if(cursor.rowcount == 0):
raise GulagDBNotFoundException(whoami(self) + "No attachments modified!")
cursor.close()
return True
except GulagDBBadInputException as e:
raise GulagDBBadInputException(whoami(self) + e.message) from e
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e.msg)) from e
def get_attachments(self): def get_attachments(self):
try: try:
query = "select Attachments.*,QuarMails.mailbox_id,QuarMails.imap_uid" query = "select Attachments.*,QuarMails.mailbox_id,QuarMails.imap_uid"
@ -433,7 +458,9 @@ class GulagDB:
dict = {} dict = {}
for (name, value) in zip(desc, tuple): for (name, value) in zip(desc, tuple):
dict[name[0]] = value dict[name[0]] = value
dict['href'] = self.uri_prefixes['attachments'] + str(dict['id']) #dict['href'] = self.uri_prefixes['attachments'] + str(dict['id'])
dict['href'] = self.uri_prefixes['quarmails'] + str(quarmail_id)
dict['href'] += "/attachments/" + str(dict['id'])
results.append(Attachment(dict).__dict__) results.append(Attachment(dict).__dict__)
return results return results
except mariadb.Error as e: except mariadb.Error as e:
@ -458,7 +485,9 @@ class GulagDB:
dict = {} dict = {}
for (name, value) in zip(desc, tuple): for (name, value) in zip(desc, tuple):
dict[name[0]] = value dict[name[0]] = value
dict['href'] = self.uri_prefixes['attachments'] + str(dict['id']) #dict['href'] = self.uri_prefixes['attachments'] + str(dict['id'])
dict['href'] = self.uri_prefixes['quarmails'] + str(quarmail_id)
dict['href'] += "/attachments/" + str(dict['id'])
return Attachment(dict).__dict__ return Attachment(dict).__dict__
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e.msg)) from e raise GulagDBException(whoami(self) + str(e.msg)) from e
@ -554,6 +583,29 @@ class GulagDB:
except mariadb.Error as e: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e.msg)) from e raise GulagDBException(whoami(self) + str(e.msg)) from e
def modify_uri(self, uri):
try:
cursor = self.conn.cursor()
mod_fields = ""
if 'id' not in uri:
raise GulagDBBadInputException("Missing URI-ID!")
if len(uri) < 2:
raise GulagDBBadInputException("No fields specified to modify!")
for field in uri:
if field == 'id':
continue
mod_fields += " " + field + "='" + uri[field] + "',"
mod_fields = str(mod_fields).rstrip(',')
cursor.execute("update URI set "+mod_fields+" where id="+str(uri['id']))
if(cursor.rowcount == 0):
raise GulagDBNotFoundException(whoami(self) + "No URIs modified!")
cursor.close()
return True
except GulagDBBadInputException as e:
raise GulagDBBadInputException(whoami(self) + e.message) from e
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e.msg)) from e
def quarmail2uri(self,quarmail_id,uri_id): def quarmail2uri(self,quarmail_id,uri_id):
try: try:
cursor = self.conn.cursor() cursor = self.conn.cursor()
@ -582,6 +634,7 @@ class GulagDB:
dict = {} dict = {}
for (name, value) in zip(desc, tuple): for (name, value) in zip(desc, tuple):
dict[name[0]] = value dict[name[0]] = value
#dict['href'] = self.uri_prefixes['uris'] + str(dict['id'])
dict['href'] = self.uri_prefixes['quarmails'] + str(quarmail_id) dict['href'] = self.uri_prefixes['quarmails'] + str(quarmail_id)
dict['href'] += "/uris/" + str(dict['id']) dict['href'] += "/uris/" + str(dict['id'])
results.append(URI(dict).__dict__) results.append(URI(dict).__dict__)
@ -612,6 +665,7 @@ class GulagDB:
dict = {} dict = {}
for (name, value) in zip(desc, tuple): for (name, value) in zip(desc, tuple):
dict[name[0]] = value dict[name[0]] = value
#dict['href'] = self.uri_prefixes['uris'] + str(dict['id'])
dict['href'] = self.uri_prefixes['quarmails'] + str(quarmail_id) dict['href'] = self.uri_prefixes['quarmails'] + str(quarmail_id)
dict['href'] += "/uris/" + str(dict['id']) dict['href'] += "/uris/" + str(dict['id'])
try: try:

View File

@ -12,6 +12,7 @@ class GulagResource(Resource):
self.gulag = gulag_object self.gulag = gulag_object
#XXX self.check_trusted_proxy() #XXX self.check_trusted_proxy()
#XXX self.check_auth() #XXX self.check_auth()
self.check_max_body_size()
def check_trusted_proxy(self): def check_trusted_proxy(self):
remote_ip = request.remote_addr remote_ip = request.remote_addr
@ -31,11 +32,12 @@ class GulagResource(Resource):
if api_key not in self.gulag.config['api_keys']: if api_key not in self.gulag.config['api_keys']:
abort(401, message="NOT AUTHORIZED!") abort(401, message="NOT AUTHORIZED!")
def check_dos(self): def check_max_body_size(self):
body_len = len(request.get_data(as_text=True)) body_len = len(request.get_data(as_text=True))
if(body_len > self.gulag.config['dos_protection']['max_body_bytes']): if(body_len > self.gulag.config['dos_protection']['max_body_bytes']):
raise GulagBadInputException(whoami(self) + raise GulagBadInputException(whoami(self) +
"Request body max size exceeded" "Request exceedes maximum body size (" +
self.gulag.config['dos_protection']['max_body_bytes'] + " bytes)!"
) )
class ResRoot(GulagResource): class ResRoot(GulagResource):
@ -191,6 +193,46 @@ class ResAttachment(GulagResource):
abort(404, message=whoami(self)+e.message) abort(404, message=whoami(self)+e.message)
except GulagException as e: except GulagException as e:
abort(500, message=whoami(self)+e.message) abort(500, message=whoami(self)+e.message)
def patch(self,attachment_id):
try:
args = json.loads(request.get_data(as_text=True))
args['id'] = attachment_id
except json.JSONDecodeError as e:
abort(400, message=whoami(self) + "Invalid JSON: " + e.msg)
try:
self.gulag.modify_attachment(args)
return Response(response=None,status=204,mimetype=None)
except GulagBadInputException as e:
abort(400, message=whoami(self)+e.message)
except GulagNotFoundException as e:
abort(404, message=whoami(self)+e.message)
except GulagException as e:
abort(500, message=whoami(self)+e.message)
class ResURI(GulagResource):
def get(self,uri_id):
args = {"id": uri_id}
try:
return self.gulag.get_uri(args)
except GulagNotFoundException as e:
abort(404, message=whoami(self)+e.message)
except GulagException as e:
abort(500, message=whoami(self)+e.message)
def patch(self,uri_id):
try:
args = json.loads(request.get_data(as_text=True))
args['id'] = uri_id
except json.JSONDecodeError as e:
abort(400, message=whoami(self) + "Invalid JSON: " + e.msg)
try:
self.gulag.modify_uri(args)
return Response(response=None,status=204,mimetype=None)
except GulagBadInputException as e:
abort(400, message=whoami(self)+e.message)
except GulagNotFoundException as e:
abort(404, message=whoami(self)+e.message)
except GulagException as e:
abort(500, message=whoami(self)+e.message)
class ResRspamd2Mailbox(GulagResource): class ResRspamd2Mailbox(GulagResource):
def post(self,mailbox_id): def post(self,mailbox_id):

View File

@ -27,7 +27,8 @@
"mailrelays": "http://127.0.0.1:9090/api/v1/mailrelays/", "mailrelays": "http://127.0.0.1:9090/api/v1/mailrelays/",
"mailboxes": "http://127.0.0.1:9090/api/v1/mailboxes/", "mailboxes": "http://127.0.0.1:9090/api/v1/mailboxes/",
"quarmails": "http://127.0.0.1:9090/api/v1/quarmails/", "quarmails": "http://127.0.0.1:9090/api/v1/quarmails/",
"attachments": "http://127.0.0.1:9090/api/v1/attachments/" "attachments": "http://127.0.0.1:9090/api/v1/attachments/",
"uris": "http://127.0.0.1:9090/api/v1/uris/"
}, },
"dos_protection": { "dos_protection": {
"max_body_bytes": 8388608 "max_body_bytes": 8388608