9e080b697d7fa369800d469c762c6423e4c1bda3
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / update / Extend.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22 package org.onap.aaf.auth.batch.update;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32
33 import org.onap.aaf.auth.batch.Batch;
34 import org.onap.aaf.auth.batch.BatchPrincipal;
35 import org.onap.aaf.auth.batch.helpers.CQLBatch;
36 import org.onap.aaf.auth.batch.helpers.UserRole;
37 import org.onap.aaf.auth.batch.reports.PrepExtend;
38 import org.onap.aaf.auth.dao.CassAccess;
39 import org.onap.aaf.auth.dao.cass.CredDAO;
40 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.auth.org.OrganizationException;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.client.Holder;
46 import org.onap.aaf.cadi.util.CSV;
47 import org.onap.aaf.misc.env.APIException;
48 import org.onap.aaf.misc.env.Env;
49 import org.onap.aaf.misc.env.TimeTaken;
50 import org.onap.aaf.misc.env.util.Chrono;
51
52 public class Extend extends Batch {
53     private final CQLBatch cqlBatch;
54     private final CredDAO credDAO;
55     private final AuthzTrans noAvg;
56     private List<File> extFiles;
57     private final int extendBy;
58     private int gcType;
59     
60     public Extend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
61         super(trans.env());
62         trans.info().log("Starting Connection Process");
63         
64         noAvg = env.newTransNoAvg();
65         noAvg.setUser(new BatchPrincipal("Extend"));
66
67         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
68         try {
69             TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
70             credDAO = new CredDAO(trans, cluster, CassAccess.KEYSPACE);
71             try {
72                 session = credDAO.getSession(trans);
73             } finally {
74                 tt.done();
75             }
76             cqlBatch = new CQLBatch(noAvg.info(),session); 
77         } finally {
78             tt0.done();
79         }
80
81         gcType = GregorianCalendar.WEEK_OF_YEAR;
82         int weeks = 4;
83         
84         Set<String> cmd = new HashSet<>();
85         for(int i=0; i< args().length;++i) {
86             if("-weeks".equals(args()[i])) {
87                 if(args().length>i+1) {
88                     weeks = Integer.parseInt(args()[++i]);
89                 }
90             } else {
91                 cmd.add(args()[i]);
92             }
93         }
94         
95         if(weeks<1 || weeks > 24) {
96             throw new APIException("Invalid --weeks");
97         }
98         extendBy = weeks;
99
100         // Create Intermediate Output 
101         File logDir = logDir();
102         extFiles = new ArrayList<>();
103         if(cmd.isEmpty()) {
104             extFiles.add(new File(logDir,PrepExtend.PREP_EXTEND+Chrono.dateOnlyStamp()+".csv"));
105         } else {
106             for(String fn : cmd) {
107                 extFiles.add(new File(logDir, fn));
108             }
109         }
110         
111         // Load Cred.  We don't follow Visitor, because we have to gather up everything into Identity Anyway
112         // to find the last one.
113     }
114
115     @Override
116     protected void run(AuthzTrans trans) {
117         final int maxBatch = 50;
118
119         // Setup Date boundaries
120         final Holder<GregorianCalendar> hgc = new Holder<>(new GregorianCalendar());
121         final GregorianCalendar now = new GregorianCalendar();
122
123         ///////////////////////////
124         trans.info().log("Bulk Extend Expiring User-Roles and Creds");
125
126         final Holder<List<String>> info = new Holder<>(null);
127         final Holder<StringBuilder> hsb = new Holder<>(null);
128
129         for(File f : extFiles) {
130             CSV csv = new CSV(env.access(),f);
131             try {
132                 csv.visit(new CSV.Visitor() {
133                     final Holder<Integer> hi = new Holder<>(0); 
134
135                     @Override
136                     public void visit(List<String> row) throws IOException, CadiException {
137                         GregorianCalendar gc;
138                         int i = hi.get();
139                         StringBuilder sb = hsb.get();
140                         if(sb==null) {
141                             hsb.set(sb=cqlBatch.begin());
142                         }
143                         switch(row.get(0)) {
144                             case "info":
145                                 info.set(row);
146                                 break;
147                             case "ur":
148                                 hi.set(++i);
149                                 gc = hgc.get();
150                                 gc.setTime(new Date(Long.parseLong(row.get(6))));
151                                 if(gc.before(now)) {
152                                     gc.setTime(now.getTime());
153                                 }
154                                 gc.add(gcType, extendBy);
155                                 UserRole.batchExtend(sb,row,gc.getTime());
156                                 break;
157                             case "cred":
158                                 int ctype = Integer.parseInt(row.get(3));
159                                 if(ctype == CredDAO.BASIC_AUTH_SHA256 || ctype == CredDAO.BASIC_AUTH) {
160                                     Result<List<Data>> result = credDAO.readID(noAvg, row.get(1));
161                                     if(result.isOKhasData()) {
162                                         for(CredDAO.Data cd : result.value) {
163                                             if(cd.type == CredDAO.BASIC_AUTH_SHA256 || cd.type == CredDAO.BASIC_AUTH) {
164                                                 String prev;
165                                                 prev=Chrono.dateOnlyStamp(cd.expires);
166                                                 if(row.get(4).equals(prev)){
167                                                     gc = hgc.get();
168                                                     gc.setTime(new Date(Long.parseLong(row.get(5))));
169                                                     if(gc.before(now)) {
170                                                         gc.setTime(now.getTime());
171                                                     }
172                                                     gc.add(gcType, extendBy);
173                                                     cd.expires = gc.getTime();
174                                                     if(dryRun) {
175                                                         noAvg.info().printf("Would extend %s, %d - %s to %s",cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
176                                                     } else {
177                                                         Result<Void> r = credDAO.update(noAvg, cd, true);
178                                                         noAvg.info().printf("%s %s, %d - %s to %s",
179                                                                 r.isOK()?"Extended":"Failed to Extend",
180                                                                 cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
181                                                     }
182                                                 }
183                                             }
184                                         }
185                                     }
186                                 }
187                                 break;
188                         }
189                         if(i%maxBatch==0 && sb!=null) {
190                             cqlBatch.execute(dryRun);
191                             hi.set(1);
192                             hsb.set(sb=null);
193                         }
194                     }
195                 });
196             } catch (IOException | CadiException e) {
197                 e.printStackTrace();
198             }
199         }
200         
201         // Cleanup, if required.
202         cqlBatch.execute(dryRun);
203
204     }
205     
206     @Override
207     protected void _close(AuthzTrans trans) {
208         trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
209         credDAO.close(trans);
210         session.close();
211     }
212
213 }