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.dao.cass.UserRoleDAO;
33 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.cadi.util.CSV;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.TimeTaken;
38 import org.onap.aaf.misc.env.Trans;
39 import org.onap.aaf.misc.env.util.Chrono;
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43 import com.datastax.driver.core.Session;
44 import com.datastax.driver.core.SimpleStatement;
45 import com.datastax.driver.core.Statement;
47 public class UserRole implements Cloneable, CacheChange.Data {
49 public static final String UR = "ur";
50 public static final String APPROVE_UR = "ur";
52 private static final String SEPARATOR = "\",\"";
55 private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
56 private static final String REPLAY_FMT = "%s|%s|%s|%s|%s\n";
57 private static final String DELETE_FMT = "# %s\n"+ REPLAY_FMT;
59 private static final List<UserRole> data = new ArrayList<>();
60 private static final SortedMap<String,List<UserRole>> byUser = new TreeMap<>();
61 private static final SortedMap<String,List<UserRole>> byRole = new TreeMap<>();
62 private static final CacheChange<UserRole> cache = new CacheChange<>();
63 private static PrintStream urDelete = System.out;
64 private static PrintStream urRecover = System.err;
65 private static int totalLoaded;
69 public static final Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
71 public UserRole create(Row row) {
72 return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
76 public String select() {
77 return "select user,role,ns,rname,expires from authz.user_role";
81 public UserRole(String user, String ns, String rname, Date expires) {
82 urdd = new UserRoleDAO.Data();
84 urdd.role = ns + '.' + rname;
87 urdd.expires = expires;
90 public UserRole(String user, String role, String ns, String rname, Date expires) {
91 urdd = new UserRoleDAO.Data();
96 urdd.expires = expires;
99 public static List<UserRole> getData() {
103 public static SortedMap<String, List<UserRole>> getByUser() {
107 public static SortedMap<String, List<UserRole>> getByRole() {
111 public static void load(Trans trans, Session session, Creator<UserRole> creator) {
112 load(trans,session,creator,null,new DataLoadVisitor());
115 public static void load(Trans trans, Session session, Creator<UserRole> creator, Visitor<UserRole> visitor ) {
116 load(trans,session,creator,null,visitor);
119 public static void loadOneRole(Trans trans, Session session, Creator<UserRole> creator, String role, Visitor<UserRole> visitor) {
120 load(trans,session,creator,"role='" + role +"' ALLOW FILTERING;",visitor);
123 public static void loadOneUser(Trans trans, Session session, Creator<UserRole> creator, String user, Visitor<UserRole> visitor ) {
124 load(trans,session,creator,"user='"+ user +'\'',visitor);
127 private static void load(Trans trans, Session session, Creator<UserRole> creator, String where, Visitor<UserRole> visitor) {
128 String query = creator.query(where);
129 trans.debug().log( "query: " + query );
130 TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
134 Statement stmt = new SimpleStatement( query );
135 results = session.execute(stmt);
140 tt = trans.start("Load UserRole", Env.SUB);
142 iterateResults(creator, results.iterator(), visitor);
147 trans.debug().log("Loaded",totalLoaded,"UserRoles");
151 private static void iterateResults(Creator<UserRole> creator, Iterator<Row> iter, Visitor<UserRole> visit ) {
153 while (iter.hasNext()) {
156 UserRole ur = creator.create(row);
161 public static class DataLoadVisitor implements Visitor<UserRole> {
163 public void visit(UserRole ur) {
166 List<UserRole> lur = byUser.get(ur.urdd.user);
168 lur = new ArrayList<>();
169 byUser.put(ur.urdd.user, lur);
173 lur = byRole.get(ur.urdd.role);
175 lur = new ArrayList<>();
176 byRole.put(ur.urdd.role, lur);
182 public int totalLoaded() {
186 public int deleted() {
191 public void expunge() {
194 List<UserRole> lur = byUser.get(urdd.user);
199 lur = byRole.get(urdd.role);
205 public static void setDeleteStream(PrintStream ds) {
209 public static void setRecoverStream(PrintStream ds) {
213 public static long count(Trans trans, Session session) {
214 String query = "select count(*) from authz.user_role LIMIT 1000000;";
215 trans.info().log( "query: " + query );
216 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
219 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
220 results = session.execute(stmt);
221 return results.one().getLong(0);
227 public UserRoleDAO.Data urdd() {
231 public String user() {
235 public String role() {
243 public String rname() {
247 public Date expires() {
251 public void expires(Date time) {
255 public String toString() {
256 return "\"" + urdd.user + SEPARATOR + urdd.role + SEPARATOR + urdd.ns + SEPARATOR + urdd.rname + SEPARATOR
257 + Chrono.dateOnlyStamp(urdd.expires);
260 public static UserRole get(String u, String r) {
261 List<UserRole> lur = byUser.get(u);
263 for (UserRole ur : lur) {
265 if (ur.urdd.role.equals(r)) {
273 // SAFETY - DO NOT DELETE USER ROLES DIRECTLY FROM BATCH FILES!!!
274 // We write to a file, and validate. If the size is iffy, we email Support
275 public void delayDelete(AuthzTrans trans, String text, boolean dryRun) {
276 String dt = Chrono.dateTime(urdd.expires);
278 trans.info().printf(LOG_FMT,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
280 trans.info().printf(LOG_FMT,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
282 urDelete.printf(DELETE_FMT,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
283 urRecover.printf(REPLAY_FMT,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
285 cache.delayedDelete(this);
291 * Calls expunge() for all deleteCached entries
293 public static void resetLocalData() {
294 cache.resetLocalData();
297 public void row(final CSV.Writer csvw, String tag) {
298 csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime());
301 public void row(final CSV.Writer csvw, String tag, String reason) {
302 csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime(),reason);
305 public static Data row(List<String> row) {
306 Data data = new Data();
307 data.user = row.get(1);
308 data.role = row.get(2);
309 data.ns = row.get(3);
310 data.rname = row.get(4);
311 data.expires = new Date(Long.parseLong(row.get(6)));
315 public static void batchDelete(StringBuilder sb, List<String> row) {
316 sb.append("DELETE from authz.user_role WHERE user='");
317 sb.append(row.get(1));
318 sb.append("' AND role='");
319 sb.append(row.get(2));
323 public static void batchExtend(StringBuilder sb, List<String> row, Date newDate ) {
324 sb.append("UPDATE authz.user_role SET expires='");
325 sb.append(Chrono.dateTime(newDate));
326 sb.append("' WHERE user='");
327 sb.append(row.get(1));
328 sb.append("' AND role='");
329 sb.append(row.get(2));
333 public void batchUpdateExpires(StringBuilder sb) {
334 sb.append("UPDATE authz.user_role SET expires='");
335 sb.append(Chrono.dateTime(expires()));
336 sb.append("' WHERE user='");
338 sb.append("' AND role='");
343 public static String histMemo(String fmt, List<String> row) {
345 if(row.size()>7) { // Reason included
346 reason = String.format("%s removed from %s because %s",
347 row.get(1),row.get(2),row.get(7));
349 reason = String.format(fmt, row.get(1),row.get(2), row.get(5));
354 public static String histSubject(List<String> row) {
355 return row.get(1) + '|' + row.get(2);
358 public static void clear() {
362 cache.resetLocalData();