summaryrefslogtreecommitdiff
path: root/java/broker-plugins/management-jmx/src/test/java/org/apache/qpid/server/jmx/ManagementLogActorTest.java
blob: c1df9afc5d3c28e7d6644f6c01079f96afc1074e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.server.jmx;

import java.util.HashMap;
import java.util.Map;

import javax.management.JMException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.qpid.server.configuration.ServerConfiguration;
import org.apache.qpid.server.configuration.plugins.ConfigurationPlugin;
import org.apache.qpid.server.logging.actors.CurrentActor;
import org.apache.qpid.server.registry.ApplicationRegistry;
import org.apache.qpid.server.security.Result;
import org.apache.qpid.server.security.SecurityPlugin;
import org.apache.qpid.server.security.access.ObjectProperties;
import org.apache.qpid.server.security.access.ObjectType;
import org.apache.qpid.server.security.access.Operation;
import org.apache.qpid.server.util.TestApplicationRegistry;
import org.apache.qpid.test.utils.QpidTestCase;

public class ManagementLogActorTest extends QpidTestCase
{
    private ApplicationRegistry _registry;
    private JMXManagedObjectRegistry _objectRegistry;
    private int _registryPort;
    private int _connectorPort;
    private TestPlugin _plugin;

    @Override
    public void setUp() throws Exception
    {
        super.setUp();

        _registryPort = findFreePort();
        _connectorPort = getNextAvailable(_registryPort + 1);
        XMLConfiguration config = new XMLConfiguration();
        config.addProperty(ServerConfiguration.MGMT_JMXPORT_REGISTRYSERVER, _registryPort + "");
        config.addProperty(ServerConfiguration.MGMT_JMXPORT_CONNECTORSERVER, _connectorPort + "");
        _registry = new TestApplicationRegistry(new ServerConfiguration(config));
        ApplicationRegistry.initialise(_registry);

        _plugin = new TestPlugin();
        _registry.getSecurityManager().addHostPlugin(_plugin);

        _objectRegistry = new JMXManagedObjectRegistry();
        new TestMBean(_objectRegistry);
        _objectRegistry.start();
    }

    public void tearDown() throws Exception
    {
        _objectRegistry.close();
        ApplicationRegistry.remove();
        super.tearDown();
    }

    public void testPrincipalInLogMessage() throws Throwable
    {
        Map<String, Object> environment = new HashMap<String, Object>();
        environment.put(JMXConnector.CREDENTIALS, new String[] { "admin", "admin" });
        String urlString = "service:jmx:rmi:///jndi/rmi://localhost:" + _registryPort + "/jmxrmi";
        JMXServiceURL url = new JMXServiceURL(urlString);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(url, environment);
        MBeanServerConnection mbsc = jmxConnector.getMBeanServerConnection();
        ObjectName mbeanObject = new ObjectName("org.apache.qpid:type=TestMBean,name=test");

        String actorLogMessage = (String) mbsc.getAttribute(mbeanObject, "ActorLogMessage");

        assertTrue("Unexpected log principal in security plugin", _plugin.getLogMessage().startsWith("[mng:admin"));
        assertTrue("Unexpected log principal in MBean", actorLogMessage.startsWith("[mng:admin"));
    }

    public static class TestMBean extends DefaultManagedObject implements CurrentActorRetriever
    {

        public TestMBean(ManagedObjectRegistry registry) throws JMException
        {
            super(CurrentActorRetriever.class, "TestMBean", registry);
            register();
        }

        @Override
        public String getObjectInstanceName()
        {
            return "test";
        }

        @Override
        public ManagedObject getParentObject()
        {
            return null;
        }

        @Override
        public String getActorLogMessage()
        {
            return CurrentActor.get().getLogMessage();
        }

    }

    public static interface CurrentActorRetriever
    {
        String getActorLogMessage();
    }

    public static class TestPlugin implements SecurityPlugin
    {
        private String _logMessage;

        @Override
        public void configure(ConfigurationPlugin config) throws ConfigurationException
        {
        }

        @Override
        public Result getDefault()
        {
            return Result.ALLOWED;
        }

        @Override
        public Result access(ObjectType objectType, Object instance)
        {
            return Result.ALLOWED;
        }

        @Override
        public Result authorise(Operation operation, ObjectType objectType, ObjectProperties properties)
        {
            // set thread name to work around logic in MangementActor
            Thread.currentThread().setName("RMI TCP Connection(1)-" + System.currentTimeMillis());
            _logMessage = CurrentActor.get().getLogMessage();
            return Result.ALLOWED;
        }

        public String getLogMessage()
        {
            return _logMessage;
        }

    }

}