Refactor Batch: Remove
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.batch.helpers;
23
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.security.cert.X509Certificate;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.onap.aaf.cadi.Hash;
31 import org.onap.aaf.cadi.util.CSV;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.TimeTaken;
34 import org.onap.aaf.misc.env.Trans;
35 import org.onap.aaf.misc.env.util.Chrono;
36 import org.onap.aaf.misc.env.util.Split;
37
38 import com.datastax.driver.core.ResultSet;
39 import com.datastax.driver.core.Row;
40 import com.datastax.driver.core.Session;
41 import com.datastax.driver.core.SimpleStatement;
42 import com.datastax.driver.core.Statement;
43
44 public class X509 {
45     public final String ca,id,x500,x509;
46     public ByteBuffer serial;
47     
48     public X509(String ca, String id, String x500, String x509, ByteBuffer serial) {
49         this.ca = ca;
50         this.id = id;
51         this.x500 = x500;
52         this.x509 = x509;
53         this.serial = serial;
54     }
55     
56
57     public static void load(Trans trans, Session session, Visitor<X509> visitor) {
58         load(trans,session,"select ca, id, x500, x509, serial from authz.x509;", visitor);
59     }
60
61     private static void load(Trans trans, Session session, String query, Visitor<X509> visitor) {
62         trans.info().log( "query: " + query );
63         TimeTaken tt = trans.start("Read X509", Env.REMOTE);
64        
65         ResultSet results;
66         try {
67             Statement stmt = new SimpleStatement( query );
68             results = session.execute(stmt);
69         } finally {
70             tt.done();
71         }
72
73         int count = 0;
74         try {
75             Iterator<Row> iter = results.iterator();
76             Row row;
77             tt = trans.start("Load X509s", Env.SUB);
78             try {
79                 while (iter.hasNext()) {
80                         ++count;
81                     row = iter.next();
82                     visitor.visit(new X509(row.getString(0),row.getString(1), row.getString(2),row.getString(3),row.getBytes(4)));
83                 }
84             } finally {
85                 tt.done();
86             }
87         } finally {
88             trans.info().log("Found",count,"X509 Certificates");
89         }
90     }
91     
92     public static long count(Trans trans, Session session) {
93         String query = "select count(*) from authz.x509 LIMIT 1000000;";
94         trans.info().log( "query: " + query );
95         TimeTaken tt = trans.start("Count x509s", Env.REMOTE);
96         ResultSet results;
97         try {
98             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
99             results = session.execute(stmt);
100             return results.one().getLong(0);
101         } finally {
102             tt.done();
103         }
104     }
105     
106
107         public void row(CSV.Writer cw, X509Certificate x509Cert) throws IOException {
108                 cw.row("x509",ca,Hash.toHex(serial.array()),Chrono.dateOnlyStamp(x509Cert.getNotAfter()),x500);
109         }
110
111
112         public static void row(StringBuilder sb, List<String> row) throws IOException {
113         sb.append("DELETE from authz.x509 WHERE ca='");
114         sb.append(row.get(1));
115         sb.append("' AND serial=");
116         sb.append(row.get(2));
117         sb.append(";\n");
118         }
119
120
121         public static String histSubject(List<String> row) {
122                 return row.get(4);
123         }
124
125
126         public static String histMemo(String fmt, List<String> row) {
127                 String id="n/a";
128                 for(String s : Split.splitTrim(',', row.get(4))) {
129                         if(s.startsWith("OU=") && s.indexOf('@')>=0) {
130                                 int colon = s.indexOf(':');
131                                 if(colon<0) {
132                                         colon=s.length();
133                                 }
134                                 id=s.substring(3,colon);
135                                 break;
136                         }
137                 }
138                 return String.format(fmt, "Cert for " + id,"CA " + row.get(1),row.get(3));
139         }
140
141 }