/
KapuaBrokerSecurityPlugin.java
71 lines (61 loc) · 2.5 KB
/
KapuaBrokerSecurityPlugin.java
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
/*******************************************************************************
* Copyright (c) 2011, 2018 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
* Red Hat Inc
*******************************************************************************/
package org.eclipse.kapua.broker.core;
import java.io.InputStream;
import java.net.URL;
import org.eclipse.kapua.broker.core.plugin.KapuaSecurityBrokerFilter;
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerPlugin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Install {@link KapuaSecurityBrokerFilter} into activeMQ filter chain plugin.<BR>
* <p>
* Is called by activeMQ broker by configuring plugin tag inside broker tag into activemq.xml.<BR>
* <BR>
* <BR>
* <p>
* <pre>
* <plugins>
* <bean xmlns="http://www.springframework.org/schema/beans" id="kapuaFilter" class="org.eclipse.kapua.broker.core.KapuaSecurityBrokerFilter"/>
* </plugins>
* </pre>
*
* @since 1.0
*/
public class KapuaBrokerSecurityPlugin implements BrokerPlugin {
private static final Logger logger = LoggerFactory.getLogger(KapuaBrokerSecurityPlugin.class);
@Override
public Broker installPlugin(final Broker broker) throws Exception {
logger.info("Installing Kapua broker plugin...");
try {
// initialize shiro context for broker plugin from shiro ini file
final URL shiroIniUrl = getClass().getResource("/shiro.ini");
Ini shiroIni = new Ini();
try (final InputStream input = shiroIniUrl.openStream()) {
shiroIni.load(input);
}
SecurityManager securityManager = new IniSecurityManagerFactory(shiroIni).getInstance();
SecurityUtils.setSecurityManager(securityManager);
// install the filters
return new KapuaSecurityBrokerFilter(broker);
} catch (Throwable t) {
logger.error("Error in plugin installation.", t);
throw new SecurityException(t);
}
}
}