1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.dao.aaf.cass;
\r
25 import java.io.ByteArrayOutputStream;
\r
26 import java.io.DataInputStream;
\r
27 import java.io.DataOutputStream;
\r
28 import java.io.IOException;
\r
29 import java.nio.ByteBuffer;
\r
30 import java.util.HashSet;
\r
31 import java.util.List;
\r
32 import java.util.Set;
\r
34 import com.att.authz.env.AuthzTrans;
\r
35 import com.att.authz.layer.Result;
\r
36 import com.att.dao.Bytification;
\r
37 import com.att.dao.Cached;
\r
38 import com.att.dao.CassAccess;
\r
39 import com.att.dao.CassDAOImpl;
\r
40 import com.att.dao.DAOException;
\r
41 import com.att.dao.Loader;
\r
42 import com.att.dao.Streamer;
\r
43 import com.att.dao.aaf.hl.Question;
\r
44 import com.att.inno.env.APIException;
\r
45 import com.att.inno.env.util.Split;
\r
46 import com.datastax.driver.core.Cluster;
\r
47 import com.datastax.driver.core.Row;
\r
48 import com.datastax.driver.core.exceptions.DriverException;
\r
50 public class PermDAO extends CassDAOImpl<AuthzTrans,PermDAO.Data> {
\r
52 public static final String TABLE = "perm";
\r
54 public static final int CACHE_SEG = 0x40; // yields segment 0x0-0x3F
\r
55 private static final String STAR = "*";
\r
57 private final HistoryDAO historyDAO;
\r
58 private final CacheInfoDAO infoDAO;
\r
60 private PSInfo psNS, psChildren, psByType;
\r
62 public PermDAO(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
\r
63 super(trans, PermDAO.class.getSimpleName(),cluster,keyspace,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
\r
65 historyDAO = new HistoryDAO(trans, this);
\r
66 infoDAO = new CacheInfoDAO(trans,this);
\r
69 public PermDAO(AuthzTrans trans, HistoryDAO hDAO, CacheInfoDAO ciDAO) {
\r
70 super(trans, PermDAO.class.getSimpleName(),hDAO,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
\r
77 private static final int KEYLIMIT = 4;
\r
78 public static class Data extends CacheableData implements Bytification {
\r
81 public String instance;
\r
82 public String action;
\r
83 public Set<String> roles;
\r
84 public String description;
\r
88 public Data(NsSplit nss, String instance, String action) {
\r
91 this.instance = instance;
\r
92 this.action = action;
\r
95 public String fullType() {
\r
96 return ns + '.' + type;
\r
99 public String fullPerm() {
\r
100 return ns + '.' + type + '|' + instance + '|' + action;
\r
103 public String encode() {
\r
104 return ns + '|' + type + '|' + instance + '|' + action;
\r
108 * Decode Perm String, including breaking into appropriate Namespace
\r
115 public static Result<Data> decode(AuthzTrans trans, Question q, String p) {
\r
116 String[] ss = Split.splitTrim('|', p,4);
\r
118 return Result.err(Status.ERR_BadData,"Perm Encodings must be separated by '|'");
\r
120 Data data = new Data();
\r
121 if(ss[3]==null) { // older 3 part encoding must be evaluated for NS
\r
122 Result<NsSplit> nss = q.deriveNsSplit(trans, ss[0]);
\r
124 return Result.err(nss);
\r
126 data.ns=nss.value.ns;
\r
127 data.type=nss.value.name;
\r
128 data.instance=ss[1];
\r
130 } else { // new 4 part encoding
\r
133 data.instance=ss[2];
\r
136 return Result.ok(data);
\r
140 * Decode Perm String, including breaking into appropriate Namespace
\r
147 public static Result<String[]> decodeToArray(AuthzTrans trans, Question q, String p) {
\r
148 String[] ss = Split.splitTrim('|', p,4);
\r
150 return Result.err(Status.ERR_BadData,"Perm Encodings must be separated by '|'");
\r
153 if(ss[3]==null) { // older 3 part encoding must be evaluated for NS
\r
156 Result<NsSplit> nss = q.deriveNsSplit(trans, ss[0]);
\r
158 return Result.err(nss);
\r
160 ss[1] = nss.value.name;
\r
161 ss[0] = nss.value.ns;
\r
163 return Result.ok(ss);
\r
166 public static Data create(NsDAO.Data ns, String name) {
\r
167 NsSplit nss = new NsSplit(ns,name);
\r
168 Data rv = new Data();
\r
170 String[] s = nss.name.split("\\|");
\r
184 rv.instance = STAR;
\r
190 public static Data create(AuthzTrans trans, Question q, String name) {
\r
191 String[] s = name.split("\\|");
\r
192 Result<NsSplit> rdns = q.deriveNsSplit(trans, s[0]);
\r
193 Data rv = new PermDAO.Data();
\r
194 if(rdns.isOKhasData()) {
\r
208 rv.instance = STAR;
\r
215 ////////////////////////////////////////
\r
217 public Set<String> roles(boolean mutable) {
\r
218 if (roles == null) {
\r
219 roles = new HashSet<String>();
\r
220 } else if (mutable && !(roles instanceof HashSet)) {
\r
221 roles = new HashSet<String>(roles);
\r
227 public int[] invalidate(Cached<?,?> cache) {
\r
230 seg(cache,ns,type),
\r
231 seg(cache,ns,type,STAR),
\r
232 seg(cache,ns,type,instance,action)
\r
237 public ByteBuffer bytify() throws IOException {
\r
238 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
239 PermLoader.deflt.marshal(this, new DataOutputStream(baos));
\r
240 return ByteBuffer.wrap(baos.toByteArray());
\r
244 public void reconstitute(ByteBuffer bb) throws IOException {
\r
245 PermLoader.deflt.unmarshal(this, toDIS(bb));
\r
249 public String toString() {
\r
254 private static class PermLoader extends Loader<Data> implements Streamer<Data> {
\r
255 public static final int MAGIC=283939453;
\r
256 public static final int VERSION=1;
\r
257 public static final int BUFF_SIZE=96;
\r
259 public static final PermLoader deflt = new PermLoader(KEYLIMIT);
\r
261 public PermLoader(int keylimit) {
\r
266 public Data load(Data data, Row row) {
\r
267 // Int more efficient Match "fields" string
\r
268 data.ns = row.getString(0);
\r
269 data.type = row.getString(1);
\r
270 data.instance = row.getString(2);
\r
271 data.action = row.getString(3);
\r
272 data.roles = row.getSet(4,String.class);
\r
273 data.description = row.getString(5);
\r
278 protected void key(Data data, int _idx, Object[] obj) {
\r
281 obj[++idx]=data.type;
\r
282 obj[++idx]=data.instance;
\r
283 obj[++idx]=data.action;
\r
287 protected void body(Data data, int _idx, Object[] obj) {
\r
289 obj[idx]=data.roles;
\r
290 obj[++idx]=data.description;
\r
294 public void marshal(Data data, DataOutputStream os) throws IOException {
\r
295 writeHeader(os,MAGIC,VERSION);
\r
296 writeString(os, data.ns);
\r
297 writeString(os, data.type);
\r
298 writeString(os, data.instance);
\r
299 writeString(os, data.action);
\r
300 writeStringSet(os, data.roles);
\r
301 writeString(os, data.description);
\r
305 public void unmarshal(Data data, DataInputStream is) throws IOException {
\r
306 /*int version = */readHeader(is,MAGIC,VERSION);
\r
307 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
\r
308 byte[] buff = new byte[BUFF_SIZE];
\r
309 data.ns = readString(is, buff);
\r
310 data.type = readString(is,buff);
\r
311 data.instance = readString(is,buff);
\r
312 data.action = readString(is,buff);
\r
313 data.roles = readStringSet(is,buff);
\r
314 data.description = readString(is,buff);
\r
318 private void init(AuthzTrans trans) {
\r
319 // the 3 is the number of key fields
\r
320 String[] helpers = setCRUD(trans, TABLE, Data.class, PermLoader.deflt);
\r
322 // Other SELECT style statements... match with a local Method
\r
323 psByType = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
\r
324 " WHERE ns = ? AND type = ?", new PermLoader(2) {
\r
326 protected void key(Data data, int idx, Object[] obj) {
\r
327 obj[idx]=data.type;
\r
329 },readConsistency);
\r
331 psNS = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
\r
332 " WHERE ns = ?", new PermLoader(1),readConsistency);
\r
334 psChildren = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
\r
335 " WHERE ns=? AND type > ? AND type < ?",
\r
336 new PermLoader(3) {
\r
338 protected void key(Data data, int _idx, Object[] obj) {
\r
340 obj[idx] = data.ns;
\r
341 obj[++idx]=data.type + DOT;
\r
342 obj[++idx]=data.type + DOT_PLUS_ONE;
\r
344 },readConsistency);
\r
350 * Add a single Permission to the Role's Permission Collection
\r
353 * @param roleFullName
\r
359 public Result<Void> addRole(AuthzTrans trans, PermDAO.Data perm, String roleFullName) {
\r
360 // Note: Prepared Statements for Collection updates aren't supported
\r
363 getSession(trans).execute(UPDATE_SP + TABLE + " SET roles = roles + {'" + roleFullName + "'} " +
\r
365 "ns = '" + perm.ns + "' AND " +
\r
366 "type = '" + perm.type + "' AND " +
\r
367 "instance = '" + perm.instance + "' AND " +
\r
368 "action = '" + perm.action + "';"
\r
370 } catch (DriverException | APIException | IOException e) {
\r
371 reportPerhapsReset(trans,e);
\r
372 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
\r
375 wasModified(trans, CRUD.update, perm, "Added role " + roleFullName + " to perm " +
\r
376 perm.ns + '.' + perm.type + '|' + perm.instance + '|' + perm.action);
\r
377 return Result.ok();
\r
381 * Remove a single Permission from the Role's Permission Collection
\r
383 * @param roleFullName
\r
389 public Result<Void> delRole(AuthzTrans trans, PermDAO.Data perm, String roleFullName) {
\r
390 // Note: Prepared Statements for Collection updates aren't supported
\r
393 getSession(trans).execute(UPDATE_SP + TABLE + " SET roles = roles - {'" + roleFullName + "'} " +
\r
395 "ns = '" + perm.ns + "' AND " +
\r
396 "type = '" + perm.type + "' AND " +
\r
397 "instance = '" + perm.instance + "' AND " +
\r
398 "action = '" + perm.action + "';"
\r
400 } catch (DriverException | APIException | IOException e) {
\r
401 reportPerhapsReset(trans,e);
\r
402 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
\r
405 //TODO how can we tell when it doesn't?
\r
406 wasModified(trans, CRUD.update, perm, "Removed role " + roleFullName + " from perm " +
\r
407 perm.ns + '.' + perm.type + '|' + perm.instance + '|' + perm.action);
\r
408 return Result.ok();
\r
414 * Additional method:
\r
415 * Select all Permissions by Name
\r
419 * @throws DAOException
\r
421 public Result<List<Data>> readByType(AuthzTrans trans, String ns, String type) {
\r
422 return psByType.read(trans, R_TEXT, new Object[]{ns, type});
\r
425 public Result<List<Data>> readChildren(AuthzTrans trans, String ns, String type) {
\r
426 return psChildren.read(trans, R_TEXT, new Object[]{ns, type+DOT, type + DOT_PLUS_ONE});
\r
429 public Result<List<Data>> readNS(AuthzTrans trans, String ns) {
\r
430 return psNS.read(trans, R_TEXT, new Object[]{ns});
\r
434 * Add description to this permission
\r
441 * @param description
\r
444 public Result<Void> addDescription(AuthzTrans trans, String ns, String type,
\r
445 String instance, String action, String description) {
\r
447 getSession(trans).execute(UPDATE_SP + TABLE + " SET description = '"
\r
448 + description + "' WHERE ns = '" + ns + "' AND type = '" + type + "'"
\r
449 + "AND instance = '" + instance + "' AND action = '" + action + "';");
\r
450 } catch (DriverException | APIException | IOException e) {
\r
451 reportPerhapsReset(trans,e);
\r
452 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
\r
455 Data data = new Data();
\r
458 data.instance=instance;
\r
459 data.action=action;
\r
460 wasModified(trans, CRUD.update, data, "Added description " + description + " to permission "
\r
461 + data.encode(), null );
\r
462 return Result.ok();
\r
466 * Log Modification statements to History
\r
469 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
\r
470 boolean memo = override.length>0 && override[0]!=null;
\r
471 boolean subject = override.length>1 && override[1]!=null;
\r
473 // Need to update history
\r
474 HistoryDAO.Data hd = HistoryDAO.newInitedData();
\r
475 hd.user = trans.user();
\r
476 hd.action = modified.name();
\r
478 hd.subject = subject ? override[1] : data.fullType();
\r
480 hd.memo = String.format("%s", override[0]);
\r
482 hd.memo = String.format("%sd %s|%s|%s", modified.name(),data.fullType(),data.instance,data.action);
\r
485 if(modified==CRUD.delete) {
\r
487 hd.reconstruct = data.bytify();
\r
488 } catch (IOException e) {
\r
489 trans.error().log(e,"Could not serialize PermDAO.Data");
\r
493 if(historyDAO.create(trans, hd).status!=Status.OK) {
\r
494 trans.error().log("Cannot log to History");
\r
496 if(infoDAO.touch(trans, TABLE,data.invalidate(cache)).notOK()) {
\r
497 trans.error().log("Cannot touch CacheInfo");
\r