summaryrefslogtreecommitdiffstats
path: root/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Cred.java
blob: 8db2b47a336a7f9b929c2134c0ed75f073dbf016 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
 * ============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.batch.helpers;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import org.onap.aaf.auth.dao.cass.CredDAO;
import org.onap.aaf.auth.dao.hl.Question;
import org.onap.aaf.cadi.util.CSV;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.env.Trans;
import org.onap.aaf.misc.env.util.Chrono;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;

public class Cred  {
    public static final TreeMap<String,Cred> data = new TreeMap<>();
    public static final TreeMap<String,List<Cred>> byNS = new TreeMap<>();

    public final String id;
    public final List<Instance> instances;
    public final String ns;
    
    public Cred(String id) {
        this.id = id;
        instances = new ArrayList<>();
        ns=Question.domain2ns(id);
    }
    
    public static class Instance {
        public final int type;
        public final Date expires,written;
        public final Integer other;
        public final String tag;
        public List<Note> notes;

        
        public Instance(int type, Date expires, Integer other, long written, String tag) {
            this.type = type;
            this.expires = expires;
            this.other = other;
            this.written = new Date(written);
            this.tag = tag;
        }
        
        /**
         * Usually returns Null...
         * @return
         */
        public List<Note> notes() {
        	return notes;
        }
        
        public void addNote(int level, String note) {
        	if(notes==null) {
        		notes=new ArrayList<>();
        	} 
        	notes.add(new Note(level,note));
        }
        
        public String toString() {
        	return expires.toString() + ": " + type + ' ' + tag;
        }
    }
    
    public static class Note {
    	public final int level;
    	public final String note;
    	
    	public Note(int level, String note) {
    		this.level = level;
    		this.note = note;
    	}
    }
    public Date last(final int ... types) {
        Date last = null;
        for (Instance i : instances) {
            if (types.length>0) { // filter by types, if requested
                boolean quit = true;
                for (int t : types) {
                    if (t==i.type) {
                        quit=false;
                        break;
                    }
                }
                if (quit) {
                    continue;
                }
            }
            if (last==null || i.expires.after(last)) {
                last = i.expires;
            }
        }
        return last;
    }

    
    public Set<Integer> types() {
        Set<Integer> types = new HashSet<>();
        for (Instance i : instances) {
            types.add(i.type);
        }
        return types;
    }

    public static void load(Trans trans, Session session, int ... types ) {
        load(trans, session,"select id, type, expires, other, writetime(cred), tag from authz.cred;",types);
        
    }

    public static void loadOneNS(Trans trans, Session session, String ns,int ... types ) {
        load(trans, session,"select id, type, expires, other, writetime(cred), tag from authz.cred WHERE ns='" + ns + "';");
    }

    private static void load(Trans trans, Session session, String query, int ...types) {

        trans.info().log( "query: " + query );
        TimeTaken tt = trans.start("Read Creds", Env.REMOTE);
       
        ResultSet results;
        try {
            Statement stmt = new SimpleStatement( query );
            results = session.execute(stmt);
        } finally {
            tt.done();
        }
        int count = 0;
        try {
            Iterator<Row> iter = results.iterator();
            Row row;
            tt = trans.start("Load Credentials", Env.SUB);
            try {
                while (iter.hasNext()) {
                    ++count;
                    row = iter.next();
                    int type = row.getInt(1);
                    if (types.length>0) { // filter by types, if requested
                        boolean hastype = false;
                        for (int t : types) {
                            if (t==type) {
                            	hastype=true;
                                break;
                            }
                        }
                        if (!hastype) {
                            continue;
                        }
                    }
                    add(row.getString(0), row.getInt(1),row.getTimestamp(2),row.getInt(3),row.getLong(4),
                    		row.getString(5));
                }
            } finally {
                tt.done();
            }
        } finally {
            trans.info().log("Found",count,"creds");
        }
    }

    public static void add(
    		final String id, 
    		final int type,
    		final Date timestamp,
    		final int other,
    		final long written,
    		final String tag
    		) {
        Cred cred = data.get(id);
        if (cred==null) {
            cred = new Cred(id);
            data.put(id, cred);
        }
        cred.instances.add(new Instance(type, timestamp, other, written/1000,tag));
        
        List<Cred> lscd = byNS.get(cred.ns);
        if (lscd==null) {
            byNS.put(cred.ns, (lscd=new ArrayList<>()));
        }
        boolean found = false;
        for (Cred c : lscd) {
            if (c.id.equals(cred.id)) {
                found=true;
                break;
            }
        }
        if (!found) {
            lscd.add(cred);
        }
	}


