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.cached;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.TreeMap;
33 import org.onap.aaf.auth.dao.Cached.Getter;
34 import org.onap.aaf.auth.dao.cass.CredDAO;
35 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.cadi.Access;
38 import org.onap.aaf.cadi.Access.Level;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.Hash;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.util.CSV;
44 public class FileGetter {
45 private static final String AAF_FILEGETTER = "aaf_filegetter";
46 private static boolean isLoaded = false;
47 private static FileGetter singleton;
48 private static List<CredDAO.Data> EMPTY = new ArrayList<>();
49 private Map<String,List<CredDAO.Data>> data;
50 private SimpleDateFormat sdf;
51 private FileGetter(Access access) {
53 String filename = access.getProperty(AAF_FILEGETTER,null);
54 if((filename!=null)&&(!isLoaded)) {
55 data = new TreeMap<>();
56 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SSSS");
57 CSV csv = new CSV(access, filename).setDelimiter('|');
59 access.log(Level.INIT, "Loading Filebased Cred from",filename);
62 access.log(Level.INIT, "Bad Row");
66 type =Integer.parseInt(row.get(1));
67 } catch(Exception e) {
68 access.log(Level.INIT, e, "skipping ", row.get(0));
71 if(CredDAO.CERT_SHA256_RSA == type) {
75 CredDAO.Data cdd = new CredDAO.Data();
79 cdd.expires = sdf.parse(row.get(2));
80 // Note: Note sure this can be null, but throwing was
81 // part of original "fromHex" method. Remove if you can
82 // prove ba will never be null J - May 19,2020
83 if((ba=Hash.fromHex(row.get(3)))==null) {
84 throw new CadiException("Invalid Cred");
86 cdd.cred = ByteBuffer.wrap(ba);
87 cdd.notes= row.get(4);
89 cdd.other = Integer.parseInt(row.get(6));
95 List<CredDAO.Data> lcdd = data.get(cdd.id);
97 lcdd = new ArrayList<>();
98 data.put(cdd.id, lcdd);
101 } catch (ParseException e) {
102 access.log(Level.INIT, e);
106 access.printf(Level.INIT, "Filebased Cred finished...");
108 } catch( CadiException | IOException e) {
109 access.log(Level.ERROR, e);
115 public static synchronized FileGetter singleton(Access access) {
116 if(singleton==null) {
117 singleton = new FileGetter(access);
122 public Getter<CredDAO.Data> getter(String id) {
123 return new FGetter(id);
126 public class FGetter implements Getter<CredDAO.Data> {
127 private final List<CredDAO.Data> lcdd;
128 public FGetter(final String id) {
132 public Result<List<Data>> get() {
133 return Result.ok(lcdd==null?EMPTY:lcdd);
137 public static void main(String[] args) {
138 PropAccess access = new PropAccess(args);
139 access.setProperty(AAF_FILEGETTER,"/opt/app/aaf/data/cred.dat");
140 FileGetter fg = FileGetter.singleton(access);
142 for(String id : new String[] {"m01891@aaf.att.com","bogus"}) {
143 Getter<CredDAO.Data> g = fg.getter(id);
144 Result<List<CredDAO.Data>> r = g.get();
145 if(r.isOKhasData()) {
146 for(CredDAO.Data cdd : r.value) {
147 System.out.println(cdd);
153 public static boolean isLoaded() {