-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lookup_tables] Support lookup tables
- Loading branch information
Chunyong Lin
committed
May 25, 2018
1 parent
5c24f16
commit 7fc9d7d
Showing
7 changed files
with
327 additions
and
2 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
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,104 @@ | ||
""" | ||
Copyright 2017-present, Airbnb Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
from datetime import datetime, timedelta | ||
import json | ||
import os | ||
import time | ||
|
||
import boto3 | ||
from botocore.exceptions import ClientError | ||
|
||
from stream_alert.shared import LOGGER | ||
|
||
class LookupTables(object): | ||
"""Lookup Tables to useful information which can be referenced from rules""" | ||
|
||
_LOOKUP_TABLES_LAST_REFRESH = datetime(year=1970, month=1, day=1) | ||
|
||
def __init__(self, buckets_info): | ||
self._s3_client = boto3.resource('s3') | ||
self._buckets_info = buckets_info | ||
|
||
def download_s3_objects(self): | ||
"""Download S3 files (json format) from S3 buckets into memory. | ||
Returns: | ||
dict: A dictionary contains information loaded from S3. The file name | ||
will be the key, and value is file content in json format. | ||
""" | ||
|
||
_lookup_tables = {} | ||
|
||
for bucket, files in self._buckets_info.iteritems(): | ||
for json_file in files: | ||
table_name = os.path.splitext(json_file)[0] | ||
try: | ||
start_time = time.time() | ||
s3_object = self._s3_client.Object(bucket, json_file).get() | ||
size_kb = round(s3_object.get('ContentLength') / 1024.0, 2) | ||
size_mb = round(size_kb / 1024.0, 2) | ||
display_size = '{}MB'.format(size_mb) if size_mb else '{}KB'.format(size_kb) | ||
LOGGER.info('Downloaded S3 file size %s and updated lookup table %s', | ||
display_size, table_name) | ||
_lookup_tables[table_name] = json.loads(s3_object.get('Body').read()) | ||
except ClientError as err: | ||
LOGGER.error('Encounterred error while downloading %s from %s, %s', | ||
json_file, bucket, err.response['Error']['Message']) | ||
return _lookup_tables | ||
|
||
total_time = time.time() - start_time | ||
LOGGER.info('Downloaded S3 file %s seconds', round(total_time, 2)) | ||
|
||
return _lookup_tables | ||
|
||
@classmethod | ||
def load_lookup_tables(cls, config): | ||
"""Load arbitrary json files to memory from S3 buckets when lookup table enabled | ||
The lookup tables will also be refreshed based on "cache_refresh_minutes" setting | ||
in the config. | ||
Args: | ||
config (dict): Loaded configuration from 'conf/' directory | ||
Returns: | ||
Return False if lookup table enabled or missing config. Otherwise, it | ||
will return an instance of LookupTables class. | ||
""" | ||
lookup_tables = config['global']['infrastructure'].get('lookup_tables') | ||
if not (lookup_tables and lookup_tables.get('enabled', False)): | ||
return False | ||
|
||
buckets_info = lookup_tables.get('buckets') | ||
if not buckets_info: | ||
LOGGER.error('Buckets not defined') | ||
return False | ||
|
||
lookup_refresh_interval = lookup_tables.get('cache_refresh_minutes', 10) | ||
now = datetime.utcnow() | ||
refresh_delta = timedelta(minutes=lookup_refresh_interval) | ||
needs_refresh = cls._LOOKUP_TABLES_LAST_REFRESH + refresh_delta < now | ||
if not needs_refresh: | ||
LOGGER.debug('lookup tables do not need refresh (last refresh time: %s; ' | ||
'current time: %s)', cls._LOOKUP_TABLES_LAST_REFRESH, now) | ||
return False | ||
|
||
LOGGER.info('Refreshing lookup tables (last refresh time: %s; current time: %s)', | ||
cls._LOOKUP_TABLES_LAST_REFRESH, now) | ||
|
||
cls._LOOKUP_TABLES_LAST_REFRESH = now | ||
|
||
return cls(buckets_info) |
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.