Improved multi Proxy DNSLocator based
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / env / AuthzTransFilter.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.auth.env;
23
24 import java.security.Principal;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.onap.aaf.auth.rserv.TransFilter;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.Connector;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.TrustChecker;
34 import org.onap.aaf.cadi.principal.TaggedPrincipal;
35 import org.onap.aaf.cadi.principal.TrustPrincipal;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.LogTarget;
38 import org.onap.aaf.misc.env.Slot;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans.Metric;
41
42 public class AuthzTransFilter extends TransFilter<AuthzTrans> {
43     private AuthzEnv env;
44     public Metric serviceMetric;
45     public static Slot transIDslot,specialLogSlot;
46
47     public static final String TRANS_ID_SLOT = "TRANS_ID_SLOT";
48     public static final String SPECIAL_LOG_SLOT = "SPECIAL_LOG_SLOT";
49
50     public static final int BUCKETSIZE = 2;
51     
52     public AuthzTransFilter(AuthzEnv env, Connector con, TrustChecker tc, Object ... additionalTafLurs) throws CadiException, LocatorException {
53         super(env.access(),con, tc, additionalTafLurs);
54         this.env = env;
55         serviceMetric = new Metric();
56         serviceMetric.buckets = new float[BUCKETSIZE];
57         if (transIDslot==null) {
58             transIDslot = env.slot(TRANS_ID_SLOT);
59         }
60         if (specialLogSlot==null) {
61             specialLogSlot = env.slot(SPECIAL_LOG_SLOT);
62         }
63     }
64     
65     @Override
66     protected AuthzTrans newTrans(HttpServletRequest req, HttpServletResponse resp) {
67         AuthzTrans at = env.newTrans();
68         at.setLur(getLur());
69         at.set(req,resp);
70         return at;
71     }
72
73     @Override
74     protected TimeTaken start(AuthzTrans trans) {
75         return trans.start("Trans " + //(context==null?"n/a":context.toString()) +
76         " IP: " + trans.ip() +
77         " Port: " + trans.port()
78         , Env.SUB);
79     }
80
81     @Override
82     protected void authenticated(AuthzTrans trans, Principal p) {
83         trans.setUser((TaggedPrincipal)p); // We only work with TaggedPrincipals in Authz
84     }
85
86     @Override
87     protected void tallyHo(AuthzTrans trans, String target) {
88         Boolean b = trans.get(specialLogSlot, false);
89         LogTarget lt = b?trans.warn():trans.debug();
90         
91         if (lt.isLoggable()) {
92             // Transaction is done, now post full Audit Trail
93             StringBuilder sb = new StringBuilder("AuditTrail\n");
94             // We'll grabAct sub-metrics for Remote Calls and JSON
95             // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
96             Metric m = trans.auditTrail(lt,1, sb, Env.REMOTE,Env.JSON);
97
98             // Add current Metrics to total metrics
99             serviceMetric.total+= m.total;
100             for (int i=0;i<serviceMetric.buckets.length;++i) {
101                 serviceMetric.buckets[i]+=m.buckets[i];
102             }
103             
104             Long tsi;
105             if ((tsi=trans.get(transIDslot, null))!=null) {
106                 sb.append("  TraceID=");
107                 sb.append(Long.toHexString(tsi));
108                 sb.append('\n');
109             }
110             // Log current info
111             sb.append("  Total: ");
112             sb.append(m.total);
113             sb.append(" Remote: ");
114             sb.append(m.buckets[0]);
115             sb.append(" JSON: ");
116             sb.append(m.buckets[1]);
117             lt.log(sb);
118         } else {
119             // Single Line entry
120             // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
121             StringBuilder content = new StringBuilder(); 
122             Metric m = trans.auditTrail(lt,1, content, Env.REMOTE,Env.JSON);
123             // Add current Metrics to total metrics
124             serviceMetric.total+= m.total;
125             for (int i=0;i<serviceMetric.buckets.length;++i) {
126                 serviceMetric.buckets[i]+=m.buckets[i];
127             }
128             
129             StringBuilder sb = new StringBuilder();
130             sb.append("user=");
131             Principal p = trans.getUserPrincipal();
132             if (p==null) {
133                 lt=trans.warn();
134                 sb.append(target);
135                 sb.append("[None]");
136             } else {
137                 lt=trans.info();
138                 sb.append(p.getName());
139                 if (p instanceof TrustPrincipal) {
140                     sb.append('(');
141                     sb.append(((TrustPrincipal)p).personalName()); // UserChain
142                     sb.append(')');
143                 } else { 
144                     sb.append('[');
145                     if (p instanceof TaggedPrincipal) {
146                         sb.append(((TaggedPrincipal)p).tag());
147                     } else {
148                         sb.append(p.getClass().getSimpleName());
149                     }
150                     sb.append(']');
151                 }
152             }
153             String tag = trans.getTag();
154             if(tag!=null) {
155                 sb.append(",tag=");
156                 sb.append(tag);
157             }
158             sb.append(",ip=");
159             sb.append(trans.ip());
160             sb.append(",port=");
161             sb.append(trans.port());
162 //            Current code won't ever get here... Always does a Full Audit Trail
163 //            Long tsi;
164 //            if ((tsi=trans.get(transIDslot, null))!=null) {
165 //                sb.append(",TraceID=");
166 //                sb.append(Long.toHexString(tsi));
167 //            }
168             sb.append(",ms=");
169             sb.append(m.total);
170             sb.append(",status=");
171             sb.append(trans.hresp().getStatus());
172             sb.append(",meth=");
173             sb.append(trans.meth());
174             sb.append(",path=");
175             sb.append(trans.path());
176
177             if (content.length()>0) {
178                 sb.append(",msg=\"");
179                 int start = content.lastIndexOf(",msg=\"");
180                 if (start>=0) {
181                     sb.append(content,start+6,content.length()-1);
182                 } else {
183                     sb.append(content);
184                 }
185                 sb.append('"');
186             }
187             
188             lt.log(sb);
189         }
190     }
191
192 }