Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
Merge bef4503 into b9c66ae
Browse files Browse the repository at this point in the history
  • Loading branch information
mikegrima committed Oct 10, 2018
2 parents b9c66ae + bef4503 commit 8cd8db7
Show file tree
Hide file tree
Showing 44 changed files with 964 additions and 621 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ venv
*.test.yml
serverless.yml
serverless_configs/*

test-reports/*

### VisualStudioCode ###
.vscode
Expand Down
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MESSAGES CONTROL]
disable=C0301,R0913,W1202,W1203,R0903,R0201,R0801
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ language: python
python:
- "3.6"

before_install:
- sudo rm -f /etc/boto.cfg

install:
- pip install tox-travis


matrix:
include:
- env:
- env: TOXENV=linters

script:
- tox
9 changes: 8 additions & 1 deletion historical/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"""
.. module: historical
:platform: Unix
:copyright: (c) 2017 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. author:: Mike Grima <mgrima@netflix.com>
"""
from __future__ import absolute_import, division, print_function

__all__ = [
Expand All @@ -15,4 +22,4 @@
__email__ = "security@netflix.com"

__license__ = "Apache License, Version 2.0"
__copyright__ = "Copyright 2017 {0}".format(__author__)
__copyright__ = f"Copyright 2017 {__author__}"
7 changes: 7 additions & 0 deletions historical/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
.. module: historical
:platform: Unix
:copyright: (c) 2017 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. author:: Mike Grima <mgrima@netflix.com>
"""
48 changes: 18 additions & 30 deletions historical/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,60 @@
import json
import decimal

from pynamodb.attributes import (
Attribute,
MapAttribute,
ListAttribute,
BooleanAttribute,
NumberAttribute,
)
from pynamodb.attributes import Attribute, BooleanAttribute, ListAttribute, MapAttribute, NumberAttribute

import pynamodb
from pynamodb.constants import STRING, NUMBER
from pynamodb.constants import NUMBER, STRING

DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'


class HistoricalUnicodeAttribute(Attribute):
"""
A Historical unicode attribute.
"""A Historical unicode attribute.
Replaces '' with '<empty>' during serialization and correctly deserialize '<empty>' to ''
"""

attr_type = STRING

def serialize(self, value):
"""
Returns a unicode string
"""
if value is None or not len(value):
"""Returns a unicode string"""
if value is None or not len(value): # pylint: disable=C1801
return '<empty>'
return value

def deserialize(self, value):
"""Strips out the `<empty>` placeholders with empty strings."""
if value == '<empty>':
return ''
return value


class EventTimeAttribute(Attribute):
"""
An attribute for storing a UTC Datetime or iso8601 string
"""
"""An attribute for storing a UTC Datetime or iso8601 string."""

attr_type = STRING

def serialize(self, value):
"""
Takes a datetime object and returns a string
"""
"""Takes a datetime object and returns a string"""
if isinstance(value, str):
return value
return value.strftime(DATETIME_FORMAT)


def decimal_default(obj):
"""Properly parse out the Decimal datatypes into proper int/float types."""
if isinstance(obj, decimal.Decimal):
if obj % 1:
return float(obj)
return int(obj)
raise TypeError


# pylint: disable=R1705,C0200
def fix_decimals(obj):
"""Removes the stupid Decimals
See: https://github.com/boto/boto3/issues/369#issuecomment-302137290
See: https://github.com/boto/boto3/issues/369#issuecomment-302137290
"""
if isinstance(obj, list):
for i in range(len(obj)):
Expand All @@ -92,21 +85,16 @@ def fix_decimals(obj):


class HistoricalDecimalAttribute(Attribute):
"""
A number attribute
"""
"""A number attribute"""

attr_type = NUMBER

def serialize(self, value):
"""
Encode numbers as JSON
"""
"""Encode numbers as JSON"""
return json.dumps(value, default=decimal_default)

def deserialize(self, value):
"""
Decode numbers from JSON
"""
"""Decode numbers from JSON"""
return json.loads(value)


Expand Down
16 changes: 12 additions & 4 deletions historical/cli.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
"""
.. module: historical.cli
:platform: Unix
:copyright: (c) 2017 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. author:: Mike Grima <mgrima@netflix.com>
"""
import os
import logging

import click
import click_log
from cookiecutter.main import cookiecutter
from cookiecutter.main import cookiecutter # pylint: disable=E0401

from historical.__about__ import __version__

log = logging.getLogger('historical')
click_log.basic_config(log)
LOG = logging.getLogger('historical')
click_log.basic_config(LOG)


@click.group()
@click_log.simple_verbosity_option(log)
@click_log.simple_verbosity_option(LOG)
@click.version_option(version=__version__)
def cli():
"""Historical commandline for managing historical functions."""
Expand Down
8 changes: 8 additions & 0 deletions historical/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
.. module: historical.common
:platform: Unix
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. author:: Kevin Glisson <kglisson@netflix.com>
.. author:: Mike Grima <mgrima@netflix.com>
"""
14 changes: 5 additions & 9 deletions historical/common/cloudwatch.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
"""
Helper functions for processing cloudwatch events.
.. module: historical.common.cloudwatch
:platform: Unix
:copyright: (c) 2017 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. author:: Kevin Glisson <kglisson@netflix.com>
.. author:: Mike Grima <mgrima@netflix.com>
"""
import logging
from datetime import datetime

from historical.constants import CURRENT_REGION

logger = logging.getLogger(__name__)


def filter_request_parameters(field_name, msg, look_in_response=False):
"""
Expand All @@ -31,7 +27,7 @@ def filter_request_parameters(field_name, msg, look_in_response=False):
val = msg['detail']['responseElements'].get(field_name, None)

# Just in case... We didn't find the value, so just make it None:
except AttributeError as _:
except AttributeError:
val = None

return val
Expand All @@ -44,8 +40,8 @@ def get_user_identity(event):

def get_principal(event):
"""Gets principal id from the event"""
ui = get_user_identity(event)
return ui.get('principalId', '').split(':')[-1]
user_identity = get_user_identity(event)
return user_identity.get('principalId', '').split(':')[-1]


def get_region(event):
Expand All @@ -69,6 +65,7 @@ def get_collected_details(event):


def get_historical_base_info(event):
"""Gets the base details from the CloudWatch Event."""
data = {
'principalId': get_principal(event),
'userIdentity': get_user_identity(event),
Expand All @@ -85,4 +82,3 @@ def get_historical_base_info(event):
data['eventSource'] = event['detail']['eventSource']

return data

0 comments on commit 8cd8db7

Please sign in to comment.