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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.SortedMap;
31 import java.util.TreeMap;
33 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
34 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.util.CSV;
38 import org.onap.aaf.misc.env.Env;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans;
41 import org.onap.aaf.misc.env.util.Chrono;
43 import com.datastax.driver.core.ResultSet;
44 import com.datastax.driver.core.Row;
45 import com.datastax.driver.core.Session;
46 import com.datastax.driver.core.SimpleStatement;
47 import com.datastax.driver.core.Statement;
49 public class UserRole implements Cloneable, CacheChange.Data {
51 public static final String UR = "ur";
52 public static final String APPROVE_UR = "ur";
54 private static final String SEPARATOR = "\",\"";
57 private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
58 private static final String REPLAY_FMT = "%s|%s|%s|%s|%s\n";
59 private static final String DELETE_FMT = "# %s\n"+ REPLAY_FMT;
61 private static final List<UserRole> data = new ArrayList<>();
62 private static final SortedMap<String,List<UserRole>> byUser = new TreeMap<>();
63 private static final SortedMap<String,List<UserRole>> byRole = new TreeMap<>();
64 private static final CacheChange<UserRole> cache = new CacheChange<>();
65 private static PrintStream urDelete = System.out;
66 private static PrintStream urRecover = System.err;
67 private static int totalLoaded;
71 public static final Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
73 public UserRole create(Row row) {
74 return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
78 public String select() {
79 return "select user,role,ns,rname,expires from authz.user_role";
83 public UserRole(String user, String ns, String rname, Date expires) {
84 urdd = new UserRoleDAO.Data();
86 urdd.role = ns + '.' + rname;
89 urdd.expires = expires;
92 public UserRole(String user, String role, String ns, String rname, Date expires) {
93 urdd = new UserRoleDAO.Data();
98 urdd.expires = expires;
101 public static List<UserRole> getData() {
105 public static SortedMap<String, List<UserRole>> getByUser() {
109 public static SortedMap<String, List<UserRole>> getByRole() {
113 public static void load(Trans trans, Session session, Creator<UserRole> creator) {
114 load(trans,session,creator,null,new DataLoadVisitor());
117 public static void load(Trans trans, Session session, Creator<UserRole> creator, Visitor<UserRole> visitor ) {
118 load(trans,session,creator,null,visitor);
121 public static void loadOneRole(Trans trans, Session session, Creator<UserRole> creator, String role, Visitor<UserRole> visitor) {
122 load(trans,session,creator,"role='" + role +"' ALLOW FILTERING;",visitor);
125 public static void loadOneUser(Trans trans, Session session, Creator<UserRole> creator, String user, Visitor<UserRole> visitor ) {
126 load(trans,session,creator,"user='"+ user +'\'',visitor);
129 public static void load(Trans trans, CSV csv, Creator<UserRole> creator, Visitor<UserRole> visitor) throws IOException, CadiException {
130 // public UserRole(String user, String role, String ns, String rname, Date expires) {
132 visitor.visit(new UserRole(row.get(1),row.get(2),row.get(3),row.get(4),
133 new Date(Long.parseLong(row.get(6)))));
137 private static void load(Trans trans, Session session, Creator<UserRole> creator, String where, Visitor<UserRole> visitor) {
138 String query = creator.query(where);
139 trans.debug().log( "query: " + query );
140 TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
144 Statement stmt = new SimpleStatement( query );
145 results = session.execute(stmt);
150 tt = trans.start("Load UserRole", Env.SUB);
152 iterateResults(creator, results.iterator(), visitor);
157 trans.debug().log("Loaded",totalLoaded,"UserRoles");
161 private static void iterateResults(Creator<UserRole> creator, Iterator<Row> iter, Visitor<UserRole> visit ) {
163 while (iter.hasNext()) {
166 UserRole ur = creator.create(row);
171 public static class DataLoadVisitor implements Visitor<UserRole> {
173 public void visit(UserRole ur) {
176 List<UserRole> lur = byUser.get(ur.urdd.user);
178 lur = new ArrayList<>();
179 byUser.put(ur.urdd.user, lur);
183 lur = byRole.get(ur.urdd.role);
185 lur = new ArrayList<>();
186 byRole.put(ur.urdd.role, lur);
192 public int totalLoaded() {
196 public int deleted() {
201 public void expunge() {
204 List<UserRole> lur = byUser.get(urdd.user);
209 lur = byRole.get(urdd.role);
215 public static void setDeleteStream(PrintStream ds) {
219 public static void setRecoverStream(PrintStream ds) {
223 public static long count(Trans trans, Session session) {
224 String query = "select count(*) from authz.user_role LIMIT 1000000;";
225 trans.info().log( "query: " + query );
226 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
229 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
230 results = session.execute(stmt);
231 return results.one().getLong(0);
237 public UserRoleDAO.Data urdd() {
241 public String user() {
245 public String role() {
253 public String rname() {
257 public Date expires() {
261 public void expires(Date time) {
265 public String toString() {
266 return "\"" + urdd.user + SEPARATOR + urdd.role + SEPARATOR + urdd.ns + SEPARATOR + urdd.rname + SEPARATOR
267 + Chrono.dateOnlyStamp(urdd.expires);
270 public static UserRole get(String u, String r) {
271 List<UserRole> lur = byUser.get(u);
273 for (UserRole ur : lur) {
275 if (ur.urdd.role.equals(r)) {
283 // SAFETY - DO NOT DELETE USER ROLES DIRECTLY FROM BATCH FILES!!!
284 // We write to a file, and validate. If the size is iffy, we email Support
285 public void delayDelete(AuthzTrans trans, String text, boolean dryRun) {
286 String dt = Chrono.dateTime(urdd.expires);
288 trans.info().printf(LOG_FMT,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
290 trans.info().printf(LOG_FMT,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
292 urDelete.printf(DELETE_FMT,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
293 urRecover.printf(REPLAY_FMT,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
295 cache.delayedDelete(this);
301 * Calls expunge() for all deleteCached entries
303 public static void resetLocalData() {
304 cache.resetLocalData();
307 public void row(final CSV.Writer csvw, String tag) {
308 csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime());
311 public void row(final CSV.Writer csvw, String tag, String reason) {
312 csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime(),reason);
315 public static Data row(List<String> row) {
316 Data data = new Data();
317 data.user = row.get(1);
318 data.role = row.get(2);
319 data.ns = row.get(3);
320 data.rname = row.get(4);
321 data.expires = new Date(Long.parseLong(row.get(6)));
325 public static void batchDelete(StringBuilder sb, List<String> row) {
326 sb.append("DELETE from authz.user_role WHERE user='");
327 sb.append(row.get(1));
328 sb.append("' AND role='");
329 sb.append(row.get(2));
333 public static void batchExtend(StringBuilder sb, List<String> row, Date newDate ) {
334 sb.append("UPDATE authz.user_role SET expires='");
335 sb.append(Chrono.dateTime(newDate));
336 sb.append("' WHERE user='");
337 sb.append(row.get(1));
338 sb.append("' AND role='");
339 sb.append(row.get(2));
343 public void batchExtend(StringBuilder sb, Date newDate) {
344 sb.append("UPDATE authz.user_role SET expires='");
345 sb.append(Chrono.dateTime(newDate));
346 sb.append("' WHERE user='");
348 sb.append("' AND role='");
353 public void batchUpdateExpires(StringBuilder sb) {
354 sb.append("UPDATE authz.user_role SET expires='");
355 sb.append(Chrono.dateTime(expires()));
356 sb.append("' WHERE user='");
358 sb.append("' AND role='");
363 public static String histMemo(String fmt, List<String> row) {
365 if(row.size()>7) { // Reason included
366 reason = String.format("%s removed from %s because %s",
367 row.get(1),row.get(2),row.get(7));
369 reason = String.format(fmt, row.get(1),row.get(2), row.get(5));
374 public static String histSubject(List<String> row) {
375 return row.get(1) + '|' + row.get(2);
378 public static void clear() {
382 cache.resetLocalData();