AT&T 2.0.19 Code drop, stage 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 Access access;
64         private HttpTaf taf;
65         private CredVal up;
66         private Lur lur;
67         private String thisPerm,companyPerm,aaf_id;
68         
69         public static final Object[] noAdditional = new Object[0]; // CadiFilter can be created each call in some systems
70
71
72         public CadiHTTPManip(Access access, Connector con, TrustChecker tc, Object ... additionalTafLurs) throws CadiException {
73                 synchronized(CADI) {
74                         this.access = access;
75 //                      Get getter = new AccessGetter(access);
76                         Config.setDefaultRealm(access);
77         
78                         aaf_id = access.getProperty(Config.CADI_ALIAS,access.getProperty(Config.AAF_APPID, null));
79                         if(aaf_id==null) {
80                                 access.printf(Level.INIT, "%s is not set. %s can be used instead",Config.AAF_APPID,Config.CADI_ALIAS);
81                         } else {
82                                 access.printf(Level.INIT, "%s is set to %s",Config.AAF_APPID,aaf_id);
83                         }
84                         String ns = aaf_id==null?null:UserChainManip.idToNS(aaf_id);
85                         if(ns!=null) {
86                                 thisPerm = ns+ACCESS_CADI_CONTROL;
87                                 int dot = ns.indexOf('.');
88                                 if(dot>=0) {
89                                         int dot2=ns.indexOf('.',dot+1);
90                                         if(dot2<0) {
91                                                 dot2=dot;
92                                         }
93                                         companyPerm = ns.substring(0, dot2)+ACCESS_CADI_CONTROL;
94                                 } else {
95                                         companyPerm = "com"+ACCESS_CADI_CONTROL;
96                                 }
97                         } else {
98                                 thisPerm = companyPerm = "com"+ACCESS_CADI_CONTROL;
99                         }
100                         SecurityInfoC<HttpURLConnection> si;
101                         si = SecurityInfoC.instance(access, HttpURLConnection.class);
102                         lur = Config.configLur(si, con, additionalTafLurs);
103                         
104                         tc.setLur(lur);
105                         if(lur instanceof EpiLur) {
106                                 up = ((EpiLur)lur).getUserPassImpl();
107                         } else if(lur instanceof CredVal) {
108                                 up = (CredVal)lur;
109                         } else {
110                                 up = null;
111                         }
112                         taf = Config.configHttpTaf(con,si, tc, up, lur, additionalTafLurs);
113                 }
114         }
115
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());
122                                 break;
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());
128                                                 break;
129                                         case HTTP_REDIRECT_INVOKED:
130                                                 access.log(Level.INFO,"Authenticating via redirection: ", tresp.desc());
131                                                 break;
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
136                                                 break;
137
138                                         default:
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
142                                 }
143                                 break;
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
148                                 break;
149                         default:
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
153                 }
154                 return tresp;
155         }
156         
157         public boolean notCadi(CadiWrap req, HttpServletResponse resp) {
158                 
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)) {
162                                 try {
163                                         if(pathInfo.contains(CADI_CACHE_PRINT)) {
164                                                 resp.getOutputStream().println(lur.toString());
165                                                 resp.setStatus(200);
166                                                 return false;
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());
171                                                 resp.setStatus(200);
172                                                 return false;
173                                         } else if(pathInfo.contains(CADI_LOG_SET))  {
174                                                 Level l;
175                                                 int slash = pathInfo.lastIndexOf('/');
176                                                 String level = pathInfo.substring(slash+1);
177                                                 try {
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);
183                                                 }
184                                                 return false;
185                                         }
186                                 } catch (IOException e) {
187                                         access.log(e);
188                                 }
189                         }
190                 }
191                 return true;
192         }
193
194         public Lur getLur() {
195                 return lur;
196         }
197         
198         public void destroy() {
199                 access.log(Level.INFO,"CadiHttpChecker destroyed.");
200                 if(lur!=null) {
201                         lur.destroy();
202                         lur=null;
203                 }
204         }
205
206         public Access getAccess() {
207                 return access;
208         }
209
210 }