Esempio n. 1
0
    def generatekey(self):
        public_key = StringIO()
        private_key = StringIO()
        public_key_value = None
        private_key_value = None
        try:
            if self.keytype == 'rsa':
                key = RSAKey.generate(self.rsabits)
            elif self.keytype == 'ecdsa':
                key = ECDSAKey.generate(bits=self.ecdsabits)
            elif self.keytype == 'dss':
                key = DSSKey.generate(bits=self.dssbits)
            else:
                return None, "sshkey暂时不支持其它类型 %s" % self.keytype
            key.write_private_key(private_key)
            public_key.write("%s %s %s" %
                             (key.get_name(), key.get_base64(), self.basename))
            public_key_value = public_key.getvalue()
            private_key_value = private_key.getvalue()
            cache.set("user_%s_private_key" % self.username, private_key_value)
            cache.set("user_%s_public_key" % self.username, public_key_value)

        except Exception as e:
            logger.error(e.args)
            return None, e.args
        finally:
            public_key.close()
            private_key.close()
        return {
            "publickey": public_key_value,
            "privatekey": private_key_value
        }, None
Esempio n. 2
0
    def _get_key_name_pattern(self, identity: Identity) -> Pattern[str]:
        """Make the regex pattern from the format string.  Put two different
        random keys, compare two outputs, and then replace the difference
        with wildcard.

        """
        cls = type(self)
        sample_keys = cls._sample_keys
        if sample_keys is None:
            sample_keys = (RSAKey.generate(bits=512),
                           RSAKey.generate(bits=512),
                           ECDSAKey.generate(bits=256),
                           ECDSAKey.generate(bits=256))
            cls._sample_keys = sample_keys
        sample_names = [self._get_key_name(identity, k) for k in sample_keys]
        if len(frozenset(sample_names)) < 2:
            return re.compile('^' + re.escape(sample_names[0]) + '$')
        prefix = os.path.commonprefix(sample_names)
        postfix = os.path.commonprefix([n[::-1] for n in sample_names])[::-1]
        return re.compile('^{}.+?{}$'.format(re.escape(prefix),
                                             re.escape(postfix)))
Esempio n. 3
0
def post_init_hook(cr, pool):
    if socket.getfqdn().endswith('odoo-community.org'):  # pragma: no cover
        # we need a different default listeing address on runbot
        pool['ir.config_parameter'].set_param(cr, SUPERUSER_ID,
                                              'document_sftp.bind',
                                              '%s:0' % socket.getfqdn())
    hostkey = pool['ir.config_parameter'].get_param(cr, SUPERUSER_ID,
                                                    'document_sftp.hostkey')
    parameters = etree.parse(
        tools.file_open('document_sftp/data/ir_config_parameter.xml'))
    default_value = None
    for node in parameters.xpath(
            "//record[@id='param_hostkey']//field[@name='value']"):
        default_value = node.text
    if not hostkey or hostkey == default_value:
        _logger.info('Generating host key for database %s', cr.dbname)
        key = StringIO.StringIO()
        ECDSAKey.generate().write_private_key(key)
        pool['ir.config_parameter'].set_param(cr, SUPERUSER_ID,
                                              'document_sftp.hostkey',
                                              key.getvalue())
        key.close()
Esempio n. 4
0
    def _get_key_name_pattern(self,
                              identity: Identity) -> Pattern[str]:
        """Make the regex pattern from the format string.  Put two different
        random keys, compare two outputs, and then replace the difference
        with wildcard.

        """
        cls = type(self)
        sample_keys = cls._sample_keys
        if sample_keys is None:
            sample_keys = (RSAKey.generate(bits=512),
                           RSAKey.generate(bits=512),
                           ECDSAKey.generate(bits=256),
                           ECDSAKey.generate(bits=256))
            cls._sample_keys = sample_keys
        sample_names = [self._get_key_name(identity, k) for k in sample_keys]
        if len(frozenset(sample_names)) < 2:
            return re.compile('^' + re.escape(sample_names[0]) + '$')
        prefix = os.path.commonprefix(sample_names)
        postfix = os.path.commonprefix([n[::-1] for n in sample_names])[::-1]
        return re.compile(
            '^{}.+?{}$'.format(re.escape(prefix), re.escape(postfix))
        )