def test_check_user_access(monkeypatch): def mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): if path_segment.endswith("current-context"): return common.make_response_record( '{"entry": [{"content": {"username": "******"}}]}' ) else: return common.make_response_record( '{"entry": [{"content": {"capabilities": ["can_read"]}}]}' ) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", mock_get) app_capabilities = { "object_type1": { "read": "read_app_object_type1", "write": "write_app_object_type1", "delete": "delete_app_object_type1", }, "object_type2": { "read": "read_app_object_type2", "write": "write_app_object_type2", "delete": "delete_app_object_type2", }, } with pytest.raises(user_access.UserAccessException): user_access.check_user_access( common.SESSION_KEY, app_capabilities, "object_type1", "read" )
def test_get_splunk_host_info(monkeypatch): common.mock_splunkhome(monkeypatch) common.mock_gethostname(monkeypatch) server_name, host_name = splunkenv.get_splunk_host_info() assert server_name == "unittestServer" assert host_name == "unittestServer"
def test_get_session_key(monkeypatch): def _mock_session_key_post(self, url, headers=None, **kwargs): return common.make_response_record( '{"sessionKey":"' + common.SESSION_KEY + '"}' ) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.HttpLib, "post", _mock_session_key_post) assert credentials.get_session_key("user", "password") == common.SESSION_KEY with pytest.raises(ValueError): credentials.get_session_key("user", "password", scheme="non-http") credentials.get_session_key("user", "password", scheme="http") credentials.get_session_key("user", "password", scheme="https") with pytest.raises(ValueError): credentials.get_session_key("user", "password", scheme="http", host="==") credentials.get_session_key("user", "password", scheme="http", host="localhost") with pytest.raises(ValueError): credentials.get_session_key( "user", "password", scheme="http", host="localhost", port=-10 ) credentials.get_session_key( "user", "password", scheme="http", host="localhost", port=10 ) credentials.get_session_key("user", "password", scheme="HTTP") credentials.get_session_key("user", "password", scheme="HTTPS")
def test_update(self, monkeypatch): common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", _mock_get) monkeypatch.setattr(binding.Context, "post", _mock_post) aclm = acl.ACLManager(common.SESSION_KEY, common.app) perms = aclm.update( "data/transforms/extractions/_acl", perms_read=["admin"], perms_write=["admin"], ) assert perms == json.loads(_new_acl1)["entry"][0]["acl"] perms = aclm.update("data/transforms/extractions/_acl", perms_read=["admin"]) assert perms == json.loads(_new_acl2)["entry"][0]["acl"] perms = aclm.update("data/transforms/extractions/_acl", perms_write=["admin"]) assert perms == json.loads(_new_acl3)["entry"][0]["acl"] perms = aclm.update("data/transforms/extractions/_acl") assert perms == json.loads(_old_acl)["entry"][0]["acl"] with pytest.raises(acl.ACLException): aclm.update("data/transforms/extractions", perms_write=["admin"])
def test_get(self, monkeypatch): common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", _mock_get) aclm = acl.ACLManager(common.SESSION_KEY, common.app) perms = aclm.get("data/transforms/extractions/_acl") assert perms == json.loads(_old_acl)["entry"][0]["acl"]
def test_get_splunkd_access_info(monkeypatch): common.mock_splunkhome(monkeypatch) scheme, host, port = splunkenv.get_splunkd_access_info() assert scheme == "https" assert host == "127.0.0.1" assert port == 8089
def test_get(self, monkeypatch): common.mock_splunkhome(monkeypatch) mr = metadata.MetadataReader(common.app) assert mr.get("sessions", "test", "modtime") == "1453272423.443622000" with pytest.raises(ValueError): mr.get("conf_bad", "stanza_bad", "modtime")
def test_get_float(self, monkeypatch): common.mock_splunkhome(monkeypatch) mr = metadata.MetadataReader("unittest") assert mr.get_float("sessions", "test", "modtime") == 1453272423.443622000 with pytest.raises(ValueError): mr.get_float("sessions", "test", "version")
def test_splunk_bin(monkeypatch): common.mock_splunkhome(monkeypatch) splunk_bin = splunkenv.get_splunk_bin() assert splunk_bin in ( os.environ["SPLUNK_HOME"] + "bin/splunk", os.environ["SPLUNK_HOME"] + "bin/splunk.exe", )
def test_splunkd_uri(monkeypatch): common.mock_splunkhome(monkeypatch) uri = splunkenv.get_splunkd_uri() assert uri == "https://127.0.0.1:8089" monkeypatch.setenv("SPLUNK_BINDIP", "10.0.0.2:7080") uri = splunkenv.get_splunkd_uri() assert uri == "https://10.0.0.2:8089" monkeypatch.setenv("SPLUNK_BINDIP", "10.0.0.3") uri = splunkenv.get_splunkd_uri() assert uri == "https://10.0.0.3:8089" monkeypatch.setenv("SPLUNKD_URI", "https://10.0.0.1:8089") uri = splunkenv.get_splunkd_uri() assert uri == "https://10.0.0.1:8089"
def test_get_current_username(monkeypatch): mode = 0 def mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): if mode == 0: return common.make_response_record( '{"entry": [{"content": {"username": "******"}}]}' ) else: raise binding.HTTPError(common.make_response_record("", status=401)) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", mock_get) assert user_access.get_current_username(common.SESSION_KEY) == "admin" mode = 1 with pytest.raises(user_access.InvalidSessionKeyException): user_access.get_current_username(common.SESSION_KEY)
def test_get_user_roles(monkeypatch): mode = 0 def mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): if mode == 0: return common.make_response_record( '{"entry": [{"content": {"roles": ["admin", "user"]}}]}' ) else: raise binding.HTTPError(common.make_response_record("", status=404)) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", mock_get) user_access.get_user_roles(common.SESSION_KEY, "admin") == ["admin", "user"] mode = 1 with pytest.raises(user_access.UserNotExistException): user_access.get_user_roles(common.SESSION_KEY, "admin")
def test_get_shc_members(self, monkeypatch): def _mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): return common.make_response_record( '{"entry": [{"name": "5B4A53C7-B824-4103-B8CC-C22E1EC6480F", "content": {"peer_scheme_host_port": "https://192.168.1.85:8089", "label": "SHC01_SearchHead02_1_85"}}, {"name": "D7E3BA03-85CE-449A-9736-38F2DA58236B", "content": {"peer_scheme_host_port": "https://192.168.1.86:8089", "label": "SHC01_SearchHead03_1_86"}}, {"name": "DA72938A-72C4-46F3-86BE-2E200EC56C76", "content": {"peer_scheme_host_port": "https://192.168.1.84:8089", "label": "SHC01_SearchHead01_1_84"}}]}' ) common.mock_splunkhome(monkeypatch) common.mock_serverinfo(monkeypatch) monkeypatch.setattr(binding.Context, "get", _mock_get) si = server_info.ServerInfo(common.SESSION_KEY) assert si.get_shc_members() == [ ("SHC01_SearchHead02_1_85", "https://192.168.1.85:8089"), ("SHC01_SearchHead03_1_86", "https://192.168.1.86:8089"), ("SHC01_SearchHead01_1_84", u"https://192.168.1.84:8089"), ]
def test_is_captain_ready(self, monkeypatch): def _mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): msg = ( '{"entry": [{"content": {"eai:acl": null,"elected_captain": 1463195590,' '"id": "9CA04BAD-0C24-4703-8A88-E20345833508","initialized_flag": true,' '"label": "ta-shc04-sh2","maintenance_mode": false,"mgmt_uri": "https://ta-shc04-sh2:8089",' '"min_peers_joined_flag": true,"peer_scheme_host_port": "https://ta-shc04-sh2:8089",' '"rolling_restart_flag": false,"service_ready_flag": true,"start_time": 1463195526}}]}' ) return common.make_response_record(msg) common.mock_splunkhome(monkeypatch) common.mock_serverinfo(monkeypatch) monkeypatch.setattr(binding.Context, "get", _mock_get) si = server_info.ServerInfo(common.SESSION_KEY) assert si.is_captain_ready()
def test_time_parser(monkeypatch): mode = 0 def mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): if mode == 0: return common.make_response_record( '{"2011-07-06T21:54:23.000-07:00": "1310014463.0"}') if mode == 1: return common.make_response_record( '{"2011-07-06T21:54:23.000-07:00": "2011-07-07T12:54:23.000+08:00"}' ) else: raise binding.HTTPError(common.make_response_record("", status=400)) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", mock_get) tp = stp.TimeParser(common.SESSION_KEY) assert tp.to_seconds("2011-07-06T21:54:23.000-07:00") == 1310014463.0 assert tp.to_utc("2011-07-06T21:54:23.000-07:00") == datetime.datetime( 2011, 7, 7, 4, 54, 23) mode = 1 assert (tp.to_local("2011-07-06T21:54:23.000-07:00") == "2011-07-07T12:54:23.000+08:00") mode = 2 with pytest.raises(stp.InvalidTimeFormatException): tp.to_seconds("2011-07-06T21:54:23.000-07;00") with pytest.raises(stp.InvalidTimeFormatException): tp.to_utc("2011-07-06T21:54:23.000-07;00") with pytest.raises(stp.InvalidTimeFormatException): tp.to_local("2011-07-06T21:54:23.000-07;00")
def test_version(self, monkeypatch): common.mock_splunkhome(monkeypatch) common.mock_serverinfo(monkeypatch) si = server_info.ServerInfo(common.SESSION_KEY) assert si.version == "6.3.1511.2"
def test_app_capability_manager(monkeypatch): APP_CAPABILITIES_COLLECTION_NAME = "app_capabilities_collection" app_capabilities = {} def mock_kvstore_collections_get( self, name="", owner=None, app=None, sharing=None, **query ): raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collections_create(self, name, indexes={}, fields={}, **kwargs): pass def mock_kvstore_collections_list(self, count=None, **kwargs): return [client.KVStoreCollection(None, None)] def mock_kvstore_collection_init(self, service, path, **kwargs): pass def mock_kvstore_collection_data_init(self, collection): pass def mock_kvstore_collection_data_batch_save(self, *documents): for document in documents: app_capabilities[document["_key"]] = document def mock_kvstore_collection_data_query_by_id(self, id): try: return app_capabilities[id] except: raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collection_data_delete_by_id(self, id): try: del app_capabilities[id] except: raise binding.HTTPError(None, status=404) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(client.KVStoreCollections, "get", mock_kvstore_collections_get) monkeypatch.setattr( client.KVStoreCollections, "create", mock_kvstore_collections_create ) monkeypatch.setattr( client.KVStoreCollections, "list", mock_kvstore_collections_list ) monkeypatch.setattr( client.KVStoreCollection, "__init__", mock_kvstore_collection_init ) monkeypatch.setattr( client.KVStoreCollection, "name", "Splunk_TA_test" + "_" + APP_CAPABILITIES_COLLECTION_NAME, ) monkeypatch.setattr( client.KVStoreCollectionData, "__init__", mock_kvstore_collection_data_init ) monkeypatch.setattr( client.KVStoreCollectionData, "batch_save", mock_kvstore_collection_data_batch_save, ) monkeypatch.setattr( client.KVStoreCollectionData, "query_by_id", mock_kvstore_collection_data_query_by_id, ) monkeypatch.setattr( client.KVStoreCollectionData, "delete_by_id", mock_kvstore_collection_data_delete_by_id, ) acm = user_access.AppCapabilityManager( APP_CAPABILITIES_COLLECTION_NAME, common.SESSION_KEY, "Splunk_TA_test" ) app_capabilities = { "object_type1": { "read": "read_app_object_type1", "write": "write_app_object_type1", "delete": "delete_app_object_type1", }, "object_type2": { "read": "read_app_object_type2", "write": "write_app_object_type2", "delete": "delete_app_object_type2", }, } with pytest.raises(user_access.AppCapabilityNotExistException): acm.get_capabilities() acm.register_capabilities(app_capabilities) assert acm.capabilities_are_registered() assert acm.get_capabilities() == app_capabilities acm.unregister_capabilities() assert not acm.capabilities_are_registered()
def test_splunkhome_path(monkeypatch): common.mock_splunkhome(monkeypatch) splunkhome_path = splunkenv.make_splunkhome_path(["etc", "apps"]) assert splunkhome_path == os.environ["SPLUNK_HOME"] + "etc/apps"
def test_conf_manager(monkeypatch): credentials_store = {} all_stanzas = {} def mock_storage_passwords_list(self, count=None, **kwargs): return credentials_store.values() def mock_storage_passwords_create(self, password, username, realm=None): title = "{}:{}:".format(realm, username) if realm else ":{}:".format(username) password = client.StoragePassword( None, "storage/passwords/{}".format(title), state=record({ "content": { "clear_password": password, "encr_password": hashlib.md5(password.encode()).digest(), "password": "******", "realm": realm, "username": username, }, "title": title, }), ) credentials_store[title] = password return password def mock_storage_passwords_delete(self, username, realm=None): title = "{}:{}:".format(realm, username) if realm else ":{}:".format(username) if title in credentials_store: del credentials_store[title] else: raise KeyError("No such entity %s" % username) def mock_storage_password_delete(self): if self.name in credentials_store: del credentials_store[self.name] else: raise KeyError("No such entity %s" % self.name) def mock_configuration_get(self, name="", owner=None, app=None, sharing=None, **query): return common.make_response_record("") def mock_configuration_file_list(self, count=None, **kwargs): if not hasattr(mock_configuration_file_list, "normal_mode"): mock_configuration_file_list.normal_mode = True raise binding.HTTPError(common.make_response_record("", status=404)) else: if "name" in kwargs: if kwargs["name"] in all_stanzas: stanza_mgr = client.Stanza( self.service, "configs/conf-test/{0}/".format(kwargs["name"]), skip_refresh=True, ) stanza_mgr._state = common.record({ "title": kwargs["name"], "access": common.record({"app": "test"}), "content": all_stanzas[kwargs["name"]], }) return [stanza_mgr] else: raise binding.HTTPError( common.make_response_record("", status=404)) else: stanza_mgrs = [] for stanza_name, stanza in list(all_stanzas.items()): stanza_mgr = client.Stanza( self.service, "configs/conf-test/{0}/".format(stanza_name), skip_refresh=True, ) stanza_mgr._state = common.record({ "title": stanza_name, "access": common.record({"app": "test"}), "content": stanza, }) stanza_mgrs.append(stanza_mgr) return stanza_mgrs def mock_configuration_file_get(self, name="", owner=None, app=None, sharing=None, **query): return common.make_response_record("") def mock_configuration_file_create(self, name, **params): stanza_mgr = client.Stanza(self.service, "configs/conf-test/{0}/".format(name), skip_refresh=True) stanza_mgr._state = common.record({"title": name, "content": {}}) return stanza_mgr def mock_configuration_file_delete(self, name, **params): del all_stanzas[name] def mock_stanza_submit(self, stanza): all_stanzas[self.name] = stanza common.mock_splunkhome(monkeypatch) monkeypatch.setattr(client.StoragePasswords, "list", mock_storage_passwords_list) monkeypatch.setattr(client.StoragePasswords, "create", mock_storage_passwords_create) monkeypatch.setattr(client.StoragePasswords, "delete", mock_storage_passwords_delete) monkeypatch.setattr(client.StoragePassword, "delete", mock_storage_password_delete) monkeypatch.setattr(client.Configurations, "get", mock_configuration_get) monkeypatch.setattr(client.ConfigurationFile, "get", mock_configuration_file_get) monkeypatch.setattr(client.ConfigurationFile, "list", mock_configuration_file_list) monkeypatch.setattr(client.ConfigurationFile, "create", mock_configuration_file_create) monkeypatch.setattr(client.ConfigurationFile, "delete", mock_configuration_file_delete) monkeypatch.setattr(client.Stanza, "submit", mock_stanza_submit) cfm = conf_manager.ConfManager(common.SESSION_KEY, common.app) conf = cfm.get_conf("test") assert not conf.stanza_exist("test_stanza") conf.update("test_stanza", {"k1": 1, "k2": 2}, ["k1", "key_not_exist"]) assert conf.get("test_stanza") == { "k2": 2, "k1": 1, "eai:access": common.record({"app": "test"}), "eai:appName": "test", } assert conf.get_all() == { "test_stanza": { "k2": 2, "k1": 1, "eai:access": common.record({"app": "test"}), "eai:appName": "test", } } conf.delete("test_stanza") with pytest.raises(conf_manager.ConfStanzaNotExistException): conf.get("test_stanza") with pytest.raises(conf_manager.ConfStanzaNotExistException): conf.delete("test_stanza") conf.reload() cfm = conf_manager.ConfManager( common.SESSION_KEY, common.app, realm="__REST_CREDENTIAL__#{}#configs/conf-test".format(common.app), ) conf = cfm.get_conf("test") assert not conf.stanza_exist("test_stanza") conf.update("test_stanza", {"k1": 1, "k2": 2}, ["k1", "key_not_exist"]) assert conf.get("test_stanza") == { "k2": 2, "k1": 1, "eai:access": common.record({"app": "test"}), "eai:appName": "test", } assert conf.get_all() == { "test_stanza": { "k2": 2, "k1": 1, "eai:access": common.record({"app": "test"}), "eai:appName": "test", } } conf.delete("test_stanza") with pytest.raises(conf_manager.ConfStanzaNotExistException): conf.get("test_stanza") with pytest.raises(conf_manager.ConfStanzaNotExistException): conf.delete("test_stanza") conf.reload()
def test_credential_manager(monkeypatch): credentials_store = {} def mock_storage_passwords_list(self, count=None, **kwargs): return list(credentials_store.values()) def mock_storage_passwords_create(self, password, username, realm=None): title = "{}:{}:".format(realm, username) if realm else ":{}:".format(username) password = client.StoragePassword( None, "storage/passwords/{}".format(title), state=record( { "content": { "clear_password": password, "encr_password": hashlib.md5(password.encode("utf-8")).digest(), "password": "******", "realm": realm, "username": username, }, "title": title, } ), ) credentials_store[title] = password return password def mock_storage_passwords_delete(self, username, realm=None): title = "{}:{}:".format(realm, username) if realm else ":{}:".format(username) if title in credentials_store: del credentials_store[title] else: raise KeyError("No such entity %s" % username) def mock_storage_password_delete(self): if self.name in credentials_store: del credentials_store[self.name] else: raise KeyError("No such entity %s" % self.name) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(client.StoragePasswords, "list", mock_storage_passwords_list) monkeypatch.setattr( client.StoragePasswords, "create", mock_storage_passwords_create ) monkeypatch.setattr( client.StoragePasswords, "delete", mock_storage_passwords_delete ) monkeypatch.setattr(client.StoragePassword, "delete", mock_storage_password_delete) cm = credentials.CredentialManager( common.SESSION_KEY, common.app, realm="realm_test" ) cm.set_password("testuser1", "password1") assert cm.get_password("testuser1") == "password1" long_password = "".join(["1111111111" for i in range(30)]) cm.set_password("testuser2", long_password) assert cm.get_password("testuser2") == long_password # change short password to long password long_password = "".join(["1111111111" for i in range(30)]) cm.set_password("testuser1", long_password) assert cm.get_password("testuser1") == long_password # change to longer password longer_password = "".join(["1111111111" for i in range(120)]) cm.set_password("testuser1", longer_password) assert cm.get_password("testuser1") == longer_password # change longer password to long password long_password = "".join(["1111111111" for i in range(30)]) cm.set_password("testuser1", long_password) assert cm.get_password("testuser1") == long_password # change long password to short password cm.set_password("testuser1", "shortpwd") assert cm.get_password("testuser1") == "shortpwd" # password length = 1 cm.set_password("testuser1", "a") assert cm.get_password("testuser1") == "a" # password length = 255 pwd_255 = "".join(["a" for i in range(255)]) cm.set_password("testuser1", pwd_255) assert cm.get_password("testuser1") == pwd_255 # password length = 256 pwd_256 = "".join(["a" for i in range(256)]) cm.set_password("testuser1", pwd_256) assert cm.get_password("testuser1") == pwd_256 # password length = 255 * 2 pwd_510 = "".join(["a" for i in range(510)]) cm.set_password("testuser1", pwd_510) assert cm.get_password("testuser1") == pwd_510 # password is empty cm.set_password("testuser1", "") assert cm.get_password("testuser1") == "" # password = '******' cm.set_password("testuser1", "`") assert cm.get_password("testuser1") == "`" # password is substring of END_MARK pwd_substr = "``splunk_cred_sep``S``splunk_cred_sep``P``" cm.set_password("testuser1", pwd_substr) assert cm.get_password("testuser1") == pwd_substr # test _update_password # Update a password which does not exist. create a new one. cm._update_password("testuser3", "beforechange") assert cm.get_password("testuser3") == "beforechange" # update an existed password cm._update_password("testuser3", "changed") assert cm.get_password("testuser3") == "changed" cm.delete_password("testuser1") with pytest.raises(Exception): cm.get_password("testuser1") cm.delete_password("testuser2") with pytest.raises(Exception): cm.get_password("testuser2") cm.delete_password("testuser3") with pytest.raises(Exception): cm.get_password("testuser3")
def test_object_acl_manager(monkeypatch): OBJECT_ACLS_COLLECTION_NAME = "object_acls_collection" object_acls = {} def mock_kvstore_collections_get( self, name="", owner=None, app=None, sharing=None, **query ): raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collections_create(self, name, indexes={}, fields={}, **kwargs): pass def mock_kvstore_collections_list(self, count=None, **kwargs): return [client.KVStoreCollection(None, None)] def mock_kvstore_collection_init(self, service, path, **kwargs): pass def mock_kvstore_collection_data_init(self, collection): pass def mock_kvstore_collection_data_batch_save(self, *documents): for document in documents: object_acls[document["_key"]] = document def mock_kvstore_collection_data_query_by_id(self, id): try: return object_acls[id] except: raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collection_data_query(self, **query): query = json.loads(query["query"]) keys = query["$or"] keys = [key["_key"] for key in keys] records = [] for key in keys: if key in object_acls: records.append(object_acls[key]) return records def mock_kvstore_collection_data_delete_by_id(self, id): try: del object_acls[id] except: raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collection_data_delete(self, query=None): query = json.loads(query) keys = query["$or"] keys = [key["_key"] for key in keys] for key in keys: if key in object_acls: del object_acls[key] common.mock_splunkhome(monkeypatch) monkeypatch.setattr(client.KVStoreCollections, "get", mock_kvstore_collections_get) monkeypatch.setattr( client.KVStoreCollections, "create", mock_kvstore_collections_create ) monkeypatch.setattr( client.KVStoreCollections, "list", mock_kvstore_collections_list ) monkeypatch.setattr( client.KVStoreCollection, "__init__", mock_kvstore_collection_init ) monkeypatch.setattr( client.KVStoreCollection, "name", "Splunk_TA_test" + "_" + OBJECT_ACLS_COLLECTION_NAME, ) monkeypatch.setattr( client.KVStoreCollectionData, "__init__", mock_kvstore_collection_data_init ) monkeypatch.setattr( client.KVStoreCollectionData, "batch_save", mock_kvstore_collection_data_batch_save, ) monkeypatch.setattr( client.KVStoreCollectionData, "query_by_id", mock_kvstore_collection_data_query_by_id, ) monkeypatch.setattr( client.KVStoreCollectionData, "query", mock_kvstore_collection_data_query ) monkeypatch.setattr( client.KVStoreCollectionData, "delete_by_id", mock_kvstore_collection_data_delete_by_id, ) monkeypatch.setattr( client.KVStoreCollectionData, "delete", mock_kvstore_collection_data_delete ) oaclm = user_access.ObjectACLManager( OBJECT_ACLS_COLLECTION_NAME, common.SESSION_KEY, "Splunk_TA_test" ) obj_collection = "test_object_collection" obj_id1 = "281a6d3310e711e6b2c9a45e60e34295" obj_id2 = "281a6d3310e711e6b2c9a45e60e34296" obj_id3 = "281a6d3310e711e6b2c9a45e60e34297" obj_id4 = "281a6d3310e711e6b2c9a45e60e34294" obj_type = "test_object_type" obj_perms1 = {"read": ["admin"], "write": ["admin"], "delete": ["admin"]} obj_perms2 = {"read": ["user1"], "write": ["admin"], "delete": ["admin"]} oaclm.update_acl( obj_collection, obj_id1, obj_type, "Splunk_TA_test", "nobody", obj_perms1, True ) oaclm.update_acl( obj_collection, obj_id1, obj_type, "Splunk_TA_test", "nobody", obj_perms2, True, replace_existing=False, ) obj_acl = oaclm.get_acl(obj_collection, obj_id1) assert obj_acl.obj_collection == "test_object_collection" assert obj_acl.obj_id == obj_id1 assert obj_acl.obj_type == "test_object_type" assert obj_acl.obj_app == "Splunk_TA_test" assert obj_acl.obj_owner == "nobody" assert obj_acl.obj_shared_by_inclusion == True assert sorted(obj_acl.obj_perms["read"]) == ["admin", "user1"] oaclm.update_acls( obj_collection, [obj_id2, obj_id3], obj_type, "Splunk_TA_test", "nobody", obj_perms1, True, replace_existing=False, ) oaclm.get_acl(obj_collection, obj_id2) oaclm.get_acl(obj_collection, obj_id3) obj_acls = oaclm.get_acls(obj_collection, [obj_id2, obj_id3, obj_id4]) assert len(obj_acls) == 2 assert oaclm.get_accessible_object_ids( "user1", "read", obj_collection, [obj_id1, obj_id2, obj_id3] ) == [obj_id1] oaclm.delete_acl(obj_collection, obj_id1) with pytest.raises(user_access.ObjectACLNotExistException): oaclm.delete_acl(obj_collection, obj_id1) oaclm.delete_acls(obj_collection, [obj_id2, obj_id3]) assert not object_acls
def test_server_name(self, monkeypatch): common.mock_splunkhome(monkeypatch) common.mock_serverinfo(monkeypatch) si = server_info.ServerInfo(common.SESSION_KEY) assert si.server_name == "unittestServer"
def test_is_cloud_instance(self, monkeypatch): common.mock_splunkhome(monkeypatch) common.mock_serverinfo(monkeypatch) si = server_info.ServerInfo(common.SESSION_KEY) assert not si.is_cloud_instance()
def test_is_shc_member(self, monkeypatch): common.mock_splunkhome(monkeypatch) common.mock_serverinfo(monkeypatch) si = server_info.ServerInfo(common.SESSION_KEY) assert si.is_shc_member()
def test_hec_event_writer(monkeypatch): def mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): if path_segment.endswith("/http"): return common.make_response_record( '{"entry": [{"content": {"port": 8088}}]}') else: return common.make_response_record( '{"entry": [{"content": {"token": "87de04d1-0823-11e6-9c94-a45e60e34295"}}]}' ) def mock_post(self, path_segment, owner=None, app=None, sharing=None, headers=None, **query): event_strings = [ json.dumps(json.loads(e), sort_keys=True) for e in query["body"].split("\n") ] assert ( event_strings[0] == '{"event": "This is a test data1.", "host": "localhost", "index": "main", "source": "Splunk", "sourcetype": "misc", "time": 1372274622.493}' ) assert ( event_strings[1] == '{"event": "This is a test data2.", "host": "localhost", "index": "main", "source": "Splunk", "sourcetype": "misc", "time": 1372274622.493}' ) def mock_get_hec_config(self, hec_input_name, session_key, scheme, host, port, **context): return "8088", "87de04d1-0823-11e6-9c94-a45e60e" common.mock_splunkhome(monkeypatch) monkeypatch.setattr(binding.Context, "get", mock_get) monkeypatch.setattr(binding.Context, "post", mock_post) monkeypatch.setattr(HECEventWriter, "_get_hec_config", mock_get_hec_config) for i in range(4): ew = create_hec_event_writer(i) events = [] events.append( ew.create_event( data="This is a test data1.", time=1372274622.493, index="main", host="localhost", source="Splunk", sourcetype="misc", stanza="test_scheme://test", unbroken=True, done=False, )) events.append( ew.create_event( data="This is a test data2.", time=1372274622.493, index="main", host="localhost", source="Splunk", sourcetype="misc", stanza="test_scheme://test", unbroken=True, done=True, )) ew.write_events(events) # length of this list will indicate how many times post was called times_post_called = [] def mock_post_2(self, path_segment, owner=None, app=None, sharing=None, headers=None, **query): times_post_called.append(1) monkeypatch.setattr(binding.Context, "post", mock_post_2) # test that there are 2 event batches created for write_event and post is called 2 times # max batch size is 1,000,000. If the max size is exceeded then a new batch is created. assert len(times_post_called) == 0 ew = create_hec_event_writer(1) events = [] # each event length will be ~500 characters, 3000 events length will equal ~1,500,000 characters # which means there will be 2 event batches required when writing events via post events = [] for i in range(3000): # length of this data_str is approximately 400 characters data_str = ( "DATA" + str(i) + ": This is test data. This is test data. This is test data. This is test data. " "This is test data. This is test data. This is test data. This is test data. " "This is test data. This is test data. This is test data. This is test data. " "This is test data. This is test data. This is test data. This is test data. " "This is test data. This is test data. This is test data. " "This is test data. This is test data.") # total length of the events will be ~500 characters event = ew.create_event( data=data_str, time=1372274622.493, index="main", host="localhost", source="Splunk", sourcetype="misc", stanza="test_scheme://test", unbroken=True, done=True, ) events.append(event) ew.write_events(events) # test that post is called 2 times assert len(times_post_called) == 2
def test_kvstore_checkpointer(monkeypatch): KVSTORE_CHECKPOINTER_COLLECTION_NAME = "TestKVStoreCheckpointer" checkpoint_states = {} def mock_kvstore_collections_get(self, name="", owner=None, app=None, sharing=None, **query): raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collections_create(self, name, indexes={}, fields={}, **kwargs): pass def mock_kvstore_collections_list(self, count=None, **kwargs): return [client.KVStoreCollection(None, None)] def mock_kvstore_collection_init(self, service, path, **kwargs): pass def mock_kvstore_collection_data_init(self, collection): pass def mock_kvstore_collection_data_batch_save(self, *documents): for document in documents: checkpoint_states[document["_key"]] = document def mock_kvstore_collection_data_query_by_id(self, id): try: return checkpoint_states[id] except: raise binding.HTTPError(common.make_response_record("", status=404)) def mock_kvstore_collection_data_delete_by_id(self, id): try: del checkpoint_states[id] except: raise binding.HTTPError(None, status=404) common.mock_splunkhome(monkeypatch) monkeypatch.setattr(client.KVStoreCollections, "get", mock_kvstore_collections_get) monkeypatch.setattr(client.KVStoreCollections, "create", mock_kvstore_collections_create) monkeypatch.setattr(client.KVStoreCollections, "list", mock_kvstore_collections_list) monkeypatch.setattr(client.KVStoreCollection, "__init__", mock_kvstore_collection_init) monkeypatch.setattr(client.KVStoreCollection, "name", KVSTORE_CHECKPOINTER_COLLECTION_NAME) monkeypatch.setattr(client.KVStoreCollectionData, "__init__", mock_kvstore_collection_data_init) monkeypatch.setattr( client.KVStoreCollectionData, "batch_save", mock_kvstore_collection_data_batch_save, ) monkeypatch.setattr( client.KVStoreCollectionData, "query_by_id", mock_kvstore_collection_data_query_by_id, ) monkeypatch.setattr( client.KVStoreCollectionData, "delete_by_id", mock_kvstore_collection_data_delete_by_id, ) ck = KVStoreCheckpointer(KVSTORE_CHECKPOINTER_COLLECTION_NAME, common.SESSION_KEY, "Splunk_TA_test") ck.update("test_state_key1", "test_state_value1") ck.batch_update([ { "_key": "test_state_key2", "state": "test_state_value2" }, { "_key": "test_state_key3", "state": "test_state_value3" }, ]) assert ck.get("test_state_key1") == "test_state_value1" assert ck.get("test_state_key2") == "test_state_value2" assert ck.get("test_state_key3") == "test_state_value3" ck.delete("test_state_key1") ck.delete("test_state_key2") ck.delete("test_state_key3") assert ck.get("test_state_key1") is None assert ck.get("test_state_key2") is None assert ck.get("test_state_key3") is None