示例#1
0
文件: tokens.py 项目: r3boot/pki
    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)
示例#2
0
文件: tokens.py 项目: r3boot/pki
    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]
示例#3
0
文件: tokens.py 项目: r3boot/pki
    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
示例#4
0
文件: tokens.py 项目: r3boot/pki
    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
示例#5
0
文件: checks.py 项目: r3boot/pki
 def test_fqdn_3component(self):
     assert checks.valid_fqdn('some.host.name') is True
示例#6
0
文件: checks.py 项目: r3boot/pki
 def test_fqdn_1component(self):
     assert checks.valid_fqdn('some') is True
示例#7
0
文件: checks.py 项目: r3boot/pki
 def test_tld_single_dash(self):
     assert checks.valid_fqdn('some.host.-') is False
示例#8
0
文件: checks.py 项目: r3boot/pki
 def test_tld_end_dash(self):
     assert checks.valid_fqdn('some.host.tld-') is False
示例#9
0
文件: checks.py 项目: r3boot/pki
 def test_tld_start_dash(self):
     assert checks.valid_fqdn('some.host.-tld') is False
示例#10
0
文件: checks.py 项目: r3boot/pki
 def test_fqdn_has_underscore(self):
     assert checks.valid_fqdn('some_host.domain') is False
示例#11
0
文件: checks.py 项目: r3boot/pki
 def test_empty_fqdn(self):
     assert checks.valid_fqdn('') == False
示例#12
0
文件: checks.py 项目: r3boot/pki
 def test_undefined_fqdn(self):
     assert checks.valid_fqdn(None) == False
示例#13
0
文件: checks.py 项目: r3boot/pki
 def test_integer_fqdn(self):
     assert checks.valid_fqdn(123456789) == False