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.auth.helpers.Cred.Instance;
36 import org.onap.aaf.cadi.util.CSV;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.TimeTaken;
39 import org.onap.aaf.misc.env.Trans;
40 import org.onap.aaf.misc.env.util.Chrono;
42 import com.datastax.driver.core.ResultSet;
43 import com.datastax.driver.core.Row;
44 import com.datastax.driver.core.Session;
45 import com.datastax.driver.core.SimpleStatement;
46 import com.datastax.driver.core.Statement;
49 public static final TreeMap<String,Cred> data = new TreeMap<>();
50 public static final TreeMap<String,List<Cred>> byNS = new TreeMap<>();
52 public final String id;
53 public final List<Instance> instances;
54 public final String ns;
56 public Cred(String id) {
58 instances = new ArrayList<>();
59 ns=Question.domain2ns(id);
62 public static class Instance {
63 public final int type;
64 public final Date expires,written;
65 public final Integer other;
67 public Instance(int type, Date expires, Integer other, long written) {
69 this.expires = expires;
71 this.written = new Date(written);
74 public String toString() {
75 return expires.toString() + type;
79 public Date last(final int ... types) {
81 for (Instance i : instances) {
82 if (types.length>0) { // filter by types, if requested
94 if (last==null || i.expires.after(last)) {
102 public Set<Integer> types() {
103 Set<Integer> types = new HashSet<>();
104 for (Instance i : instances) {
110 public static void load(Trans trans, Session session, int ... types ) {
111 load(trans, session,"select id, type, expires, other, writetime(cred) from authz.cred;",types);
115 public static void loadOneNS(Trans trans, Session session, String ns,int ... types ) {
116 load(trans, session,"select id, type, expires, other, writetime(cred) from authz.cred WHERE ns='" + ns + "';");
119 private static void load(Trans trans, Session session, String query, int ...types) {
121 trans.info().log( "query: " + query );
122 TimeTaken tt = trans.start("Read Creds", Env.REMOTE);
126 Statement stmt = new SimpleStatement( query );
127 results = session.execute(stmt);
133 Iterator<Row> iter = results.iterator();
135 int type; // for filtering
137 tt = trans.start("Load Credentials", Env.SUB);
139 while (iter.hasNext()) {
142 id = row.getString(0);
143 type = row.getInt(1);
144 if (types.length>0) { // filter by types, if requested
146 for (int t : types) {
156 Cred cred = data.get(id);
161 cred.instances.add(new Instance(type, row.getTimestamp(2), row.getInt(3), row.getLong(4)/1000));
163 List<Cred> lscd = byNS.get(cred.ns);
165 byNS.put(cred.ns, (lscd=new ArrayList<>()));
167 boolean found = false;
168 for (Cred c : lscd) {
169 if (c.id.equals(cred.id)) {
182 trans.info().log("Found",count,"creds");
187 * Count entries in Cred data.
188 * Note, as opposed to other methods, need to load the whole cred table for the Types.
192 public static CredCount count(int numbuckets) {
193 CredCount cc = new CredCount(numbuckets);
194 for (Cred c : data.values()) {
195 for (Instance ci : c.instances) {
196 cc.inc(ci.type,ci.written, ci.expires);
202 public static class CredCount {
204 public int basic_auth[];
205 public int basic_auth_256[];
207 public int x509Added[];
208 public int x509Expired[];
211 public CredCount(int numbuckets) {
212 raw = new int[numbuckets];
213 basic_auth = new int[numbuckets];
214 basic_auth_256 = new int[numbuckets];
215 cert = new int[numbuckets];
216 x509Added = new int[numbuckets];
217 x509Expired = new int[numbuckets];
218 dates = new Date[numbuckets];
219 GregorianCalendar gc = new GregorianCalendar();
220 dates[0]=gc.getTime(); // now
221 gc.set(GregorianCalendar.DAY_OF_MONTH, 1);
222 gc.set(GregorianCalendar.HOUR, 0);
223 gc.set(GregorianCalendar.MINUTE, 0);
224 gc.set(GregorianCalendar.SECOND,0);
225 gc.set(GregorianCalendar.MILLISECOND,0);
226 gc.add(GregorianCalendar.MILLISECOND, -1); // last milli of month
227 for (int i=1;i<numbuckets;++i) {
228 dates[i] = gc.getTime();
229 gc.add(GregorianCalendar.MONTH, -1);
234 public void inc(int type, Date start, Date expires) {
235 for (int i=0;i<dates.length-1;++i) {
236 if (start.before(dates[i])) {
237 if (type==CredDAO.CERT_SHA256_RSA) {
238 if (start.after(dates[i+1])) {
242 if (expires.after(dates[i])) {
247 case CredDAO.BASIC_AUTH:
250 case CredDAO.BASIC_AUTH_SHA256:
253 case CredDAO.CERT_SHA256_RSA:
262 public long authCount(int idx) {
263 return (long)basic_auth[idx]+basic_auth_256[idx];
266 public long x509Count(int idx) {
272 public void row(CSV.Writer csvw, Instance inst) {
273 csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),Long.toString(inst.expires.getTime()));
277 public static void row(StringBuilder sb, List<String> row) {
278 sb.append("DELETE from authz.cred WHERE id='");
279 sb.append(row.get(1));
280 sb.append("' AND type=");
281 sb.append(Integer.parseInt(row.get(3)));
282 sb.append(" AND expires=dateof(maxtimeuuid(");
283 sb.append(row.get(5));
288 public String toString() {
289 StringBuilder sb = new StringBuilder(id);
291 for (Instance i : instances) {
295 sb.append(i.expires);
299 return sb.toString();
303 * @see java.lang.Object#hashCode()
306 public int hashCode() {
307 return id.hashCode();
311 * @see java.lang.Object#equals(java.lang.Object)
314 public boolean equals(Object obj) {
315 return id.equals(obj);