acf4022c38a3af74db38b024e2a2472417b88815
[moon.git] /
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.aaa.shiro.web.env;
10
11 import java.io.File;
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;
22
23 /**
24  * Identical to <code>IniWebEnvironment</code> except the Ini is loaded from
25  * <code>$KARAF_HOME/etc/shiro.ini</code>.
26  *
27  * @author Ryan Goulding (ryandgoulding@gmail.com)
28  *
29  */
30 public class KarafIniWebEnvironment extends IniWebEnvironment {
31
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:/";
35
36     public KarafIniWebEnvironment() {
37     }
38
39     @Override
40     public void init() {
41         // Initialize the Shiro environment from etc/shiro.ini then delegate to
42         // the parent class
43         Ini ini;
44         try {
45             ini = createDefaultShiroIni();
46             // appendCustomIniRules(ini);
47             setIni(ini);
48         } catch (FileNotFoundException e) {
49             final String ERROR_MESSAGE = "Could not find etc/shiro.ini";
50             LOG.error(ERROR_MESSAGE, e);
51         }
52         super.init();
53     }
54
55     /**
56      * A hook for installing custom default RBAC rules for security purposes.
57      *
58      * @param ini
59      */
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));
67         }
68     }
69
70     /**
71      * Extracts the url section of the Ini file, or creates one if it doesn't
72      * already exist
73      *
74      * @param ini
75      * @return
76      */
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);
83         } else {
84             LOG.debug("shiro.ini contains a [urls] section; appending rules to existing");
85         }
86         return urlSection;
87     }
88
89     /**
90      *
91      * @return Ini associated with <code>$KARAF_HOME/etc/shiro.ini</code>
92      * @throws FileNotFoundException
93      */
94     static Ini createDefaultShiroIni() throws FileNotFoundException {
95         return createShiroIni(DEFAULT_SHIRO_INI_FILE);
96     }
97
98     /**
99      *
100      * @param path
101      *            the file path, which is either absolute or relative to
102      *            <code>$KARAF_HOME</code>
103      * @return Ini loaded from <code>path</code>
104      */
105     static Ini createShiroIni(final String path) throws FileNotFoundException {
106         File f = new File(path);
107         Ini ini = new Ini();
108         final String fileBasedIniPath = createFileBasedIniPath(f.getAbsolutePath());
109         ini.loadFromPath(fileBasedIniPath);
110         return ini;
111     }
112
113     /**
114      *
115      * @param path
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>
119      */
120     static String createFileBasedIniPath(final String path) {
121         String fileBasedIniPath = SHIRO_FILE_PREFIX + path;
122         LOG.debug(fileBasedIniPath);
123         return fileBasedIniPath;
124     }
125 }