def validate(self, fqdn, token): """Check if fqdn is defined in the in-memory token store and if the stored token matches the supplied token. It will return True if the fqdn has a token in the store, and this matches the supplied token. It will return False if the fqdn or token is invalid. :param fqdn: Fully-Qualified Domain-Name of the host :type fqdn: str :param token: Token to validate :type token: str :returns: True if the fqdn/token pair is found, False if not :rtype: bool """ regexp = re.compile('[0-9a-f]{64,64}') if not checks.valid_fqdn(fqdn): return False if token is None: log.warning('token cannot be None') return False if not isinstance(token, str): log.warning('token needs to be a string') return False if regexp.search(token) is None: log.warning('token needs to be a valid hex64 string') return False return token == self.get(fqdn)
def get(self, fqdn): """Helper function to lookup an fqdn in the in-memory database. It will return the token if it is found, or False if the fqn is invalid or there is no token for fqdn :param fqdn: Fully-Qualified Domain-Name of host to lookup :type fqdn: str :returns: Token for fqdn, or False if an error occurred :rtype: str, bool """ if not checks.valid_fqdn(fqdn): log.debug('invalid fqdn') return False if fqdn not in self._store: log.debug('fqdn not defined') return False return self._store[fqdn]
def new(self, fqdn): """Generates a new token for a fqdn if it does not yet exist and return it. This function will return False if the fqdn is invalid or if the token already exists :param fqdn: Fully-Qualified Domain-Name for the host :type fqdn: str :returns: Token for the new host, or False if an error occurred :rtype: str, bool """ if not checks.valid_fqdn(fqdn): return False if self.get(fqdn): log.warning('Token for {0} already exists'.format(fqdn)) return False token = utils.gentoken() self._store[fqdn] = token self.save() return token
def validate_store(data=None): """This function will validate if the list specified in data represents a correct tokenstore list. The format of this list is as follows:: {'<fqdn>': '<token>'} Use it in the following manner: >>> store_data = {'some.host.name': '<hex64 string>'} >>> validate_store(store_data) True This function will return True if the data matches the above format, and False if it does not. :param data: List containing the token store :rtype data: list :returns: True if data is a valid token store, else False :rtype: bool """ if data is None: log.warning('data cannot be None') return False if not isinstance(data, dict): log.warning('data needs to be a dictionary') return False regexp = re.compile('[0-9a-f]{64,64}') for fqdn, token in data.items(): if not checks.valid_fqdn(fqdn): return False if token is None or not isinstance(token, str): log.warning('Token needs to be a string') return False if regexp.search(token) is None: log.warning('Invalid token supplied') return False return True
def test_fqdn_3component(self): assert checks.valid_fqdn('some.host.name') is True
def test_fqdn_1component(self): assert checks.valid_fqdn('some') is True
def test_tld_single_dash(self): assert checks.valid_fqdn('some.host.-') is False
def test_tld_end_dash(self): assert checks.valid_fqdn('some.host.tld-') is False
def test_tld_start_dash(self): assert checks.valid_fqdn('some.host.-tld') is False
def test_fqdn_has_underscore(self): assert checks.valid_fqdn('some_host.domain') is False
def test_empty_fqdn(self): assert checks.valid_fqdn('') == False
def test_undefined_fqdn(self): assert checks.valid_fqdn(None) == False
def test_integer_fqdn(self): assert checks.valid_fqdn(123456789) == False