2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.GregorianCalendar;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
31 import java.util.TreeMap;
33 import org.onap.aaf.auth.dao.cass.CredDAO;
34 import org.onap.aaf.auth.dao.hl.Question;
35 import org.onap.aaf.cadi.util.CSV;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.TimeTaken;
38 import org.onap.aaf.misc.env.Trans;
39 import org.onap.aaf.misc.env.util.Chrono;
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43 import com.datastax.driver.core.Session;
44 import com.datastax.driver.core.SimpleStatement;
45 import com.datastax.driver.core.Statement;
48 public static final TreeMap<String,Cred> data = new TreeMap<>();
49 public static final TreeMap<String,List<Cred>> byNS = new TreeMap<>();
51 public final String id;
52 public final List<Instance> instances;
53 public final String ns;
55 public Cred(String id) {
57 instances = new ArrayList<>();
58 ns=Question.domain2ns(id);
61 public static class Instance {
62 public final int type;
63 public final Date expires,written;
64 public final Integer other;
65 public final String tag;
66 public List<Note> notes;
69 public Instance(int type, Date expires, Integer other, long written, String tag) {
71 this.expires = expires;
73 this.written = new Date(written);
78 * Usually returns Null...
81 public List<Note> notes() {
85 public void addNote(int level, String note) {
87 notes=new ArrayList<>();
89 notes.add(new Note(level,note));
92 public String toString() {
93 return expires.toString() + ": " + type + ' ' + tag;
97 public static class Note {
98 public final int level;
99 public final String note;
101 public Note(int level, String note) {
106 public Date last(final int ... types) {
108 for (Instance i : instances) {
109 if (types.length>0) { // filter by types, if requested
111 for (int t : types) {
121 if (last==null || i.expires.after(last)) {
129 public Set<Integer> types() {
130 Set<Integer> types = new HashSet<>();
131 for (Instance i : instances) {
137 public static void load(Trans trans, Session session, int ... types ) {
138 load(trans, session,"select id, type, expires, other, writetime(cred), tag from authz.cred;",types);
141 public static void loadOneNS(Trans trans, Session session, String ns,int ... types ) {
142 load(trans, session,"select id, type, expires, other, writetime(cred), tag from authz.cred WHERE ns='" + ns + "';");
145 private static void load(Trans trans, Session session, String query, int ...types) {
147 trans.info().log( "query: " + query );
148 TimeTaken tt = trans.start("Read Creds", Env.REMOTE);
152 Statement stmt = new SimpleStatement( query );
153 results = session.execute(stmt);
159 Iterator<Row> iter = results.iterator();
161 tt = trans.start("Load Credentials", Env.SUB);
163 while (iter.hasNext()) {
166 int type = row.getInt(1);
167 if (types.length>0) { // filter by types, if requested
168 boolean hastype = false;
169 for (int t : types) {
179 add(row.getString(0), row.getInt(1),row.getTimestamp(2),row.getInt(3),row.getLong(4),
186 trans.info().log("Found",count,"creds");
190 public static void add(
193 final Date timestamp,
198 Cred cred = data.get(id);
203 cred.instances.add(new Instance(type, timestamp, other, written/1000,tag));
205 List<Cred> lscd = byNS.get(cred.ns);
207 byNS.put(cred.ns, (lscd=new ArrayList<>()));
209 boolean found = false;
210 for (Cred c : lscd) {
211 if (c.id.equals(cred.id)) {
223 * Count entries in Cred data.
224 * Note, as opposed to other methods, need to load the whole cred table for the Types.
228 public static CredCount count(int numbuckets) {
229 CredCount cc = new CredCount(numbuckets);
230 for (Cred c : data.values()) {
231 for (Instance ci : c.instances) {
232 cc.inc(ci.type,ci.written, ci.expires);
238 public static class CredCount {
240 public int basic_auth[];
241 public int basic_auth_256[];
243 public int x509Added[];
244 public int x509Expired[];
247 public CredCount(int numbuckets) {
248 raw = new int[numbuckets];
249 basic_auth = new int[numbuckets];
250 basic_auth_256 = new int[numbuckets];
251 cert = new int[numbuckets];
252 x509Added = new int[numbuckets];
253 x509Expired = new int[numbuckets];
254 dates = new Date[numbuckets];
255 GregorianCalendar gc = new GregorianCalendar();
256 dates[0]=gc.getTime(); // now
257 gc.set(GregorianCalendar.DAY_OF_MONTH, 1);
258 gc.set(GregorianCalendar.HOUR, 0);
259 gc.set(GregorianCalendar.MINUTE, 0);
260 gc.set(GregorianCalendar.SECOND,0);
261 gc.set(GregorianCalendar.MILLISECOND,0);
262 gc.add(GregorianCalendar.MILLISECOND, -1); // last milli of month
263 for (int i=1;i<numbuckets;++i) {
264 dates[i] = gc.getTime();
265 gc.add(GregorianCalendar.MONTH, -1);
270 public void inc(int type, Date start, Date expires) {
271 for (int i=0;i<dates.length-1;++i) {
272 if (start.before(dates[i])) {
273 if (type==CredDAO.CERT_SHA256_RSA) {
274 if (start.after(dates[i+1])) {
278 if (expires.after(dates[i])) {
283 case CredDAO.BASIC_AUTH:
286 case CredDAO.BASIC_AUTH_SHA256:
289 case CredDAO.CERT_SHA256_RSA:
298 public long authCount(int idx) {
299 return (long)basic_auth[idx]+basic_auth_256[idx];
302 public long x509Count(int idx) {
308 public void row(final CSV.Writer csvw, final Instance inst) {
309 csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
310 inst.expires.getTime(),inst.tag);
313 public void row(final CSV.Writer csvw, final Instance inst, final String reason) {
314 csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
315 inst.expires.getTime(),inst.tag,reason);
319 public static void batchDelete(StringBuilder sb, List<String> row) {
320 sb.append("DELETE from authz.cred WHERE id='");
321 sb.append(row.get(1));
322 sb.append("' AND type=");
323 sb.append(Integer.parseInt(row.get(3)));
324 // Note: We have to work with long, because Expires is part of Key... can't easily do date.
325 sb.append(" AND expires=dateof(maxtimeuuid(");
326 sb.append(row.get(5));
330 public String toString() {
331 StringBuilder sb = new StringBuilder(id);
333 for (Instance i : instances) {
337 sb.append(i.expires);
341 return sb.toString();
345 * @see java.lang.Object#hashCode()
348 public int hashCode() {
349 return id.hashCode();
353 * @see java.lang.Object#equals(java.lang.Object)
356 public boolean equals(Object obj) {
357 return id.equals(obj);
361 public static String histSubject(List<String> row) {
366 public static String histMemo(String fmt, String orgName, List<String> row) {
368 if(row.size()>5) { // Reason included
371 reason = String.format(fmt, row.get(1),orgName,row.get(4));
377 public static void clear() {