aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/main/java/org/onap/ccsdk/apps/cadi/filter/CadiFilter.java
blob: 35d59d52ca5b8e785fd80c786396e803423521e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
 * ============LICENSE_START====================================================
 * org.onap.ccsdk
 * ===========================================================================
 * Copyright (c) 2023 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.ccsdk.apps.cadi.filter;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.onap.ccsdk.apps.cadi.Access;
import org.onap.ccsdk.apps.cadi.Access.Level;
import org.onap.ccsdk.apps.cadi.CadiException;
import org.onap.ccsdk.apps.cadi.CadiWrap;
import org.onap.ccsdk.apps.cadi.LocatorException;
import org.onap.ccsdk.apps.cadi.Lur;
import org.onap.ccsdk.apps.cadi.PropAccess;
import org.onap.ccsdk.apps.cadi.ServletContextAccess;
import org.onap.ccsdk.apps.cadi.TrustChecker;
import org.onap.ccsdk.apps.cadi.config.Config;
import org.onap.ccsdk.apps.cadi.config.Get;
import org.onap.ccsdk.apps.cadi.taf.TafResp;
import org.onap.ccsdk.apps.cadi.taf.TafResp.RESP;
import org.onap.ccsdk.apps.cadi.util.Timing;

/**
 * CadiFilter
 *
 * This class implements Servlet Filter, and ties together CADI implementations
 *
 * This class can be used in a standard J2EE Servlet manner.  Optimal usage is for POJO operations, where
 * one can enforce this Filter being first and primary.  Depending on the Container, it
 * may be more effective, in some cases, to utilize features that allow earlier determination of
 * AUTHN (Authorization).  An example would be "Tomcat Valve".  These implementations, however, should
 * be modeled after the "init" and "doFilter" functions, and be kept up to date as this class changes.
 *
 *
 * @author Jonathan
 *
 */
public class CadiFilter implements Filter {
    private static CadiHTTPManip httpChecker;
    private static String[] pathExceptions;
    private static List<Pair> mapPairs;
    private Access access;
    private Object[] additionalTafLurs;
    private SideChain sideChain;
    private static int count=0;

    public Lur getLur() {
        return httpChecker.getLur();
    }

    /**
     * Construct a viable Filter
     *
     * Due to the vagaries of many containers, there is a tendency to create Objects and call "Init" on
     * them at a later time.  Therefore, this object creates with an object that denies all access
     * until appropriate Init happens, just in case the container lets something slip by in the meantime.
     *
     */
    public CadiFilter() {
        additionalTafLurs = CadiHTTPManip.noAdditional;
    }

    /**
     * This constructor to be used when directly constructing and placing in HTTP Engine
     *
     * @param access
     * @param moreTafLurs
     * @throws ServletException
     */
    public CadiFilter(Access access, Object ... moreTafLurs) throws ServletException {
        additionalTafLurs = moreTafLurs;
        init(new AccessGetter(this.access = access));
    }


    /**
     * Use this to pass in a PreContructed CADI Filter, but with initializing... let Servlet do it
     * @param init
     * @param access
     * @param moreTafLurs
     * @throws ServletException
     */
    public CadiFilter(boolean init, PropAccess access, Object ... moreTafLurs) throws ServletException {
        this.access = access;
        additionalTafLurs = moreTafLurs;
        if (init) {
            init(new AccessGetter(access));
        }
    }

    /**
     * Init
     *
     * Standard Filter "init" call with FilterConfig to obtain properties.  POJOs can construct a
     * FilterConfig with the mechanism of their choice, and standard J2EE Servlet engines utilize this
     * mechanism already.
     */
    //TODO Always validate changes against Tomcat AbsCadiValve and Jaspi CadiSAM Init functions
    public void init(FilterConfig filterConfig) throws ServletException {
        // need the Context for Logging, instantiating ClassLoader, etc
        ServletContextAccess sca=new ServletContextAccess(filterConfig);
        if (access==null) {
            access = sca;
        }

        // Set Protected getter with base Access, for internal class instantiations
        init(new FCGet(access, sca.context(), filterConfig));
    }


