2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ===========================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END====================================================
24 package org.onap.aaf.auth.batch.reports;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.security.cert.Certificate;
30 import java.security.cert.CertificateException;
31 import java.security.cert.X509Certificate;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
38 import java.util.TreeMap;
40 import org.onap.aaf.auth.batch.Batch;
41 import org.onap.aaf.auth.batch.helpers.Cred;
42 import org.onap.aaf.auth.batch.helpers.ExpireRange;
43 import org.onap.aaf.auth.batch.helpers.UserRole;
44 import org.onap.aaf.auth.batch.helpers.Visitor;
45 import org.onap.aaf.auth.batch.helpers.X509;
46 import org.onap.aaf.auth.batch.helpers.Cred.Instance;
47 import org.onap.aaf.auth.batch.helpers.ExpireRange.Range;
48 import org.onap.aaf.auth.dao.cass.CredDAO;
49 import org.onap.aaf.auth.env.AuthzTrans;
50 import org.onap.aaf.auth.org.OrganizationException;
51 import org.onap.aaf.cadi.configure.Factory;
52 import org.onap.aaf.cadi.util.CSV;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.env.Env;
55 import org.onap.aaf.misc.env.TimeTaken;
56 import org.onap.aaf.misc.env.util.Chrono;
59 public class Expiring extends Batch {
61 private static final String CSV = ".csv";
62 private static final String INFO = "info";
63 private static final String EXPIRED_OWNERS = "ExpiredOwners";
64 private int minOwners;
65 private Map<String, CSV.Writer> writerList;
66 private ExpireRange expireRange;
67 private Date deleteDate;
69 public Expiring(AuthzTrans trans) throws APIException, IOException, OrganizationException {
71 trans.info().log("Starting Connection Process");
73 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
75 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
77 session = cluster.connect();
82 // Load Cred. We don't follow Visitor, because we have to gather up everything into Identity Anyway
83 Cred.load(trans, session);
87 // Create Intermediate Output
88 writerList = new HashMap<>();
90 expireRange = new ExpireRange(trans.env().access());
91 String sdate = Chrono.dateOnlyStamp(expireRange.now);
92 for( List<Range> lr : expireRange.ranges.values()) {
94 if(writerList.get(r.name())==null) {
95 File file = new File(logDir(),r.name() + sdate +CSV);
96 CSV csv = new CSV(file);
97 CSV.Writer cw = csv.writer(false);
98 cw.row(INFO,r.name(),Chrono.dateOnlyStamp(expireRange.now),r.reportingLevel());
99 writerList.put(r.name(),cw);
100 if("Delete".equals(r.name())) {
101 deleteDate = r.getEnd();
103 trans.init().log("Creating File:",file.getAbsolutePath());
114 protected void run(AuthzTrans trans) {
116 File file = new File(logDir(), EXPIRED_OWNERS + Chrono.dateOnlyStamp(expireRange.now) + CSV);
117 final CSV ownerCSV = new CSV(file);
119 Map<String, Set<UserRole>> owners = new TreeMap<String, Set<UserRole>>();
120 trans.info().log("Process UserRoles");
121 UserRole.load(trans, session, UserRole.v2_0_11, ur -> {
122 // Cannot just delete owners, unless there is at least one left. Process later
123 if ("owner".equals(ur.rname())) {
124 Set<UserRole> urs = owners.get(ur.role());
126 urs = new HashSet<UserRole>();
127 owners.put(ur.role(), urs);
131 writeAnalysis(trans,ur);
135 // Now Process Owners, one owner Role at a time, ensuring one is left,
137 // a good one. If so, process the others as normal. Otherwise, write
140 if (!owners.values().isEmpty()) {
142 CSV.Writer expOwner = null;
144 for (Set<UserRole> sur : owners.values()) {
146 for (UserRole ur : sur) {
147 if (ur.expires().after(expireRange.now)) {
152 for (UserRole ur : sur) {
153 if (goodOwners >= minOwners) {
154 writeAnalysis(trans, ur);
156 if (expOwner == null) {
157 expOwner = ownerCSV.writer();
158 expOwner.row(INFO,EXPIRED_OWNERS,Chrono.dateOnlyStamp(expireRange.now),2);
160 expOwner.row("owner",ur.role(), ur.user(), Chrono.dateOnlyStamp(ur.expires()));
171 trans.info().log("Checking for Expired Credentials");
173 for (Cred cred : Cred.data.values()) {
174 List<Instance> linst = cred.instances;
176 Instance lastBath = null;
177 for(Instance inst : linst) {
178 // Special Behavior: only eval the LAST Instance
179 if (inst.type == CredDAO.BASIC_AUTH || inst.type == CredDAO.BASIC_AUTH_SHA256) {
180 if(deleteDate!=null && inst.expires.before(deleteDate)) {
181 writeAnalysis(trans, cred, inst); // will go to Delete
182 } else if(lastBath==null || lastBath.expires.before(inst.expires)) {
186 writeAnalysis(trans, cred, inst);
190 writeAnalysis(trans, cred, lastBath);
195 trans.info().log("Checking for Expired X509s");
196 X509.load(trans, session, new Visitor<X509>() {
198 public void visit(X509 x509) {
200 for(Certificate cert : Factory.toX509Certificate(x509.x509)) {
201 writeAnalysis(trans, x509, (X509Certificate)cert);
203 } catch (CertificateException | IOException e) {
204 trans.error().log(e, "Error Decrypting X509");
209 } catch (FileNotFoundException e) {
215 private void writeAnalysis(AuthzTrans trans, UserRole ur) {
216 Range r = expireRange.getRange("ur", ur.expires());
218 CSV.Writer cw = writerList.get(r.name());
225 private void writeAnalysis(AuthzTrans trans, Cred cred, Instance inst) {
226 if(cred!=null && inst!=null) {
227 Range r = expireRange.getRange("cred", inst.expires);
229 CSV.Writer cw = writerList.get(r.name());
237 private void writeAnalysis(AuthzTrans trans, X509 x509, X509Certificate x509Cert) throws IOException {
238 Range r = expireRange.getRange("x509", x509Cert.getNotAfter());
240 CSV.Writer cw = writerList.get(r.name());
242 x509.row(cw,x509Cert);
248 private String[] contacts(final AuthzTrans trans, final String ns, final int levels) {
249 List<UserRole> owners = UserRole.getByRole().get(ns+".owner");
250 List<UserRole> current = new ArrayList<>();
251 for(UserRole ur : owners) {
252 if(expireRange.now.before(ur.expires())) {
256 if(current.isEmpty()) {
257 trans.warn().log(ns,"has no current owners");
261 List<String> email = new ArrayList<>();
262 for(UserRole ur : current) {
267 id = org.getIdentity(trans, ur.user());
270 email.add(id.email());
272 id = id.responsibleTo();
280 } catch (OrganizationException e) {
281 trans.error().log(e);
285 return email.toArray(new String[email.size()]);
290 protected void _close(AuthzTrans trans) {
292 for(CSV.Writer cw : writerList.values()) {