def instance_admin(cls,host,realm,admin,password): """ Initializes the security database of a newly initialized server. :param host: The name or IP address of the host to initialize :param realm: The security realm to install :param admin: The name of the admin user :param password: The password of the admin user """ conn = Connection(host, None) payload = { 'admin-username': admin, 'admin-password': password, 'realm': realm } uri = "{0}://{1}:8001/admin/v1/instance-admin".format( conn.protocol, conn.host) logger = logging.getLogger("marklogic") logger.debug("Initializing security for {0}".format(host)) # N.B. Can't use conn.post here because we don't need auth yet response = requests.post(uri, json=payload, headers={'content-type': 'application/json', 'accept': 'application/json'}) if response.status_code != 202: raise UnexpectedManagementAPIResponse(response.text) # From now on connections require auth... conn = Connection(host, HTTPDigestAuth(admin, password)) data = json.loads(response.text) conn.wait_for_restart(data["restart"]["last-startup"][0]["value"])
def instance_init(cls, host): """ Performs first-time initialization of a newly installed server. :param host: The name or IP address of the host to initialize """ conn = Connection(host, None) uri = "{0}://{1}:8001/admin/v1/init".format(conn.protocol, conn.host) logger = logging.getLogger("marklogic") logger.debug("Initializing {0}".format(host)) # This call is a little odd; we special case the 400 error that # occurs if the host has alreadya been initialized. try: response = conn.post( uri, content_type='application/x-www-form-urlencoded') except UnexpectedManagementAPIResponse: response = conn.response if response.status_code == 400: err = json.loads(response.text) if "errorResponse" in err: if "messageCode" in err["errorResponse"]: if err["errorResponse"][ "messageCode"] == "MANAGE-ALREADYINIT": return Host(host) raise if response.status_code != 202: raise UnexpectedManagementAPIResponse(response.text) return Host(host)._set_just_initialized()
def instance_init(cls,host): """ Performs first-time initialization of a newly installed server. :param host: The name or IP address of the host to initialize """ conn = Connection(host, None) uri = "{0}://{1}:8001/admin/v1/init".format(conn.protocol,conn.host) logger = logging.getLogger("marklogic") logger.debug("Initializing {0}".format(host)) # This call is a little odd; we special case the 400 error that # occurs if the host has alreadya been initialized. try: response = conn.post(uri, content_type='application/x-www-form-urlencoded') except UnexpectedManagementAPIResponse: response = conn.response if response.status_code == 400: err = json.loads(response.text) if "errorResponse" in err: if "messageCode" in err["errorResponse"]: if err["errorResponse"]["messageCode"] == "MANAGE-ALREADYINIT": return Host(host) raise if response.status_code != 202: raise UnexpectedManagementAPIResponse(response.text) return Host(host)._set_just_initialized()
def _get_server_config(self): """ Obtain the server configuration. This is the data necessary for the first part of the handshake necessary to join a host to a cluster. The returned data is not intended for introspection. :return: The config. This is always XML. """ connection = Connection(self.host_name(), None) uri = "http://{0}:8001/admin/v1/server-config".format(connection.host) response = connection.get(uri, accept="application/xml") if response.status_code != 200: raise UnexpectedManagementAPIResponse(response.text) return response.text # this is always XML
def test_template(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) req = Request(countryName="US", stateOrProvinceName="TX", localityName="Austin", organizationName="MarkLogic", emailAddress="*****@*****.**", version=0) temp = Template("Test Template", "Test description", req) self.assertEqual("Test Template", temp.template_name()) temp.create(connection) names = Template.list(connection) self.assertGreater(len(names), 0) self.assertIn(temp.template_id(), names) temp.set_template_name("New Name") temp.set_template_description("New Description") temp.update(connection) self.assertIsNotNone(temp) newtemp = Template.lookup(connection, temp.template_id()) self.assertEqual(temp.template_name(), newtemp.template_name()) temp.delete(connection) self.assertIsNotNone(temp)
def test_simple_create(self): """ TODO: The hostname should come from the server's hostname Test the basic create function. Creates a database and then check to see that it exists by getting the database configuration from the server. It then destroys the database. :return: None """ conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password)) hosts = Host.list(conn) db = Database("test-db", hosts[0]) db.create(conn) validate_db = Database.lookup(conn, "test-db") try: self.assertIsNotNone(validate_db) self.assertEqual('test-db', validate_db.database_name()) finally: validate_db.delete(connection=conn) validate_db = Database.lookup(conn, "test-db") self.assertIsNone(validate_db)
def test_create_simple_forests(self): """ Test the following scenario: The database is given the names of two forests. It should then create the two named forests. """ conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password)) hosts = Host.list(conn) db = Database("simple-forest-create-test-db", hosts[0], connection=conn) db.set_forest_names( ["simple-forest-create-forest1", "simple-forest-create-forest2"]) db.create() db = Database.lookup(conn, "simple-forest-create-test-db") try: self.assertEqual(2, len(db.forest_names())) self.assertIn("simple-forest-create-forest1", db.forest_names()) self.assertIn("simple-forest-create-forest2", db.forest_names()) finally: db.delete(connection=conn)
def test_create(self): pem = ("-----BEGIN CERTIFICATE-----\n" "MIIC3TCCAkYCCQCJtpKDQbobyTANBgkqhkiG9w0BAQsFADCBsjELMAkGA1UEBhMC\n" "VVMxCzAJBgNVBAgMAlRYMQ8wDQYDVQQHDAZBdXN0aW4xHjAcBgNVBAoMFU1hcmtM\n" "b2dpYyBDb3Jwb3JhdGlvbjEXMBUGA1UECwwOVFggRW5naW5lZXJpbmcxITAfBgNV\n" "BAMMGE1hcmtMb2dpYyBUWCBFbmdpbmVlcmluZzEpMCcGCSqGSIb3DQEJARYabm9y\n" "bWFuLndhbHNoQG1hcmtsb2dpYy5jb20wHhcNMTQwODI3MTkyMzQyWhcNMTUwODI3\n" "MTkyMzQyWjCBsjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlRYMQ8wDQYDVQQHDAZB\n" "dXN0aW4xHjAcBgNVBAoMFU1hcmtMb2dpYyBDb3Jwb3JhdGlvbjEXMBUGA1UECwwO\n" "VFggRW5naW5lZXJpbmcxITAfBgNVBAMMGE1hcmtMb2dpYyBUWCBFbmdpbmVlcmlu\n" "ZzEpMCcGCSqGSIb3DQEJARYabm9ybWFuLndhbHNoQG1hcmtsb2dpYy5jb20wgZ8w\n" "DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJSo3wFMDvTV7Q+4NDDMu9aJZ6uK4l8b\n" "ACIk5/Ug+MoST+CuIfeBlb2Y6dxNCwkADwVPpykslDcHYFygxFIcnHHVhgqZ0xzu\n" "LPXBagXmHyj+mb6im1tkbqAxQ7gj/SDeCnQYRIwNRlGgWZJFViaYJH3CC8G/f16F\n" "IhDyQS3h28W3AgMBAAEwDQYJKoZIhvcNAQELBQADgYEAWbidV4huPlf8Ac0c3Cbs\n" "Nx2xogODSjNPKqwug0Y3jKx33uxeY7i9oParWSnVFkG0JYUZEfrO5fmtS6JSA1Lk\n" "e3BioC9xgclEYFiDoZSARasL8hdNvu7v+EYZEnS43rR4M7CQiq/Tf50o4VjiVM9S\n" "I0Bo+VZSaShQKipBEHS8sP8=\n" "-----END CERTIFICATE-----\n") connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) cert = Authority.create(connection, pem) self.assertIsNotNone(cert) self.assertEqual('true', cert.enabled()) self.assertIsNotNone(cert.properties()) cert.delete(connection)
def __init__(self, *args, **kwargs): super(MLConfig,self).__init__(*args, **kwargs) config = { "hostname": "localhost", \ "username": "******", \ "password": "******", \ "protocol": "http", \ "port": 8000, \ "management-port": 8002, \ "root": "manage", \ "version": "v2", \ "client-version": "v1" } try: data_file = open("mlconfig.json").read() data = json.loads(data_file) for key in data: config[key] = data[key] except FileNotFoundError: pass self.auth = HTTPDigestAuth(config["username"], config["password"]) self.connection = Connection(config["hostname"], self.auth, \ protocol=config["protocol"], \ port=config["port"], \ management_port=config["management-port"], \ root=config["root"], \ version=config["version"], \ client_version=config["client-version"])
def test_list(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) users = User.list(connection) self.assertGreater(len(users), 2) self.assertIn("nobody", users)
def test_lookup(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) user = User.lookup(connection, "nobody") self.assertIsNotNone(user) self.assertEqual(user.user_name(), "nobody")
def test_list(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) names = Role.list(connection) self.assertGreater(len(names), 65) self.assertIn("admin", names)
def test_load(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) server = HttpServer("Manage", "Default") self.assertEqual(server.server_name(), "Manage") self.assertIsNotNone(server.read(connection)) self.assertEqual("http", server.server_type())
def test_lookup(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) role = Role.lookup(connection, "admin") self.assertIsNotNone(role) self.assertEqual(role.role_name(), "admin")
def couple_cluster(self): conn = Connection(self.host, HTTPDigestAuth(self.adminuser, self.adminpass)) self.marklogic = MarkLogic(conn) if self.couple is not None: for couple in self.couple: print("{0}: couple with {1}...".format(self.host, couple)) altconn = Connection( couple, HTTPDigestAuth(self.couple_user, self.couple_pass)) altml = MarkLogic(altconn) altcluster = altml.cluster() cluster = self.marklogic.cluster() cluster.couple(altcluster) print("Finished")
def test_lookup(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) server = Server.lookup(connection, "Manage") self.assertIsNotNone(server) self.assertEqual(server.server_name(), "Manage")
def test_lookup(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) privilege = Privilege.lookup(connection, "manage-admin", "execute") self.assertIsNotNone(privilege) self.assertEqual(privilege.privilege_name(), "manage-admin")
def test_create_single_detailed_forest(self): """ Test the following scenario: The database is given a forest object. It should create a forest with the given name. That forest should match the features of the datailed forest. """ conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password)) hosts = Host.list(conn) db = Database("detailed-forest-create-test-db", hosts[0]) forest = Forest("detailed-forest-create-forest1", host=hosts[0]) forest.set_large_data_directory(ds.large_data_directory) db.set_forest_names([forest.forest_name()]) db.create(conn) forest = Forest.lookup(conn, "detailed-forest-create-forest1") try: self.assertEqual("detailed-forest-create-forest1", forest.forest_name()) #this isn't in the properties...oddly. #self.assertEqual(ds.large_data_directory, forest.large_data_directory()) finally: db.delete(connection=conn)
def test_list(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) names = Server.list(connection) self.assertGreater(len(names), 3) self.assertIn("Default|Manage", names)
def test_list(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) privileges = Privilege.list(connection) self.assertGreater(len(privileges), 300) self.assertIn("execute|manage-admin", privileges)
def test_lookup(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) names = Authority.list(connection) auth = Authority.lookup(connection, names[0]) self.assertIsNotNone(auth) self.assertEqual(auth.certificate_id(), names[0])
def join_cluster(self, cluster, cluster_connection=None): if cluster_connection is None: cluster_connection = cluster.connection xml = self._get_server_config() cfgzip = cluster._post_server_config(xml, cluster_connection) connection = Connection(self.host_name(), cluster_connection.auth) self._post_cluster_config(cfgzip, connection)
def test_lookup_action(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) privilege = Privilege.lookup(connection, kind="execute", \ action="http://marklogic.com/xdmp/privileges/admin-module-write") self.assertIsNotNone(privilege) self.assertEqual(privilege.privilege_name(), "admin-module-write")
def test_list_databases(self): conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password)) db_names = Database.list(conn) self.assertGreater(len(db_names), 4) self.assertTrue("Modules" in db_names) self.assertTrue("Documents" in db_names)
def _test_ssl_certificate_pems(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) server = HttpServer("foo-http", "Default", 10101, '/', 'Documents') self.assertEqual(server.server_name(), "foo-http") server.create(connection) self.assertIsNotNone(server) self.assertEqual("http", server.server_type()) pem1 = "-----BEGIN CERTIFICATE-----\n\ MIICgjCCAeugAwIBAgIBBDANBgkqhkiG9w0BAQQFADBTMQswCQYDVQQGEwJVUzEc\n\ MBoGA1UEChMTRXF1aWZheCBTZWN1cmUgSW5jLjEmMCQGA1UEAxMdRXF1aWZheCBT\n\ ZWN1cmUgZUJ1c2luZXNzIENBLTEwHhcNOTkwNjIxMDQwMDAwWhcNMjAwNjIxMDQw\n\ MDAwWjBTMQswCQYDVQQGEwJVUzEcMBoGA1UEChMTRXF1aWZheCBTZWN1cmUgSW5j\n\ LjEmMCQGA1UEAxMdRXF1aWZheCBTZWN1cmUgZUJ1c2luZXNzIENBLTEwgZ8wDQYJ\n\ KoZIhvcNAQEBBQADgY0AMIGJAoGBAM4vGbwXt3fek6lfWg0XTzQaDJj0ItlZ1MRo\n\ RvC0NcWFAyDGr0WlIVFFQesWWDYyb+JQYmT5/VGcqiTZ9J2DKocKIdMSODRsjQBu\n\ WqDZQu4aIZX5UkxVWsUPOE9G+m34LjXWHXzr4vCwdYDIqROsvojvOm6rXyo4YgKw\n\ Env+j6YDAgMBAAGjZjBkMBEGCWCGS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Y+6j/s1dr\n\ aGwwHQYDVR0OBBYEFL6ooHRyUGtEt8kj2Puo/7NXa2hsMA0GCSqGSIb3DQEBBAUA\n\ A4GBADDiAVGqx+pf2rnQZQ8w1j7aDRRJbpGTJxQx78T3LUX47Me/okENI7SS+RkA\n\ Z70Br83gcfxaz2TE4JaY0KNA4gGK7ycH8WUBikQtBmV1UsCGECAhX2xrD2yuCRyv\n\ 8qIYNMR1pHMc8Y3c7635s3a0kr/clRAevsvIO1qEYBlWlKlV\n\ -----END CERTIFICATE-----" server.add_ssl_client_certificate_pem(pem1) server.update(connection) server.read(connection) self.assertEqual(1, len(server.ssl_client_certificate_pems())) server.set_ssl_client_certificate_pems([pem1,pem2]) server.update(connection) server.read(connection) self.assertEqual(2, len(server.ssl_client_certificate_pems())) server.delete(connection) server = Server.lookup(connection, "foo-http") self.assertIsNone(server)
def test_create_webdav_server(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) server = WebDAVServer("foo-webdav", "Default", 10101, '/', 'Documents') self.assertEqual(server.server_name(), "foo-webdav") server.create(connection) self.assertIsNotNone(server) self.assertEqual("webdav", server.server_type()) server.delete(connection) server = Server.lookup(connection, "foo-webdav") self.assertIsNone(server)
def test_list(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) names = Authority.list(connection, include_names=True) self.assertGreater(len(names), 100) found = False for name in names: found = found or "Equifax" in name self.assertEqual(True, found)
def test_save_privilege(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) privilege = Privilege("foo-privilege","http://example.com/","execute") privilege.create(connection) privilege.add_role_name("manage-user") privilege.update(connection) self.assertIn("manage-user", privilege.role_names()) privilege.delete(connection)
def test_create_privilege(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) new_privilege = Privilege("foo-privilege","http://example.com/","execute") self.assertEqual(new_privilege.privilege_name(), "foo-privilege") new_privilege.create(connection) privileges = Privilege.list(connection) self.assertIn("execute|foo-privilege", privileges) new_privilege.delete(connection)
def add_host(self, host, connection=None): if connection is None: connection = self.connection if isinstance(host, str): host = Host(host) xml = host._get_server_config() cfgzip = self._post_server_config(xml,connection) host_connection = Connection(host.host_name(), connection.auth) host._post_cluster_config(cfgzip,host_connection)
def test_create_remove_role(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) role = Role("foo-role") role.create(connection) the_role = Role.lookup(connection, "foo-role") self.assertIsNotNone(the_role) the_role.delete(connection) the_role = Role.lookup(connection, "foo-role") self.assertIsNone(the_role)
def test_ssl_certificate_pems(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) cluster = LocalCluster(connection=connection) version = cluster.version() if (version.startswith("4") or version.startswith("5") or version.startswith("6") or version.startswith("7") or version.startswith("8.0-1") or version.startswith("8.0-2") or version.startswith("8.0-3")): pass else: self._test_ssl_certificate_pems()
def test_create_remove_privilege(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) privilege = Privilege("foo-privilege","http://example.com/","execute") privilege.create(connection) the_privilege = Privilege.lookup(connection, "foo-privilege", "execute") self.assertIsNotNone(the_privilege) the_privilege.delete(connection) the_privilege = Privilege.lookup(connection, "foo-privilege", "execute") self.assertIsNone(the_privilege)
def test_create_user(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) new_user = User("foo-user", "password") self.assertEqual(new_user.user_name(), "foo-user") new_user.create(connection) users = User.list(connection) self.assertIn("foo-user", users) new_user.delete(connection)
def test_create_remove_user(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) user = User("foo-user", "password") user.create(connection) the_user = User.lookup(connection, "foo-user") self.assertIsNotNone(the_user) the_user.delete(connection) the_user = User.lookup(connection, "foo-user") self.assertIsNone(the_user)
def load_data(self): simpleapp = SimpleApplication(tc.appname, tc.port) conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password)) hostname = Host.list(conn)[0] exampleapp = simpleapp.create(conn, hostname) loader = MLCPLoader() loader.load_directory(conn, exampleapp['content'], "data", collections=["example1"], prefix="/test/data1")
def test_save_role(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) role = Role("foo-role") self.assertIsNone(role.create(connection).description()) role.set_description("This is the foo role") role.update(connection) role = Role.lookup(connection, "foo-role") self.assertEqual("This is the foo role", role.description()) role.delete(connection)
def test_save_user(self): connection = Connection.make_connection(tc.hostname, tc.admin, tc.password) user = User("foo-user", "password") self.assertIsNone(user.create(connection).description()) user.set_description("This is the foo user") user.update(connection) user = User.lookup(connection, "foo-user") self.assertEqual("This is the foo user", user.description()) user.delete(connection)