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