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====================================================
21 package org.onap.aaf.auth.batch.temp;
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;
34 import java.util.TreeMap;
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;
49 import com.datastax.driver.core.ResultSet;
50 import com.datastax.driver.core.Row;
52 public class DataMigrateDublin extends Batch {
53 private final SecureRandom sr;
54 private final AuthzTrans noAvg;
56 public DataMigrateDublin(AuthzTrans trans) throws APIException, IOException, OrganizationException {
58 trans.info().log("Starting Connection Process");
60 noAvg = env.newTransNoAvg();
61 noAvg.setUser(new BatchPrincipal("Migrate"));
63 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
65 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
67 session = cluster.connect();
75 sr = new SecureRandom();
79 protected void run(AuthzTrans trans) {
80 ///////////////////////////
81 trans.info().log("Add UniqueTag to Passwords");
83 CQLBatchLoop cbl = new CQLBatchLoop(new CQLBatch(noAvg.info(),session), 50, dryRun);
85 ResultSet rs = session.execute("SELECT id,type,expires,cred,tag FROM authz.cred");
86 Iterator<Row> iter = rs.iterator();
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()) {
95 String tag = row.getString(4);
96 int type = row.getInt(1);
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);
103 if(tag==null || tag.isEmpty()) {
104 sr.nextBytes(babytes);
105 btag = Hash.toHexNo0x(babytes);
109 ba_tag.put(key, btag);
112 if(!btag.equals(tag)) {
113 update(cbl,row,btag);
116 case CredDAO.CERT_SHA256_RSA:
117 if(tag==null || tag.isEmpty()) {
118 String id = row.getString(0);
119 List<CredInfo> ld = mlci.get(id);
121 ld = new ArrayList<>();
124 ld.add(new CredInfo(id,row.getInt(1),row.getTimestamp(2)));
130 trans.info().printf("Processes %d cred records, updated %d records in %d batches.", count, cbl.total(), cbl.batches());
135 trans.info().log("Add Serial to X509 Creds");
136 rs = session.execute("SELECT ca, id, x509 FROM authz.x509");
137 iter = rs.iterator();
138 while(iter.hasNext()) {
141 String ca = row.getString(0);
142 String id = row.getString(1);
143 List<CredInfo> list = mlci.get(id);
145 ByteBuffer bb = row.getBytesUnsafe(2);
147 Collection<? extends Certificate> x509s = Factory.toX509Certificate(bb.array());
148 for(Certificate c : x509s) {
149 X509Certificate xc = (X509Certificate)c;
150 for(CredInfo ci : list) {
151 if(xc.getNotAfter().equals(ci.expires)) {
152 ci.update(cbl, ca + '|' + xc.getSerialNumber());
161 trans.info().printf("Processed %d x509 records, updated %d records in %d batches.", count, cbl.total(), cbl.batches());
163 } catch (Exception e) {
164 trans.error().log(e);
168 private static class CredInfo {
169 public final String id;
170 public final int type;
171 public final Date expires;
173 public CredInfo(String id, int type, Date expires) {
176 this.expires = expires;
179 public void update(CQLBatchLoop cbl, String newtag) {
180 StringBuilder sb = cbl.inc();
181 sb.append("UPDATE authz.cred SET tag='");
183 sb.append("' WHERE id='");
185 sb.append("' AND type=");
187 sb.append(" AND expires=dateof(maxtimeuuid(");
188 sb.append(expires.getTime());
193 private void update(CQLBatchLoop cbl, Row row, String newtag) {
194 StringBuilder sb = cbl.inc();
195 sb.append("UPDATE authz.cred SET tag='");
197 sb.append("' WHERE id='");
198 sb.append(row.getString(0));
199 sb.append("' AND type=");
200 sb.append(row.getInt(1));
201 sb.append(" AND expires=dateof(maxtimeuuid(");
202 Date lc = row.getTimestamp(2);
203 sb.append(lc.getTime());
208 protected void _close(AuthzTrans trans) {
209 trans.info().log("End " + this.getClass().getSimpleName() + " processing" );