unexpected logging and handling exceptions

Asked by Brandon Murphy

sorry, in typing this I've realized this is probably less of a question and more of a request or maybe event just feedback.

when importing the module and calling dkim_verify as follows:
    dkim_result = dkim.dkim_verify(message)

results in several unexpected logs being written

    ERROR:dkimpy:missing public key:
    ERROR:dkimpy:bad version
    ERROR:dkimpy:"k=rsa
    ERROR:dkimpy:body hash mismatch

I had assumed that by not passing a logger variable to dkim_verify that all logging would be disabled - clearly this isn't the case

my project consists of generating metadata on emails - knowing that an email's DKIM signature's validation had a missing public key, or bad version can be a useful piece of information regarding the email.

as a user importing the module into another project, I'd prefer the exceptions be handled further "upstream" instead of being logged and handled in returning False. For instance, in my use case I could handle the exception when I call dkim.dkim_verify and take different actions based on the content of the exceptions.

However, because these exceptions are just being logged (and returning false) instead of allowed to be raised through to the "upstream" functions, I am unable to do so.

I assume the fact the verify function returns True or False (regardless of errors encountered) is by design, it does limit the options in being able to use other useful information generated when parsing the DKIM signature.

Question information

Language:
English Edit question
Status:
Answered
For:
dkimpy Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Scott Kitterman (kitterman) said :
#1

You probably want to call verify_sig directly for your application. You are correct, it's by design to return True/False for the verification result.

Revision history for this message
Brandon Murphy (zoomequipd) said :
#2

I think there is one notable exception (no pun intended) with that

lines 652-954 catch the KeyFormatError exception

    except KeyFormatError as e:
      self.logger.error("%s" % e)
      return False

I'm also not sure I fully understand the how to get all the variables from the message (as a string) required to call verify_sig either - is there any chance for an example of working through that process?

Revision history for this message
Scott Kitterman (kitterman) said :
#3

True. You could add that check to your code before you call verify_sig and skip over the call if it fails. Not ideal, but given the code, that's the best I can see getting.

Since this is an internal function, I don't know of any examples of how to use it externally. You'd have to look at how it's used within dkimpy. Also, if you're on python3, you want to do this in bytes, not string (doesn't matter for python2).

I'd be willing to consider code changes to better support your use case, but you'd have to supply the proposed change.

Can you help with this problem?

Provide an answer of your own, or ask Brandon Murphy for more information if necessary.

To post a message you must log in.