2014-05-13 16:17:26 +00:00
|
|
|
import string
|
2016-03-18 19:09:25 +00:00
|
|
|
import re
|
2016-07-18 22:20:00 +00:00
|
|
|
import json
|
2014-05-13 16:17:26 +00:00
|
|
|
|
2016-03-18 19:48:41 +00:00
|
|
|
import anunidecode # Don't listen to pylint's lies. This import is required.
|
|
|
|
|
2019-03-07 19:15:38 +00:00
|
|
|
from peewee import OperationalError
|
2013-09-27 22:16:26 +00:00
|
|
|
|
2013-10-10 17:44:34 +00:00
|
|
|
INVALID_PASSWORD_MESSAGE = 'Invalid password, password must be at least ' + \
|
|
|
|
'8 characters and contain no whitespace.'
|
2015-10-14 18:41:39 +00:00
|
|
|
VALID_CHARACTERS = string.digits + string.lowercase
|
|
|
|
|
2017-01-18 22:42:27 +00:00
|
|
|
MIN_USERNAME_LENGTH = 2
|
|
|
|
MAX_USERNAME_LENGTH = 255
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
VALID_LABEL_KEY_REGEX = r'^[a-z0-9](([a-z0-9]|[-.](?![.-]))*[a-z0-9])?$'
|
2016-09-20 19:21:26 +00:00
|
|
|
VALID_USERNAME_REGEX = r'^([a-z0-9]+(?:[._-][a-z0-9]+)*)$'
|
|
|
|
|
|
|
|
INVALID_USERNAME_CHARACTERS = r'[^a-z0-9_]'
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_label_key(label_key):
|
|
|
|
if len(label_key) > 255:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return bool(re.match(VALID_LABEL_KEY_REGEX, label_key))
|
2014-05-13 16:17:26 +00:00
|
|
|
|
2013-09-27 22:16:26 +00:00
|
|
|
|
|
|
|
def validate_email(email_address):
|
2016-09-08 16:24:47 +00:00
|
|
|
if not email_address:
|
|
|
|
return False
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
return bool(re.match(r'[^@]+@[^@]+\.[^@]+', email_address))
|
2013-09-27 22:38:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_username(username):
|
2014-04-08 00:37:02 +00:00
|
|
|
# Based off the restrictions defined in the Docker Registry API spec
|
2016-09-20 19:21:26 +00:00
|
|
|
if not re.match(VALID_USERNAME_REGEX, username):
|
2017-01-17 22:31:24 +00:00
|
|
|
return (False, 'Namespace must match expression ' + VALID_USERNAME_REGEX)
|
2014-04-08 00:37:02 +00:00
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
length_match = (len(username) >= MIN_USERNAME_LENGTH and len(username) <= MAX_USERNAME_LENGTH)
|
2014-04-08 00:37:02 +00:00
|
|
|
if not length_match:
|
2017-01-17 22:31:24 +00:00
|
|
|
return (False, 'Namespace must be between %s and %s characters in length' %
|
2016-07-18 22:20:00 +00:00
|
|
|
(MIN_USERNAME_LENGTH, MAX_USERNAME_LENGTH))
|
2014-04-08 00:37:02 +00:00
|
|
|
|
|
|
|
return (True, '')
|
2013-09-27 22:38:41 +00:00
|
|
|
|
2013-09-28 00:03:07 +00:00
|
|
|
|
2013-09-27 22:38:41 +00:00
|
|
|
def validate_password(password):
|
|
|
|
# No whitespace and minimum length of 8
|
|
|
|
if re.search(r'\s', password):
|
|
|
|
return False
|
|
|
|
return len(password) > 7
|
2014-05-13 16:17:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _gen_filler_chars(num_filler_chars):
|
|
|
|
if num_filler_chars == 0:
|
|
|
|
yield ''
|
|
|
|
else:
|
|
|
|
for char in VALID_CHARACTERS:
|
|
|
|
for suffix in _gen_filler_chars(num_filler_chars - 1):
|
|
|
|
yield char + suffix
|
|
|
|
|
|
|
|
|
|
|
|
def generate_valid_usernames(input_username):
|
2014-08-01 19:50:25 +00:00
|
|
|
normalized = input_username.encode('unidecode', 'ignore').strip().lower()
|
2017-01-18 22:42:27 +00:00
|
|
|
prefix = re.sub(INVALID_USERNAME_CHARACTERS, '_', normalized)[:MAX_USERNAME_LENGTH]
|
2015-10-14 18:41:39 +00:00
|
|
|
prefix = re.sub(r'_{2,}', '_', prefix)
|
|
|
|
|
|
|
|
if prefix.endswith('_'):
|
|
|
|
prefix = prefix[0:len(prefix) - 1]
|
2014-05-13 16:17:26 +00:00
|
|
|
|
2016-09-20 19:21:26 +00:00
|
|
|
while prefix.startswith('_'):
|
|
|
|
prefix = prefix[1:]
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
num_filler_chars = max(0, MIN_USERNAME_LENGTH - len(prefix))
|
2014-05-13 16:17:26 +00:00
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
while num_filler_chars + len(prefix) <= MAX_USERNAME_LENGTH:
|
2014-05-13 16:17:26 +00:00
|
|
|
for suffix in _gen_filler_chars(num_filler_chars):
|
|
|
|
yield prefix + suffix
|
|
|
|
num_filler_chars += 1
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_json(value):
|
|
|
|
if ((value.startswith('{') and value.endswith('}')) or
|
|
|
|
(value.startswith('[') and value.endswith(']'))):
|
|
|
|
try:
|
|
|
|
json.loads(value)
|
|
|
|
return True
|
2017-11-14 18:40:11 +00:00
|
|
|
except (TypeError, ValueError):
|
2016-07-18 22:20:00 +00:00
|
|
|
return False
|
2017-11-14 18:40:11 +00:00
|
|
|
return False
|
2019-03-07 19:15:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_postgres_precondition(driver):
|
|
|
|
cursor = driver.execute_sql("SELECT extname FROM pg_extension", ("public",))
|
|
|
|
if 'pg_trgm' not in [extname for extname, in cursor.fetchall()]:
|
|
|
|
raise OperationalError("""
|
|
|
|
"pg_trgm" extension does not exists in the database.
|
|
|
|
Please run `CREATE EXTENSION IF NOT EXISTS pg_trgm;` as superuser on this database.
|
|
|
|
""")
|