2015-06-22 22:47:27 +02:00
|
|
|
"""
|
|
|
|
.. module: lemur.common.utils
|
|
|
|
:platform: Unix
|
|
|
|
:copyright: (c) 2015 by Netflix Inc., see AUTHORS for more
|
|
|
|
:license: Apache, see LICENSE for more details.
|
|
|
|
|
|
|
|
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
|
|
|
|
"""
|
2015-07-23 17:52:30 +02:00
|
|
|
import string
|
|
|
|
import random
|
2015-06-22 22:47:27 +02:00
|
|
|
|
2016-07-28 22:08:24 +02:00
|
|
|
from cryptography import x509
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
2015-06-22 22:47:27 +02:00
|
|
|
|
2016-11-23 06:11:20 +01:00
|
|
|
from flask_restful.reqparse import RequestParser
|
2016-07-28 22:08:24 +02:00
|
|
|
|
|
|
|
paginated_parser = RequestParser()
|
|
|
|
|
|
|
|
paginated_parser.add_argument('count', type=int, default=10, location='args')
|
|
|
|
paginated_parser.add_argument('page', type=int, default=1, location='args')
|
|
|
|
paginated_parser.add_argument('sortDir', type=str, dest='sort_dir', location='args')
|
|
|
|
paginated_parser.add_argument('sortBy', type=str, dest='sort_by', location='args')
|
|
|
|
paginated_parser.add_argument('filter', type=str, location='args')
|
2015-06-22 22:47:27 +02:00
|
|
|
|
|
|
|
|
2015-07-23 17:52:30 +02:00
|
|
|
def get_psuedo_random_string():
|
|
|
|
"""
|
|
|
|
Create a random and strongish challenge.
|
|
|
|
"""
|
|
|
|
challenge = ''.join(random.choice(string.ascii_uppercase) for x in range(6)) # noqa
|
|
|
|
challenge += ''.join(random.choice("~!@#$%^&*()_+") for x in range(6)) # noqa
|
|
|
|
challenge += ''.join(random.choice(string.ascii_lowercase) for x in range(6))
|
|
|
|
challenge += ''.join(random.choice(string.digits) for x in range(6)) # noqa
|
|
|
|
return challenge
|
|
|
|
|
|
|
|
|
2016-07-28 22:08:24 +02:00
|
|
|
def parse_certificate(body):
|
2016-11-21 23:03:04 +01:00
|
|
|
if isinstance(body, str):
|
2016-11-07 23:33:07 +01:00
|
|
|
body = body.encode('utf-8')
|
|
|
|
|
|
|
|
return x509.load_pem_x509_certificate(body, default_backend())
|
2016-10-15 09:04:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_weekend(date):
|
|
|
|
"""
|
|
|
|
Determines if a given date is on a weekend.
|
|
|
|
|
|
|
|
:param date:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if date.weekday() > 5:
|
|
|
|
return True
|
2016-11-28 19:50:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def validate_conf(app, required_vars):
|
|
|
|
"""
|
|
|
|
Ensures that the given fields are set in the applications conf.
|
|
|
|
|
|
|
|
:param app:
|
|
|
|
:param required_vars: list
|
|
|
|
"""
|
|
|
|
for var in required_vars:
|
|
|
|
if not app.config.get(var):
|
|
|
|
raise Exception("Required variable {var} is not set, ensure that it is set in Lemur's configuration file".format(
|
|
|
|
var=var
|
|
|
|
))
|