示例#1
0
    def test_formatter(self):

            for prefix in ['±±±', 'abc']:

                with LogCapture() as lc:

                    data = '{}{}'.format(prefix, uuid4().hex)
                    key = genkey()
                    fernet = Fernet(key)

                    level = logging.INFO
                    format = '%(levelname)s - %(message)s'
                    formatter = EncryptedLogFormatter(key, format)

                    handler = logging.StreamHandler()
                    handler.setFormatter(formatter)

                    logger = logging.getLogger('')
                    logger.addHandler(handler)
                    logger.setLevel(level)

                    logger.info(data)

                    encrypted = list(lc.records)[0].msg
                    encrypted = encrypted.split(log_prefix)[1]
                    decrypted = fernet.decrypt(encrypted.encode('utf8')).decode('utf8')

                    self.assertEquals(data, decrypted)
示例#2
0
    def test_crypto_round_trip(self):
        clear_text = uuid4().hex.encode('utf8')
        key = genkey()
        fernet = Fernet(key)
        encrypted = fernet.encrypt(clear_text)
        decrypted = fernet.decrypt(encrypted)

        self.assertEquals(clear_text, decrypted)
示例#3
0
 def test_genkey(self):
     key = genkey()
     Fernet(key) # Must not raise an exception otherwise we've generated an invalid key