summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/security/access/ACLManager.java
blob: 539f32a73246c0ef93f8b572f26c3d78c5a3d7e3 (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
/*
 *  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.security.access;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.qpid.server.registry.ApplicationRegistry;
import org.apache.qpid.server.security.access.plugins.DenyAll;
import org.apache.qpid.configuration.PropertyUtils;
import org.apache.log4j.Logger;

import java.util.List;
import java.lang.reflect.Method;

public class ACLManager
{
    private static final Logger _logger = Logger.getLogger(ACLManager.class);

    public static ACLPlugin loadACLManager(String name, Configuration hostConfig) throws ConfigurationException
    {
        ACLPlugin aclPlugin = ApplicationRegistry.getInstance().getAccessManager();

        if (hostConfig == null)
        {
            _logger.warn("No Configuration specified. Using default ACLPlugin '" + aclPlugin.getPluginName()
                         + "' for VirtualHost:'" + name + "'");
            return aclPlugin;
        }

        String accessClass = hostConfig.getString("security.access.class");
        if (accessClass == null)
        {

            _logger.warn("No ACL Plugin specified. Using default ACL Plugin '" + aclPlugin.getPluginName() +
                         "' for VirtualHost:'" + name + "'");
            return aclPlugin;
        }

        Object o;
        try
        {
            o = Class.forName(accessClass).newInstance();
        }
        catch (Exception e)
        {
            throw new ConfigurationException("Error initialising ACL: " + e, e);
        }

        if (!(o instanceof ACLPlugin))
        {
            throw new ConfigurationException("ACL Plugins must implement the ACLPlugin interface");
        }

        initialiseAccessControl((ACLPlugin) o, hostConfig);

        aclPlugin = getManager((ACLPlugin) o);
        if (_logger.isInfoEnabled())
        {
            _logger.info("Initialised ACL Plugin '" + aclPlugin.getPluginName()
                         + "' for virtualhost '" + name + "' successfully");
        }

        return aclPlugin;
    }


    private static void initialiseAccessControl(ACLPlugin accessManager, Configuration config)
            throws ConfigurationException
    {
        //First provide the ACLPlugin with the host configuration

        accessManager.setConfiguaration(config);

        //Provide additional attribute customisation.        
        String baseName = "security.access.attributes.attribute.";
        List<String> argumentNames = config.getList(baseName + "name");
        List<String> argumentValues = config.getList(baseName + "value");
        for (int i = 0; i < argumentNames.size(); i++)
        {
            String argName = argumentNames.get(i);
            if (argName == null || argName.length() == 0)
            {
                throw new ConfigurationException("Access Control argument names must have length >= 1 character");
            }
            if (Character.isLowerCase(argName.charAt(0)))
            {
                argName = Character.toUpperCase(argName.charAt(0)) + argName.substring(1);
            }
            String methodName = "set" + argName;
            Method method = null;
            try
            {
                method = accessManager.getClass().getMethod(methodName, String.class);
            }
            catch (NoSuchMethodException e)
            {
                //do nothing as method will be null
            }

            if (method == null)
            {
                throw new ConfigurationException("No method " + methodName + " found in class " + accessManager.getClass() +
                                                 " hence unable to configure access control. The method must be public and " +
                                                 "have a single String argument with a void return type");
            }
            try
            {
                method.invoke(accessManager, PropertyUtils.replaceProperties(argumentValues.get(i)));
            }
            catch (Exception e)
            {
                ConfigurationException ce = new ConfigurationException(e.getMessage(), e.getCause());
                ce.initCause(e);
                throw ce;
            }
        }
    }


    private static ACLPlugin getManager(ACLPlugin manager)
    {
        if (manager == null)
        {
            if (ApplicationRegistry.getInstance().getAccessManager() == null)
            {
                return new DenyAll();
            }
            else
            {
                return ApplicationRegistry.getInstance().getAccessManager();
            }
        }
        else
        {
            return manager;
        }
    }

    public static Logger getLogger()
    {
        return _logger;
    }
}