Update Fixes from testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / X509.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright © 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
26 import java.nio.ByteBuffer;
27 import java.security.cert.X509Certificate;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.onap.aaf.cadi.Hash;
32 import org.onap.aaf.cadi.util.CSV;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.TimeTaken;
35 import org.onap.aaf.misc.env.Trans;
36 import org.onap.aaf.misc.env.util.Chrono;
37 import org.onap.aaf.misc.env.util.Split;
38
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;
44
45 public class X509 {
46     public final String ca;
47     public final String id;
48     public final String x500;
49     public final String x509;
50     public ByteBuffer serial;
51     
52     public X509(String ca, String id, String x500, String x509, ByteBuffer serial) {
53         this.ca = ca;
54         this.id = id;
55         this.x500 = x500;
56         this.x509 = x509;
57         this.serial = serial;
58     }
59     
60
61     public static void load(Trans trans, Session session, Visitor<X509> visitor) {
62         load(trans,session, "" , visitor);
63     }
64     
65     public static void load(Trans trans, Session session, String where, Visitor<X509> visitor) {
66         load(trans,session, visitor,"select ca, id, x500, x509, serial from authz.x509 " + where +';');
67     }
68
69
70     private static void load(Trans trans, Session session, Visitor<X509> visitor, String query) {
71         trans.info().log( "query: " + query );
72         TimeTaken tt = trans.start("Read X509", Env.REMOTE);
73        
74         ResultSet results;
75         try {
76             Statement stmt = new SimpleStatement( query );
77             results = session.execute(stmt);
78         } finally {
79             tt.done();
80         }
81
82         int count = 0;
83         try {
84             Iterator<Row> iter = results.iterator();
85             Row row;
86             tt = trans.start("Load X509s", Env.SUB);
87             try {
88                 while (iter.hasNext()) {
89                     ++count;
90                     row = iter.next();
91                     visitor.visit(new X509(row.getString(0),row.getString(1), row.getString(2),row.getString(3),row.getBytes(4)));
92                 }
93             } finally {
94                 tt.done();
95             }
96         } finally {
97             trans.info().log("Found",count,"X509 Certificates");
98         }
99     }
100     
101     public static long count(Trans trans, Session session) {
102         String query = "select count(*) from authz.x509 LIMIT 1000000;";
103         trans.info().log( "query: " + query );
104         TimeTaken tt = trans.start("Count x509s", Env.REMOTE);
105         ResultSet results;
106         try {
107             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
108             results = session.execute(stmt);
109             return results.one().getLong(0);
110         } finally {
111             tt.done();
112         }
113     }
114     
115
116     public void row(CSV.Writer cw, X509Certificate x509Cert) {
117         cw.row("x509",ca,Hash.toHex(serial.array()),Chrono.dateOnlyStamp(x509Cert.getNotAfter()),x500);
118     }
119
120     public void row(CSV.Writer cw, X509Certificate x509Cert,String reason) {
121         cw.row("x509",ca,Hash.toHex(serial.array()),Chrono.dateOnlyStamp(x509Cert.getNotAfter()),x500,reason);
122     }
123
124
125     public static void row(StringBuilder sb, List<String> row) {
126         sb.append("DELETE from authz.x509 WHERE ca='");
127         sb.append(row.get(1));
128         sb.append("' AND serial=");
129         sb.append(row.get(2));
130         sb.append(";\n");
131     }
132
133     public static void batchDelete(StringBuilder sb, List<String> row) {
134         sb.append("DELETE from authz.x509 WHERE ca='");
135         sb.append(row.get(1));
136         sb.append("' AND serial=");
137         sb.append(row.get(2));
138         sb.append(";\n");
139     }
140     public static String histSubject(List<String> row) {
141         return row.get(4);
142     }
143
144
145     public static String histMemo(String fmt, List<String> row) {
146         String id="n/a";
147         for(String s : Split.splitTrim(',', row.get(4))) {
148             if(s.startsWith("OU=") && s.indexOf('@')>=0) {
149                 int colon = s.indexOf(':');
150                 if(colon<0) {
151                     colon=s.length();
152                 }
153                 id=s.substring(3,colon);
154                 break;
155             }
156         }
157         return String.format(fmt, "Cert for " + id,"CA " + row.get(1),row.get(3));
158     }
159
160 }