Search in sources :

Example 46 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project ats-framework by Axway.

the class SystemOperations method getJvmMbeans.

/**
     * @param host the address of the host machine
     * @param jmxPort the jmx port
     * 
     * @return all MBeans with their attributes and type
     * @throws SystemOperationException
     */
@PublicAtsApi
public String getJvmMbeans(String host, String jmxPort) {
    JMXConnector jmxCon = null;
    try {
        // Connect to JMXConnector
        JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi");
        jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, null);
        jmxCon.connect();
        // Access the MBean
        MBeanServerConnection con = jmxCon.getMBeanServerConnection();
        Set<ObjectName> queryResults = con.queryNames(null, null);
        StringBuilder results = new StringBuilder();
        for (ObjectName theName : queryResults) {
            results.append("\n---");
            results.append("\nMBean name: " + theName.getCanonicalName());
            MBeanAttributeInfo[] attributes = con.getMBeanInfo(theName).getAttributes();
            for (MBeanAttributeInfo attribute : attributes) {
                if (attribute.getType() != null) {
                    if (!"javax.management.openmbean.CompositeData".equals(attribute.getType())) {
                        if ("java.lang.Long".equals(attribute.getType()) || "java.lang.Integer".equals(attribute.getType()) || "int".equals(attribute.getType()) || "long".equals(attribute.getType()))
                            results.append("\r   " + attribute.getName() + " | " + attribute.getType());
                    } else {
                        results.append("\r   " + attribute.getName() + " | " + attribute.getType());
                        CompositeData comdata = (CompositeData) con.getAttribute(theName, attribute.getName());
                        if (comdata != null) {
                            for (String key : comdata.getCompositeType().keySet()) {
                                Object value = comdata.get(key);
                                if (value instanceof Integer || value instanceof Double || value instanceof Long)
                                    results.append("\r      " + key + " | " + value.getClass());
                            }
                        }
                    }
                }
            }
        }
        return results.toString();
    } catch (Exception e) {
        throw new SystemOperationException("MBeans with their attributes cannot be get.", e);
    } finally {
        if (jmxCon != null)
            try {
                jmxCon.close();
            } catch (IOException e) {
                log.error("JMX connection was not closed!");
            }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) SystemOperationException(com.axway.ats.common.system.SystemOperationException) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) SystemOperationException(com.axway.ats.common.system.SystemOperationException) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 47 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project geode by apache.

the class MBeanSecurityJUnitTest method testNoAccessWithWhoever.

/**
   * No user can call createBean or unregisterBean of GemFire Domain
   */
@Test
@ConnectionConfiguration(user = "super-user", password = "1234567")
public void testNoAccessWithWhoever() throws Exception {
    MBeanServerConnection con = connectionRule.getMBeanServerConnection();
    assertThatThrownBy(() -> con.createMBean("FakeClassName", new ObjectName("GemFire", "name", "foo"))).isInstanceOf(SecurityException.class);
    assertThatThrownBy(() -> con.unregisterMBean(new ObjectName("GemFire", "name", "foo"))).isInstanceOf(SecurityException.class);
    // user is allowed to create beans of other domains
    assertThatThrownBy(() -> con.createMBean("FakeClassName", new ObjectName("OtherDomain", "name", "foo"))).isInstanceOf(ReflectionException.class);
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) ConnectionConfiguration(org.apache.geode.test.dunit.rules.ConnectionConfiguration) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 48 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project geode by apache.

the class MBeanSecurityJUnitTest method testQueryBean.

/**
   * looks like everyone can query for beans, but the AccessControlMXBean is filtered from the
   * result
   */
@Test
@ConnectionConfiguration(user = "stranger", password = "1234567")
public void testQueryBean() throws MalformedObjectNameException, IOException {
    MBeanServerConnection con = connectionRule.getMBeanServerConnection();
    Set<ObjectInstance> objects = con.queryMBeans(ObjectName.getInstance(ResourceConstants.OBJECT_NAME_ACCESSCONTROL), null);
    // no AccessControlMBean in the query result
    assertThat(objects.size()).isEqualTo(0);
    objects = con.queryMBeans(ObjectName.getInstance("GemFire:service=CacheServer,*"), null);
    assertThat(objects.size()).isEqualTo(1);
}
Also used : ObjectInstance(javax.management.ObjectInstance) MBeanServerConnection(javax.management.MBeanServerConnection) ConnectionConfiguration(org.apache.geode.test.dunit.rules.ConnectionConfiguration) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 49 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project geode by apache.

the class GemFireAuthentication method populateAuthorities.

public static ArrayList<GrantedAuthority> populateAuthorities(JMXConnector jmxc) {
    ObjectName name;
    ArrayList<GrantedAuthority> authorities = new ArrayList<>();
    try {
        name = new ObjectName(PulseConstants.OBJECT_NAME_ACCESSCONTROL_MBEAN);
        MBeanServerConnection mbeanServer = jmxc.getMBeanServerConnection();
        for (String role : PulseConstants.PULSE_ROLES) {
            Object[] params = role.split(":");
            String[] signature = new String[] { String.class.getCanonicalName(), String.class.getCanonicalName() };
            boolean result = (Boolean) mbeanServer.invoke(name, "authorize", params, signature);
            if (result) {
                authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return authorities;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) ObjectName(javax.management.ObjectName) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 50 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project karaf by apache.

the class DeployMojo method deployWithJmx.

protected void deployWithJmx(List<String> locations) throws MojoExecutionException {
    try {
        JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/" + instance);
        ArrayList<String> list = new ArrayList<>();
        if (user != null)
            list.add(user);
        if (password != null)
            list.add(password);
        HashMap env = new HashMap();
        String[] credentials = list.toArray(new String[list.size()]);
        env.put(JMXConnector.CREDENTIALS, credentials);
        JMXConnector jmxConnector = null;
        if (credentials.length > 0)
            jmxConnector = JMXConnectorFactory.connect(jmxServiceURL, env);
        else
            jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
        MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
        for (String location : locations) {
            mBeanServerConnection.invoke(new ObjectName("org.apache.karaf:type=bundle,name=*"), "install", new Object[] { location, true }, new String[] { "java.lang.String", "boolean" });
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Can't deploy using JMX", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) ArrayList(java.util.ArrayList) MBeanServerConnection(javax.management.MBeanServerConnection) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ObjectName(javax.management.ObjectName)

Aggregations

MBeanServerConnection (javax.management.MBeanServerConnection)125 JMXConnector (javax.management.remote.JMXConnector)84 ObjectName (javax.management.ObjectName)73 JMXServiceURL (javax.management.remote.JMXServiceURL)59 JMXConnectorServer (javax.management.remote.JMXConnectorServer)38 Test (org.junit.Test)35 IOException (java.io.IOException)31 MBeanServer (javax.management.MBeanServer)28 HashMap (java.util.HashMap)23 Attribute (javax.management.Attribute)15 NotificationListener (javax.management.NotificationListener)13 MalformedURLException (java.net.MalformedURLException)12 ArrayList (java.util.ArrayList)12 Notification (javax.management.Notification)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Map (java.util.Map)10 List (java.util.List)8 RemoteException (java.rmi.RemoteException)7 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7