added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / api / config / OTFApiEnforcementFilter.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.api.config;\r
18 \r
19 import com.google.common.base.Strings;\r
20 import java.io.IOException;\r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 import java.util.Map;\r
24 import java.util.TreeMap;\r
25 import javax.servlet.Filter;\r
26 import javax.servlet.FilterChain;\r
27 import javax.servlet.FilterConfig;\r
28 import javax.servlet.ServletException;\r
29 import javax.servlet.ServletRequest;\r
30 import javax.servlet.ServletResponse;\r
31 import javax.servlet.http.HttpServletRequest;\r
32 import javax.servlet.http.HttpServletResponse;\r
33 import org.apache.commons.logging.Log;\r
34 import org.apache.commons.logging.LogFactory;\r
35 import org.onap.aaf.cadi.Access;\r
36 import org.onap.aaf.cadi.Access.Level;\r
37 import org.onap.aaf.cadi.ServletContextAccess;\r
38 import org.onap.aaf.cadi.util.Split;\r
39 \r
40 public class OTFApiEnforcementFilter implements Filter {\r
41   private static final Log log = LogFactory.getLog(OTFApiEnforcementFilter.class);\r
42   private String type;\r
43   private Map<String, List<String>> publicPaths;\r
44   private Access access = null;\r
45 \r
46   public OTFApiEnforcementFilter(Access access, String enforce) throws ServletException {\r
47     this.access = access;\r
48     init(enforce);\r
49   }\r
50 \r
51   @Override\r
52   public void init(FilterConfig fc) throws ServletException {\r
53     init(fc.getInitParameter("aaf_perm_type"));\r
54     // need the Context for Logging, instantiating ClassLoader, etc\r
55     ServletContextAccess sca = new ServletContextAccess(fc);\r
56     if (access == null) {\r
57       access = sca;\r
58     }\r
59   }\r
60 \r
61   private void init(final String ptypes) throws ServletException {\r
62     if (Strings.isNullOrEmpty(ptypes)) {\r
63       throw new ServletException("OTFApiEnforcement requires aaf_perm_type property");\r
64     }\r
65     String[] full = Split.splitTrim(';', ptypes);\r
66     if (full.length <= 0) {\r
67       throw new ServletException("aaf_perm_type property is empty");\r
68     }\r
69 \r
70     type = full[0];\r
71     publicPaths = new TreeMap<>();\r
72     if (full.length > 1) {\r
73       for (int i = 1; i < full.length; ++i) {\r
74         String[] pubArray = Split.split(':', full[i]);\r
75         if (pubArray.length == 2) {\r
76           List<String> ls = publicPaths.get(pubArray[0]);\r
77           if (ls == null) {\r
78             ls = new ArrayList<>();\r
79             publicPaths.put(pubArray[0], ls);\r
80           }\r
81           ls.add(pubArray[1]);\r
82         }\r
83       }\r
84     }\r
85   }\r
86 \r
87   @Override\r
88   public void doFilter(ServletRequest req, ServletResponse resp, FilterChain fc)\r
89       throws IOException, ServletException {\r
90     HttpServletRequest hreq = (HttpServletRequest) req;\r
91     final String meth = hreq.getMethod();\r
92     String path = hreq.getContextPath(); // + hreq.getPathInfo();\r
93 \r
94     if (Strings.isNullOrEmpty(path) || "null".equals(path)) {\r
95       path = hreq.getRequestURI().substring(hreq.getContextPath().length());\r
96     }\r
97 \r
98     List<String> list = publicPaths.get(meth);\r
99     if (list != null) {\r
100       for (String p : publicPaths.get(meth)) {\r
101         if (path.startsWith(p)) {\r
102           access.printf(\r
103               Level.INFO,\r
104               "%s accessed public API %s %s\n",\r
105               hreq.getUserPrincipal().getName(),\r
106               meth,\r
107               path);\r
108           fc.doFilter(req, resp);\r
109           return;\r
110         }\r
111       }\r
112     }\r
113     if (hreq.isUserInRole(type + '|' + path + '|' + meth)) {\r
114       access.printf(\r
115           Level.INFO,\r
116           "%s is allowed access to %s %s\n",\r
117           hreq.getUserPrincipal().getName(),\r
118           meth,\r
119           path);\r
120       fc.doFilter(req, resp);\r
121     } else {\r
122       access.printf(\r
123           Level.AUDIT,\r
124           "%s is denied access to %s %s\n",\r
125           hreq.getUserPrincipal().getName(),\r
126           meth,\r
127           path);\r
128       ((HttpServletResponse) resp).sendError(HttpServletResponse.SC_UNAUTHORIZED);\r
129     }\r
130   }\r
131 \r
132   @Override\r
133   public void destroy() {}\r
134 }\r