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.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.misc.env.Env;
36 import org.onap.aaf.misc.env.TimeTaken;
37 import org.onap.aaf.misc.env.Trans;
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41 import com.datastax.driver.core.Session;
42 import com.datastax.driver.core.SimpleStatement;
43 import com.datastax.driver.core.Statement;
46 public static final TreeMap<String,Cred> data = new TreeMap<>();
47 public static final TreeMap<String,List<Cred>> byNS = new TreeMap<>();
49 public final String id;
50 public final List<Instance> instances;
51 public final String ns;
53 public Cred(String id) {
55 instances = new ArrayList<>();
56 ns=Question.domain2ns(id);
59 public static class Instance {
60 public final int type;
61 public final Date expires,written;
62 public final Integer other;
64 public Instance(int type, Date expires, Integer other, long written) {
66 this.expires = expires;
68 this.written = new Date(written);
72 public Date last(final int ... types) {
74 for(Instance i : instances) {
75 if(types.length>0) { // filter by types, if requested
87 if(last==null || i.expires.after(last)) {
95 public Set<Integer> types() {
96 Set<Integer> types = new HashSet<>();
97 for(Instance i : instances) {
103 public static void load(Trans trans, Session session, int ... types ) {
104 load(trans, session,"select id, type, expires, other, writetime(cred) from authz.cred;",types);
108 public static void loadOneNS(Trans trans, Session session, String ns,int ... types ) {
109 load(trans, session,"select id, type, expires, other, writetime(cred) from authz.cred WHERE ns='" + ns + "';");
112 private static void load(Trans trans, Session session, String query, int ...types) {
114 trans.info().log( "query: " + query );
115 TimeTaken tt = trans.start("Read Creds", Env.REMOTE);
119 Statement stmt = new SimpleStatement( query );
120 results = session.execute(stmt);
126 Iterator<Row> iter = results.iterator();
128 int type; // for filtering
130 tt = trans.start("Load Credentials", Env.SUB);
132 while(iter.hasNext()) {
135 id = row.getString(0);
136 type = row.getInt(1);
137 if(types.length>0) { // filter by types, if requested
149 Cred cred = data.get(id);
154 cred.instances.add(new Instance(type, row.getTimestamp(2), row.getInt(3), row.getLong(4)/1000));
156 List<Cred> lscd = byNS.get(cred.ns);
158 byNS.put(cred.ns, (lscd=new ArrayList<>()));
160 boolean found = false;
162 if(c.id.equals(cred.id)) {
175 trans.info().log("Found",count,"creds");
180 * Count entries in Cred data.
181 * Note, as opposed to other methods, need to load the whole cred table for the Types.
185 public static CredCount count(int numbuckets) {
186 CredCount cc = new CredCount(numbuckets);
187 for(Cred c : data.values()) {
188 for (Instance ci : c.instances) {
189 cc.inc(ci.type,ci.written, ci.expires);
193 // String query = "select count(*) from authz.cred LIMIT 1000000;";
194 // trans.info().log( "query: " + query );
195 // TimeTaken tt = trans.start("Count Credentials", Env.REMOTE);
196 // ResultSet results;
198 // Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
199 // results = session.execute(stmt);
200 // return results.one().getLong(0);
206 public static class CredCount {
208 public int basic_auth[];
209 public int basic_auth_256[];
211 public int x509Added[];
212 public int x509Expired[];
215 public CredCount(int numbuckets) {
216 raw = new int[numbuckets];
217 basic_auth = new int[numbuckets];
218 basic_auth_256 = new int[numbuckets];
219 cert = new int[numbuckets];
220 x509Added = new int[numbuckets];
221 x509Expired = new int[numbuckets];
222 dates = new Date[numbuckets];
223 GregorianCalendar gc = new GregorianCalendar();
224 dates[0]=gc.getTime(); // now
225 gc.set(GregorianCalendar.DAY_OF_MONTH, 1);
226 gc.set(GregorianCalendar.HOUR, 0);
227 gc.set(GregorianCalendar.MINUTE, 0);
228 gc.set(GregorianCalendar.SECOND,0);
229 gc.set(GregorianCalendar.MILLISECOND,0);
230 gc.add(GregorianCalendar.MILLISECOND, -1); // last milli of month
231 for(int i=1;i<numbuckets;++i) {
232 dates[i] = gc.getTime();
233 gc.add(GregorianCalendar.MONTH, -1);
238 public void inc(int type, Date start, Date expires) {
239 for(int i=0;i<dates.length-1;++i) {
240 if(start.before(dates[i])) {
241 if(type==CredDAO.CERT_SHA256_RSA) {
242 if(start.after(dates[i+1])) {
246 if(expires.after(dates[i])) {
251 case CredDAO.BASIC_AUTH:
254 case CredDAO.BASIC_AUTH_SHA256:
257 case CredDAO.CERT_SHA256_RSA:
266 public long authCount(int idx) {
267 return (long)basic_auth[idx]+basic_auth_256[idx];
270 public long x509Count(int idx) {
276 public String toString() {
277 StringBuilder sb = new StringBuilder(id);
279 for(Instance i : instances) {
283 sb.append(i.expires);
287 return sb.toString();
291 * @see java.lang.Object#hashCode()
294 public int hashCode() {
295 return id.hashCode();
299 * @see java.lang.Object#equals(java.lang.Object)
302 public boolean equals(Object obj) {
303 return id.equals(obj);