Update for more Logging Info
[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.ServletRequest;
27 import javax.servlet.http.HttpServletRequest;
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) {
67         AuthzTrans at = env.newTrans();
68         at.setLur(getLur());
69         at.set(req);
70         return at;
71     }
72
73     @Override
74     protected TimeTaken start(AuthzTrans trans, ServletRequest request) {
75         trans.set((HttpServletRequest)request);
76         return trans.start("Trans " + //(context==null?"n/a":context.toString()) +
77         " IP: " + trans.ip() +
78         " Port: " + trans.port()
79         , Env.SUB);
80     }
81
82     @Override
83     protected void authenticated(AuthzTrans trans, Principal p) {
84         trans.setUser((TaggedPrincipal)p); // We only work with TaggedPrincipals in Authz
85     }
86
87     @Override
88     protected void tallyHo(AuthzTrans trans, String target) {
89         Boolean b = trans.get(specialLogSlot, false);
90         LogTarget lt = b?trans.warn():trans.debug();
91         
92         if (lt.isLoggable()) {
93             // Transaction is done, now post full Audit Trail
94             StringBuilder sb = new StringBuilder("AuditTrail\n");
95             // We'll grabAct sub-metrics for Remote Calls and JSON
96             // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
97             Metric m = trans.auditTrail(lt,1, sb, Env.REMOTE,Env.JSON);
98
99             // Add current Metrics to total metrics
100             serviceMetric.total+= m.total;
101             for (int i=0;i<serviceMetric.buckets.length;++i) {
102                 serviceMetric.buckets[i]+=m.buckets[i];
103             }
104             
105             Long tsi;
106             if ((tsi=trans.get(transIDslot, null))!=null) {
107                 sb.append("  TraceID=");
108                 sb.append(Long.toHexString(tsi));
109                 sb.append('\n');
110             }
111             // Log current info
112             sb.append("  Total: ");
113             sb.append(m.total);
114             sb.append(" Remote: ");
115             sb.append(m.buckets[0]);
116             sb.append(" JSON: ");
117             sb.append(m.buckets[1]);
118             lt.log(sb);
119         } else {
120             // Single Line entry
121             // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
122             StringBuilder content = new StringBuilder(); 
123             Metric m = trans.auditTrail(lt,1, content, Env.REMOTE,Env.JSON);
124             // Add current Metrics to total metrics
125             serviceMetric.total+= m.total;
126             for (int i=0;i<serviceMetric.buckets.length;++i) {
127                 serviceMetric.buckets[i]+=m.buckets[i];
128             }
129             
130             StringBuilder sb = new StringBuilder();
131             sb.append("user=");
132             Principal p = trans.getUserPrincipal();
133             if (p==null) {
134                 lt=trans.warn();
135                 sb.append(target);
136                 sb.append("[None]");
137             } else {
138                 lt=trans.info();
139                 sb.append(p.getName());
140                 if (p instanceof TrustPrincipal) {
141                     sb.append('(');
142                     sb.append(((TrustPrincipal)p).personalName()); // UserChain
143                     sb.append(')');
144                 } else { 
145                     sb.append('[');
146                     if (p instanceof TaggedPrincipal) {
147                         sb.append(((TaggedPrincipal)p).tag());
148                     } else {
149                         sb.append(p.getClass().getSimpleName());
150                     }
151                     sb.append(']');
152                 }
153             }
154             String tag = trans.getTag();
155             if(tag!=null) {
156                 sb.append(",tag=");
157                 sb.append(tag);
158             }
159             sb.append(",ip=");
160             sb.append(trans.ip());
161             sb.append(",port=");
162             sb.append(trans.port());
163 //            Current code won't ever get here... Always does a Full Audit Trail
164 //            Long tsi;
165 //            if ((tsi=trans.get(transIDslot, null))!=null) {
166 //                sb.append(",TraceID=");
167 //                sb.append(Long.toHexString(tsi));
168 //            }
169             sb.append(",ms=");
170             sb.append(m.total);
171             sb.append(",meth=");
172             sb.append(trans.meth());
173             sb.append(",path=");
174             sb.append(trans.path());
175
176             if (content.length()>0) {
177                 sb.append(",msg=\"");
178                 int start = content.lastIndexOf(",msg=\"");
179                 if (start>=0) {
180                     sb.append(content,start+6,content.length()-1);
181                 } else {
182                     sb.append(content);
183                 }
184                 sb.append('"');
185             }
186             
187             lt.log(sb);
188         }
189     }
190
191 }