| | |
| | | * |
| | | * |
| | | * Copyright 2006-2008 Sun Microsystems, Inc. |
| | | * Portions copyright 2013 ForgeRock AS |
| | | */ |
| | | package org.opends.server.protocols.jmx; |
| | | |
| | | |
| | | import static org.testng.Assert.*; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | |
| | | import java.security.KeyStore; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.Set; |
| | | import java.util.Map; |
| | | |
| | | import javax.management.Attribute; |
| | | import javax.management.AttributeNotFoundException; |
| | | import javax.management.MBeanServerConnection; |
| | | import javax.management.ObjectName; |
| | | import javax.net.ssl.TrustManager; |
| | | import javax.net.ssl.TrustManagerFactory; |
| | | |
| | | import org.opends.server.TestCaseUtils; |
| | | import org.opends.messages.Message; |
| | | import org.opends.server.TestCaseUtils; |
| | | import org.opends.server.admin.server.AdminTestCaseUtils; |
| | | import org.opends.server.admin.std.meta.JMXConnectionHandlerCfgDefn; |
| | | import org.opends.server.admin.std.server.JMXConnectionHandlerCfg; |
| | |
| | | import org.testng.annotations.DataProvider; |
| | | import org.testng.annotations.Test; |
| | | |
| | | import static org.testng.Assert.*; |
| | | |
| | | |
| | | |
| | | /** |
| | | * A simple test for : - JMX connection establishment withou using SSL - |
| | | * A simple test for : - JMX connection establishment without using SSL - |
| | | * JMX get and set - configuration change |
| | | */ |
| | | @SuppressWarnings("javadoc") |
| | | public class JmxConnectTest extends JmxTestCase { |
| | | |
| | | |
| | | /** |
| | | * Set up the environment for performing the tests in this suite. |
| | | * |
| | | * |
| | | * @throws Exception |
| | | * If the environment could not be set up. |
| | | */ |
| | | @Override |
| | | @BeforeClass |
| | | public void setUp() throws Exception |
| | | { |
| | | super.setUp(); |
| | | |
| | | |
| | | TestCaseUtils.addEntries( |
| | | "dn: cn=Privileged User,o=test", |
| | | "objectClass: top", |
| | |
| | | "ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," + |
| | | "cn=Password Policies,cn=config"); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * Clean up the environment after performing the tests in this suite. |
| | | * |
| | | * |
| | | * @throws Exception |
| | | * If the environment could not be set up. |
| | | */ |
| | |
| | | DeleteOperation deleteOperation = conn.processDelete(DN |
| | | .decode("cn=Privileged User,o=test")); |
| | | assertEquals(deleteOperation.getResultCode(), ResultCode.SUCCESS); |
| | | |
| | | |
| | | deleteOperation = conn.processDelete(DN |
| | | .decode("cn=Unprivileged JMX User,o=test")); |
| | | assertEquals(deleteOperation.getResultCode(), ResultCode.SUCCESS); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * Build data for the simpleConnect test. |
| | |
| | | connector.close(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Build some data for the simpleGet test. |
| | | */ |
| | |
| | | MBeanServerConnection jmxc = connector.getMBeanServerConnection(); |
| | | assertNotNull(jmxc); |
| | | |
| | | Set names = jmxc.queryNames(null, null); |
| | | names.clear(); |
| | | jmxc.queryNames(null, null).clear(); |
| | | |
| | | final String dn = "cn=config"; |
| | | final String attribute = "ds-cfg-size-limit"; |
| | | |
| | | Long val = (Long) jmxGet(dn, attribute, jmxc); |
| | | |
| | | long val = (Long) jmxGet(dn, attribute, jmxc); |
| | | jmxSet(dn, attribute, val + 1, jmxc); |
| | | |
| | | Long newVal = (Long) jmxGet(dn, attribute, jmxc); |
| | | |
| | | assertEquals((long) newVal, (long) val + 1); |
| | | long newVal = (Long) jmxGet(dn, attribute, jmxc); |
| | | assertEquals(newVal, val + 1); |
| | | |
| | | jmxSet(dn, attribute, val + 1, jmxc); |
| | | |
| | |
| | | assertNotNull(jmxc); |
| | | |
| | | // Disable the "new" connector |
| | | toggleEnableJmxConnector(connector, newJmxConnectionJmx.getDN(), |
| | | false); |
| | | toggleEnableJmxConnector(connector, newJmxConnectionJmx.getDN(), false); |
| | | Thread.sleep(100); |
| | | OpendsJmxConnector jmxcDisabled = connect("cn=Privileged User,o=test", |
| | | "password", serverJmxPort); |
| | | assertNull(jmxcDisabled); |
| | | |
| | | toggleEnableJmxConnector(connector, newJmxConnectionJmx.getDN(), |
| | | true); |
| | | toggleEnableJmxConnector(connector, newJmxConnectionJmx.getDN(), true); |
| | | Thread.sleep(100); |
| | | jmxcDisabled = connect("cn=Privileged User,o=test", "password", |
| | | serverJmxPort); |
| | | jmxcDisabled = connect("cn=Privileged User,o=test", "password", serverJmxPort); |
| | | assertNotNull(jmxcDisabled); |
| | | |
| | | // cleanup client connection |
| | |
| | | |
| | | /** |
| | | * Test changing JMX port through LDAP |
| | | * |
| | | * @throws Exception |
| | | */ |
| | | @Test(enabled = false) |
| | | public void changePort() throws Exception { |
| | |
| | | connector.close(); |
| | | assertNotNull(initJmxPort); |
| | | |
| | | // change the configuration of the connection handler to use |
| | | // a free port |
| | | ServerSocket serverJmxSocket = TestCaseUtils.bindFreePort(); |
| | | int serverJmxPort = serverJmxSocket.getLocalPort(); |
| | | // change the configuration of the connection handler to use a free port |
| | | int serverJmxPort = TestCaseUtils.findFreePort(); |
| | | |
| | | Entry entry = TestCaseUtils |
| | | .makeEntry( |
| | |
| | | "ds-cfg-enabled: true", |
| | | "ds-cfg-use-ssl: false", "ds-cfg-listen-port: " |
| | | + serverJmxPort, "cn: JMX Connection Handler"); |
| | | serverJmxSocket.close(); |
| | | configureJmx(entry); |
| | | |
| | | // connect the the JMX service using the new port |
| | | connector = connect("cn=Privileged User,o=test", "password", |
| | | serverJmxPort); |
| | | connector = connect("cn=Privileged User,o=test", "password", serverJmxPort); |
| | | jmxc = connector.getMBeanServerConnection(); |
| | | assertNotNull(jmxc); |
| | | Long val = (Long) jmxGet(dn, attribute, jmxc); |
| | | assertEquals((long) val, (long) serverJmxPort); |
| | | long val = (Long) jmxGet(dn, attribute, jmxc); |
| | | assertEquals(val, serverJmxPort); |
| | | connector.close(); |
| | | |
| | | // re-establish the initial configuration of the JMX service |
| | |
| | | OpendsJmxConnector jmxc = sslConnect("cn=Privileged User,o=test", |
| | | "password", initJmxPort); |
| | | assertNotNull(jmxc,"OpendsJmxConnector shouldn't be null"); |
| | | MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); |
| | | jmxc.getMBeanServerConnection(); |
| | | jmxc.close(); |
| | | |
| | | // Before returning the result, |
| | |
| | | /** |
| | | * Connect to the JMX service. |
| | | */ |
| | | private OpendsJmxConnector connect(String user, String password, |
| | | long jmxPort) throws MalformedURLException, IOException{ |
| | | HashMap<String, Object> env = new HashMap<String, Object>(); |
| | | private OpendsJmxConnector connect(String user, String password, int jmxPort) |
| | | throws MalformedURLException, IOException { |
| | | Map<String, Object> env = new HashMap<String, Object>(); |
| | | |
| | | // Provide the credentials required by the server to successfully |
| | | // perform user authentication |
| | |
| | | // |
| | | OpendsJmxConnector opendsConnector; |
| | | try { |
| | | opendsConnector = new OpendsJmxConnector("localhost", |
| | | (int) jmxPort, env); |
| | | opendsConnector = new OpendsJmxConnector("localhost", jmxPort, env); |
| | | opendsConnector.connect(); |
| | | return opendsConnector; |
| | | } catch (SecurityException e) { |
| | |
| | | */ |
| | | private OpendsJmxConnector sslConnect(String user, String password, |
| | | long jmxPort) throws Exception { |
| | | HashMap<String, Object> env = new HashMap<String, Object>(); |
| | | Map<String, Object> env = new HashMap<String, Object>(); |
| | | |
| | | // Provide the credentials required by the server to successfully |
| | | // perform user authentication |
| | |
| | | env.put("jmx.remote.credentials", credentials); |
| | | |
| | | // Provide the Trust manager. |
| | | KeyStore ks = null; |
| | | ks = KeyStore.getInstance("JKS"); |
| | | FileInputStream keyStoreFile = new FileInputStream( |
| | | getJmxKeystorePath()); |
| | | KeyStore ks = KeyStore.getInstance("JKS"); |
| | | FileInputStream keyStoreFile = new FileInputStream(getJmxKeystorePath()); |
| | | ks.load(keyStoreFile, "password".toCharArray()); |
| | | keyStoreFile.close(); |
| | | |
| | | TrustManagerFactory tmf = TrustManagerFactory |
| | | .getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| | | tmf.init(ks); |
| | | TrustManager tms[] = tmf.getTrustManagers(); |
| | | env.put(JmxConnectionHandler.TRUST_MANAGER_ARRAY_KEY, tms); |
| | | env.put(JmxConnectionHandler.TRUST_MANAGER_ARRAY_KEY, tmf.getTrustManagers()); |
| | | |
| | | // Create an RMI connector client and |
| | | // connect it to the RMI connector server |
| | |
| | | opendsConnector.connect(); |
| | | return opendsConnector; |
| | | } catch (Exception e) { |
| | | |
| | | return null; |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * @return |
| | | */ |
| | | private String getJmxKeystorePath() { |
| | | return DirectoryServer.getInstanceRoot() + File.separator + "config" |
| | | + File.separator + "server.keystore"; |
| | |
| | | try |
| | | { |
| | | Attribute status = (Attribute) mbsc.getAttribute(name, attributeName); |
| | | if (status == null) |
| | | return null; |
| | | else |
| | | if (status != null) |
| | | { |
| | | return status.getValue(); |
| | | } |
| | | return null; |
| | | } |
| | | catch (AttributeNotFoundException anfe) |
| | | { |