Batch upgrades for ONAP
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / 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.helpers;
23
24 import java.awt.HeadlessException;
25 import java.io.IOException;
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.Symm;
33 import org.onap.aaf.cadi.util.CSV;
34 import org.onap.aaf.misc.env.Env;
35 import org.onap.aaf.misc.env.TimeTaken;
36 import org.onap.aaf.misc.env.Trans;
37 import org.onap.aaf.misc.env.util.Chrono;
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,id,x500,x509;
47     public ByteBuffer serial;
48     
49     public X509(String ca, String id, String x500, String x509, ByteBuffer serial) {
50         this.ca = ca;
51         this.id = id;
52         this.x500 = x500;
53         this.x509 = x509;
54         this.serial = serial;
55     }
56     
57
58     public static void load(Trans trans, Session session, Visitor<X509> visitor) {
59         load(trans,session,"select ca, id, x500, x509, serial from authz.x509;", visitor);
60     }
61
62     private static void load(Trans trans, Session session, String query, Visitor<X509> visitor) {
63         trans.info().log( "query: " + query );
64         TimeTaken tt = trans.start("Read Roles", Env.REMOTE);
65        
66         ResultSet results;
67         try {
68             Statement stmt = new SimpleStatement( query );
69             results = session.execute(stmt);
70         } finally {
71             tt.done();
72         }
73
74         int count = 0;
75         try {
76             Iterator<Row> iter = results.iterator();
77             Row row;
78             tt = trans.start("Load X509s", Env.SUB);
79             try {
80                 while (iter.hasNext()) {
81                         ++count;
82                     row = iter.next();
83                     visitor.visit(new X509(row.getString(0),row.getString(1), row.getString(2),row.getString(3),row.getBytes(4)));
84                 }
85             } finally {
86                 tt.done();
87             }
88         } finally {
89             trans.info().log("Found",count,"X509 Certificates");
90         }
91     }
92     
93     public static long count(Trans trans, Session session) {
94         String query = "select count(*) from authz.x509 LIMIT 1000000;";
95         trans.info().log( "query: " + query );
96         TimeTaken tt = trans.start("Count x509s", Env.REMOTE);
97         ResultSet results;
98         try {
99             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
100             results = session.execute(stmt);
101             return results.one().getLong(0);
102         } finally {
103             tt.done();
104         }
105     }
106     
107
108         public void row(CSV.Writer cw, X509Certificate x509Cert) throws IOException {
109                 cw.row("x509",ca,Hash.toHex(serial.array()),Chrono.dateOnlyStamp(x509Cert.getNotAfter()),x500);
110         }
111
112
113         public static void row(StringBuilder sb, List<String> row) throws IOException {
114         sb.append("DELETE from authz.x509 WHERE ca='");
115         sb.append(row.get(1));
116         sb.append("' AND serial=");
117         sb.append(row.get(2));
118         sb.append(";\n");
119         }
120
121 }