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.security.SecureRandom;
30 import java.util.Date;
31 import java.util.List;
33 import org.onap.aaf.auth.dao.Bytification;
34 import org.onap.aaf.auth.dao.CIDAO;
35 import org.onap.aaf.auth.dao.Cached;
36 import org.onap.aaf.auth.dao.CassDAOImpl;
37 import org.onap.aaf.auth.dao.Loader;
38 import org.onap.aaf.auth.dao.Streamer;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.layer.Result;
41 import org.onap.aaf.misc.env.APIException;
42 import org.onap.aaf.misc.env.util.Chrono;
44 import com.datastax.driver.core.Cluster;
45 import com.datastax.driver.core.Row;
48 * CredDAO manages credentials.
52 public class CredDAO extends CassDAOImpl<AuthzTrans,CredDAO.Data> {
53 public static final String TABLE = "cred";
54 public static final int CACHE_SEG = 0x40; // yields segment 0x0-0x3F
55 public static final int RAW = -1;
56 public static final int BASIC_AUTH = 1;
57 public static final int BASIC_AUTH_SHA256 = 2;
58 public static final int CERT_SHA256_RSA =200;
59 public static final SecureRandom srand = new SecureRandom();
61 private HistoryDAO historyDAO;
62 private CIDAO<AuthzTrans> infoDAO;
66 public CredDAO(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
67 super(trans, CredDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
71 public CredDAO(AuthzTrans trans, HistoryDAO hDao, CacheInfoDAO ciDao) throws APIException, IOException {
72 super(trans, CredDAO.class.getSimpleName(),hDao, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
78 public static final int KEYLIMIT = 3;
79 public static class Data extends CacheableData implements Bytification {
88 public ByteBuffer cred; // this is a blob in cassandra
92 public int[] invalidate(Cached<?,?> cache) {
94 seg(cache,id) // cache is for all entities
99 public ByteBuffer bytify() throws IOException {
100 ByteArrayOutputStream baos = new ByteArrayOutputStream();
101 CredLoader.deflt.marshal(this,new DataOutputStream(baos));
102 return ByteBuffer.wrap(baos.toByteArray());
106 public void reconstitute(ByteBuffer bb) throws IOException {
107 CredLoader.deflt.unmarshal(this, toDIS(bb));
110 public String toString() {
111 return id + ' ' + type + ' ' + Chrono.dateTime(expires);
115 public static class CredLoader extends Loader<Data> implements Streamer<Data>{
116 public static final int MAGIC=153323443;
117 public static final int VERSION=2;
118 public static final int BUFF_SIZE=48; // Note:
120 public static final CredLoader deflt = new CredLoader(KEYLIMIT);
121 public CredLoader(int keylimit) {
126 public Data load(Data data, Row row) {
127 data.id = row.getString(0);
128 data.type = row.getInt(1); // NOTE: in datastax driver, If the int value is NULL, 0 is returned!
129 data.expires = row.getTimestamp(2);
130 data.other = row.getInt(3);
131 data.ns = row.getString(4);
132 data.tag = row.getString(5);
133 data.notes = row.getString(6);
134 data.cred = row.getBytesUnsafe(7);
139 protected void key(Data data, int _idx, Object[] obj) {
143 obj[++idx] = data.type;
144 obj[++idx] = data.expires;
148 protected void body(Data data, int idx, Object[] obj) {
150 obj[i=idx] = data.other;
153 obj[++i] = data.notes;
154 obj[++i] = data.cred;
158 public void marshal(Data data, DataOutputStream os) throws IOException {
159 writeHeader(os,MAGIC,VERSION);
160 writeString(os, data.id);
161 os.writeInt(data.type);
162 os.writeLong(data.expires==null?-1:data.expires.getTime());
163 os.writeInt(data.other==null?0:data.other);
164 writeString(os, data.ns);
165 writeString(os, data.tag);
166 writeString(os, data.notes);
167 if (data.cred==null) {
170 int l = data.cred.limit()-data.cred.position();
172 os.write(data.cred.array(),data.cred.position(),l);
177 public void unmarshal(Data data, DataInputStream is) throws IOException {
178 /*int version = */readHeader(is,MAGIC,VERSION);
179 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
180 byte[] buff = new byte[BUFF_SIZE];
181 data.id = readString(is,buff);
182 data.type = is.readInt();
184 long l = is.readLong();
185 data.expires = l<0?null:new Date(l);
186 data.other = is.readInt();
187 data.ns = readString(is,buff);
188 data.tag = readString(is,buff);
189 data.notes = readString(is,buff);
191 int i = is.readInt();
194 byte[] bytes = new byte[i]; // a bit dangerous, but lessened because of all the previous sized data reads
195 int read = is.read(bytes);
197 data.cred = ByteBuffer.wrap(bytes);
203 private void init(AuthzTrans trans) throws APIException, IOException {
205 if (historyDAO==null) {
206 historyDAO = new HistoryDAO(trans,this);
209 infoDAO = new CacheInfoDAO(trans,this);
213 String[] helpers = setCRUD(trans, TABLE, Data.class, CredLoader.deflt);
215 psNS = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
216 " WHERE ns = ?", CredLoader.deflt,readConsistency);
218 psID = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
219 " WHERE id = ?", CredLoader.deflt,readConsistency);
223 * @see org.onap.aaf.auth.dao.CassDAOImpl#create(org.onap.aaf.misc.env.TransStore, java.lang.Object)
226 public Result<Data> create(AuthzTrans trans, Data data) {
227 if(data.tag == null) {
228 long l = srand.nextLong();
229 data.tag = Long.toHexString(l);
231 return super.create(trans, data);
234 public Result<List<Data>> readNS(AuthzTrans trans, String ns) {
235 return psNS.read(trans, R_TEXT, new Object[]{ns});
238 public Result<List<Data>> readID(AuthzTrans trans, String id) {
239 return psID.read(trans, R_TEXT, new Object[]{id});
243 * Log Modification statements to History
245 * @param modified which CRUD action was done
246 * @param data entity data that needs a log entry
247 * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
250 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
251 boolean memo = override.length>0 && override[0]!=null;
252 boolean subject = override.length>1 && override[1]!=null;
254 HistoryDAO.Data hd = HistoryDAO.newInitedData();
255 hd.user = trans.user();
256 hd.action = modified.name();
258 hd.subject = subject?override[1]: data.id;
260 ? String.format("%s by %s", override[0], hd.user)
261 : (modified.name() + "d credential for " + data.id);
263 if (modified==CRUD.delete) {
265 hd.reconstruct = data.bytify();
266 } catch (IOException e) {
267 trans.error().log(e,"Could not serialize CredDAO.Data");
271 if (historyDAO.create(trans, hd).status!=Status.OK) {
272 trans.error().log("Cannot log to History");
274 if (infoDAO.touch(trans, TABLE,data.invalidate(cache)).status!=Status.OK) {
275 trans.error().log("Cannot touch Cred");