Skip to content
This repository was archived by the owner on Sep 17, 2021. It is now read-only.

Commit 2f5302b

Browse files
authored
Merge pull request #1042 from mikegrima/settingsfix
Possible fix for notifications bug in #998
2 parents 69b0842 + 3d71b8f commit 2f5302b

File tree

9 files changed

+47
-29
lines changed

9 files changed

+47
-29
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ matrix:
1616
env:
1717
- UNIT_TEST_JOB=true
1818
- PIP_DOWNLOAD_CACHE=".pip_download_cache"
19+
- SECURITY_MONKEY_SETTINGS="$( pwd )/env-config/config.py"
1920

2021
addons:
2122
postgresql: "9.4"
@@ -31,8 +32,8 @@ matrix:
3132
- pip install openstacksdk
3233
- pip install cloudaux\[gcp\]
3334
- pip install cloudaux\[openstack\]
34-
- python setup.py develop
35-
- pip install .[tests]
35+
- pip install -e .
36+
- pip install ."[tests]"
3637
- pip install coveralls
3738
- monkey db upgrade
3839
- monkey amazon_accounts
@@ -130,7 +131,7 @@ matrix:
130131
- BUILD_DOCKER=True
131132
- DOCKER_COMPOSE_VERSION=1.18.0
132133
- PIP_DOWNLOAD_CACHE=".pip_download_cache"
133-
- SECURITY_MONKEY_SETTINGS=`pwd`/env-config/config.py
134+
- SECURITY_MONKEY_SETTINGS="$( pwd )/env-config/config.py"
134135

135136
services:
136137
- docker

docs/autostarting.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ firewall rules to permit worker instances access. This is documented in the abov
9191
If installing on the `localhost` of the scheduler instance, you will need to install Redis on the instance (this is complete if following the quickstart guide).
9292

