
This patch addresses inconsistent code style and enforces it with a gate for future submissions. Separate work will be done in the future to address several of the PEP8 ignores for docstrings, and attempt to bring the tests directory to PEP8 compliance. This patch: 1. Updates .style.yapf to set the knobs desired for YAPF. 2. Updates tox.ini to allow one of the knobs to work. 3. Removes unused code from several __init__.py files. 4. Updates the YAPF version in test-requirements.txt to latest (this is needed for several knobs to work). 5. Stylistic changes to the python codebase in Pegleg. 6. Updates to tox.ini to run YAPF during PEP8 check. Change-Id: Ieaa0fdef2b601d01c875d64b840986e54df73abf
105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
|
|
#
|
|
# 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.
|
|
|
|
import random
|
|
import string
|
|
|
|
__all__ = ['CryptoString']
|
|
|
|
|
|
class CryptoString(object):
|
|
def __init__(self):
|
|
punctuation = '@#&-+=?'
|
|
self._pool = string.ascii_letters + string.digits + punctuation
|
|
self._random = random.SystemRandom()
|
|
|
|
def has_upper(self, crypto_str):
|
|
"""Check if string contains an uppercase letter
|
|
|
|
:param str crypto_str: The string to test.
|
|
:returns: True if string contains at least one uppercase letter.
|
|
:rtype: boolean
|
|
"""
|
|
|
|
return any(char in string.ascii_uppercase for char in crypto_str)
|
|
|
|
def has_lower(self, crypto_str):
|
|
"""Check if string contains a lowercase letter
|
|
|
|
:param str crypto_str: The string to test.
|
|
:returns: True if string contains at least one lowercase letter.
|
|
:rtype: boolean
|
|
"""
|
|
|
|
return any(char in string.ascii_lowercase for char in crypto_str)
|
|
|
|
def has_number(self, crypto_str):
|
|
"""Check if string contains a number
|
|
|
|
:param str crypto_str: The string to test.
|
|
:returns: True if string contains at least one number.
|
|
:rtype: boolean
|
|
"""
|
|
|
|
return any(char in string.digits for char in crypto_str)
|
|
|
|
def has_symbol(self, crypto_str):
|
|
"""Check if string contains a symbol
|
|
|
|
:param str crypto_str: The string to test.
|
|
:returns: True if string contains at least one symbol.
|
|
:rtype: boolean
|
|
"""
|
|
|
|
return any(char in string.punctuation for char in crypto_str)
|
|
|
|
def validate_crypto_str(self, crypto_str):
|
|
"""Ensure cryptostring contains characters from all sets
|
|
|
|
:param str crypto_str: The string to test.
|
|
:returns: True if string contains at least one each: uppercase letter,
|
|
lowercase letter, number and symbol
|
|
:rtype: boolean
|
|
"""
|
|
|
|
for test in [self.has_upper, self.has_lower, self.has_number,
|
|
self.has_symbol]:
|
|
if not test(crypto_str):
|
|
return False
|
|
|
|
return True
|
|
|
|
def get_crypto_string(self, length=24):
|
|
"""Create and return a random cryptographic string.
|
|
|
|
When the string is generated, it will be checked to determine if it
|
|
contains uppercase letters, lowercase letters, numbers and symbols.
|
|
If it does not contain at least one character from each set it will
|
|
be re-generated until it does.
|
|
|
|
:param int length: Length of crypto string to generate. If this length
|
|
is smaller than 24, or not defined, the length will default to 24.
|
|
:returns: The generated cryptographic string
|
|
:rtype: string
|
|
"""
|
|
|
|
while True:
|
|
crypto_str = ''.join(
|
|
self._random.choice(self._pool)
|
|
for _ in range(max(24, length)))
|
|
if self.validate_crypto_str(crypto_str):
|
|
break
|
|
|
|
return crypto_str
|