def calculate(self): kvstore_client = KVStoreCollectionAccessObject( MOBILE_ALERTS_COLLECTION_NAME, self.session_token, owner=NOBODY) r, jsn = kvstore_client.get_collection_keys() collection_keys = json.loads(jsn) collection_size = len(collection_keys) return {self.METRIC_NAME: collection_size}
def get_all_mobile_users(authtoken): """ Returns a list of all Splunk users with registered mobile devices :param authtoken: Authorization token :return: List of users """ kvstore = KvStore(constants.REGISTERED_USERS_COLLECTION_NAME, authtoken) _, content = kvstore.get_collection_keys() registered_user_records = json.loads(content) return [ registered_user_record[u'_key'] for registered_user_record in registered_user_records ]
def run(self): """ Attempts to sync the migration. If the kvstore is not yet available, schedules a non-blocking retry attempt in 5 seconds """ LOGGER.info("Attempting Migration from Splunk Cloud Gateway to Splunk Secure Gateway") try: meta_collection = KvStore(META_COLLECTION_NAME, self.session_key, owner=NOBODY) migration_info = {KEY: MIGRATION_DONE, STATUS: '0'} meta_keys = meta_collection.get_collection_keys() if MIGRATION_DONE in meta_keys: migration_status = meta_collection.get_item_by_key(MIGRATION_DONE) else: migration_status = '0' if migration_status.isdigit() and int(migration_status): LOGGER.debug("Migration modular input will not run because migration from Splunk Cloud Gateway " "is already done") else: # List of all collections _, collections_content = get_all_collections(self.session_key, app_name=constants.CLOUDGATEWAY_APP_NAME) all_collections = json.loads(collections_content)['entry'] app_collections = [x for x in all_collections if x['acl']['app'] == CLOUDGATEWAY_APP_NAME] app_collection_names = {x['name'] for x in app_collections if x['name'] not in collections_not_to_migrate} # Special case this collection because it doesn't show up in the SCG /collections/config endpoint app_collection_names.add("user_meta") registered_users_kvstore = KvStore(constants.REGISTERED_USERS_COLLECTION_NAME, self.session_key, app=constants.CLOUDGATEWAY_APP_NAME, owner=NOBODY) _, users_content = registered_users_kvstore.get_collection_keys() registered_user_records = json.loads(users_content) all_registered_users = [registered_user_record[u'_key'] for registered_user_record in registered_user_records] if NOBODY not in all_registered_users: all_registered_users.append(NOBODY) for collection in app_collection_names: # Iterate through all users and insert data per user for user in all_registered_users: cloud_gateway_kvstore = KvStore(collection, self.session_key, app=constants.CLOUDGATEWAY_APP_NAME, owner=user) _, scg_content = cloud_gateway_kvstore.get_all_items() scg_content = json.loads(scg_content) if scg_content: if collection == constants.REGISTERED_DEVICES_COLLECTION_NAME: scg_content = [resolve_device_platform_and_type(device) for device in scg_content] secure_gateway_kvstore = KvStore(collection, self.session_key, app=constants.SPACEBRIDGE_APP_NAME, owner=user) _, ssg_content = secure_gateway_kvstore.insert_multiple_items(scg_content) # Copying passwords.conf to Splunk Secure Gateway for key in password_keys: try: value = fetch_sensitive_data(self.session_key, key, app=CLOUDGATEWAY_APP_NAME) update_or_create_sensitive_data(self.session_key, key, value) except splunk.ResourceNotFound: LOGGER.debug('key=%s not found in storage/passwords', key) migration_info[STATUS] = '1' meta_collection.insert_or_update_item_containing_key(migration_info) except splunk.RESTException as e: if e.statusCode == HTTPStatus.SERVICE_UNAVAILABLE: LOGGER.info("KVStore is not yet setup. Retrying migration in 5 seconds") time.sleep(TIMEOUT_SECONDS) self.run() else: raise e