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 StringBuilder sb = new StringBuilder();
101 sb.append(ns.indexOf('@')<0?'.':':');
104 return sb.toString();
107 public String fullPerm() {
108 StringBuilder sb = new StringBuilder(ns);
109 sb.append(ns.indexOf('@')<0?'.':':');
115 return sb.toString();
118 public String encode() {
119 return ns + '|' + type + '|' + instance + '|' + action;
123 * Decode Perm String, including breaking into appropriate Namespace
130 public static Result<Data> decode(AuthzTrans trans, Question q, String p) {
131 String[] ss = Split.splitTrim('|', p,4);
133 return Result.err(Status.ERR_BadData,"Perm Encodings must be separated by '|'");
135 Data data = new Data();
136 if (ss[3]==null) { // older 3 part encoding must be evaluated for NS
137 Result<NsSplit> nss = q.deriveNsSplit(trans, ss[0]);
139 return Result.err(nss);
141 data.ns=nss.value.ns;
142 data.type=nss.value.name;
145 } else { // new 4 part encoding
151 return Result.ok(data);
155 * Decode Perm String, including breaking into appropriate Namespace
162 public static Result<String[]> decodeToArray(AuthzTrans trans, Question q, String p) {
163 String[] ss = Split.splitTrim('|', p,4);
165 return Result.err(Status.ERR_BadData,"Perm Encodings must be separated by '|'");
168 if (ss[3]==null) { // older 3 part encoding must be evaluated for NS
171 Result<NsSplit> nss = q.deriveNsSplit(trans, ss[0]);
173 return Result.err(nss);
175 ss[1] = nss.value.name;
176 ss[0] = nss.value.ns;
178 return Result.ok(ss);
181 public static Data create(NsDAO.Data ns, String name) {
182 NsSplit nss = new NsSplit(ns,name);
183 Data rv = new Data();
185 String[] s = nss.name.split("\\|");
205 public static Data create(AuthzTrans trans, Question q, String name) {
206 String[] s = name.split("\\|");
207 Result<NsSplit> rdns = q.deriveNsSplit(trans, s[0]);
208 Data rv = new PermDAO.Data();
209 if (rdns.isOKhasData()) {
239 ////////////////////////////////////////
241 public Set<String> roles(boolean mutable) {
243 roles = new HashSet<>();
244 } else if (mutable && !(roles instanceof HashSet)) {
245 roles = new HashSet<>(roles);
251 public int[] invalidate(Cached<?,?> cache) {
255 seg(cache,ns,type,STAR),
256 seg(cache,ns,type,instance,action)
261 public ByteBuffer bytify() throws IOException {
262 ByteArrayOutputStream baos = new ByteArrayOutputStream();
263 PermLoader.deflt.marshal(this, new DataOutputStream(baos));
264 return ByteBuffer.wrap(baos.toByteArray());
268 public void reconstitute(ByteBuffer bb) throws IOException {
269 PermLoader.deflt.unmarshal(this, toDIS(bb));
273 public String toString() {
278 private static class PermLoader extends Loader<Data> implements Streamer<Data> {
279 public static final int MAGIC=283939453;
280 public static final int VERSION=1;
281 public static final int BUFF_SIZE=96;
283 public static final PermLoader deflt = new PermLoader(KEYLIMIT);
285 public PermLoader(int keylimit) {
290 public Data load(Data data, Row row) {
291 // Int more efficient Match "fields" string
292 data.ns = row.getString(0);
293 data.type = row.getString(1);
294 data.instance = row.getString(2);
295 data.action = row.getString(3);
296 data.roles = row.getSet(4,String.class);
297 data.description = row.getString(5);
302 protected void key(Data data, int _idx, Object[] obj) {
305 obj[++idx]=data.type;
306 obj[++idx]=data.instance;
307 obj[++idx]=data.action;
311 protected void body(Data data, int _idx, Object[] obj) {
314 obj[++idx]=data.description;
318 public void marshal(Data data, DataOutputStream os) throws IOException {
319 writeHeader(os,MAGIC,VERSION);
320 writeString(os, data.ns);
321 writeString(os, data.type);
322 writeString(os, data.instance);
323 writeString(os, data.action);
324 writeStringSet(os, data.roles);
325 writeString(os, data.description);
329 public void unmarshal(Data data, DataInputStream is) throws IOException {
330 /*int version = */readHeader(is,MAGIC,VERSION);
331 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
332 byte[] buff = new byte[BUFF_SIZE];
333 data.ns = readString(is, buff);
334 data.type = readString(is,buff);
335 data.instance = readString(is,buff);
336 data.action = readString(is,buff);
337 data.roles = readStringSet(is,buff);
338 data.description = readString(is,buff);
342 private void init(AuthzTrans trans) {
343 // the 3 is the number of key fields
344 String[] helpers = setCRUD(trans, TABLE, Data.class, PermLoader.deflt);
346 // Other SELECT style statements... match with a local Method
347 psByType = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
348 " WHERE ns = ? AND type = ?", new PermLoader(2) {
350 protected void key(Data data, int idx, Object[] obj) {
355 psNS = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
356 " WHERE ns = ?", new PermLoader(1),readConsistency);
358 psChildren = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
359 " WHERE ns=? AND type > ? AND type < ?",
362 protected void key(Data data, int _idx, Object[] obj) {
365 obj[++idx]=data.type + DOT;
366 obj[++idx]=data.type + DOT_PLUS_ONE;
374 * Add a single Permission to the Role's Permission Collection
377 * @param roleFullName
383 public Result<Void> addRole(AuthzTrans trans, PermDAO.Data perm, String roleFullName) {
384 // Note: Prepared Statements for Collection updates aren't supported
387 getSession(trans).execute(UPDATE_SP + TABLE + " SET roles = roles + {'" + roleFullName + "'} " +
389 "ns = '" + perm.ns + "' AND " +
390 "type = '" + perm.type + "' AND " +
391 "instance = '" + perm.instance + "' AND " +
392 "action = '" + perm.action + "';"
394 } catch (DriverException | APIException | IOException e) {
395 reportPerhapsReset(trans,e);
396 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
399 wasModified(trans, CRUD.update, perm, "Added role " + roleFullName + " to perm " +
400 perm.ns + '.' + perm.type + '|' + perm.instance + '|' + perm.action);
405 * Remove a single Permission from the Role's Permission Collection
407 * @param roleFullName
413 public Result<Void> delRole(AuthzTrans trans, PermDAO.Data perm, String roleFullName) {
414 // Note: Prepared Statements for Collection updates aren't supported
417 getSession(trans).execute(UPDATE_SP + TABLE + " SET roles = roles - {'" + roleFullName + "'} " +
419 "ns = '" + perm.ns + "' AND " +
420 "type = '" + perm.type + "' AND " +
421 "instance = '" + perm.instance + "' AND " +
422 "action = '" + perm.action + "';"
424 } catch (DriverException | APIException | IOException e) {
425 reportPerhapsReset(trans,e);
426 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
429 //TODO how can we tell when it doesn't?
430 wasModified(trans, CRUD.update, perm, "Removed role " + roleFullName + " from perm " +
431 perm.ns + '.' + perm.type + '|' + perm.instance + '|' + perm.action);
439 * Select all Permissions by Name
443 * @throws DAOException
445 public Result<List<Data>> readByType(AuthzTrans trans, String ns, String type) {
446 return psByType.read(trans, R_TEXT, new Object[]{ns, type});
449 public Result<List<Data>> readChildren(AuthzTrans trans, String ns, String type) {
450 return psChildren.read(trans, R_TEXT, new Object[]{ns, type+DOT, type + DOT_PLUS_ONE});
453 public Result<List<Data>> readNS(AuthzTrans trans, String ns) {
454 return psNS.read(trans, R_TEXT, new Object[]{ns});
458 * Add description to this permission
468 public Result<Void> addDescription(AuthzTrans trans, String ns, String type,
469 String instance, String action, String description) {
471 getSession(trans).execute(UPDATE_SP + TABLE + " SET description = '"
472 + description + "' WHERE ns = '" + ns + "' AND type = '" + type + "'"
473 + "AND instance = '" + instance + "' AND action = '" + action + "';");
474 } catch (DriverException | APIException | IOException e) {
475 reportPerhapsReset(trans,e);
476 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
479 Data data = new Data();
482 data.instance=instance;
484 wasModified(trans, CRUD.update, data, "Added description " + description + " to permission "
485 + data.encode(), null );
490 * Log Modification statements to History
493 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
494 boolean memo = override.length>0 && override[0]!=null;
495 boolean subject = override.length>1 && override[1]!=null;
497 // Need to update history
498 HistoryDAO.Data hd = HistoryDAO.newInitedData();
499 hd.user = trans.user();
500 hd.action = modified.name();
502 hd.subject = subject ? override[1] : data.fullType();
504 hd.memo = String.format("%s", override[0]);
506 hd.memo = String.format("%sd %s|%s|%s", modified.name(),data.fullType(),data.instance,data.action);
509 if (modified==CRUD.delete) {
511 hd.reconstruct = data.bytify();
512 } catch (IOException e) {
513 trans.error().log(e,"Could not serialize PermDAO.Data");
517 if (historyDAO.create(trans, hd).status!=Status.OK) {
518 trans.error().log("Cannot log to History");
520 if (infoDAO.touch(trans, TABLE,data.invalidate(cache)).notOK()) {
521 trans.error().log("Cannot touch CacheInfo");