2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.shiro.web.env;
12 import java.io.FileNotFoundException;
13 import java.util.Collection;
14 import org.apache.shiro.config.Ini;
15 import org.apache.shiro.config.Ini.Section;
16 import org.apache.shiro.web.env.IniWebEnvironment;
17 import org.opendaylight.aaa.shiro.accounting.Accounter;
18 import org.opendaylight.aaa.shiro.authorization.DefaultRBACRules;
19 import org.opendaylight.aaa.shiro.authorization.RBACRule;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Identical to <code>IniWebEnvironment</code> except the Ini is loaded from
25 * <code>$KARAF_HOME/etc/shiro.ini</code>.
27 * @author Ryan Goulding (ryandgoulding@gmail.com)
30 public class KarafIniWebEnvironment extends IniWebEnvironment {
32 private static final Logger LOG = LoggerFactory.getLogger(KarafIniWebEnvironment.class);
33 public static final String DEFAULT_SHIRO_INI_FILE = "etc/shiro.ini";
34 public static final String SHIRO_FILE_PREFIX = "file:/";
36 public KarafIniWebEnvironment() {
41 // Initialize the Shiro environment from etc/shiro.ini then delegate to
45 ini = createDefaultShiroIni();
46 // appendCustomIniRules(ini);
48 } catch (FileNotFoundException e) {
49 final String ERROR_MESSAGE = "Could not find etc/shiro.ini";
50 LOG.error(ERROR_MESSAGE, e);
56 * A hook for installing custom default RBAC rules for security purposes.
60 private void appendCustomIniRules(final Ini ini) {
61 final String INSTALL_MESSAGE = "Installing the RBAC rule: %s";
62 Section urlSection = getOrCreateUrlSection(ini);
63 Collection<RBACRule> rbacRules = DefaultRBACRules.getInstance().getRBACRules();
64 for (RBACRule rbacRule : rbacRules) {
65 urlSection.put(rbacRule.getUrlPattern(), rbacRule.getRolesInShiroFormat());
66 Accounter.output(String.format(INSTALL_MESSAGE, rbacRule));
71 * Extracts the url section of the Ini file, or creates one if it doesn't
77 private Section getOrCreateUrlSection(final Ini ini) {
78 final String URL_SECTION_TITLE = "urls";
79 Section urlSection = ini.getSection(URL_SECTION_TITLE);
80 if (urlSection == null) {
81 LOG.debug("shiro.ini does not contain a [urls] section; creating one");
82 urlSection = ini.addSection(URL_SECTION_TITLE);
84 LOG.debug("shiro.ini contains a [urls] section; appending rules to existing");
91 * @return Ini associated with <code>$KARAF_HOME/etc/shiro.ini</code>
92 * @throws FileNotFoundException
94 static Ini createDefaultShiroIni() throws FileNotFoundException {
95 return createShiroIni(DEFAULT_SHIRO_INI_FILE);
101 * the file path, which is either absolute or relative to
102 * <code>$KARAF_HOME</code>
103 * @return Ini loaded from <code>path</code>
105 static Ini createShiroIni(final String path) throws FileNotFoundException {
106 File f = new File(path);
108 final String fileBasedIniPath = createFileBasedIniPath(f.getAbsolutePath());
109 ini.loadFromPath(fileBasedIniPath);
116 * the file path, which is either absolute or relative to
117 * <code>$KARAF_HOME</code>
118 * @return <code>file:/$KARAF_HOME/etc/shiro.ini</code>
120 static String createFileBasedIniPath(final String path) {
121 String fileBasedIniPath = SHIRO_FILE_PREFIX + path;
122 LOG.debug(fileBasedIniPath);
123 return fileBasedIniPath;