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.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.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 private static final String SEPARATOR = "\",\"";
53 private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
54 private static final String REPLAY_FMT = "%s|%s|%s|%s|%s\n";
55 private static final String DELETE_FMT = "# %s\n"+ REPLAY_FMT;
57 private static final List<UserRole> data = new ArrayList<>();
58 private static final SortedMap<String,List<UserRole>> byUser = new TreeMap<>();
59 private static final SortedMap<String,List<UserRole>> byRole = new TreeMap<>();
60 private static final CacheChange<UserRole> cache = new CacheChange<>();
61 private static PrintStream urDelete = System.out;
62 private static PrintStream urRecover = System.err;
63 private static int totalLoaded;
67 public static final Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
69 public UserRole create(Row row) {
70 return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
74 public String select() {
75 return "select user,role,ns,rname,expires from authz.user_role";
79 public UserRole(String user, String ns, String rname, Date expires) {
80 urdd = new UserRoleDAO.Data();
82 urdd.role = ns + '.' + rname;
85 urdd.expires = expires;
88 public UserRole(String user, String role, String ns, String rname, Date expires) {
89 urdd = new UserRoleDAO.Data();
94 urdd.expires = expires;
97 public static List<UserRole> getData() {
101 public static SortedMap<String, List<UserRole>> getByUser() {
105 public static SortedMap<String, List<UserRole>> getByRole() {
109 public static void load(Trans trans, Session session, Creator<UserRole> creator, Visitor<UserRole> visitor ) {
110 load(trans,session,creator,null,visitor);
113 public static void loadOneRole(Trans trans, Session session, Creator<UserRole> creator, String role, Visitor<UserRole> visitor) {
114 load(trans,session,creator,"role='" + role +"' ALLOW FILTERING;",visitor);
117 public static void loadOneUser(Trans trans, Session session, Creator<UserRole> creator, String user, Visitor<UserRole> visitor ) {
118 load(trans,session,creator,"role='"+ user +"';",visitor);
121 private static void load(Trans trans, Session session, Creator<UserRole> creator, String where, Visitor<UserRole> visitor) {
122 String query = creator.query(where);
123 trans.info().log( "query: " + query );
124 TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
128 Statement stmt = new SimpleStatement( query );
129 results = session.execute(stmt);
134 tt = trans.start("Load UserRole", Env.SUB);
136 iterateResults(creator, results.iterator(), visitor);
141 trans.info().log("Loaded",totalLoaded,"UserRoles");
145 private static void iterateResults(Creator<UserRole> creator, Iterator<Row> iter, Visitor<UserRole> visit ) {
147 while (iter.hasNext()) {
150 UserRole ur = creator.create(row);
155 public static class DataLoadVisitor implements Visitor<UserRole> {
157 public void visit(UserRole ur) {
160 List<UserRole> lur = byUser.get(ur.urdd.user);
162 lur = new ArrayList<>();
163 byUser.put(ur.urdd.user, lur);
167 lur = byRole.get(ur.urdd.role);
169 lur = new ArrayList<>();
170 byRole.put(ur.urdd.role, lur);
176 public int totalLoaded() {
180 public int deleted() {
185 public void expunge() {
188 List<UserRole> lur = byUser.get(urdd.user);
193 lur = byRole.get(urdd.role);
199 public static void setDeleteStream(PrintStream ds) {
203 public static void setRecoverStream(PrintStream ds) {
207 public static long count(Trans trans, Session session) {
208 String query = "select count(*) from authz.user_role LIMIT 1000000;";
209 trans.info().log( "query: " + query );
210 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
213 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
214 results = session.execute(stmt);
215 return results.one().getLong(0);
221 public UserRoleDAO.Data urdd() {
225 public String user() {
229 public String role() {
237 public String rname() {
241 public Date expires() {
245 public void expires(Date time) {
249 public String toString() {
250 return "\"" + urdd.user + SEPARATOR + urdd.role + SEPARATOR + urdd.ns + SEPARATOR + urdd.rname + SEPARATOR
251 + Chrono.dateOnlyStamp(urdd.expires);
254 public static UserRole get(String u, String r) {
255 List<UserRole> lur = byUser.get(u);
257 for (UserRole ur : lur) {
259 if (ur.urdd.role.equals(r)) {
267 // SAFETY - DO NOT DELETE USER ROLES DIRECTLY FROM BATCH FILES!!!
268 // We write to a file, and validate. If the size is iffy, we email Support
269 public void delayDelete(AuthzTrans trans, String text, boolean dryRun) {
270 String dt = Chrono.dateTime(urdd.expires);
272 trans.info().printf(LOG_FMT,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
274 trans.info().printf(LOG_FMT,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
276 urDelete.printf(DELETE_FMT,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
277 urRecover.printf(REPLAY_FMT,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
279 cache.delayedDelete(this);
285 * Calls expunge() for all deleteCached entries
287 public static void resetLocalData() {
288 cache.resetLocalData();
291 public static int sizeForDeletion() {
292 return cache.cacheSize();
295 public static boolean pendingDelete(UserRole ur) {
296 return cache.contains(ur);
299 public static void actuateDeletionNow(AuthzTrans trans, URDelete directDel) {
300 for (UserRole ur : cache.getRemoved()) {
301 directDel.exec(trans, ur, "Actuating UserRole Deletion");
303 cache.getRemoved().clear();
304 cache.resetLocalData();
307 public void row(CSV.Writer csvw) {
308 csvw.row("ur",user(),role(),Chrono.dateOnlyStamp(expires()));
311 public static void row(StringBuilder sb, List<String> row) {
312 sb.append("DELETE from authz.user_role WHERE user='");
313 sb.append(row.get(1));
314 sb.append("' AND role='");
315 sb.append(row.get(2));