This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
325 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Our app specific CEF loggers.""" | ||
from django.conf import settings | ||
from django.http import HttpRequest | ||
|
||
from cef import log_cef as _log_cef | ||
|
||
|
||
class CEFLogger: | ||
"""Abstract base CEF logger. | ||
Class attributes to set in a concrete class: | ||
**sig_prefix** | ||
Prefix to the CEF signature. Example: RECEIPT | ||
**cs2label** | ||
cs2label parameter. Example: ReceiptTransaction | ||
**msg_prefix** | ||
Prefix to all CEF log messages. Example: Receipt | ||
**default_severity** | ||
If set, this should be a 0-10 int. | ||
""" | ||
sig_prefix = '' | ||
cs2label = None | ||
msg_prefix = '' | ||
default_severity = None | ||
|
||
def log(self, environ, app, msg, longer, severity=None, | ||
extra_kwargs=None): | ||
"""Log something important using the CEF library. | ||
Parameters: | ||
**environ** | ||
Typically a Django request object. It can also be | ||
a plain dict. | ||
**app** | ||
An app/addon object. | ||
**msg** | ||
A short message about the incident. | ||
**longer** | ||
A more description message about the incident. | ||
**severity=None** | ||
A 0-10 int to override the default severity. | ||
**extra_kwargs** | ||
A dict to override anything sent to the CEF library. | ||
""" | ||
c = {'cef.product': getattr(settings, 'CEF_PRODUCT', 'AMO'), | ||
'cef.vendor': getattr(settings, 'CEF_VENDOR', 'Mozilla'), | ||
'cef.version': getattr(settings, 'CEF_VERSION', '0'), | ||
'cef.device_version': getattr(settings, 'CEF_DEVICE_VERSION', '0'), | ||
'cef.file': getattr(settings, 'CEF_FILE', 'syslog'), } | ||
|
||
kwargs = {'username': getattr(environ, 'amo_user', ''), | ||
'signature': '%s%s' % (self.sig_prefix, msg.upper()), | ||
'msg': longer, 'config': c, | ||
'cs2': app, 'cs2Label': self.cs2label} | ||
if extra_kwargs: | ||
kwargs.update(extra_kwargs) | ||
|
||
if not severity: | ||
severity = self.default_severity | ||
if not severity: | ||
raise ValueError('CEF severity was not defined') | ||
|
||
if isinstance(environ, HttpRequest): | ||
environ = environ.META.copy() | ||
return _log_cef('%s %s' % (self.msg_prefix, msg), severity, | ||
environ, **kwargs) | ||
|
||
|
||
class ReceiptCEFLogger(CEFLogger): | ||
sig_prefix = 'RECEIPT' | ||
cs2label = 'ReceiptTransaction' | ||
msg_prefix = 'Receipt' | ||
default_severity = 5 | ||
|
||
|
||
receipt_cef = ReceiptCEFLogger() | ||
|
||
|
||
class InappCEFLogger(CEFLogger): | ||
sig_prefix = 'INAPP_PAY' | ||
cs2label = 'InappPayment' | ||
msg_prefix = 'InappPayment' | ||
default_severity = 5 | ||
|
||
|
||
inapp_cef = InappCEFLogger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import base64 | ||
import os | ||
|
||
|
||
__all__ = ['generate_key'] | ||
|
||
|
||
def generate_key(byte_length): | ||
"""Return a true random ascii string that is byte_length long. | ||
"""Return a true random ascii string containing byte_length of randomness. | ||
The resulting key is suitable for cryptogrpahy. | ||
The key will be hex encoded which means it will be twice as long | ||
as byte_length, i.e. 40 random bytes yields an 80 byte string. | ||
byte_length must be at least 32. | ||
""" | ||
if byte_length < 32: # at least 256 bit | ||
raise ValueError('um, %s is probably not long enough for cryptography' | ||
% byte_length) | ||
key = os.urandom(byte_length) | ||
key = base64.b64encode(key).rstrip('=') # strip off padding | ||
key = key[0:byte_length] | ||
return key | ||
return os.urandom(byte_length).encode('hex') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.