예제 #1
0
    def sync(self):
        """
        Goes through every user to identify those with registered devices, and syncs to KVstore. The list of
        old_registered_users must be fetched first to avoid a possible race condition.
        :return:
        """
        old_registered_users = get_all_mobile_users(self.session_key)
        all_splunk_users = get_all_users(self.session_key) + ['nobody']
        registered_users = [
            user for user in all_splunk_users
            if user_has_registered_devices(user, self.session_key)
        ]

        kvstore_users = KvStore(constants.REGISTERED_USERS_COLLECTION_NAME,
                                self.session_key)
        try:
            [
                kvstore_users.insert_single_item({u'_key': user})
                for user in registered_users
                if user not in old_registered_users
            ]
            [
                kvstore_users.delete_item_by_key(user)
                for user in old_registered_users
                if user not in registered_users
            ]
            LOGGER.info(
                "Completed Registered Users Sync in process PID={}".format(
                    os.getpid()))
        except:
            LOGGER.exception(
                "Exception performing RegisteredUsersSync for collection={}".
                format(constants.REGISTERED_USERS_COLLECTION_NAME))
예제 #2
0
def delete_all_devices_of_type(app_type, authtoken, system_authtoken):
    """
    Removes all devices of a given type from all users in the kvstore
    """
    users = get_all_users(authtoken) + [NOBODY]
    for user in users:
        delete_all_devices_of_type_for_user(user, app_type, authtoken, system_authtoken)
예제 #3
0
def clean_up_orphaned_configs(request_context, async_kvstore_client):
    """
    This function is to clean up configs that are tied to unregistered devices
    :param request_context: Request context used to make kvstore requests
    :param async_kvstore_client: Kvstore client used to make kvstore requests
    """
    users = get_all_users(request_context.system_auth_header.session_token)
    requests = []
    for user in users:
        drone_mode_tvs = yield get_drone_mode_tvs(request_context,
                                                  async_kvstore_client,
                                                  user=user)
        if drone_mode_tvs:
            registered_tv_list = yield get_registered_tvs(
                request_context.auth_header, user, async_kvstore_client)

            registered_device_ids = {
                device[constants.DEVICE_ID]
                for device in registered_tv_list
            }
            tv_config_ids_to_delete = [
                config[constants.KEY] for config in drone_mode_tvs
                if urlsafe_b64_to_b64(config[constants.KEY]) not in
                registered_device_ids
            ]
            if tv_config_ids_to_delete:
                LOGGER.debug(
                    'orphaned tv config ids to delete for user=%s ids=%s',
                    user, tv_config_ids_to_delete)
                query = build_containedin_clause(constants.KEY,
                                                 tv_config_ids_to_delete)
                params = {constants.QUERY: json.dumps(query)}
                request = async_kvstore_client.async_kvstore_delete_request(
                    collection=constants.DRONE_MODE_TVS_COLLECTION_NAME,
                    auth_header=request_context.system_auth_header,
                    owner=user,
                    params=params)
                requests.append(request)

    exceptions = []
    responses = yield defer.DeferredList(requests, consumeErrors=True)
    for response in responses:
        LOGGER.debug('response=%s', response)
        if isinstance(response[1], Failure):
            exceptions.append(response[1])
    if exceptions:
        LOGGER.error(
            'Encountered exceptions deleting drone mode tv configs: e=%s',
            exceptions)
예제 #4
0
def get_devices_for_registered_users(authtoken):
    """
    This function gets all devices from the kvstore owned by all users (or the users viewable
    using the supplied authorization token). This function:
        1. Generates a list of Splunk users
        2. Retrieves lists of all devices from each user's devices kvstore collection and concatenates them together

    :param authtoken: Authorization token to supply to the kvstore interface
    :return: List of devices
    """
    users = [
        user for user in get_all_mobile_users(authtoken)
        if user in get_all_users(authtoken)
    ] + ['nobody']

    devices = reduce(
        lambda acc, user: acc + get_devices_for_user(user, authtoken), users,
        [])
    return devices