Improve Batches
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / temp / DataMigrateDublin.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 package org.onap.aaf.auth.batch.temp;
22
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.security.SecureRandom;
26 import java.security.cert.Certificate;
27 import java.security.cert.X509Certificate;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.TreeMap;
35
36 import org.onap.aaf.auth.batch.Batch;
37 import org.onap.aaf.auth.batch.BatchPrincipal;
38 import org.onap.aaf.auth.batch.helpers.CQLBatch;
39 import org.onap.aaf.auth.batch.helpers.CQLBatchLoop;
40 import org.onap.aaf.auth.dao.cass.CredDAO;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.org.OrganizationException;
43 import org.onap.aaf.cadi.Hash;
44 import org.onap.aaf.cadi.configure.Factory;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Env;
47 import org.onap.aaf.misc.env.TimeTaken;
48
49 import com.datastax.driver.core.ResultSet;
50 import com.datastax.driver.core.Row;
51
52 public class DataMigrateDublin extends Batch {
53         private final SecureRandom sr;
54         private final AuthzTrans noAvg;
55         
56         public DataMigrateDublin(AuthzTrans trans) throws APIException, IOException, OrganizationException {
57                 super(trans.env());
58         trans.info().log("Starting Connection Process");
59         
60         noAvg = env.newTransNoAvg();
61         noAvg.setUser(new BatchPrincipal("Migrate"));
62
63         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
64         try {
65                         TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
66                         try {
67                                 session = cluster.connect();
68                         } finally {
69                                 tt.done();
70                         }
71         } finally {
72             tt0.done();
73         }
74         
75         sr = new SecureRandom();
76         }
77
78         @Override
79         protected void run(AuthzTrans trans) {
80         ///////////////////////////
81         trans.info().log("Add UniqueTag to Passwords");
82
83         CQLBatchLoop cbl = new CQLBatchLoop(new CQLBatch(noAvg.info(),session), 50, dryRun);
84         try {
85                 ResultSet rs = session.execute("SELECT id,type,expires,cred,tag FROM authz.cred");
86                 Iterator<Row> iter = rs.iterator();
87                 Row row;
88                 int count = 0;
89                 byte[] babytes = new byte[6];
90                 Map<String, List<CredInfo>> mlci = new TreeMap<>();
91                 Map<String, String> ba_tag = new TreeMap<>();
92                 while(iter.hasNext()) {
93                         ++count;
94                         row = iter.next();
95                         String tag = row.getString(4);
96                         int type = row.getInt(1);
97                         switch(type) {
98                                 case CredDAO.BASIC_AUTH:
99                                 case CredDAO.BASIC_AUTH_SHA256:
100                                 String key = row.getString(0) + '|' + type + '|' + Hash.toHex(row.getBytesUnsafe(3).array()); 
101                                 String btag = ba_tag.get(key);
102                                 if(btag == null) {
103                                         if(tag==null || tag.isEmpty()) {
104                                                 sr.nextBytes(babytes);
105                                                 btag = Hash.toHexNo0x(babytes);
106                                         } else {
107                                                 btag = tag;
108                                         }
109                                         ba_tag.put(key, btag);
110                                 }
111                                 
112                                 if(!btag.equals(tag)) {
113                                         cbl.preLoop();
114                                         update(cbl,row,btag);
115                                 }
116                                         break;
117                                 case CredDAO.CERT_SHA256_RSA:
118                                         if(tag==null || tag.isEmpty()) {
119                                                 String id = row.getString(0);
120                                                 List<CredInfo> ld = mlci.get(id);
121                                                 if(ld==null) {
122                                                         ld = new ArrayList<>();
123                                                         mlci.put(id,ld);
124                                                 }
125                                                         ld.add(new CredInfo(id,row.getInt(1),row.getTimestamp(2)));
126                                         }
127                                                 break;
128                         }
129                 }
130                 cbl.flush();
131                 trans.info().printf("Processes %d cred records, updated %d records in %d batches.", count, cbl.total(), cbl.batches());
132                 count = 0;
133                 
134                 cbl.reset();
135                 
136             trans.info().log("Add Serial to X509 Creds");
137             rs = session.execute("SELECT ca, id, x509 FROM authz.x509");
138             iter = rs.iterator();
139                 while(iter.hasNext()) {
140                         ++count;
141                         row = iter.next();
142                         String ca = row.getString(0);
143                         String id = row.getString(1);
144                         List<CredInfo> list = mlci.get(id);
145                         if(list!=null) {
146                                 ByteBuffer bb = row.getBytesUnsafe(2);
147                                 if(bb!=null) {
148                                         Collection<? extends Certificate> x509s = Factory.toX509Certificate(bb.array());
149                                         for(Certificate c : x509s) {
150                                                 X509Certificate xc = (X509Certificate)c;
151                                                 for(CredInfo ci : list) {
152                                                         if(xc.getNotAfter().equals(ci.expires)) {
153                                                                 cbl.preLoop();
154                                                                 ci.update(cbl, ca + '|' + xc.getSerialNumber());
155                                                                 break;
156                                                         }
157                                                 }
158                                         }
159                                 }
160                         }
161                 }
162                 cbl.flush();
163                 trans.info().printf("Processed %d x509 records, updated %d records in %d batches.", count, cbl.total(), cbl.batches());
164                 count = 0;
165         } catch (Exception e) {
166                         e.printStackTrace();
167         }
168         }
169         
170         private static class CredInfo {
171                 public final String id;
172                 public final int type;
173                 public final Date expires;
174                 
175                 public CredInfo(String id, int type, Date expires) {
176                         this.id = id;
177                         this.type = type;
178                         this.expires = expires;
179                 }
180                 
181                 public void update(CQLBatchLoop cbl, String newtag) {
182                         StringBuilder sb = cbl.inc();
183                         sb.append("UPDATE authz.cred SET tag='");
184                         sb.append(newtag);
185                         sb.append("' WHERE id='");
186                         sb.append(id);
187                         sb.append("' AND type=");
188                         sb.append(type);
189                         sb.append(" AND expires=dateof(maxtimeuuid(");
190                         sb.append(expires.getTime());
191                         sb.append("));");
192                 }
193         }
194                 
195         private void update(CQLBatchLoop cbl, Row row, String newtag) {
196                 StringBuilder sb = cbl.inc();
197                 sb.append("UPDATE authz.cred SET tag='");
198                 sb.append(newtag);
199                 sb.append("' WHERE id='");
200                 sb.append(row.getString(0));
201                 sb.append("' AND type=");
202                 sb.append(row.getInt(1));
203                 sb.append(" AND expires=dateof(maxtimeuuid(");
204                 Date lc = row.getTimestamp(2);
205                 sb.append(lc.getTime());
206                 sb.append("));");
207         }
208
209         @Override
210         protected void _close(AuthzTrans trans) {
211         trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
212         session.close();
213         }
214
215 }