6f4d5cc728d8b0e1643173ed3ecdb2ae11698ff7
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / CadiWrap.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;
23
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletRequestWrapper;
30
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.filter.NullPermConverter;
33 import org.onap.aaf.cadi.filter.PermConverter;
34 import org.onap.aaf.cadi.lur.EpiLur;
35 import org.onap.aaf.cadi.principal.TaggedPrincipal;
36 import org.onap.aaf.cadi.taf.TafResp;
37 import org.onap.aaf.cadi.util.Timing;
38
39
40
41 /**
42  * Inherit the HttpServletRequestWrapper, which calls methods of delegate it's created with, but
43  * overload the key security mechanisms with CADI mechanisms
44  * 
45  * This works with mechanisms working strictly with HttpServletRequest (i.e. Servlet Filters)
46  * 
47  * Specialty cases, i.e. Tomcat, which for their containers utilize their own mechanisms and Wrappers, you may
48  * need something similar.  See AppServer specific code (i.e. tomcat) for these.
49  * 
50  * @author Jonathan
51  *
52  */
53 public class CadiWrap extends HttpServletRequestWrapper implements HttpServletRequest, BasicCred {
54         private TaggedPrincipal principal;
55         private Lur lur;
56         private String user; // used to set user/pass from brain-dead protocols like WSSE 
57         private byte[] password;
58         private PermConverter pconv;
59         private Access access; 
60         
61         /**
62          * Standard Wrapper constructor for Delegate pattern
63          * @param request
64          */
65         public CadiWrap(HttpServletRequest request, TafResp tafResp, Lur lur) {
66                 super(request);
67                 principal = tafResp.getPrincipal();
68                 access = tafResp.getAccess();
69                 this.lur = lur;
70                 pconv = NullPermConverter.singleton();
71         }
72
73         /**
74          * Standard Wrapper constructor for Delegate pattern, with PermConverter
75          * @param request
76          */
77         public CadiWrap(HttpServletRequest request, TafResp tafResp, Lur lur, PermConverter pc) {
78                 super(request);
79                 principal = tafResp.getPrincipal();
80                 access = tafResp.getAccess();
81                 this.lur = lur;
82                 pconv = pc;
83         }
84
85
86         /**
87          * Part of the HTTP Security API.  Declare the User associated with this HTTP Transaction.
88          * CADI does this by reporting the name associated with the Principal obtained, if any.
89          */
90         @Override
91         public String getRemoteUser() {
92                 return principal==null?null:principal.getName();
93         }
94
95         /**
96          * Part of the HTTP Security API.  Return the User Principal associated with this HTTP 
97          * Transaction.
98          */
99         @Override
100         public Principal getUserPrincipal() {
101                 return principal;
102         }
103         
104         /**
105          * This is the key API call for AUTHZ in J2EE.  Given a Role (String passed in), is the user
106          * associated with this HTTP Transaction allowed to function in this Role?
107          * 
108          * For CADI, we pass the responsibility for determining this to the "LUR", which may be
109          * determined by the Enterprise.
110          * 
111          * Note: Role check is also done in "CadiRealm" in certain cases...
112          * 
113          *
114          */
115         @Override
116         public boolean isUserInRole(String perm) {
117                 return perm==null?false:checkPerm(access,"isUserInRole",principal,pconv,lur,perm);
118         }
119         
120         public static boolean checkPerm(Access access, String caller, Principal principal, PermConverter pconv, Lur lur, String perm) {
121                 if(principal== null) {
122                         access.log(Level.AUDIT,caller, "No Principal in Transaction");
123                         return false;
124                 } else { 
125                         final long start = System.nanoTime();
126                         perm = pconv.convert(perm);
127                         if(lur.fish(principal,lur.createPerm(perm))) {
128                                 access.printf(Level.DEBUG,"%s: %s has %s, %f ms", caller, principal.getName(), perm, Timing.millis(start));
129                                 return true;
130                         } else {
131                                 access.printf(Level.DEBUG,"%s: %s does not have %s, %f ms", caller, principal.getName(), perm, Timing.millis(start));
132                                 return false;
133                         }
134                 }
135
136         }
137
138         /** 
139          * CADI Function (Non J2EE standard). GetPermissions will read the Permissions from AAF (if configured) and Roles from Local Lur, etc
140          *  as implemented with lur.fishAll
141          *  
142          *  To utilize, the Request must be a "CadiWrap" object, then call.
143          */
144         public List<Permission> getPermissions(Principal p) {
145                 List<Permission> perms = new ArrayList<>();
146                 lur.fishAll(p, perms);
147                 return perms;
148         }
149         /**
150          * Allow setting of tafResp and lur after construction
151          * 
152          * This can happen if the CadiWrap is constructed in a Valve other than CadiValve
153          */
154         public void set(TafResp tafResp, Lur lur) {
155                 principal = tafResp.getPrincipal();
156                 access = tafResp.getAccess();
157                 this.lur = lur;
158         }
159
160         public String getUser() {
161                 if(user==null && principal!=null) {
162                         user = principal.getName();
163                 }
164                 return user;
165         }
166
167         public byte[] getCred() {
168                 return password;
169         }
170
171         public void setUser(String user) {
172                 this.user = user;
173         }
174
175         public void setCred(byte[] passwd) {
176                 password = passwd;
177         }
178         
179         public CadiWrap setPermConverter(PermConverter pc) {
180                 pconv = pc;
181                 return this;
182         }
183         
184         // Add a feature
185         public void invalidate(String id) {
186                 if(lur instanceof EpiLur) {
187                         ((EpiLur)lur).remove(id);
188                 } else if(lur instanceof CachingLur) {
189                         ((CachingLur<?>)lur).remove(id);
190                 }
191         }
192         
193         public Lur getLur() {
194                 return lur;
195         }
196         
197         public Access access() {
198                 return access;
199         }
200 }