2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2018 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.update;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.text.ParseException;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.HashSet;
34 import java.util.List;
37 import java.util.TreeMap;
39 import org.onap.aaf.auth.batch.Batch;
40 import org.onap.aaf.auth.batch.BatchPrincipal;
41 import org.onap.aaf.auth.batch.actions.Email;
42 import org.onap.aaf.auth.batch.actions.EmailPrint;
43 import org.onap.aaf.auth.batch.helpers.Notification;
44 import org.onap.aaf.auth.batch.helpers.UserRole;
45 import org.onap.aaf.auth.env.AuthzTrans;
46 import org.onap.aaf.auth.org.OrganizationException;
47 import org.onap.aaf.cadi.CadiException;
48 import org.onap.aaf.cadi.util.CSV;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.env.Env;
51 import org.onap.aaf.misc.env.TimeTaken;
52 import org.onap.aaf.misc.env.util.Chrono;
55 public class NotifyCredExpiring extends Batch {
57 private static final String UNKNOWN_ID = "unknown@deprecated.id";
58 private static final String AAF_INSTANTIATED_MECHID = "AAF INSTANTIATED MECHID";
59 private static final String EXPIRATION_DATE = "EXPIRATION DATE";
60 private static final String QUICK_LINK = "QUICK LINK TO UPDATE PAGE";
61 private static final String DASH_1 = "-----------------------";
62 private static final String DASH_2 = "---------------";
63 private static final String DASH_3 = "----------------------------------------------------";
64 private static final String LINE = "\n----------------------------------------------------------------";
66 private int maxEmails;
67 private final PrintStream ps;
68 private final AuthzTrans noAvg;
69 private String supportEmailAddr;
71 private CSVInfo csvInfo;
73 public NotifyCredExpiring(AuthzTrans trans) throws APIException, IOException, OrganizationException, CadiException {
75 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
77 session = cluster.connect();
82 noAvg = env.newTransNoAvg();
83 noAvg.setUser(new BatchPrincipal("batch:NotifyCredExpiring"));
86 email = new EmailPrint();
88 maxEmails = Integer.parseInt(trans.getProperty("MAX_EMAILS","3"));
91 maxEmails = Integer.parseInt(trans.getProperty("MAX_EMAILS","3"));
94 email.subject("AAF Password Expiration Notification (ENV: %s)",batchEnv);
95 email.preamble("AAF (MOTS 22830) is the AT&T Authorization System used by many AT&T Tools and Applications.\n\n" +
96 " The following Credentials are expiring on the dates shown. Failure to act before the expiration date "
97 + "will cause your App's Authentications to fail.\n");
98 email.signature("Sincerely,\nAAF Team (Our MOTS# 22830)\n"
99 + "https://wiki.web.att.com/display/aaf/Contact+Us\n"
100 + "(Use 'Other Misc Requests (TOPS)')");
102 boolean quit = false;
103 if(args().length<1) {
104 System.err.println("Need CSV formatted Expiring Report");
107 File f = new File(logDir(),args()[0]);
108 System.out.println("Reading " + f.getCanonicalPath());
109 csv = new CSV(env.access(),f);
112 if(args().length<2) {
113 System.err.println("Need Email Template");
120 csvInfo = new CSVInfo(System.err);
123 Notification.load(trans, session, Notification.v2_0_18);
125 ps = new PrintStream(new FileOutputStream(logDir() + "/email"+Chrono.dateOnlyStamp()+".log",true));
126 ps.printf("### Approval Notify %s for %s%s\n",Chrono.dateTime(),batchEnv,dryRun?", DryRun":"");
130 protected void run(AuthzTrans trans) {
133 Map<String,List<LastCred>> ownerCreds = new TreeMap<>();
136 List<LastCred> noOwner = new ArrayList<>();
137 ownerCreds.put(UNKNOWN_ID,noOwner);
140 // // Get a list of ONLY the ones needing email by Owner
141 // for (Entry<String, List<Cred>> es : Cred.byNS.entrySet()) {
142 // for (Cred c : es.getValue()) {
143 // List<UserRole> ownerURList = UserRole.getByRole().get(es.getKey()+".owner");
144 // if (ownerURList!=null) {
145 // for (UserRole ur:ownerURList) {
146 // String owner = ur.user();
147 // List<LastCred> llc = ownerCreds.get(owner);
149 // ownerCreds.put(owner, (llc=new ArrayList<>()));
151 // llc.add(new LastCred(c,last));
154 // noOwner.add(new LastCred(c,last));
159 // boolean bCritical,bNormal,bEarly;
160 // Message msg = new Message();
161 // Notification ownNotf;
162 // StringBuilder logMessage = new StringBuilder();
163 // for (Entry<String,List<LastCred>> es : ownerCreds.entrySet()) {
164 // String owner = es.getKey();
165 // boolean header = true;
167 // Organization org = OrganizationFactory.obtain(env, owner);
168 // Identity user = org.getIdentity(noAvg, owner);
169 // if (!UNKNOWN_ID.equals(owner) && user==null) {
170 // ps.printf("Invalid Identity: %s\n", owner);
172 // logMessage.setLength(0);
173 // if (maxEmails>emailCount) {
174 // bCritical=bNormal=bEarly = false;
177 // email.addTo(user==null?supportEmailAddr:user.email());
179 // ownNotf = Notification.get(es.getKey(),TYPE.CN);
180 // if (ownNotf==null) {
181 // ownNotf = Notification.create(user==null?UNKNOWN_ID:user.fullID(), TYPE.CN);
183 // last = ownNotf.last;
184 // // Get Max ID size for formatting purposes
185 // int length = AAF_INSTANTIATED_MECHID.length();
186 // for (LastCred lc : es.getValue()) {
187 // length = Math.max(length, lc.cred.id.length());
189 // String id_exp_fmt = "\t%-"+length+"s %15s %s";
191 // Collections.sort(es.getValue(),LastCred.COMPARE);
192 // for (LastCred lc : es.getValue()) {
193 // if (lc.last.after(must) && lc.last.before(early) &&
194 // (ownNotf.last==null || ownNotf.last.before(withinLastWeek))) {
195 // if (!bEarly && header) {
196 // msg.line("\tThe following are friendly 2 month reminders, just in case you need to schedule your updates early. "
197 // + "You will be reminded next month\n");
198 // msg.line(id_exp_fmt, AAF_INSTANTIATED_MECHID,EXPIRATION_DATE, QUICK_LINK);
199 // msg.line(id_exp_fmt, DASH_1, DASH_2, DASH_3);
203 // } else if (lc.last.after(critical) && lc.last.before(must) &&
204 // (ownNotf.last==null || ownNotf.last.before(withinLastWeek))) {
206 // boolean last2wks = lc.last.before(within2Weeks);
209 // Identity supvsr = user.responsibleTo();
210 // email.addCC(supvsr.email());
211 // } catch (OrganizationException e) {
212 // trans.error().log(e, "Supervisor cannot be looked up");
216 // msg.line("\tIt is now important for you to update Passwords all all configurations using them for the following.\n" +
217 // (last2wks?"\tNote: Your Supervisor is CCd\n":"\tNote: Your Supervisor will be notified if this is not being done before the last 2 weeks\n"));
218 // msg.line(id_exp_fmt, AAF_INSTANTIATED_MECHID,EXPIRATION_DATE, QUICK_LINK);
219 // msg.line(id_exp_fmt, DASH_1, DASH_2, DASH_3);
224 // } else if (lc.last.after(tooLate) && lc.last.before(critical)) { // Email Every Day, with Supervisor
225 // if (!bCritical && header) {
226 // msg.line("\t!!! WARNING: These Credentials will expire in LESS THAN ONE WEEK !!!!\n" +
227 // "\tYour supervisor is added to this Email\n");
228 // msg.line(id_exp_fmt, AAF_INSTANTIATED_MECHID,EXPIRATION_DATE, QUICK_LINK);
229 // msg.line(id_exp_fmt, DASH_1, DASH_2, DASH_3);
235 // Identity supvsr = user.responsibleTo();
236 // if (supvsr!=null) {
237 // email.addCC(supvsr.email());
238 // supvsr = supvsr.responsibleTo();
239 // if (supvsr!=null) {
240 // email.addCC(supvsr.email());
244 // } catch (OrganizationException e) {
245 // trans.error().log(e, "Supervisor cannot be looked up");
248 // if (bEarly || bNormal || bCritical) {
249 // if (logMessage.length()==0) {
250 // logMessage.append("NotifyCredExpiring");
252 // logMessage.append("\n\t");
253 // logMessage.append(lc.cred.id);
254 // logMessage.append('\t');
255 // logMessage.append(Chrono.dateOnlyStamp(lc.last));
256 // msg.line(id_exp_fmt, lc.cred.id, Chrono.dateOnlyStamp(lc.last)+" ",env.getProperty(GUI_URL)+"/creddetail?ns="+Question.domain2ns(lc.cred.id));
260 // if (bEarly || bNormal || bCritical) {
262 // msg.line("Why are you receiving this Notification?\n");
263 // msg.line("You are the listed owner of one or more AAF Namespaces. ASPR requires that those responsible for "
264 // + "applications and their access review them regularly for accuracy. The AAF WIKI page for AT&T is https://wiki.web.att.com/display/aaf. "
265 // + "You might like https://wiki.web.att.com/display/aaf/AAF+in+a+Nutshell. More detailed info regarding questions of being a Namespace Owner is available at https://wiki.web.att.com/pages/viewpage.action?pageId=594741363\n");
266 // msg.line("You may view the Namespaces you listed as Owner for in this AAF Env by viewing the following webpage:\n");
267 // msg.line(" %s/ns\n\n",env.getProperty(GUI_URL));
269 // Result<Void> rv = email.exec(trans, org,"");
272 // if (!isDryRun()) {
273 // ownNotf.update(noAvg, session, false);
274 // // SET LastNotification
276 // email.log(ps,logMessage.toString());
278 // trans.error().log(rv.errorString());
283 // } catch (OrganizationException e) {
284 // trans.info().log(e);
287 trans.info().printf("%d emails sent for %s", emailCount,batchEnv);
291 private static class CSVInfo implements CSV.Visitor {
292 private PrintStream out;
293 private Set<String> unsupported;
294 private NotifyCredVisitor credv;
295 private List<LastCred> llc;
297 public CSVInfo(PrintStream out) {
299 credv = new NotifyCredVisitor(llc = new ArrayList<>());
303 public void visit(List<String> row) throws IOException, CadiException {
306 case NotifyCredVisitor.SUPPORTS:
310 if(unsupported==null) {
311 unsupported = new HashSet<String>();
313 if(!unsupported.contains(row.get(0))) {
314 unsupported.add(row.get(0));
315 out.println("Unsupported Type: " + row.get(0));
321 private static class Contact {
322 public List<String> contacts;
323 private List<UserRole> owners;
325 public Contact(final String ns) {
326 contacts = new ArrayList<>();
330 public void loadFromNS(final String ns) {
331 owners = UserRole.getByRole().get(ns+".owner");
335 private static class LastCred extends Contact {
336 public final String id;
337 public final int type;
338 public final Date expires;
340 public LastCred(final String id, final String ns, final int type, final Date expires) {
344 this.expires = expires;
348 private static class NotifyCredVisitor implements CSV.Visitor {
349 public static final String SUPPORTS = "cred";
350 private final List<LastCred> lastCred;
352 public NotifyCredVisitor(final List<LastCred> lastCred) {
353 this.lastCred = lastCred;
357 public void visit(List<String> row) throws IOException, CadiException {
359 lastCred.add(new LastCred(
362 Integer.parseInt(row.get(3)),
363 Chrono.dateOnlyFmt.parse(row.get(4))
366 } catch (NumberFormatException | ParseException e) {
367 throw new CadiException(e);
373 protected void _close(AuthzTrans trans) {