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.PrintStream;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.SortedMap;
30 import java.util.TreeMap;
32 import org.onap.aaf.auth.batch.actions.URDelete;
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.util.CSV;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.TimeTaken;
39 import org.onap.aaf.misc.env.Trans;
40 import org.onap.aaf.misc.env.util.Chrono;
42 import com.datastax.driver.core.ResultSet;
43 import com.datastax.driver.core.Row;
44 import com.datastax.driver.core.Session;
45 import com.datastax.driver.core.SimpleStatement;
46 import com.datastax.driver.core.Statement;
48 public class UserRole implements Cloneable, CacheChange.Data {
50 public static final String UR = "ur";
51 public static final String APPROVE_UR = "ur";
53 private static final String SEPARATOR = "\",\"";
56 private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
57 private static final String REPLAY_FMT = "%s|%s|%s|%s|%s\n";
58 private static final String DELETE_FMT = "# %s\n"+ REPLAY_FMT;
60 private static final List<UserRole> data = new ArrayList<>();
61 private static final SortedMap<String,List<UserRole>> byUser = new TreeMap<>();
62 private static final SortedMap<String,List<UserRole>> byRole = new TreeMap<>();
63 private static final CacheChange<UserRole> cache = new CacheChange<>();
64 private static PrintStream urDelete = System.out;
65 private static PrintStream urRecover = System.err;
66 private static int totalLoaded;
70 public static final Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
72 public UserRole create(Row row) {
73 return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
77 public String select() {
78 return "select user,role,ns,rname,expires from authz.user_role";
82 public UserRole(String user, String ns, String rname, Date expires) {
83 urdd = new UserRoleDAO.Data();
85 urdd.role = ns + '.' + rname;
88 urdd.expires = expires;
91 public UserRole(String user, String role, String ns, String rname, Date expires) {
92 urdd = new UserRoleDAO.Data();
97 urdd.expires = expires;
100 public static List<UserRole> getData() {
104 public static SortedMap<String, List<UserRole>> getByUser() {
108 public static SortedMap<String, List<UserRole>> getByRole() {
112 public static void load(Trans trans, Session session, Creator<UserRole> creator) {
113 load(trans,session,creator,null,new DataLoadVisitor());
116 public static void load(Trans trans, Session session, Creator<UserRole> creator, Visitor<UserRole> visitor ) {
117 load(trans,session,creator,null,visitor);
120 public static void loadOneRole(Trans trans, Session session, Creator<UserRole> creator, String role, Visitor<UserRole> visitor) {
121 load(trans,session,creator,"role='" + role +"' ALLOW FILTERING;",visitor);
124 public static void loadOneUser(Trans trans, Session session, Creator<UserRole> creator, String user, Visitor<UserRole> visitor ) {
125 load(trans,session,creator,"role='"+ user +"';",visitor);
128 private static void load(Trans trans, Session session, Creator<UserRole> creator, String where, Visitor<UserRole> visitor) {
129 String query = creator.query(where);
130 trans.info().log( "query: " + query );
131 TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
135 Statement stmt = new SimpleStatement( query );
136 results = session.execute(stmt);
141 tt = trans.start("Load UserRole", Env.SUB);
143 iterateResults(creator, results.iterator(), visitor);
148 trans.info().log("Loaded",totalLoaded,"UserRoles");
152 private static void iterateResults(Creator<UserRole> creator, Iterator<Row> iter, Visitor<UserRole> visit ) {
154 while (iter.hasNext()) {
157 UserRole ur = creator.create(row);
162 public static class DataLoadVisitor implements Visitor<UserRole> {
164 public void visit(UserRole ur) {
167 List<UserRole> lur = byUser.get(ur.urdd.user);
169 lur = new ArrayList<>();
170 byUser.put(ur.urdd.user, lur);
174 lur = byRole.get(ur.urdd.role);
176 lur = new ArrayList<>();
177 byRole.put(ur.urdd.role, lur);
183 public int totalLoaded() {
187 public int deleted() {
192 public void expunge() {
195 List<UserRole> lur = byUser.get(urdd.user);
200 lur = byRole.get(urdd.role);
206 public static void setDeleteStream(PrintStream ds) {
210 public static void setRecoverStream(PrintStream ds) {
214 public static long count(Trans trans, Session session) {
215 String query = "select count(*) from authz.user_role LIMIT 1000000;";
216 trans.info().log( "query: " + query );
217 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
220 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
221 results = session.execute(stmt);
222 return results.one().getLong(0);
228 public UserRoleDAO.Data urdd() {
232 public String user() {
236 public String role() {
244 public String rname() {
248 public Date expires() {
252 public void expires(Date time) {
256 public String toString() {
257 return "\"" + urdd.user + SEPARATOR + urdd.role + SEPARATOR + urdd.ns + SEPARATOR + urdd.rname + SEPARATOR
258 + Chrono.dateOnlyStamp(urdd.expires);
261 public static UserRole get(String u, String r) {
262 List<UserRole> lur = byUser.get(u);
264 for (UserRole ur : lur) {
266 if (ur.urdd.role.equals(r)) {
274 // SAFETY - DO NOT DELETE USER ROLES DIRECTLY FROM BATCH FILES!!!
275 // We write to a file, and validate. If the size is iffy, we email Support
276 public void delayDelete(AuthzTrans trans, String text, boolean dryRun) {
277 String dt = Chrono.dateTime(urdd.expires);
279 trans.info().printf(LOG_FMT,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
281 trans.info().printf(LOG_FMT,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
283 urDelete.printf(DELETE_FMT,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
284 urRecover.printf(REPLAY_FMT,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
286 cache.delayedDelete(this);
292 * Calls expunge() for all deleteCached entries
294 public static void resetLocalData() {
295 cache.resetLocalData();
298 public static int sizeForDeletion() {
299 return cache.cacheSize();
302 public static boolean pendingDelete(UserRole ur) {
303 return cache.contains(ur);
306 public static void actuateDeletionNow(AuthzTrans trans, URDelete directDel) {
307 for (UserRole ur : cache.getRemoved()) {
308 directDel.exec(trans, ur, "Actuating UserRole Deletion");
310 cache.getRemoved().clear();
311 cache.resetLocalData();
314 public void row(final CSV.Writer csvw, String tag) {
315 csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime());
318 public void row(final CSV.Writer csvw, String tag, String reason) {
319 csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime(),reason);
322 public static Data row(List<String> row) {
323 Data data = new Data();
324 data.user = row.get(1);
325 data.role = row.get(2);
326 data.ns = row.get(3);
327 data.rname = row.get(4);
328 data.expires = new Date(Long.parseLong(row.get(6)));
332 public static void batchDelete(StringBuilder sb, List<String> row) {
333 sb.append("DELETE from authz.user_role WHERE user='");
334 sb.append(row.get(1));
335 sb.append("' AND role='");
336 sb.append(row.get(2));
340 public static void batchExtend(StringBuilder sb, List<String> row, String newDate ) {
341 sb.append("UPDATE authz.user_role SET expires='");
343 sb.append("' WHERE user='");
344 sb.append(row.get(1));
345 sb.append("' AND role='");
346 sb.append(row.get(2));
350 public static String histMemo(String fmt, List<String> row) {
352 if(row.size()>7) { // Reason included
353 reason = String.format("%s removed from %s because %s",
354 row.get(1),row.get(2),row.get(7));
356 reason = String.format(fmt, row.get(1),row.get(2), row.get(5));
361 public static String histSubject(List<String> row) {
362 return row.get(1) + '|' + row.get(2);