'Create a new primary key and revoke excess active keys.
:param keystone_user_id: User ID of the Keystone user.
:param keystone_group_id: Group ID of the Keystone user.
Key rotation utilizes the following behaviors:
- The highest key number is used as the primary key (used for
encryption).
- All keys can be used for decryption.
- New keys are always created as key "0," which serves as a placeholder
before promoting it to be the primary key.
This strategy allows you to safely perform rotation on one node in a
cluster, before syncing the results of the rotation to all other nodes
(during both key rotation and synchronization, all nodes must recognize
all primary keys).'

def rotate_keys(self, keystone_user_id=None, keystone_group_id=None):

    key_files = dict()
    for filename in os.listdir(self.key_repository):
        path = os.path.join(self.key_repository, str(filename))
        if os.path.isfile(path):
            try:
                key_id = int(filename)
            except ValueError:
                pass
            else:
                key_files[key_id] = path
    LOG.info('Starting    key    rotation    with    %(count)s    key    files:    %(list)s', {'count': len(key_files), 'list': list(key_files.values())})
    self._create_tmp_new_key(keystone_user_id, keystone_group_id)
    current_primary_key = max(key_files.keys())
    LOG.info('Current    primary    key    is:    %s', current_primary_key)
    new_primary_key = (current_primary_key + 1)
    LOG.info('Next    primary    key    will    be:    %s', new_primary_key)
    os.rename(os.path.join(self.key_repository, '0'), os.path.join(self.key_repository, str(new_primary_key)))
    key_files.pop(0)
    key_files[new_primary_key] = os.path.join(self.key_repository, str(new_primary_key))
    LOG.info('Promoted    key    0    to    be    the    primary:    %s', new_primary_key)
    self._become_valid_new_key()
    max_active_keys = self.max_active_keys
    keys = sorted(key_files.keys(), reverse=True)
    while (len(keys) > (max_active_keys - 1)):
        index_to_purge = keys.pop()
        key_to_purge = key_files[index_to_purge]
        LOG.info('Excess    key    to    purge:    %s', key_to_purge)
        os.remove(key_to_purge)
