summaryrefslogtreecommitdiffstats
path: root/auth/auth-core/src/main/java/org/onap/aaf/auth/env/AuthzTransFilter.java
blob: bda23e1390381e1eab42a73308d83a41f7648d10 (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
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 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.aaf.auth.env;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.onap.aaf.auth.rserv.TransFilter;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.Connector;
import org.onap.aaf.cadi.LocatorException;
import org.onap.aaf.cadi.TrustChecker;
import org.onap.aaf.cadi.principal.TaggedPrincipal;
import org.onap.aaf.cadi.principal.TrustPrincipal;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.LogTarget;
import org.onap.aaf.misc.env.Slot;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.env.Trans.Metric;

public class AuthzTransFilter extends TransFilter<AuthzTrans> {
    private AuthzEnv env;
    public Metric serviceMetric;
    public static Slot transIDslot,specialLogSlot;

    public static final String TRANS_ID_SLOT = "TRANS_ID_SLOT";
    public static final String SPECIAL_LOG_SLOT = "SPECIAL_LOG_SLOT";

    public static final int BUCKETSIZE = 2;
    
    public AuthzTransFilter(AuthzEnv env, Connector con, TrustChecker tc, Object ... additionalTafLurs) throws CadiException, LocatorException {
        super(env.access(),con, tc, additionalTafLurs);
        this.env = env;
        serviceMetric = new Metric();
        serviceMetric.buckets = new float[BUCKETSIZE];
        if (transIDslot==null) {
            transIDslot = env.slot(TRANS_ID_SLOT);
        }
        if (specialLogSlot==null) {
            specialLogSlot = env.slot(SPECIAL_LOG_SLOT);
        }
    }
    
    @Override
    protected AuthzTrans newTrans(HttpServletRequest req, HttpServletResponse resp) {
        AuthzTrans at = env.newTrans();
        at.setLur(getLur());
        at.set(req,resp);
        return at;
    }

    @Override
    protected TimeTaken start(AuthzTrans trans) {
        return trans.start("Trans " + //(context==null?"n/a":context.toString()) +
        " IP: " + trans.ip() +
        " Port: " + trans.port()
        , Env.SUB);
    }

    @Override
    protected void authenticated(AuthzTrans trans, Principal p) {
        trans.setUser((TaggedPrincipal)p); // We only work with TaggedPrincipals in Authz
    }

    @Override
    protected void tallyHo(AuthzTrans trans, String target) {
        Boolean b = trans.get(specialLogSlot, false);
        LogTarget lt = b?trans.warn():trans.debug();
        
        if (lt.isLoggable()) {
            // Transaction is done, now post full Audit Trail
            StringBuilder sb = new StringBuilder("AuditTrail\n");
            // We'll grabAct sub-metrics for Remote Calls and JSON
            // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
            Metric m = trans.auditTrail(lt,1, sb, Env.REMOTE,Env.JSON);

            // Add current Metrics to total metrics
            serviceMetric.total+= m.total;
            for (int i=0;i<serviceMetric.buckets.length;++i) {
                serviceMetric.buckets[i]+=m.buckets[i];
            }
            
            Long tsi;
            if ((tsi=trans.get(transIDslot, null))!=null) {
                sb.append("  TraceID=");
                sb.append(Long.toHexString(tsi));
                sb.append('\n');
            }
            // Log current info
            sb.append("  Total: ");
            sb.append(m.total);
            sb.append(" Remote: ");
            sb.append(m.buckets[0]);
            sb.append(" JSON: ");
            sb.append(m.buckets[1]);
            lt.log(sb);
        } else {
            // Single Line entry
            // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
            StringBuilder content = new StringBuilder(); 
            Metric m = trans.auditTrail(lt,1, content, Env.REMOTE,Env.JSON);
            // Add current Metrics to total metrics
            serviceMetric.total+= m.total;
            for (int i=0;i<serviceMetric.buckets.length;++i) {
                serviceMetric.buckets[i]+=m.buckets[i];
            }
            
            StringBuilder sb = new StringBuilder();
            sb.append("user=");
            Principal p = trans.getUserPrincipal();
            if (p==null) {
            	lt=trans.warn();
                sb.append(target);
                sb.append("[None]");
            } else {
            	lt=trans.info();
                sb.append(p.getName());
                if (p instanceof TrustPrincipal) {
                    sb.append('(');
                    sb.append(((TrustPrincipal)p).personalName()); // UserChain
                    sb.append(')');
                } else { 
                    sb.append('[');
                    if (p instanceof TaggedPrincipal) {
                        sb.append(((TaggedPrincipal)p).tag());
                    } else {
                        sb.append(p.getClass().getSimpleName());
                    }
                    sb.append(']');
                }
            }
            String tag = trans.getTag();
            if(tag!=null) {
                sb.append(",tag=");
                sb.append(tag);
            }
            sb.append(",ip=");
            sb.append(trans.ip());
            sb.append(",port=");
            sb.append(trans.port());
//            Current code won't ever get here... Always does a Full Audit Trail
//            Long tsi;
//            if ((tsi=trans.get(transIDslot, null))!=null) {
//                sb.append(",TraceID=");
//                sb.append(Long.toHexString(tsi));
//            }
            sb.append(",ms=");
            sb.append(m.total);
            sb.append(",meth=");
            sb.append(trans.meth());
            sb.append(",path=");
            sb.append(trans.path());

            if (content.length()>0) {
                sb.append(",msg=\"");
                int start = content.lastIndexOf(",msg=\"");
                if (start>=0) {
                    sb.append(content,start+6,content.length()-1);
                } else {
                    sb.append(content);
                }
                sb.append('"');
            }
            
            lt.log(sb);
        }
    }

}