1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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====================================================
21 ******************************************************************************/
22 package org.onap.aaf.org;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.HashSet;
30 import java.util.List;
32 import java.util.regex.Pattern;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.org.EmailWarnings;
36 import org.onap.aaf.auth.org.Executor;
37 import org.onap.aaf.auth.org.Mailer;
38 import org.onap.aaf.auth.org.Organization;
39 import org.onap.aaf.auth.org.OrganizationException;
40 import org.onap.aaf.cadi.util.FQI;
41 import org.onap.aaf.misc.env.Env;
43 public class DefaultOrg implements Organization {
44 private static final String AAF_DATA_DIR = "aaf_data_dir";
47 final String atDomain;
50 private final String NAME;
51 private final Set<String> supportedRealms;
54 public DefaultOrg(Env env, String realm) throws OrganizationException {
57 supportedRealms=new HashSet<>();
58 supportedRealms.add(realm);
59 domain=FQI.reverseDomain(realm);
60 atDomain = '@'+domain;
61 NAME=env.getProperty(realm + ".name","Default Organization");
65 String temp=env.getProperty(defFile = (getClass().getName()+".file"));
66 File fIdentities=null;
68 temp = env.getProperty(AAF_DATA_DIR);
70 env.warn().log(defFile, " is not defined. Using default: ",temp+"/identities.dat");
71 File dir = new File(temp);
72 fIdentities=new File(dir,"identities.dat");
74 if (!fIdentities.exists()) {
75 env.warn().log("No",fIdentities.getCanonicalPath(),"exists. Creating.");
79 fIdentities.createNewFile();
83 fIdentities = new File(temp);
84 if (!fIdentities.exists()) {
85 String dataDir = env.getProperty(AAF_DATA_DIR);
87 fIdentities = new File(dataDir,temp);
92 if (fIdentities!=null && fIdentities.exists()) {
93 identities = new Identities(fIdentities);
95 if (fIdentities==null) {
96 throw new OrganizationException("No Identities: set \"" + AAF_DATA_DIR + '"');
98 throw new OrganizationException(fIdentities.getCanonicalPath() + " does not exist.");
101 } catch (IOException e) {
102 throw new OrganizationException(e);
106 // Implement your own Delegation System
107 static final List<String> NULL_DELEGATES = new ArrayList<>();
109 public Identities identities;
110 private boolean dryRun;
111 private Mailer mailer;
112 public enum Types {Employee, Contractor, Application, NotActive};
113 private final static Set<String> typeSet;
116 typeSet = new HashSet<>();
117 for (Types t : Types.values()) {
118 typeSet.add(t.name());
122 private static final EmailWarnings emailWarnings = new DefaultOrgWarnings();
125 public String getName() {
130 public String getRealm() {
135 public String getDomain() {
140 public DefaultOrgIdentity getIdentity(AuthzTrans trans, String id) throws OrganizationException {
141 int at = id.indexOf('@');
142 return new DefaultOrgIdentity(trans,at<0?id:id.substring(0, at),this);
145 // Note: Return a null if found; return a String Message explaining why not found.
147 public String isValidID(final AuthzTrans trans, final String id) {
149 DefaultOrgIdentity u = getIdentity(trans,id);
150 return (u==null||!u.isFound())?id + "is not an Identity in " + getName():null;
151 } catch (OrganizationException e) {
152 return getName() + " could not lookup " + id + ": " + e.getLocalizedMessage();
155 // Possible ID Pattern
156 // private static final Pattern ID_PATTERN=Pattern.compile("([\\w.-]+@[\\w.-]+).{4-13}");
157 // Another one: ID_PATTERN = "(a-z[a-z0-9]{5-8}@.*).{4-13}";
160 public boolean isValidCred(final AuthzTrans trans, final String id) {
162 int at = id.indexOf('@');
165 // Use this to prevent passwords to any but THIS domain.
166 // if (!id.regionMatches(at+1, domain, 0, id.length()-at-1)) {
169 sid = id.substring(0,at);
173 // We'll validate that it exists, rather than check patterns.
175 return isValidID(trans, sid)==null;
176 // Check Pattern (if checking existing is too long)
177 // if (id.endsWith(SUFFIX) && ID_PATTERN.matcher(id).matches()) {
183 private static final String SPEC_CHARS = "!@#$%^*-+?/,:;.";
184 private static final Pattern PASS_PATTERN=Pattern.compile("(((?=.*[a-z,A-Z])(((?=.*\\d))|(?=.*[" + SPEC_CHARS +"]))).{6,20})");
187 * (?=.*[a-z,A-Z]) # must contain one character
189 * (?=.*\d) # must contain one digit from 0-9
191 * (?=.*[@#$%]) # must contain one special symbols in the list SPEC_CHARS
193 * . # match anything with previous condition checking
194 * {6,20} # length at least 6 characters and maximum of 20
197 * Another example, more stringent pattern
198 private static final Pattern PASS_PATTERN=Pattern.compile("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[" + SPEC_CHARS +"]).{6,20})");
199 * Attribution: from mkyong.com
201 * (?=.*\d) # must contain one digit from 0-9
202 * (?=.*[a-z]) # must contain one lowercase characters
203 * (?=.*[A-Z]) # must contain one uppercase characters
204 * (?=.*[@#$%]) # must contain one special symbols in the list SPEC_CHARS
205 * . # match anything with previous condition checking
206 * {6,20} # length at least 6 characters and maximum of 20
210 public String isValidPassword(final AuthzTrans trans, final String user, final String password, final String... prev) {
211 for (String p : prev) {
212 if (password.contains(p)) { // A more sophisticated algorithm might be better.
213 return "Password too similar to previous passwords";
216 // If you have an Organization user/Password scheme, replace the following
217 if (PASS_PATTERN.matcher(password).matches()) {
220 return "Password does not match " + NAME + " Password Standards";
223 private static final String[] rules = new String[] {
224 "Passwords must contain letters",
225 "Passwords must contain one of the following:",
227 " One special symbols in the list \""+ SPEC_CHARS + '"',
228 "Passwords must be between 6 and 20 chars in length",
232 public String[] getPasswordRules() {
237 public Set<String> getIdentityTypes() {
242 public Response notify(AuthzTrans trans, Notify type, String url, String[] identities, String[] ccs, String summary, Boolean urgent) {
243 String system = trans.getProperty("CASS_ENV", "");
245 ArrayList<String> toList = new ArrayList<>();
247 if (identities != null) {
248 for (String user : identities) {
250 identity = getIdentity(trans, user);
251 if (identity == null) {
253 "Failure to obtain User " + user + " for "
256 toList.add(identity.email());
258 } catch (Exception e) {
261 "Failure to obtain User " + user + " for "
267 if (toList.isEmpty()) {
268 trans.error().log("No Users listed to email");
269 return Response.ERR_NotificationFailure;
272 ArrayList<String> ccList = new ArrayList<>();
274 // If we're sending an urgent email, CC the user's supervisor
277 trans.info().log("urgent msg for: " + identities[0]);
279 List<Identity> supervisors = getApprovers(trans, identities[0]);
280 for (Identity us : supervisors) {
281 trans.info().log("supervisor: " + us.email());
282 ccList.add(us.email());
284 } catch (Exception e) {
286 "Failed to find supervisor for " + identities[0]);
291 for (String user : ccs) {
293 identity = getIdentity(trans, user);
294 ccList.add(identity.email());
295 } catch (Exception e) {
298 "Failure to obtain User " + user + " for "
304 if (summary == null) {
311 sendEmail(trans, toList, ccList,
312 "AAF Approval Notification "
313 + (system.length() == 0 ? "" : "(ENV: "
317 + "System for Fine-Grained Authorizations. You are being asked to Approve"
318 + (system.length() == 0 ? "" : " in the "
319 + system + " environment")
320 + " before AAF Actions can be taken.\n\n"
321 + "Please follow this link: \n\n\t" + url
322 + "\n\n" + summary, urgent);
323 } catch (Exception e) {
325 trans.error().log(e, "Failure to send Email");
326 return Response.ERR_NotificationFailure;
329 case PasswordExpiration:
334 "AAF Password Expiration Warning "
335 + (system.length() == 0 ? "" : "(ENV: "
339 + " System for Authorizations.\n\nOne or more passwords will expire soon or have expired"
340 + (system.length() == 0 ? "" : " in the "
341 + system + " environment")
342 + ".\n\nPasswords expired for more than 30 days without action are subject to deletion.\n\n"
343 + "Please follow each link to add a New Password with Expiration Date. Either are valid until expiration. "
344 + "Use this time to change the passwords on your system. If issues, reply to this email.\n\n"
346 } catch (Exception e) {
347 trans.error().log(e, "Failure to send Email");
348 return Response.ERR_NotificationFailure;
358 "AAF Role Expiration Warning "
359 + (system.length() == 0 ? "" : "(ENV: "
363 + " System for Authorizations. One or more roles will expire soon"
364 + (system.length() == 0 ? "" : " in the "
365 + system + " environment")
366 + ".\n\nRoles expired for more than 30 days are subject to deletion."
367 + "Please follow this link the GUI Command line, and either 'extend' or 'del' the user in the role.\n"
368 + "If issues, reply to this email.\n\n\t" + url
369 + "\n\n" + summary, urgent);
370 } catch (Exception e) {
371 trans.error().log(e, "Failure to send Email");
372 return Response.ERR_NotificationFailure;
376 return Response.ERR_NotImplemented;
383 * Default Policy is to set to 6 Months for Notification Types.
384 * add others/change as required
387 public Date whenToValidate(Notify type, Date lastValidated) {
390 case PasswordExpiration:
393 GregorianCalendar gc = new GregorianCalendar();
394 gc.setTime(lastValidated);
395 gc.add(GregorianCalendar.MONTH, 6); // 6 month policy
401 public GregorianCalendar expiration(GregorianCalendar gc, Expiration exp, String... extra) {
402 GregorianCalendar now = new GregorianCalendar();
403 GregorianCalendar rv = gc==null?now:(GregorianCalendar)gc.clone();
406 // Extending Password give 5 extra days, max 8 days from now
407 rv.add(GregorianCalendar.DATE, 5);
408 now.add(GregorianCalendar.DATE, 8);
414 // Future requests last 15 days.
415 now.add(GregorianCalendar.DATE, 15);
419 // Passwords expire in 90 days
420 now.add(GregorianCalendar.DATE, 90);
424 // Temporary Passwords last for 12 hours.
425 now.add(GregorianCalendar.DATE, 90);
429 // Delegations expire max in 2 months, renewable to 3
430 rv.add(GregorianCalendar.MONTH, 2);
431 now.add(GregorianCalendar.MONTH, 3);
437 // Roles expire in 6 months
438 now.add(GregorianCalendar.MONTH, 6);
442 // Unless other wise set, 6 months is default
443 now.add(GregorianCalendar.MONTH, 6);
451 public EmailWarnings emailWarningPolicy() {
452 return emailWarnings;
456 * Assume the Supervisor is the Approver.
459 public List<Identity> getApprovers(AuthzTrans trans, String user) throws OrganizationException {
460 Identity orgIdentity = getIdentity(trans, user);
461 List<Identity> orgIdentitys = new ArrayList<>();
462 if (orgIdentity!=null) {
463 Identity supervisor = orgIdentity.responsibleTo();
464 if (supervisor!=null) {
465 orgIdentitys.add(supervisor);
472 public String getApproverType() {
477 public int startOfDay() {
478 // TODO Auto-generated method stub
483 public boolean canHaveMultipleCreds(String id) {
484 // External entities are likely mono-password... if you change it, it is a global change.
485 // This is great for people, but horrible for Applications.
487 // AAF's Password can have multiple Passwords, each with their own Expiration Date.
488 // For Default Org, we'll assume true for all, but when you add your external
489 // Identity stores, you need to return "false" if they cannot support multiple Passwords like AAF
494 public String validate(AuthzTrans trans, Policy policy, Executor executor, String... vars) throws OrganizationException {
499 DefaultOrgIdentity thisID = getIdentity(trans,vars[0]);
500 if ("a".equals(thisID.identity.status)) { // MechID
501 DefaultOrgIdentity requestor = getIdentity(trans, trans.user());
502 if (requestor!=null) {
503 Identity mechid = getIdentity(trans, vars[0]);
505 Identity sponsor = mechid.responsibleTo();
506 if (sponsor!=null && requestor.fullID().equals(sponsor.fullID())) {
509 return trans.user() + " is not the Sponsor of MechID " + vars[0];
517 case CREATE_MECHID_BY_PERM_ONLY:
518 return getName() + " only allows sponsors to create MechIDs";
521 return policy.name() + " is unsupported at " + getName();
526 public boolean isTestEnv() {
531 public void setTestMode(boolean dryRun) {
532 this.dryRun = dryRun;
535 private String extractRealm(final String r) {
537 if ((at=r.indexOf('@'))>=0) {
538 return FQI.reverseDomain(r.substring(at+1));
543 public boolean supportsRealm(final String r) {
544 if (r.endsWith(realm)) {
547 String erealm = extractRealm(r);
548 for (String sr : supportedRealms) {
549 if (erealm.startsWith(sr)) {
558 public synchronized void addSupportedRealm(final String r) {
559 supportedRealms.add(extractRealm(r));
563 public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body,
564 Boolean urgent) throws OrganizationException {
566 String mailFrom = mailer.mailFrom();
567 List<String> to = new ArrayList<>();
568 for (String em : toList) {
569 if (em.indexOf('@')<0) {
570 to.add(new DefaultOrgIdentity(trans, em, this).email());
576 List<String> cc = new ArrayList<>();
578 if (!ccList.isEmpty()) {
580 for (String em : ccList) {
581 if (em.indexOf('@')<0) {
582 cc.add(new DefaultOrgIdentity(trans, em, this).email());
589 // for now, I want all emails so we can see what goes out. Remove later
590 if (!ccList.contains(mailFrom)) {
591 ccList.add(mailFrom);
595 return mailer.sendEmail(trans,dryRun,mailFrom,to,cc,subject,body,urgent);