2016-12-08 01:24:59 +01:00
|
|
|
"""
|
|
|
|
.. module: lemur.sources.cli
|
|
|
|
:platform: Unix
|
2018-05-29 19:18:16 +02:00
|
|
|
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more
|
2016-12-08 01:24:59 +01:00
|
|
|
:license: Apache, see LICENSE for more details.
|
|
|
|
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
from tabulate import tabulate
|
|
|
|
|
|
|
|
from flask_script import Manager
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
2018-01-03 00:26:31 +01:00
|
|
|
from lemur.constants import SUCCESS_METRIC_STATUS, FAILURE_METRIC_STATUS
|
|
|
|
|
2017-07-13 23:49:04 +02:00
|
|
|
from lemur.extensions import metrics, sentry
|
2016-12-27 19:31:33 +01:00
|
|
|
from lemur.plugins.base import plugins
|
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
from lemur.sources import service as source_service
|
|
|
|
from lemur.users import service as user_service
|
2016-12-27 19:31:33 +01:00
|
|
|
from lemur.certificates import service as certificate_service
|
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
|
|
|
|
manager = Manager(usage="Handles all source related tasks.")
|
|
|
|
|
|
|
|
|
|
|
|
def validate_sources(source_strings):
|
|
|
|
sources = []
|
|
|
|
if not source_strings:
|
|
|
|
table = []
|
|
|
|
for source in source_service.get_all():
|
|
|
|
table.append([source.label, source.active, source.description])
|
|
|
|
|
|
|
|
print("No source specified choose from below:")
|
2019-05-16 16:57:02 +02:00
|
|
|
print(tabulate(table, headers=["Label", "Active", "Description"]))
|
2016-12-08 01:24:59 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
if "all" in source_strings:
|
2016-12-08 01:24:59 +01:00
|
|
|
sources = source_service.get_all()
|
|
|
|
else:
|
|
|
|
for source_str in source_strings:
|
|
|
|
source = source_service.get_by_label(source_str)
|
|
|
|
|
|
|
|
if not source:
|
2019-05-16 16:57:02 +02:00
|
|
|
print(
|
|
|
|
"Unable to find specified source with label: {0}".format(source_str)
|
|
|
|
)
|
2016-12-08 01:24:59 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
sources.append(source)
|
|
|
|
return sources
|
|
|
|
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
@manager.option(
|
|
|
|
"-s",
|
|
|
|
"--sources",
|
|
|
|
dest="source_strings",
|
|
|
|
action="append",
|
|
|
|
help="Sources to operate on.",
|
|
|
|
)
|
2016-12-08 01:24:59 +01:00
|
|
|
def sync(source_strings):
|
2016-12-27 19:31:33 +01:00
|
|
|
sources = validate_sources(source_strings)
|
|
|
|
for source in sources:
|
2018-01-03 00:26:31 +01:00
|
|
|
status = FAILURE_METRIC_STATUS
|
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
start_time = time.time()
|
|
|
|
print("[+] Staring to sync source: {label}!\n".format(label=source.label))
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
user = user_service.get_by_username("lemur")
|
2016-12-08 01:24:59 +01:00
|
|
|
|
|
|
|
try:
|
2016-12-15 19:26:59 +01:00
|
|
|
data = source_service.sync(source, user)
|
|
|
|
print(
|
|
|
|
"[+] Certificates: New: {new} Updated: {updated}".format(
|
2019-05-16 16:57:02 +02:00
|
|
|
new=data["certificates"][0], updated=data["certificates"][1]
|
2016-12-15 19:26:59 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"[+] Endpoints: New: {new} Updated: {updated}".format(
|
2019-05-16 16:57:02 +02:00
|
|
|
new=data["endpoints"][0], updated=data["endpoints"][1]
|
2016-12-15 19:26:59 +01:00
|
|
|
)
|
|
|
|
)
|
2016-12-08 01:24:59 +01:00
|
|
|
print(
|
|
|
|
"[+] Finished syncing source: {label}. Run Time: {time}".format(
|
2019-05-16 16:57:02 +02:00
|
|
|
label=source.label, time=(time.time() - start_time)
|
2016-12-08 01:24:59 +01:00
|
|
|
)
|
|
|
|
)
|
2018-01-03 00:26:31 +01:00
|
|
|
status = SUCCESS_METRIC_STATUS
|
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
print("[X] Failed syncing source {label}!\n".format(label=source.label))
|
2016-12-08 01:24:59 +01:00
|
|
|
|
2017-07-13 23:49:04 +02:00
|
|
|
sentry.captureException()
|
2019-05-16 16:57:02 +02:00
|
|
|
metrics.send(
|
|
|
|
"source_sync_fail",
|
|
|
|
"counter",
|
|
|
|
1,
|
|
|
|
metric_tags={"source": source.label, "status": status},
|
|
|
|
)
|
2016-12-08 01:24:59 +01:00
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
metrics.send(
|
|
|
|
"source_sync",
|
|
|
|
"counter",
|
|
|
|
1,
|
|
|
|
metric_tags={"source": source.label, "status": status},
|
|
|
|
)
|
2018-01-03 00:26:31 +01:00
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
@manager.option(
|
|
|
|
"-s",
|
|
|
|
"--sources",
|
|
|
|
dest="source_strings",
|
|
|
|
action="append",
|
|
|
|
help="Sources to operate on.",
|
|
|
|
)
|
|
|
|
@manager.option(
|
|
|
|
"-c",
|
|
|
|
"--commit",
|
|
|
|
dest="commit",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Persist changes.",
|
|
|
|
)
|
2016-12-27 19:31:33 +01:00
|
|
|
def clean(source_strings, commit):
|
|
|
|
sources = validate_sources(source_strings)
|
|
|
|
for source in sources:
|
|
|
|
s = plugins.get(source.plugin_name)
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
if not hasattr(s, "clean"):
|
|
|
|
print(
|
|
|
|
"Cannot clean source: {0}, source plugin does not implement 'clean()'".format(
|
|
|
|
source.label
|
|
|
|
)
|
|
|
|
)
|
2016-12-27 19:31:33 +01:00
|
|
|
continue
|
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
start_time = time.time()
|
2016-12-27 19:31:33 +01:00
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
print("[+] Staring to clean source: {label}!\n".format(label=source.label))
|
2016-12-27 19:31:33 +01:00
|
|
|
|
|
|
|
cleaned = 0
|
|
|
|
for certificate in certificate_service.get_all_pending_cleaning(source):
|
2018-01-03 00:26:31 +01:00
|
|
|
status = FAILURE_METRIC_STATUS
|
|
|
|
if commit:
|
|
|
|
try:
|
|
|
|
s.clean(certificate, source.options)
|
|
|
|
certificate.sources.remove(source)
|
|
|
|
certificate_service.database.update(certificate)
|
|
|
|
status = SUCCESS_METRIC_STATUS
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
sentry.captureException()
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
metrics.send(
|
|
|
|
"clean",
|
|
|
|
"counter",
|
|
|
|
1,
|
|
|
|
metric_tags={"source": source.label, "status": status},
|
|
|
|
)
|
2018-01-03 00:26:31 +01:00
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
current_app.logger.warning(
|
|
|
|
"Removed {0} from source {1} during cleaning".format(
|
|
|
|
certificate.name, source.label
|
|
|
|
)
|
|
|
|
)
|
2018-01-03 00:26:31 +01:00
|
|
|
|
|
|
|
cleaned += 1
|
2016-12-27 19:31:33 +01:00
|
|
|
|
2016-12-08 01:24:59 +01:00
|
|
|
print(
|
2016-12-27 19:31:33 +01:00
|
|
|
"[+] Finished cleaning source: {label}. Removed {cleaned} certificates from source. Run Time: {time}\n".format(
|
2019-05-16 16:57:02 +02:00
|
|
|
label=source.label, time=(time.time() - start_time), cleaned=cleaned
|
2016-12-08 01:24:59 +01:00
|
|
|
)
|
|
|
|
)
|