	/** 
     * Count entries in Cred data.
     * Note, as opposed to other methods, need to load the whole cred table for the Types.
     * @param numbuckets 
     * @return
     */
    public static CredCount count(int numbuckets) {
        CredCount cc = new CredCount(numbuckets);
        for (Cred c : data.values()) {
            for (Instance ci : c.instances) {
                cc.inc(ci.type,ci.written, ci.expires);
            }
        }
        return cc;
    }

    public static class CredCount {
        public int raw[];
        public int basic_auth[];
        public int basic_auth_256[];
        public int cert[];
        public int x509Added[];
        public int x509Expired[];
        public Date dates[];
        
        public CredCount(int numbuckets) {
            raw = new int[numbuckets];
            basic_auth = new int[numbuckets];
            basic_auth_256 = new int[numbuckets];
            cert = new int[numbuckets];
            x509Added = new int[numbuckets];
            x509Expired = new int[numbuckets];
            dates = new Date[numbuckets];
            GregorianCalendar gc = new GregorianCalendar();
            dates[0]=gc.getTime(); // now
            gc.set(GregorianCalendar.DAY_OF_MONTH, 1);
            gc.set(GregorianCalendar.HOUR, 0);
            gc.set(GregorianCalendar.MINUTE, 0);
            gc.set(GregorianCalendar.SECOND,0);
            gc.set(GregorianCalendar.MILLISECOND,0);
            gc.add(GregorianCalendar.MILLISECOND, -1); // last milli of month
            for (int i=1;i<numbuckets;++i) {
                dates[i] = gc.getTime();
                gc.add(GregorianCalendar.MONTH, -1);
            }
            
        }
        
        public void inc(int type, Date start, Date expires) {
            for (int i=0;i<dates.length-1;++i) {
                if (start.before(dates[i])) {
                    if (type==CredDAO.CERT_SHA256_RSA) {
                        if (start.after(dates[i+1])) {
                            ++x509Added[i];
                        }
                    }
                    if (expires.after(dates[i])) {
                        switch(type) {
                            case CredDAO.RAW:
                                ++raw[i];
                                break;
                            case CredDAO.BASIC_AUTH:
                                ++basic_auth[i];
                                break;
                            case CredDAO.BASIC_AUTH_SHA256:
                                ++basic_auth_256[i];
                                break;
                            case CredDAO.CERT_SHA256_RSA:
                                ++cert[i];
                                break;
                        }
                    }
                }
            }
        }

        public long authCount(int idx) {
            return (long)basic_auth[idx]+basic_auth_256[idx];
        }
        
        public long x509Count(int idx) {
            return cert[idx];
        }

    }
    
    public void row(final CSV.Writer csvw, final Instance inst) {
    	csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
    			inst.expires.getTime(),inst.tag);
    }

    public void row(final CSV.Writer csvw, final Instance inst, final String reason) {
    	csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
    			inst.expires.getTime(),inst.tag,reason);
    }


    public static void batchDelete(StringBuilder sb, List<String> row) {
    	sb.append("DELETE from authz.cred WHERE id='");
    	sb.append(row.get(1));
    	sb.append("' AND type=");
    	sb.append(Integer.parseInt(row.get(3)));
    	// Note: We have to work with long, because Expires is part of Key... can't easily do date.
    	sb.append(" AND expires=dateof(maxtimeuuid(");
    	sb.append(row.get(5));
    	sb.append("));\n");
	}

	public String toString() {
        StringBuilder sb = new StringBuilder(id);
        sb.append('[');
        for (Instance i : instances) {
            sb.append('{');
            sb.append(i.type);
            sb.append(",\"");
            sb.append(i.expires);
            sb.append("\"}");
        }
        sb.append(']');
        return sb.toString();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return id.hashCode();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        return id.equals(obj);
    }


	public static String histSubject(List<String> row) {
		return row.get(1);
	}


	public static String histMemo(String fmt, String orgName, List<String> row) {
		String reason;
		if(row.size()>5) { // Reason included
			reason = row.get(5);
		} else {
			reason = String.format(fmt, row.get(1),orgName,row.get(4));
		}
		return reason;
	}
}