Clean up Sonar results 2
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / filter / CadiHTTPManip.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.filter;
23
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
45
46 /**
47  * Encapsulate common HTTP Manipulation Behavior.  It will appropriately set
48  * HTTPServletResponse for Redirect or Forbidden, as needed.
49  * 
50  * Further, this is useful, because it avoids multiple creates of Connections, where some Filters
51  * are created and destroyed regularly.
52  * 
53  * @author Jonathan
54  *
55  */
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;
65         private HttpTaf taf;
66         private CredVal up;
67         private Lur lur;
68         private String thisPerm,companyPerm,aaf_id;
69         
70         public static final Object[] noAdditional = new Object[0]; // CadiFilter can be created each call in some systems
71
72
73         public CadiHTTPManip(Access access, Connector con, TrustChecker tc, Object ... additionalTafLurs) throws CadiException {
74                 synchronized(LOCK) {
75                         this.access = access;
76 //                      Get getter = new AccessGetter(access);
77                         Config.setDefaultRealm(access);
78         
79                         aaf_id = access.getProperty(Config.CADI_ALIAS,access.getProperty(Config.AAF_APPID, null));
80                         if(aaf_id==null) {
81                                 access.printf(Level.INIT, "%s is not set. %s can be used instead",Config.AAF_APPID,Config.CADI_ALIAS);
82                         } else {
83                                 access.printf(Level.INIT, "%s is set to %s",Config.AAF_APPID,aaf_id);
84                         }
85                         String ns = aaf_id==null?null:UserChainManip.idToNS(aaf_id);
86                         if(ns!=null) {
87                                 thisPerm = ns+ACCESS_CADI_CONTROL;
88                                 int dot = ns.indexOf('.');
89                                 if(dot>=0) {
90                                         int dot2=ns.indexOf('.',dot+1);
91                                         if(dot2<0) {
92                                                 dot2=dot;
93                                         }
94                                         companyPerm = ns.substring(0, dot2)+ACCESS_CADI_CONTROL;
95                                 } else {
96                                         companyPerm = "com"+ACCESS_CADI_CONTROL;
97                                 }
98                         } else {
99                                 thisPerm = companyPerm = "com"+ACCESS_CADI_CONTROL;
100                         }
101                         SecurityInfoC<HttpURLConnection> si;
102                         si = SecurityInfoC.instance(access, HttpURLConnection.class);
103                         lur = Config.configLur(si, con, additionalTafLurs);
104                         
105                         tc.setLur(lur);
106                         if(lur instanceof EpiLur) {
107                                 up = ((EpiLur)lur).getUserPassImpl();
108                         } else if(lur instanceof CredVal) {
109                                 up = (CredVal)lur;
110                         } else {
111                                 up = null;
112                         }
113                         taf = Config.configHttpTaf(con,si, tc, up, lur, additionalTafLurs);
114                 }
115         }
116
117         public TafResp validate(HttpServletRequest hreq, HttpServletResponse hresp, Object state) throws IOException {
118                 TafResp tresp = taf.validate(Taf.LifeForm.LFN, hreq, hresp);
119                 switch(tresp.isAuthenticated()) {
120                         case IS_AUTHENTICATED:
121                                 access.printf(Level.INFO,"Authenticated: %s from %s:%d", 
122                                                 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
123                                 break;
124                         case TRY_AUTHENTICATING:
125                                 switch (tresp.authenticate()) {
126                                         case IS_AUTHENTICATED:
127                                                 access.printf(Level.INFO,"Authenticated: %s from %s:%d", 
128                                                                 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
129                                                 break;
130                                         case HTTP_REDIRECT_INVOKED:
131                                                 access.log(Level.INFO,"Authenticating via redirection: ", tresp.desc());
132                                                 break;
133                                         case NO_FURTHER_PROCESSING:
134                                                 access.printf(Level.AUDIT,"Authentication Failure: %s from %s:%d"
135                                                                 , tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
136                                                 hresp.sendError(403, tresp.desc()); // Forbidden
137                                                 break;
138
139                                         default:
140                                                 access.printf(Level.AUDIT,"No TAF will authorize for request from %s:%d"
141                                                                 , hreq.getRemoteAddr(), hreq.getRemotePort());
142                                                 hresp.sendError(403, tresp.desc()); // Forbidden
143                                 }
144                                 break;
145                         case NO_FURTHER_PROCESSING:
146                                 access.printf(Level.AUDIT,"Authentication Failure: %s from %s:%d", 
147                                                 tresp.desc(), hreq.getRemoteAddr(), hreq.getRemotePort());
148                                 hresp.sendError(403, "Access Denied"); // FORBIDDEN
149                                 break;
150                         default:
151                                 access.printf(Level.AUDIT,"No TAF will authorize for request from %s:%d"
152                                                 , hreq.getRemoteAddr(), hreq.getRemotePort());
153                                 hresp.sendError(403, "Access Denied"); // FORBIDDEN
154                 }
155                 return tresp;
156         }
157         
158         public boolean notCadi(CadiWrap req, HttpServletResponse resp) {
159                 
160                 String pathInfo = req.getPathInfo();
161                 if(METH.equalsIgnoreCase(req.getMethod()) && pathInfo!=null && pathInfo.contains(CADI)) {
162                         if(req.getUser().equals(aaf_id) || req.isUserInRole(thisPerm) || req.isUserInRole(companyPerm)) {
163                                 try {
164                                         if(pathInfo.contains(CADI_CACHE_PRINT)) {
165                                                 resp.getOutputStream().println(lur.toString());
166                                                 resp.setStatus(200);
167                                                 return false;
168                                         } else if(pathInfo.contains(CADI_CACHE_CLEAR)) {
169                                                 StringBuilder report = new StringBuilder();
170                                                 lur.clear(req.getUserPrincipal(), report);
171                                                 resp.getOutputStream().println(report.toString());
172                                                 resp.setStatus(200);
173                                                 return false;
174                                         } else if(pathInfo.contains(CADI_LOG_SET))  {
175                                                 Level l;
176                                                 int slash = pathInfo.lastIndexOf('/');
177                                                 String level = pathInfo.substring(slash+1);
178                                                 try {
179                                                         l = Level.valueOf(level);
180                                                         access.printf(Level.AUDIT, "%s has set CADI Log Level to '%s'",req.getUser(),l.name());
181                                                         access.setLogLevel(l);
182                                                 } catch (IllegalArgumentException e) {
183                                                         access.printf(Level.AUDIT, "'%s' is not a valid CADI Log Level",level);
184                                                 }
185                                                 return false;
186                                         }
187                                 } catch (IOException e) {
188                                         access.log(e);
189                                 }
190                         }
191                 }
192                 return true;
193         }
194
195         public Lur getLur() {
196                 return lur;
197         }
198         
199         public void destroy() {
200                 access.log(Level.INFO,"CadiHttpChecker destroyed.");
201                 if(lur!=null) {
202                         lur.destroy();
203                         lur=null;
204                 }
205         }
206
207         public Access getAccess() {
208                 return access;
209         }
210
211 }