Skip to content

Commit

Permalink
Principal IAM Resolver OSS logic (Netflix#94)
Browse files Browse the repository at this point in the history
* Principal IAM Resolver OSS logic

* Remove unused code
  • Loading branch information
castrapel authored May 20, 2020
1 parent a3a1802 commit 5e623af
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
8 changes: 8 additions & 0 deletions consoleme/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,11 @@ class DataNotRetrievable(BaseException):
def __init__(self, msg=""):
stats.count("DataNotRetrievable")
super().__init__(msg)


class MissingConfigurationValue(BaseException):
"""Unable to find expected configuration value"""

def __init__(self, msg=""):
stats.count("MissingConfigurationValue")
super().__init__(msg)
Empty file.
39 changes: 39 additions & 0 deletions consoleme/lib/aws_config/aws_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import List

import boto3
import ujson as json

from consoleme.config import config
from consoleme.exceptions.exceptions import MissingConfigurationValue
from consoleme.lib.plugins import get_plugin_by_name

log = config.get_logger()
stats = get_plugin_by_name(config.get("plugins.metrics"))()

client = boto3.client("config")


def query(query: str) -> List:
resources = []
configuration_aggregator_name: str = config.get(
"aws_config.configuration_aggregator.name"
)
if not configuration_aggregator_name:
raise MissingConfigurationValue("Invalid configuration for aws_config")
response = client.select_aggregate_resource_config(
Expression=query,
ConfigurationAggregatorName=configuration_aggregator_name,
Limit=100,
)
for r in response.get("Results", []):
resources.append(json.loads(r))
while response.get("NextToken"):
response = client.select_aggregate_resource_config(
Expression=query,
ConfigurationAggregatorName=configuration_aggregator_name,
Limit=100,
NextToken=response["NextToken"],
)
for r in response.get("Results", []):
resources.append(json.loads(r))
return resources
21 changes: 18 additions & 3 deletions consoleme/lib/generic.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
import random
import re
from datetime import datetime
from random import randint
from typing import Dict, List, Optional, Union

import pandas as pd
import re
import ujson as json
from datetime import datetime
from dateutil import parser
from tornado.web import RequestHandler
from typing import Dict, List, Optional, Union


def str2bool(v: Optional[Union[bool, str]]) -> bool:
Expand All @@ -19,6 +19,21 @@ def str2bool(v: Optional[Union[bool, str]]) -> bool:
return v.lower() in ["true", "True"]


# Yield successive n-sized
# chunks from list l.
def divide_chunks(l, n):
"""
Yields successive n=zied chunks from list l by looping
until length l.
`divide_chunks(["a","b","c","d","e"], 2)` yields:
['a', 'b', 'c']
['d', 'e']
"""
for i in range(0, len(l), n):
yield l[i : i + n]


def generate_html(d: List[Dict[str, Union[str, bool]]]) -> str:
"""
Pass in a dict with a list of rows to include in a formatted table. This will return the HTML for the table.
Expand Down
9 changes: 6 additions & 3 deletions tests/lib/test_generic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from unittest import TestCase

from datetime import datetime
from unittest import TestCase

from consoleme.lib.generic import is_in_time_range
from consoleme.lib.generic import divide_chunks, is_in_time_range

VALID_RANGE = {
"days": [0, 1, 2, 3],
Expand Down Expand Up @@ -33,3 +32,7 @@ def test_is_in_time_range_wrong_day(self):
x = datetime(2020, 3, 6, 15, 30) # Friday
result = is_in_time_range(x, VALID_RANGE)
self.assertEqual(result, False)

def test_divide_chunks(self):
r = list(divide_chunks(["a", "b", "c", "d", "e"], 3))
self.assertEqual(r, [["a", "b", "c"], ["d", "e"]])
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ skips = B104
exclude = /tests

[flake8]
ignore = E501,I100,I201,I202,D102,D100,C901,D104,D101,D105,D107,D400,D103,D205,W503
ignore = E203,E501,I100,I201,I202,D102,D100,C901,D104,D101,D105,D107,D400,D103,D205,W503
exclude =
*.egg-info,
*.pyc,
Expand Down

0 comments on commit 5e623af

Please sign in to comment.