Ejemplo n.º 1
0
    def set_key(self, path, key_data, force=False):
        """Add a key to the store or update an existing one.

        :param str path: The key to write.

        :param str key_data: The data of the key.

        :param bool foce: (optional) If ``True`` path will be
            overwritten if it exists.

        :raises FileExistsError: if a key already exists for path and
            overwrite is ``False``.

        """
        if path is None or path == '':
            return
        path = os.path.normpath(path)

        key_path = os.path.join(self.store_dir, path + '.gpg')
        key_dir = os.path.dirname(key_path)
        if os.path.exists(key_path) and not force:
            raise FileExistsError(
                'An entry already exists for {0}.'.format(path))

        os.makedirs(os.path.join(self.store_dir, key_dir), exist_ok=True)
        write_key(key_path, key_data, self.gpg_bin, self.gpg_opts)

        git_add_path(self.repo,
                     key_path,
                     'Add given password for {0} to store.'.format(path),
                     verbose=self.verbose)
Ejemplo n.º 2
0
    def set_key(self, path, key_data, force=False):
        """Add a key to the store or update an existing one.

        :param str path: The key to write.

        :param str key_data: The data of the key.

        :param bool foce: (optional) If ``True`` path will be
            overwritten if it exists.

        :raises FileExistsError: if a key already exists for path and
            overwrite is ``False``.

        """
        if path is None or path == '':
            return
        path = os.path.normpath(path)

        key_path = os.path.join(self.store_dir, path + '.gpg')
        key_dir = os.path.dirname(key_path)
        if os.path.exists(key_path) and not force:
            raise FileExistsError('An entry already exists for {0}.'
                                  .format(path))

        os.makedirs(os.path.join(self.store_dir, key_dir), exist_ok=True)
        write_key(key_path, key_data, self.gpg_bin, self.gpg_opts)

        git_add_path(self.repo, key_path,
                     'Add given password for {0} to store.'.format(path),
                     verbose=self.verbose)
Ejemplo n.º 3
0
    def gen_key(self, path, length, symbols=True, force=False, inplace=False):
        """Generate a new password for a key.

        :param str path: The path of the key.

        :param int length: The length of the new password.

        :param bool symbols: (optional) If ``True`` non alphanumeric
            characters will also be used in the new password.

        :param bool force: (optional) If ``True`` an existing key at
            `path` will be overwritten.

        :param bool inplace: (optional) If ``True`` only the first
            line of an existing key at `path` will be overwritten with
            the new password.

        """
        if path is None or path == '':
            return None
        path = os.path.normpath(path)
        key_path = os.path.join(self.store_dir, path + '.gpg')
        key_dir = os.path.dirname(key_path)
        if os.path.exists(key_path) and not (force or inplace):
            if self.interactive:
                answer = input('An entry already exists for {0}. '
                               'Overwrite it? [y/N] '.format(path))
                if answer.lower() != 'y':
                    return None
            else:
                raise FileExistsError(
                    'An entry already exists for {0}.'.format(path))

        os.makedirs(os.path.join(self.store_dir, key_dir), exist_ok=True)

        password = gen_password(length, symbols=symbols)
        action = 'Add'
        if not inplace:
            write_key(key_path, password, self.gpg_bin, self.gpg_opts)
            action = 'Add'
        else:
            action = 'Replace'
            key_data = read_key(key_path,
                                gpg_bin=self.gpg_bin,
                                gpg_opts=self.gpg_opts)
            lines = key_data.split('\n')
            lines[0] = password
            write_key(key_path,
                      '\n'.join(lines),
                      gpg_bin=self.gpg_bin,
                      gpg_opts=self.gpg_opts)

        git_add_path(self.repo,
                     key_path,
                     '{0} generated password for {1}.'.format(action, path),
                     verbose=self.verbose)
        return password
Ejemplo n.º 4
0
    def gen_key(self, path, length, symbols=True, force=False,
                inplace=False):
        """Generate a new password for a key.

        :param str path: The path of the key.

        :param int length: The length of the new password.

        :param bool symbols: (optional) If ``True`` non alphanumeric
            characters will also be used in the new password.

        :param bool force: (optional) If ``True`` an existing key at
            `path` will be overwritten.

        :param bool inplace: (optional) If ``True`` only the first
            line of an existing key at `path` will be overwritten with
            the new password.

        """
        if path is None or path == '':
            return None
        path = os.path.normpath(path)
        key_path = os.path.join(self.store_dir, path + '.gpg')
        key_dir = os.path.dirname(key_path)
        if os.path.exists(key_path) and not (force or inplace):
            if self.interactive:
                answer = input('An entry already exists for {0}. '
                               'Overwrite it? [y/N] '.format(path))
                if answer.lower() != 'y':
                    return None
            else:
                raise FileExistsError('An entry already exists for {0}.'
                                      .format(path))

        os.makedirs(os.path.join(self.store_dir, key_dir), exist_ok=True)

        password = gen_password(length, symbols=symbols)
        action = 'Add'
        if not inplace:
            write_key(key_path, password, self.gpg_bin, self.gpg_opts)
            action = 'Add'
        else:
            action = 'Replace'
            key_data = read_key(key_path, gpg_bin=self.gpg_bin,
                                gpg_opts=self.gpg_opts)
            lines = key_data.split('\n')
            lines[0] = password
            write_key(key_path, '\n'.join(lines), gpg_bin=self.gpg_bin,
                      gpg_opts=self.gpg_opts)

        git_add_path(self.repo, key_path,
                     '{0} generated password for {1}.'.format(action, path),
                     verbose=self.verbose)
        return password