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 static final Object LOCK = new Object();
64 private Access access;
68 private String thisPerm,companyPerm,aaf_id;
70 public static final Object[] noAdditional = new Object[0]; // CadiFilter can be created each call in some systems
73 public CadiHTTPManip(Access access, Connector con, TrustChecker tc, Object ... additionalTafLurs) throws CadiException {
76 // Get getter = new AccessGetter(access);
77 Config.setDefaultRealm(access);
79 aaf_id = access.getProperty(Config.CADI_ALIAS,access.getProperty(Config.AAF_APPID, null));
81 access.printf(Level.INIT, "%s is not set. %s can be used instead",Config.AAF_APPID,Config.CADI_ALIAS);
83 access.printf(Level.INIT, "%s is set to %s",Config.AAF_APPID,aaf_id);
85 String ns = aaf_id==null?null:UserChainManip.idToNS(aaf_id);
87 thisPerm = ns+ACCESS_CADI_CONTROL;
88 int dot = ns.indexOf('.');
90 int dot2=ns.indexOf('.',dot+1);
94 companyPerm = ns.substring(0, dot2)+ACCESS_CADI_CONTROL;
96 companyPerm = "com"+ACCESS_CADI_CONTROL;
99 thisPerm = companyPerm = "com"+ACCESS_CADI_CONTROL;
101 SecurityInfoC<HttpURLConnection> si;
102 si = SecurityInfoC.instance(access, HttpURLConnection.class);
104 lur = Config.configLur(si, con, additionalTafLurs);
107 if(lur instanceof EpiLur) {
108 up = ((EpiLur)lur).getUserPassImpl();
109 } else if(lur instanceof CredVal) {
114 taf = Config.configHttpTaf(con,si, tc, up, lur, additionalTafLurs);
118 public TafResp validate(HttpServletRequest hreq, HttpServletResponse hresp, Object state) throws IOException {
119 TafResp tresp = taf.validate(Taf.LifeForm.LFN, hreq, hresp);
120 switch(tresp.isAuthenticated()) {
121 case IS_AUTHENTICATED:
122 access.printf(Level.INFO,"Authenticated: %s from %s:%d",
123 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
125 case TRY_AUTHENTICATING:
126 switch (tresp.authenticate()) {
127 case IS_AUTHENTICATED:
128 access.printf(Level.INFO,"Authenticated: %s from %s:%d",
129 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
131 case HTTP_REDIRECT_INVOKED:
132 access.log(Level.INFO,"Authenticating via redirection: ", tresp.desc());
134 case NO_FURTHER_PROCESSING:
135 access.printf(Level.AUDIT,"Authentication Failure: %s from %s:%d"
136 , tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
137 hresp.sendError(403, tresp.desc()); // Forbidden
141 access.printf(Level.AUDIT,"No TAF will authorize for request from %s:%d"
142 , hreq.getRemoteAddr(), hreq.getRemotePort());
143 hresp.sendError(403, tresp.desc()); // Forbidden
146 case NO_FURTHER_PROCESSING:
147 access.printf(Level.AUDIT,"Authentication Failure: %s from %s:%d",
148 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
149 hresp.sendError(403, "Access Denied"); // FORBIDDEN
152 access.printf(Level.AUDIT,"No TAF will authorize for request from %s:%d"
153 , hreq.getRemoteAddr(), hreq.getRemotePort());
154 hresp.sendError(403, "Access Denied"); // FORBIDDEN
159 public boolean notCadi(CadiWrap req, HttpServletResponse resp) {
161 String pathInfo = req.getPathInfo();
162 if(METH.equalsIgnoreCase(req.getMethod()) && pathInfo!=null && pathInfo.contains(CADI)) {
163 if(req.getUser().equals(aaf_id) || req.isUserInRole(thisPerm) || req.isUserInRole(companyPerm)) {
165 if(pathInfo.contains(CADI_CACHE_PRINT)) {
166 resp.getOutputStream().println(lur.toString());
169 } else if(pathInfo.contains(CADI_CACHE_CLEAR)) {
170 StringBuilder report = new StringBuilder();
171 lur.clear(req.getUserPrincipal(), report);
172 resp.getOutputStream().println(report.toString());
175 } else if(pathInfo.contains(CADI_LOG_SET)) {
177 int slash = pathInfo.lastIndexOf('/');
178 String level = pathInfo.substring(slash+1);
180 l = Level.valueOf(level);
181 access.printf(Level.AUDIT, "%s has set CADI Log Level to '%s'",req.getUser(),l.name());
182 access.setLogLevel(l);
183 } catch (IllegalArgumentException e) {
184 access.printf(Level.AUDIT, "'%s' is not a valid CADI Log Level",level);
188 } catch (IOException e) {
196 public Lur getLur() {
200 public void destroy() {
201 access.log(Level.INFO,"CadiHttpChecker destroyed.");
208 public Access getAccess() {