9393
### Celery Configuration
94-
You will need to modify the `celeryconfig.py` file that is stored in the base Security Monkey location
95-
at `/usr/local/src/security_monkey/celeryconfig.py`(https://github.com/Netflix/security_monkey/blob/develop/celeryconfig.py).
94+
You will need to modify the `security_monkey/celeryconfig.py` file that is stored in the base Security Monkey location
95+
at `/usr/local/src/security_monkey/security_monkey/celeryconfig.py`(https://github.com/Netflix/security_monkey/blob/develop/security_monkey/celeryconfig.py).
9696

9797
This file looks like this:
9898
```
@@ -153,7 +153,7 @@ The workers are instances that fetch data from your configured accounts. These a
153153

154154
You are able to deploy as many workers as you like for your environment. Security Monkey splits up tasks based on the account and technology pair.
155155

156-
Similar to configuring the Scheduler above, the workers need to have the **same** `celeryconfig.py` as the scheduler. In here, you can optionally configure
156+
Similar to configuring the Scheduler above, the workers need to have the **same** `security_monkey/celeryconfig.py` as the scheduler. In here, you can optionally configure
157157
the number of processes that exist within each instance (via the `worker_concurrency` configuration). By default 10 is selected. You can adjust this as necessary. In general, if you would like to
158158
scale horizontally, you should deploy more worker instances. This will allow for maximum parallelization.
159159

docs/tuneworkers.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Create a new Redis cache ([ElastiCache works well](elasticache_directions.md)),
2020
**Keep note of the endpoint, you'll need this later**.
2121

2222
## Create a dedicated Celery configuration
23-
For this use case, you would have two different [Celery configuration Python](https://github.com/Netflix/security_monkey/blob/develop/celeryconfig.py) files.
23+
For this use case, you would have two different [Celery configuration Python](https://github.com/Netflix/security_monkey/blob/develop/securitymonkey/celeryconfig.py) files.
2424
You will need to make note of the following section:
2525
```
2626
# This specifies a list of technologies that workers for the above Redis broker should IGNORE.
@@ -36,15 +36,16 @@ In these variables, you will enter in the index name of the technology. For exam
3636
of the technologies as they appear in the UI.
3737

3838
For this use case, we are going to have a dedicated stack of workers (called the `iam` stack) for IAM Roles, and another stack for everything else (called the `main` stack).
39-
1. Make a copy of `celeryconfig.py`, and call it `mainceleryconfig.py`
40-
1. In `mainceleryconfig.py`, make a modification to the `security_monkey_watcher_ignore` variable such that its value is:
39+
1. Make a copy of `security_monkey/celeryconfig.py`, and call it `security_monkey/mainceleryconfig.py`
40+
1. In `security_monkey/mainceleryconfig.py`, make a modification to the `security_monkey_watcher_ignore` variable such that its value is:
4141
```
4242
security_monkey_watcher_ignore = set(['iamrole'])
4343
```
4444
1. Save the file.
4545
4646
Next, you will need to make it so that your scheduler and corresponding set of workers that will load this configuration. There is a new environment variable
4747
that Security Monkey will check to properly load this configuration: `SM_CELERY_CONFIG`. For this stack, `SM_CELERY_CONFIG` needs to be set to: `"mainceleryconfig.py"`.
48+
(Do not place `security_monkey` in the variable name...just call it the destination name of the file that resides within the `security_monkey/` python code location -- this is the same place that `manage.py` lives)
4849
Because we utilize `supervisor`, you will need to add this to the `environment` section. Here are sample configurations:
4950
5051
*MAIN-SCHEDULER*

celeryconfig.py renamed to security_monkey/celeryconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
.. module: celeryconfig
2+
.. module: securitymonkey.celeryconfig
33
:platform: Unix
44
:synopsis: Use this file to set up the Celery configuration for task scheduling.
55

security_monkey/manage.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ def amazon_accounts():
166166
""" Pre-populates standard AWS owned accounts """
167167
import json
168168
from security_monkey.datastore import Account, AccountType
169-
from os.path import dirname, join
170169

171-
data_file = join(dirname(dirname(__file__)), "data", "aws_accounts.json")
172-
data = json.load(open(data_file, 'r'))
170+
data = json.load(open("data/aws_accounts.json", 'r'))
173171

174172
app.logger.info('Adding / updating Amazon owned accounts')
175173
try:

security_monkey/task_scheduler/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
from security_monkey.common.utils import find_modules
1414

1515
import os
16+
import importlib
1617

1718
from security_monkey.exceptions import InvalidCeleryConfigurationType
1819

1920

2021
def get_celery_config_file():
2122
"""This gets the Celery configuration file as a module that Celery uses"""
22-
return __import__(os.environ.get("SM_CELERY_CONFIG", "celeryconfig"))
23+
return importlib.import_module("security_monkey.{}".format(os.environ.get("SM_CELERY_CONFIG", "celeryconfig")),
24+
"security_monkey")
2325

2426

2527
def make_celery(app):
@@ -38,6 +40,7 @@ def make_celery(app):
3840
celery.conf.update(app.config)
3941

4042
TaskBase = celery.Task
43+
4144
class ContextTask(TaskBase):
4245
abstract = True
4346

security_monkey/tests/scheduling/test_celery_scheduler.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ def test_celery_purge(self):
354354
assert mock.control.purge.called
355355

356356
def test_get_sm_celery_config_value(self):
357-
import celeryconfig
358-
setattr(celeryconfig, "test_value", {"some", "set", "of", "things"})
357+
import security_monkey.celeryconfig
358+
setattr(security_monkey.celeryconfig, "test_value", {"some", "set", "of", "things"})
359359
# We should get the proper thing back out:
360360
from security_monkey.task_scheduler.util import get_sm_celery_config_value, get_celery_config_file
361361
c = get_celery_config_file()
@@ -382,8 +382,8 @@ def test_get_sm_celery_config_value(self):
382382
def test_get_celery_config_file(self):
383383
import os
384384
from security_monkey.task_scheduler.util import get_celery_config_file
385-
os.environ["SM_CELERY_CONFIG"] = "security_monkey"
386-
assert hasattr(get_celery_config_file(), "app")
385+
os.environ["SM_CELERY_CONFIG"] = "celeryconfig"
386+
assert hasattr(get_celery_config_file(), "broker_url")
387387

388388
del os.environ["SM_CELERY_CONFIG"]
389389
assert hasattr(get_celery_config_file(), "broker_url")
@@ -559,8 +559,8 @@ def test_celery_skipabeat(self, mock_store_exception, mock_expired_exceptions, m
559559
@patch("security_monkey.task_scheduler.beat.store_exception")
560560
def test_celery_only_tech(self, mock_store_exception, mock_expired_exceptions, mock_account_tech, mock_purge,
561561
mock_setup):
562-
import celeryconfig
563-
celeryconfig.security_monkey_only_watch = {"iamrole"}
562+
import security_monkey.celeryconfig
563+
security_monkey.celeryconfig.security_monkey_only_watch = {"iamrole"}
564564

565565
from security_monkey.task_scheduler.beat import setup_the_tasks
566566
from security_monkey.watchers.iam.iam_role import IAMRole
@@ -619,8 +619,8 @@ def test_celery_only_tech(self, mock_store_exception, mock_expired_exceptions, m
619619
@patch("security_monkey.task_scheduler.beat.store_exception")
620620
def test_celery_ignore_tech(self, mock_store_exception, mock_expired_exceptions, mock_account_tech, mock_purge,
621621
mock_setup):
622-
import celeryconfig
623-
celeryconfig.security_monkey_watcher_ignore = {"policy"}
622+
import security_monkey.celeryconfig
623+
security_monkey.celeryconfig.security_monkey_watcher_ignore = {"policy"}
624624

625625
from security_monkey.task_scheduler.beat import setup_the_tasks
626626
from security_monkey.watchers.iam.iam_role import IAMRole

security_monkey/views/user_settings.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from flask import request
15+
from marshmallow.validate import OneOf
16+
from werkzeug.exceptions import BadRequest
1417

1518
from security_monkey.views import AuthenticatedService
1619
from security_monkey.views import USER_SETTINGS_FIELDS
1720
from security_monkey.datastore import Account
1821
from security_monkey.datastore import User
1922
from security_monkey import db, rbac
2023

21-
from flask_restful import marshal, reqparse
24+
from flask_restful import marshal, abort
2225
from flask_login import current_user
26+
from marshmallow import Schema, fields, ValidationError
27+
28+
29+
class SaveSettingsSchema(Schema):
30+
accounts = fields.List(fields.Integer())
31+
daily_audit_email = fields.Boolean(allow_none=True, required=True)
32+
change_report_setting = fields.String(allow_none=True, required=True, validate=OneOf(["ISSUES", "ALL", "NONE"]))
2333

2434

2535
class UserSettings(AuthenticatedService):
@@ -148,11 +158,14 @@ def post(self):
148158
:statuscode 200: no error
149159
:statuscode 401: Authentication Error. Please Login.
150160
"""
151-
152-
self.reqparse.add_argument('accounts', required=True, type=list, help='Must provide accounts', location='json')
153-
self.reqparse.add_argument('change_report_setting', required=True, type=str, help='Must provide change_report_setting', location='json')
154-
self.reqparse.add_argument('daily_audit_email', required=True, type=bool, help='Must provide daily_audit_email', location='json')
155-
args = self.reqparse.parse_args()
161+
json_data = request.get_json()
162+
if not json_data:
163+
raise BadRequest()
164+
165+
try:
166+
args = SaveSettingsSchema(strict=True).load(json_data).data
167+
except ValidationError as ve:
168+
abort(400, message=ve.message)
156169

157170
current_user.daily_audit_email = args['daily_audit_email']
158171
current_user.change_reports = args['change_report_setting']

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
'pyjwt>=1.01',
7979
'netaddr',
8080
'swag-client>=0.3.7',
81-
'idna==2.6'
81+
'idna==2.6',
82+
'marshmallow==2.15.0',
83+
'flask-marshmallow==0.8.0'
8284
],
8385
extras_require={
8486
'onelogin': ['python-saml>=2.4.0'],

0 commit comments

Comments
 (0)