    @SuppressWarnings("unchecked")
    protected void init(Get getter) throws ServletException {
       sideChain = new SideChain();
        // Start with the assumption of "Don't trust anyone".
       TrustChecker tc = TrustChecker.NOTRUST; // default position
       try {
           Class<TrustChecker> ctc = (Class<TrustChecker>) Class.forName("org.onap.ccsdk.apps.cadi.aaf.v2_0.AAFTrustChecker");
           if (ctc!=null) {
               Constructor<TrustChecker> contc = ctc.getConstructor(Access.class);
               if (contc!=null) {
                   tc = contc.newInstance(access);
               }
           }
       } catch (Exception e) {
           access.log(Level.INIT, "AAFTrustChecker cannot be loaded",e.getMessage());
       }

       try {
           Class<Filter> cf=null;
           try {
               cf= (Class<Filter>) Class.forName("org.onap.ccsdk.apps.cadi.oauth.OAuthFilter");
               sideChain.add(cf.newInstance());
           } catch (ClassNotFoundException e) {
               access.log(Level.DEBUG, "OAuthFilter not enabled");
           }
       } catch (Exception e) {
           access.log(Level.INIT, "AAFTrustChecker cannot be loaded",e.getMessage());
       }


        // Synchronize, because some instantiations call init several times on the same object
        // In this case, the epiTaf will be changed to a non-NullTaf, and thus not instantiate twice.
        synchronized(CadiHTTPManip.noAdditional /*will always remain same Object*/) {
            ++count;
            if (httpChecker == null) {
                if (access==null) {
                    access = new PropAccess();
                }
                try {
                    httpChecker = new CadiHTTPManip(access,null /*reuseable Con*/,tc, additionalTafLurs);
                } catch (CadiException | LocatorException e1) {
                    throw new ServletException(e1);
                }
            } else if (access==null) {
                access= httpChecker.getAccess();
            }

            /*
             * Setup Authn Path Exceptions
             */
            if (pathExceptions==null) {
                String str = getter.get(Config.CADI_NOAUTHN, null, true);
                if (str!=null) {
                    pathExceptions = str.split("\\s*:\\s*");
                }
            }

            /*
             * SETUP Permission Converters... those that can take Strings from a Vendor Product, and convert to appropriate AAF Permissions
             */
            if (mapPairs==null) {
                String str = getter.get(Config.AAF_PERM_MAP, null, true);
                if (str!=null) {
                    String mstr = getter.get(Config.AAF_PERM_MAP, null, true);
                    if (mstr!=null) {
                        String map[] = mstr.split("\\s*:\\s*");
                        if (map.length>0) {
                            MapPermConverter mpc=null;
                            int idx;
                            mapPairs = new ArrayList<>();
                            for (String entry : map) {
                                if ((idx=entry.indexOf('='))<0) { // it's a Path, so create a new converter
                                    access.log(Level.INIT,"Loading Perm Conversions for:",entry);
                                    mapPairs.add(new Pair(entry,mpc=new MapPermConverter()));
                                } else {
                                    if (mpc!=null) {
                                        mpc.map().put(entry.substring(0,idx),entry.substring(idx+1));
                                    } else {
                                        access.log(Level.ERROR,"cadi_perm_map is malformed; ",entry, "is skipped");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Add API Enforcement Point
        String enforce = getter.get(Config.CADI_API_ENFORCEMENT, null, true);
        if(enforce!=null && enforce.length()>0) {
            sideChain.add(new CadiApiEnforcementFilter(access,enforce));
        }
        // Remove Getter
        getter = Get.NULL;
    }

    /**
     * Containers call "destroy" when time to cleanup
     */
    public void destroy() {
        // Synchronize, in case multiCadiFilters are used.
        synchronized(CadiHTTPManip.noAdditional) {
            if (--count<=0 && httpChecker!=null) {
                httpChecker.destroy();
                httpChecker=null;
                access=null;
                pathExceptions=null;
            }
        }
    }

    /**
     * doFilter
     *
     * This is the standard J2EE invocation.  Analyze the request, modify response as necessary, and
     * only call the next item in the filterChain if request is suitably Authenticated.
     */
    //TODO Always validate changes against Tomcat AbsCadiValve and Jaspi CadiSAM functions
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        final long startAll = System.nanoTime();
        long startCode, startValidate;
        float code=0f, validate=0f;
        String user = "n/a";
        String tag = "";
        TafResp tresp = null;
        try {
            HttpServletRequest hreq = (HttpServletRequest)request;
            if (noAuthn(hreq)) {
                startCode=System.nanoTime();
                chain.doFilter(request, response);
                code = Timing.millis(startCode);
            } else {
                HttpServletResponse hresp = (HttpServletResponse)response;
                startValidate=System.nanoTime();
                tresp = httpChecker.validate(hreq, hresp, hreq);
                validate = Timing.millis(startValidate);
                if (tresp.isAuthenticated()==RESP.IS_AUTHENTICATED) {
                    user = tresp.getPrincipal().personalName();
                    tag = tresp.getPrincipal().tag();
                    CadiWrap cw = new CadiWrap(hreq, tresp, httpChecker.getLur(),getConverter(hreq));
                    if (httpChecker.notCadi(cw, hresp)) {
                        startCode=System.nanoTime();
                        sideChain.doFilter(cw,response,chain);
                        code = Timing.millis(startCode);
                    }
                }
            }
        } catch (ClassCastException e) {
            throw new ServletException("CadiFilter expects Servlet to be an HTTP Servlet",e);
        } finally {
            if (tresp != null) {
                access.printf(Level.INFO, "Trans: user=%s[%s],ip=%s,ms=%f,validate=%f,code=%f,result=%s",
                    user,tag,request.getRemoteAddr(),
                    Timing.millis(startAll),validate,code,tresp.isAuthenticated().toString());
            } else {
                access.printf(Level.INFO, "Trans: user=%s[%s],ip=%s,ms=%f,validate=%f,code=%f,result=FAIL",
                    user,tag,request.getRemoteAddr(),
                    Timing.millis(startAll),validate,code);
            }
        }
    }


    /**
     * If PathExceptions exist, report if these should not have Authn applied.
     * @param hreq
     * @return
     */
    private boolean noAuthn(HttpServletRequest hreq) {
        if (pathExceptions!=null) {
            String pi = hreq.getPathInfo();
            if (pi==null) {
                // Attempt to get from URI only  (Daniel Rose)
                pi = hreq.getRequestURI().substring(hreq.getContextPath().length());
                if(pi==null) {
                    // Nothing works.
                    return false; // JBoss sometimes leaves null
                }
            }
            for (String pe : pathExceptions) {
                if (pi.startsWith(pe))return true;
            }
        }
        return false;
    }

    /**
     * Get Converter by Path
     */
    private PermConverter getConverter(HttpServletRequest hreq) {
        if (mapPairs!=null) {
            String pi = hreq.getPathInfo();
            if (pi !=null) {
                for (Pair p: mapPairs) {
                    if (pi.startsWith(p.name))return p.pc;
                }
            }
        }
        return NullPermConverter.singleton();
    }

    /**
     * store PermConverters by Path prefix
     * @author Jonathan
     *
     */
    private class Pair {
        public Pair(String key, PermConverter pc) {
            name = key;
            this.pc = pc;
        }
        public String name;
        public PermConverter pc;
    }

}