forked from Netflix/security_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
107 lines (82 loc) · 3.45 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright 2014 Netflix, 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 flask.ext.script import Manager, Command, Option
from security_monkey import app, db
from security_monkey.common.route53 import Route53Service
from gunicorn.app.base import Application
from flask.ext.migrate import Migrate, MigrateCommand
from security_monkey.scheduler import run_change_reporter as sm_run_change_reporter
from security_monkey.scheduler import find_changes as sm_find_changes
from security_monkey.scheduler import audit_changes as sm_audit_changes
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.command
def drop_db():
""" Drops the database. """
db.drop_all()
@manager.option('-a', '--accounts', dest='accounts', type=unicode, default=u'all')
def run_change_reporter(accounts):
""" Runs Reporter """
sm_run_change_reporter(accounts)
@manager.option('-a', '--accounts', dest='accounts', type=unicode, default=u'all')
@manager.option('-m', '--monitors', dest='monitors', type=unicode, default=u'all')
def find_changes(accounts, monitors):
"""Runs watchers"""
sm_find_changes(accounts, monitors)
@manager.option('-a', '--accounts', dest='accounts', type=unicode, default=u'all')
@manager.option('-m', '--monitors', dest='monitors', type=unicode, default=u'all')
@manager.option('-r', '--send_report', dest='send_report', type=bool, default=False)
def audit_changes(accounts, monitors, send_report):
""" Runs auditors """
sm_audit_changes(accounts, monitors, send_report)
@manager.command
def start_scheduler():
""" starts the python scheduler to run the watchers and auditors"""
from security_monkey import scheduler
scheduler.setup_scheduler()
scheduler.scheduler.start()
class APIServer(Command):
def __init__(self, host='127.0.0.1', port=app.config.get('API_PORT'), workers=6):
self.address = "{}:{}".format(host, port)
self.workers = workers
def get_options(self):
return (
Option('-b', '--bind',
dest='address',
type=str,
default=self.address),
Option('-w', '--workers',
dest='workers',
type=int,
default=self.workers),
)
def handle(self, app, *args, **kwargs):
if app.config.get('USE_ROUTE53'):
route53 = Route53Service()
route53.register(app.config.get('FQDN'), exclusive=True)
workers = kwargs['workers']
address = kwargs['address']
class FlaskApplication(Application):
def init(self, parser, opts, args):
return {
'bind': address,
'workers': workers
}
def load(self):
return app
FlaskApplication().run()
if __name__ == "__main__":
manager.add_command("run_api_server", APIServer())
manager.run()