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.reports;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.List;
31 import org.onap.aaf.auth.Batch;
32 import org.onap.aaf.auth.dao.cass.CredDAO;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.helpers.Cred;
35 import org.onap.aaf.auth.helpers.UserRole;
36 import org.onap.aaf.auth.helpers.Cred.Instance;
37 import org.onap.aaf.auth.org.OrganizationException;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.env.Env;
40 import org.onap.aaf.misc.env.TimeTaken;
41 import org.onap.aaf.misc.env.util.Chrono;
43 public class ExpiringNext extends Batch {
45 public ExpiringNext(AuthzTrans trans) throws APIException, IOException, OrganizationException {
47 trans.info().log("Starting Connection Process");
49 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
51 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
53 session = cluster.connect();
58 UserRole.load(trans, session, UserRole.v2_0_11);
59 Cred.load(trans, session);
66 protected void run(AuthzTrans trans) {
67 GregorianCalendar gc = new GregorianCalendar();
68 Date now = gc.getTime();
69 gc.add(GregorianCalendar.WEEK_OF_MONTH, 2);
70 Date twoWeeks = gc.getTime();
72 gc.set(GregorianCalendar.YEAR, 3000);
73 Date earliestUR = gc.getTime();
74 Date earliestCred = gc.getTime();
76 List<String> expiring = new ArrayList<>();
78 trans.info().log("Checking for Expired UserRoles");
79 for(UserRole ur : UserRole.data) {
80 if(ur.expires().after(now)) {
81 if(ur.expires().before(twoWeeks)) {
82 expiring.add(Chrono.dateOnlyStamp(ur.expires()) + ":\t" + ur.user() + '\t' + ur.role());
84 if(ur.expires().before(earliestUR)) {
85 earliestUR = ur.expires();
90 if(expiring.size()>0) {
91 Collections.sort(expiring,Collections.reverseOrder());
92 for(String s : expiring) {
93 System.err.print('\t');
94 System.err.println(s);
96 trans.info().printf("Earliest Expiring UR is %s\n\n", Chrono.dateOnlyStamp(earliestUR));
98 trans.info().printf("No Expiring UserRoles within 2 weeks");
103 trans.info().log("Checking for Expired Credentials");
104 for( Cred creds : Cred.data.values()) {
105 Instance lastInstance=null;
106 for(Instance inst : creds.instances) {
107 if(inst.type==CredDAO.BASIC_AUTH || inst.type==CredDAO.BASIC_AUTH_SHA256) {
108 if(lastInstance == null || inst.expires.after(lastInstance.expires)) {
113 if(lastInstance!=null) {
114 if(lastInstance.expires.after(now)) {
115 if(lastInstance.expires.before(twoWeeks)) {
116 expiring.add(Chrono.dateOnlyStamp(lastInstance.expires) + ": \t" + creds.id);
119 if(lastInstance.expires.before(earliestCred)) {
120 earliestCred = lastInstance.expires;
125 if(expiring.size()>0) {
126 Collections.sort(expiring,Collections.reverseOrder());
127 for(String s : expiring) {
128 System.err.print('\t');
129 System.err.println(s);
131 trans.info().printf("Earliest Expiring Cred is %s\n\n", Chrono.dateOnlyStamp(earliestCred));
133 trans.info().printf("No Expiring Creds within 2 weeks");
139 protected void _close(AuthzTrans trans) {