2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.filter;
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.CadiWrap;
33 import org.onap.aaf.cadi.Connector;
34 import org.onap.aaf.cadi.CredVal;
35 import org.onap.aaf.cadi.Lur;
36 import org.onap.aaf.cadi.Taf;
37 import org.onap.aaf.cadi.TrustChecker;
38 import org.onap.aaf.cadi.Access.Level;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.config.SecurityInfoC;
41 import org.onap.aaf.cadi.lur.EpiLur;
42 import org.onap.aaf.cadi.taf.HttpTaf;
43 import org.onap.aaf.cadi.taf.TafResp;
44 import org.onap.aaf.cadi.util.UserChainManip;
47 * Encapsulate common HTTP Manipulation Behavior. It will appropriately set
48 * HTTPServletResponse for Redirect or Forbidden, as needed.
50 * Further, this is useful, because it avoids multiple creates of Connections, where some Filters
51 * are created and destroyed regularly.
56 public class CadiHTTPManip {
57 private static final String ACCESS_CADI_CONTROL = ".access|cadi|control";
58 private static final String METH = "OPTIONS";
59 private static final String CADI = "/cadi/";
60 private static final String CADI_CACHE_PRINT = "/cadi/cache/print";
61 private static final String CADI_CACHE_CLEAR = "/cadi/cache/clear";
62 private static final String CADI_LOG_SET = "/cadi/log/set/";
63 private Access access;
67 private String thisPerm,companyPerm,aaf_id;
69 public static final Object[] noAdditional = new Object[0]; // CadiFilter can be created each call in some systems
72 public CadiHTTPManip(Access access, Connector con, TrustChecker tc, Object ... additionalTafLurs) throws CadiException {
75 // Get getter = new AccessGetter(access);
76 Config.setDefaultRealm(access);
78 aaf_id = access.getProperty(Config.CADI_ALIAS,access.getProperty(Config.AAF_APPID, null));
80 access.printf(Level.INIT, "%s is not set. %s can be used instead",Config.AAF_APPID,Config.CADI_ALIAS);
82 access.printf(Level.INIT, "%s is set to %s",Config.AAF_APPID,aaf_id);
84 String ns = aaf_id==null?null:UserChainManip.idToNS(aaf_id);
86 thisPerm = ns+ACCESS_CADI_CONTROL;
87 int dot = ns.indexOf('.');
89 int dot2=ns.indexOf('.',dot+1);
93 companyPerm = ns.substring(0, dot2)+ACCESS_CADI_CONTROL;
95 companyPerm = "com"+ACCESS_CADI_CONTROL;
98 thisPerm = companyPerm = "com"+ACCESS_CADI_CONTROL;
100 SecurityInfoC<HttpURLConnection> si;
101 si = SecurityInfoC.instance(access, HttpURLConnection.class);
102 lur = Config.configLur(si, con, additionalTafLurs);
105 if(lur instanceof EpiLur) {
106 up = ((EpiLur)lur).getUserPassImpl();
107 } else if(lur instanceof CredVal) {
112 taf = Config.configHttpTaf(con,si, tc, up, lur, additionalTafLurs);
116 public TafResp validate(HttpServletRequest hreq, HttpServletResponse hresp, Object state) throws IOException {
117 TafResp tresp = taf.validate(Taf.LifeForm.LFN, hreq, hresp);
118 switch(tresp.isAuthenticated()) {
119 case IS_AUTHENTICATED:
120 access.printf(Level.INFO,"Authenticated: %s from %s:%d",
121 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
123 case TRY_AUTHENTICATING:
124 switch (tresp.authenticate()) {
125 case IS_AUTHENTICATED:
126 access.printf(Level.INFO,"Authenticated: %s from %s:%d",
127 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
129 case HTTP_REDIRECT_INVOKED:
130 access.log(Level.INFO,"Authenticating via redirection: ", tresp.desc());
132 case NO_FURTHER_PROCESSING:
133 access.printf(Level.AUDIT,"Authentication Failure: %s from %s:%d"
134 , tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
135 hresp.sendError(403, tresp.desc()); // Forbidden
139 access.printf(Level.AUDIT,"No TAF will authorize for request from %s:%d"
140 , hreq.getRemoteAddr(), hreq.getRemotePort());
141 hresp.sendError(403, tresp.desc()); // Forbidden
144 case NO_FURTHER_PROCESSING:
145 access.printf(Level.AUDIT,"Authentication Failure: %s from %s:%d",
146 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
147 hresp.sendError(403, "Access Denied"); // FORBIDDEN
150 access.printf(Level.AUDIT,"No TAF will authorize for request from %s:%d"
151 , hreq.getRemoteAddr(), hreq.getRemotePort());
152 hresp.sendError(403, "Access Denied"); // FORBIDDEN
157 public boolean notCadi(CadiWrap req, HttpServletResponse resp) {
159 String pathInfo = req.getPathInfo();
160 if(METH.equalsIgnoreCase(req.getMethod()) && pathInfo!=null && pathInfo.contains(CADI)) {
161 if(req.getUser().equals(aaf_id) || req.isUserInRole(thisPerm) || req.isUserInRole(companyPerm)) {
163 if(pathInfo.contains(CADI_CACHE_PRINT)) {
164 resp.getOutputStream().println(lur.toString());
167 } else if(pathInfo.contains(CADI_CACHE_CLEAR)) {
168 StringBuilder report = new StringBuilder();
169 lur.clear(req.getUserPrincipal(), report);
170 resp.getOutputStream().println(report.toString());
173 } else if(pathInfo.contains(CADI_LOG_SET)) {
175 int slash = pathInfo.lastIndexOf('/');
176 String level = pathInfo.substring(slash+1);
178 l = Level.valueOf(level);
179 access.printf(Level.AUDIT, "%s has set CADI Log Level to '%s'",req.getUser(),l.name());
180 access.setLogLevel(l);
181 } catch (IllegalArgumentException e) {
182 access.printf(Level.AUDIT, "'%s' is not a valid CADI Log Level",level);
186 } catch (IOException e) {
194 public Lur getLur() {
198 public void destroy() {
199 access.log(Level.INFO,"CadiHttpChecker destroyed.");
206 public Access getAccess() {