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.hl;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.SecureRandom;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Date;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Random;
36 import java.util.TreeSet;
38 import org.onap.aaf.auth.common.Define;
39 import org.onap.aaf.auth.dao.AbsCassDAO;
40 import org.onap.aaf.auth.dao.CachedDAO;
41 import org.onap.aaf.auth.dao.DAOException;
42 import org.onap.aaf.auth.dao.cached.CachedCertDAO;
43 import org.onap.aaf.auth.dao.cached.CachedCredDAO;
44 import org.onap.aaf.auth.dao.cached.CachedNSDAO;
45 import org.onap.aaf.auth.dao.cached.CachedPermDAO;
46 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
47 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
48 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
49 import org.onap.aaf.auth.dao.cass.CacheInfoDAO;
50 import org.onap.aaf.auth.dao.cass.CertDAO;
51 import org.onap.aaf.auth.dao.cass.CredDAO;
52 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
53 import org.onap.aaf.auth.dao.cass.DelegateDAO;
54 import org.onap.aaf.auth.dao.cass.FutureDAO;
55 import org.onap.aaf.auth.dao.cass.HistoryDAO;
56 import org.onap.aaf.auth.dao.cass.LocateDAO;
57 import org.onap.aaf.auth.dao.cass.NsDAO;
58 import org.onap.aaf.auth.dao.cass.NsSplit;
59 import org.onap.aaf.auth.dao.cass.NsType;
60 import org.onap.aaf.auth.dao.cass.PermDAO;
61 import org.onap.aaf.auth.dao.cass.RoleDAO;
62 import org.onap.aaf.auth.dao.cass.Status;
63 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
64 import org.onap.aaf.auth.env.AuthzEnv;
65 import org.onap.aaf.auth.env.AuthzTrans;
66 import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE;
67 import org.onap.aaf.auth.env.AuthzTransFilter;
68 import org.onap.aaf.auth.layer.Result;
69 import org.onap.aaf.auth.org.Organization;
70 import org.onap.aaf.cadi.Hash;
71 import org.onap.aaf.cadi.aaf.PermEval;
72 import org.onap.aaf.cadi.config.Config;
73 import org.onap.aaf.misc.env.APIException;
74 import org.onap.aaf.misc.env.Env;
75 import org.onap.aaf.misc.env.Slot;
76 import org.onap.aaf.misc.env.TimeTaken;
77 import org.onap.aaf.misc.env.util.Chrono;
79 import com.datastax.driver.core.Cluster;
84 * A Data Access Combination Object which asks Security and other Questions
89 public class Question {
91 // DON'T CHANGE FROM lower Case!!!
96 public static final String OWNER="owner";
97 public static final String ADMIN="admin";
98 public static final String DOT_OWNER=".owner";
99 public static final String DOT_ADMIN=".admin";
100 public static final String ACCESS = "access";
102 static final String ASTERIX = "*";
108 public static final String READ = Access.read.name();
109 public static final String WRITE = Access.write.name();
110 public static final String CREATE = Access.create.name();
112 public static final String ROLE = Type.role.name();
113 public static final String PERM = Type.perm.name();
114 public static final String NS = Type.ns.name();
115 public static final String CRED = Type.cred.name();
116 private static final String DELG = "delg";
117 public static final String ROOT_NS = Define.isInitialized() ? Define.ROOT_NS() : "undefined";
118 public static final String ATTRIB = "attrib";
121 public static final int MAX_SCOPE = 10;
122 public static final int APP_SCOPE = 3;
123 public static final int COMPANY_SCOPE = 2;
126 private static Set<String> specialLog = null;
127 public static final Random random = new SecureRandom();
128 private static long traceID = random.nextLong();
129 private static Slot specialLogSlot = null;
130 private static Slot transIDSlot = null;
133 private final HistoryDAO historyDAO;
134 public HistoryDAO historyDAO() {
138 private final CachedNSDAO nsDAO;
139 public CachedNSDAO nsDAO() {
143 private final CachedRoleDAO roleDAO;
144 public CachedRoleDAO roleDAO() {
148 private final CachedPermDAO permDAO;
149 public CachedPermDAO permDAO() {
153 private final CachedUserRoleDAO userRoleDAO;
154 public CachedUserRoleDAO userRoleDAO() {
158 private final CachedCredDAO credDAO;
159 public CachedCredDAO credDAO() {
163 private final CachedCertDAO certDAO;
164 public CachedCertDAO certDAO() {
168 private final DelegateDAO delegateDAO;
169 public DelegateDAO delegateDAO() {
173 private final FutureDAO futureDAO;
174 public FutureDAO futureDAO() {
178 private final ApprovalDAO approvalDAO;
179 public ApprovalDAO approvalDAO() {
183 public final LocateDAO locateDAO;
184 public LocateDAO locateDAO() {
188 private final CacheInfoDAO cacheInfoDAO;
189 private final int cldays;
190 private final boolean alwaysSpecial;
192 public Question(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
193 PERMS = trans.slot("USER_PERMS");
194 trans.init().log("Instantiating DAOs");
195 long expiresIn = Long.parseLong(trans.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF));
196 historyDAO = new HistoryDAO(trans, cluster, keyspace);
198 // Deal with Cached Entries
199 cacheInfoDAO = new CacheInfoDAO(trans, historyDAO);
201 nsDAO = new CachedNSDAO(new NsDAO(trans, historyDAO, cacheInfoDAO),cacheInfoDAO, expiresIn);
202 permDAO = new CachedPermDAO(new PermDAO(trans, historyDAO, cacheInfoDAO), cacheInfoDAO, expiresIn);
203 roleDAO = new CachedRoleDAO(new RoleDAO(trans, historyDAO, cacheInfoDAO), cacheInfoDAO, expiresIn);
204 userRoleDAO = new CachedUserRoleDAO(new UserRoleDAO(trans, historyDAO,cacheInfoDAO), cacheInfoDAO, expiresIn);
205 credDAO = new CachedCredDAO(new CredDAO(trans, historyDAO, cacheInfoDAO), cacheInfoDAO, expiresIn);
206 certDAO = new CachedCertDAO(new CertDAO(trans, historyDAO, cacheInfoDAO), cacheInfoDAO, expiresIn);
208 locateDAO = new LocateDAO(trans,historyDAO);
209 futureDAO = new FutureDAO(trans, historyDAO);
210 delegateDAO = new DelegateDAO(trans, historyDAO);
211 approvalDAO = new ApprovalDAO(trans, historyDAO);
213 if (specialLogSlot==null) {
214 specialLogSlot = trans.slot(AuthzTransFilter.SPECIAL_LOG_SLOT);
217 if (transIDSlot==null) {
218 transIDSlot = trans.slot(AuthzTransFilter.TRANS_ID_SLOT);
221 AbsCassDAO.primePSIs(trans);
223 cldays = Integer.parseInt(trans.getProperty(Config.AAF_CRED_WARN_DAYS, Config.AAF_CRED_WARN_DAYS_DFT));
225 alwaysSpecial = Boolean.parseBoolean(trans.getProperty("aaf_always_special", Boolean.FALSE.toString()));
228 public void startTimers(AuthzEnv env) {
229 // Only want to aggressively cleanse User related Caches... The others,
230 // just normal refresh
231 CachedDAO.startCleansing(env, credDAO, userRoleDAO);
232 CachedDAO.startRefresh(env, cacheInfoDAO);
235 public void close(AuthzTrans trans) {
236 historyDAO.close(trans);
237 cacheInfoDAO.close(trans);
239 permDAO.close(trans);
240 roleDAO.close(trans);
241 userRoleDAO.close(trans);
242 credDAO.close(trans);
243 certDAO.close(trans);
244 delegateDAO.close(trans);
245 futureDAO.close(trans);
246 approvalDAO.close(trans);
249 public Result<PermDAO.Data> permFrom(AuthzTrans trans, String type,
250 String instance, String action) {
251 Result<NsDAO.Data> rnd = deriveNs(trans, type);
253 return Result.ok(new PermDAO.Data(new NsSplit(rnd.value, type),
256 return Result.err(rnd);
263 * Because this call is frequently called internally, AND because we already
264 * look for it in the initial Call, we cache within the Transaction
270 public Result<List<PermDAO.Data>> getPermsByUser(AuthzTrans trans, String user, boolean lookup) {
271 return PermLookup.get(trans, this, user).getPerms(lookup);
274 public Result<List<PermDAO.Data>> getPermsByUserFromRolesFilter(AuthzTrans trans, String user, String forUser) {
275 PermLookup plUser = PermLookup.get(trans, this, user);
276 Result<Set<String>> plPermNames = plUser.getPermNames();
277 if (plPermNames.notOK()) {
278 return Result.err(plPermNames);
282 if (forUser.equals(user)) {
285 // Setup a TreeSet to check on Namespaces to
286 nss = new TreeSet<>();
287 PermLookup fUser = PermLookup.get(trans, this, forUser);
288 Result<Set<String>> forUpn = fUser.getPermNames();
289 if (forUpn.notOK()) {
290 return Result.err(forUpn);
293 for (String pn : forUpn.value) {
294 Result<String[]> decoded = PermDAO.Data.decodeToArray(trans, this, pn);
295 if (decoded.isOKhasData()) {
296 nss.add(decoded.value[0]);
298 trans.error().log(pn,", derived from a Role, is invalid:",decoded.errorString());
303 List<PermDAO.Data> rlpUser = new ArrayList<>();
304 Result<PermDAO.Data> rpdd;
306 for (String pn : plPermNames.value) {
307 rpdd = PermDAO.Data.decode(trans, this, pn);
308 if (rpdd.isOKhasData()) {
310 if (nss==null || nss.contains(pdd.ns)) {
314 trans.error().log(pn,", derived from a Role, is invalid. Run Data Cleanup:",rpdd.errorString());
317 return Result.ok(rlpUser);
320 public Result<List<PermDAO.Data>> getPermsByType(AuthzTrans trans, String perm) {
321 Result<NsSplit> nss = deriveNsSplit(trans, perm);
323 return Result.err(nss);
325 return permDAO.readByType(trans, nss.value.ns, nss.value.name);
328 public Result<List<PermDAO.Data>> getPermsByName(AuthzTrans trans, String type, String instance, String action) {
329 if(type.indexOf('@') >= 0) {
330 int colon = type.indexOf(':');
332 return permDAO.read(trans, type.substring(0, colon),type.substring(colon+1), instance,action);
334 return Result.err(Result.ERR_BadData, "%s is malformed",type);
337 Result<NsSplit> nss = deriveNsSplit(trans, type);
339 return Result.err(nss);
342 return permDAO.read(trans, nss.value.ns, nss.value.name, instance,action);
346 public Result<List<PermDAO.Data>> getPermsByRole(AuthzTrans trans, String role, boolean lookup) {
347 Result<NsSplit> nss = deriveNsSplit(trans, role);
349 return Result.err(nss);
352 Result<List<RoleDAO.Data>> rlrd = roleDAO.read(trans, nss.value.ns,
354 if (rlrd.notOKorIsEmpty()) {
355 return Result.err(rlrd);
357 // Using Set to avoid duplicates
358 Set<String> permNames = new HashSet<>();
359 if (rlrd.isOKhasData()) {
360 for (RoleDAO.Data drr : rlrd.value) {
361 permNames.addAll(drr.perms(false));
365 // Note: It should be ok for a Valid user to have no permissions -
366 // Jonathan 8/12/2013
367 List<PermDAO.Data> perms = new ArrayList<>();
368 for (String perm : permNames) {
369 Result<PermDAO.Data> pr = PermDAO.Data.decode(trans, this, perm);
371 return Result.err(pr);
375 Result<List<PermDAO.Data>> rlpd = permDAO.read(trans, pr.value);
376 if (rlpd.isOKhasData()) {
377 for (PermDAO.Data pData : rlpd.value) {
386 return Result.ok(perms);
389 public Result<List<RoleDAO.Data>> getRolesByName(AuthzTrans trans, String role) {
390 if(role.startsWith(trans.user()) ) {
391 if(role.endsWith(":user")) {
392 return roleDAO.read(trans,trans.user(), "user");
394 return Result.err(Result.ERR_BadData,"%s is a badly formatted role",role);
397 Result<NsSplit> nss = deriveNsSplit(trans, role);
399 return Result.err(nss);
401 String r = nss.value.name;
402 if (r.endsWith(".*")) { // do children Search
403 return roleDAO.readChildren(trans, nss.value.ns,
404 r.substring(0, r.length() - 2));
405 } else if (ASTERIX.equals(r)) {
406 return roleDAO.readChildren(trans, nss.value.ns, ASTERIX);
408 return roleDAO.read(trans, nss.value.ns, r);
415 * Given a Child Namespace, figure out what the best Namespace parent is.
417 * For instance, if in the NS table, the parent "org.osaaf" exists, but not
418 * "org.osaaf.child" or "org.osaaf.a.b.c", then passing in either
419 * "org.osaaf.child" or "org.osaaf.a.b.c" will return "org.osaaf"
421 * Uses recursive search on Cached DAO data
427 public Result<NsDAO.Data> deriveNs(AuthzTrans trans, String child) {
428 Result<List<NsDAO.Data>> r = nsDAO.read(trans, child);
430 if (r.isOKhasData()) {
431 return Result.ok(r.value.get(0));
433 int dot = child.lastIndexOf('.');
435 return Result.err(Status.ERR_NsNotFound, "No Namespace for [%s]", child);
437 return deriveNs(trans, child.substring(0, dot));
442 public Result<NsDAO.Data> deriveFirstNsForType(AuthzTrans trans, String str, NsType type) {
445 for (String lookup = str;!".".equals(lookup) && lookup!=null;) {
446 Result<List<NsDAO.Data>> rld = nsDAO.read(trans, lookup);
447 if (rld.isOKhasData()) {
448 nsd=rld.value.get(0);
450 if (type.type == nsd.type) {
451 return Result.ok(nsd);
453 int dot = str.lastIndexOf('.');
456 return Result.err(Status.ERR_NsNotFound, "No Namespace for [%s]", str);
458 return deriveFirstNsForType(trans, str.substring(0, dot),type);
462 int dot = str.lastIndexOf('.');
465 return Result.err(Status.ERR_NsNotFound,"There is no valid Company Namespace for %s",str);
467 return deriveFirstNsForType(trans, str.substring(0, dot),type);
471 return Result.err(Status.ERR_NotFound, str + " does not contain type " + type.name());
474 public Result<NsSplit> deriveNsSplit(AuthzTrans trans, String child) {
475 Result<NsDAO.Data> ndd = deriveNs(trans, child);
477 NsSplit nss = new NsSplit(ndd.value, child);
479 return Result.ok(nss);
481 return Result.err(Status.ERR_NsNotFound,
482 "Cannot split [%s] into valid namespace elements",
486 return Result.err(ndd);
490 * Translate an ID into it's domain
492 * i.e. myid1234@aaf.att.com results in domain of com.att.aaf
497 public static String domain2ns(String id) {
498 int at = id.indexOf('@');
500 String[] domain = id.substring(at + 1).split("\\.");
501 StringBuilder ns = new StringBuilder(id.length());
502 boolean first = true;
503 for (int i = domain.length - 1; i >= 0; --i) {
509 ns.append(domain[i]);
511 return ns.toString();
519 * Validate Namespace of ID@Domain
521 * Namespace is reverse order of Domain.
527 public Result<NsDAO.Data> validNSOfDomain(AuthzTrans trans, String id) {
528 // Take domain, reverse order, and check on NS
530 if (id.indexOf('@')<0) { // it's already an ns, not an ID
535 if (ns.length() > 0) {
536 if (!trans.org().getDomain().equals(ns)) {
537 Result<List<NsDAO.Data>> rlnsd = nsDAO.read(trans, ns);
538 if (rlnsd.isOKhasData()) {
539 return Result.ok(rlnsd.value.get(0));
543 return Result.err(Status.ERR_NsNotFound,
544 "A Namespace is not available for %s", id);
547 public Result<NsDAO.Data> mayUser(AuthzTrans trans, String user,NsDAO.Data ndd, Access access) {
548 // <ns>.access|:role:<role name>|<read|write>
549 String ns = ndd.name;
552 if (isGranted(trans, user, ns, ACCESS, ":ns", access.name())) {
553 return Result.ok(ndd);
555 if ((last = ns.lastIndexOf('.')) >= 0) {
556 ns = ns.substring(0, last);
559 // com.att.aaf.ns|:<client ns>:ns|<access>
560 // AAF-724 - Make consistent response for May User", and not take the
561 // last check... too confusing.
562 Result<NsDAO.Data> rv = mayUserVirtueOfNS(trans, user, ndd, ":" + ndd.name + ":ns", access.name());
565 } else if (rv.status==Result.ERR_Backend) {
566 return Result.err(rv);
568 return Result.err(Status.ERR_Denied, "[%s] may not %s in NS [%s]",
569 user, access.name(), ndd.name);
573 public Result<NsDAO.Data> mayUser(AuthzTrans trans, String user, RoleDAO.Data rdd, Access access) {
574 if(trans.user().equals(rdd.ns)) {
575 return Result.ok((NsDAO.Data)null);
577 Result<NsDAO.Data> rnsd = deriveNs(trans, rdd.ns);
579 return mayUser(trans, user, rnsd.value, rdd, access);
584 public Result<NsDAO.Data> mayUser(AuthzTrans trans, String user, NsDAO.Data ndd, RoleDAO.Data rdd, Access access) {
585 // 1) Is User in the Role?
586 Result<List<UserRoleDAO.Data>> rurd = userRoleDAO.readUserInRole(trans, user, rdd.fullName());
587 if (rurd.isOKhasData()) {
588 return Result.ok(ndd);
591 String roleInst = ":role:" + rdd.name;
592 // <ns>.access|:role:<role name>|<read|write>
596 if (isGranted(trans, user, ns,ACCESS, roleInst, access.name())) {
597 return Result.ok(ndd);
599 if ((last = ns.lastIndexOf('.')) >= 0) {
600 ns = ns.substring(0, last);
604 // Check if Access by Global Role perm
605 // com.att.aaf.ns|:<client ns>:role:name|<access>
606 Result<NsDAO.Data> rnsd = mayUserVirtueOfNS(trans, user, ndd, ":"
607 + rdd.ns + roleInst, access.name());
610 } else if (rnsd.status==Result.ERR_Backend) {
611 return Result.err(rnsd);
614 // Check if Access to Whole NS
615 // AAF-724 - Make consistent response for May User", and not take the
616 // last check... too confusing.
617 Result<org.onap.aaf.auth.dao.cass.NsDAO.Data> rv = mayUserVirtueOfNS(trans, user, ndd,
618 ":" + rdd.ns + ":ns", access.name());
621 } else if (rnsd.status==Result.ERR_Backend) {
622 return Result.err(rnsd);
624 return Result.err(Status.ERR_Denied, "[%s] may not %s Role [%s]",
625 user, access.name(), rdd.fullName());
630 public Result<NsDAO.Data> mayUser(AuthzTrans trans, String user,PermDAO.Data pdd, Access access) {
631 if(pdd.ns.indexOf('@')>-1) {
632 if(user.equals(pdd.ns) || isGranted(trans,user,Define.ROOT_NS(),"access",pdd.instance,READ)) {
633 NsDAO.Data ndd = new NsDAO.Data();
635 ndd.type = NsDAO.USER;
637 return Result.ok(ndd);
639 return Result.err(Result.ERR_Security,"Only a User may modify User");
642 Result<NsDAO.Data> rnsd = deriveNs(trans, pdd.ns);
644 return mayUser(trans, user, rnsd.value, pdd, access);
649 public Result<NsDAO.Data> mayUser(AuthzTrans trans, String user,NsDAO.Data ndd, PermDAO.Data pdd, Access access) {
650 if (isGranted(trans, user, pdd.ns, pdd.type, pdd.instance, pdd.action)) {
651 return Result.ok(ndd);
653 String permInst = ":perm:" + pdd.type + ':' + pdd.instance + ':' + pdd.action;
654 // <ns>.access|:role:<role name>|<read|write>
655 String ns = ndd.name;
658 if (isGranted(trans, user, ns, ACCESS, permInst, access.name())) {
659 return Result.ok(ndd);
661 if ((last = ns.lastIndexOf('.')) >= 0) {
662 ns = ns.substring(0, last);
666 // Check if Access by NS perm
667 // com.att.aaf.ns|:<client ns>:role:name|<access>
668 Result<NsDAO.Data> rnsd = mayUserVirtueOfNS(trans, user, ndd, ":" + pdd.ns + permInst, access.name());
671 } else if (rnsd.status==Result.ERR_Backend) {
672 return Result.err(rnsd);
675 // Check if Access to Whole NS
676 // AAF-724 - Make consistent response for May User", and not take the
677 // last check... too confusing.
678 Result<NsDAO.Data> rv = mayUserVirtueOfNS(trans, user, ndd, ":" + pdd.ns + ":ns", access.name());
682 return Result.err(Status.ERR_Denied,
683 "[%s] may not %s Perm [%s|%s|%s]", user, access.name(),
684 pdd.fullType(), pdd.instance, pdd.action);
689 public Result<Void> mayUser(AuthzTrans trans, DelegateDAO.Data dd, Access access) {
691 Result<NsDAO.Data> rnsd = deriveNs(trans, domain2ns(trans.user()));
692 if (rnsd.isOKhasData() && mayUserVirtueOfNS(trans,trans.user(),rnsd.value, ":" + rnsd.value.name + ":ns", access.name()).isOK()) {
695 boolean isUser = trans.user().equals(dd.user);
696 boolean isDelegate = dd.delegate != null
697 && (dd.user.equals(dd.delegate) || trans.user().equals(
699 Organization org = trans.org();
702 if (org.getIdentity(trans, dd.user) == null) {
703 return Result.err(Status.ERR_UserNotFound,
704 "[%s] is not a user in the company database.",
707 if (!dd.user.equals(dd.delegate) && org.getIdentity(trans, dd.delegate) == null) {
708 return Result.err(Status.ERR_UserNotFound,
709 "[%s] is not a user in the company database.",
712 if (!trans.requested(REQD_TYPE.force) && dd.user != null && dd.user.equals(dd.delegate)) {
713 return Result.err(Status.ERR_BadData,
714 "[%s] cannot be a delegate for self", dd.user);
716 if (!isUser && !isGranted(trans, trans.user(), ROOT_NS,DELG,
717 org.getDomain(), Question.CREATE)) {
718 return Result.err(Status.ERR_Denied,
719 "[%s] may not create a delegate for [%s]",
720 trans.user(), dd.user);
725 if (!isUser && !isDelegate &&
726 !isGranted(trans, trans.user(), ROOT_NS,DELG,org.getDomain(), access.name())) {
727 return Result.err(Status.ERR_Denied,
728 "[%s] may not %s delegates for [%s]", trans.user(),
729 access.name(), dd.user);
733 return Result.err(Status.ERR_BadData,"Unknown Access type [%s]", access.name());
735 } catch (Exception e) {
736 return Result.err(e);
742 * Check (recursively, if necessary), if able to do something based on NS
744 private Result<NsDAO.Data> mayUserVirtueOfNS(AuthzTrans trans, String user, NsDAO.Data nsd, String ns_and_type, String access) {
745 String ns = nsd.name;
747 // If an ADMIN of the Namespace, then allow
749 Result<List<UserRoleDAO.Data>> rurd;
750 if ((rurd = userRoleDAO.readUserInRole(trans, user, ns+DOT_ADMIN)).isOKhasData()) {
751 return Result.ok(nsd);
752 } else if (rurd.status==Result.ERR_Backend) {
753 return Result.err(rurd);
756 // If Specially granted Global Permission
757 if (isGranted(trans, user, ROOT_NS,NS, ns_and_type, access)) {
758 return Result.ok(nsd);
763 int dot = ns.length();
764 if ((dot = ns.lastIndexOf('.', dot - 1)) >= 0) {
765 Result<NsDAO.Data> rnsd = deriveNs(trans, ns.substring(0, dot));
767 rnsd = mayUserVirtueOfNS(trans, user, rnsd.value, ns_and_type,access);
768 } else if (rnsd.status==Result.ERR_Backend) {
769 return Result.err(rnsd);
772 return Result.ok(nsd);
773 } else if (rnsd.status==Result.ERR_Backend) {
774 return Result.err(rnsd);
777 return Result.err(Status.ERR_Denied, "%s may not %s %s", user, access,
785 * Important function - Check internal Permission Schemes for Permission to
794 public boolean isGranted(AuthzTrans trans, String user, String ns, String type,String instance, String action) {
795 Result<List<PermDAO.Data>> perms = getPermsByUser(trans, user, false);
797 for (PermDAO.Data pd : perms.value) {
798 if (ns.equals(pd.ns)) {
799 if (type.equals(pd.type)) {
800 if (PermEval.evalInstance(pd.instance, instance)) {
801 if (PermEval.evalAction(pd.action, action)) { // don't return action here, might miss other action
812 public Result<Date> doesUserCredMatch(AuthzTrans trans, String user, byte[] cred) throws DAOException {
813 Result<List<CredDAO.Data>> result;
814 TimeTaken tt = trans.start("Read DB Cred", Env.REMOTE);
816 result = credDAO.readID(trans, user);
821 Result<Date> rv = null;
823 if (result.isEmpty()) {
824 rv = Result.err(Status.ERR_UserNotFound, user);
825 if (willSpecialLog(trans,user)) {
826 trans.audit().log("Special DEBUG:", user, " does not exist in DB");
829 Date now = new Date();
830 // Bug noticed 6/22. Sorting on the result can cause Concurrency Issues.
831 List<CredDAO.Data> cddl;
832 if (result.value.size() > 1) {
833 cddl = new ArrayList<>(result.value.size());
834 for (CredDAO.Data old : result.value) {
835 if (old.type==CredDAO.BASIC_AUTH || old.type==CredDAO.BASIC_AUTH_SHA256) {
840 Collections.sort(cddl, (a, b) -> b.expires.compareTo(a.expires));
847 StringBuilder debug = willSpecialLog(trans,user)?new StringBuilder():null;
848 for (CredDAO.Data cdd : cddl) {
849 if (!cdd.id.equals(user)) {
850 trans.error().log("doesUserCredMatch DB call does not match for user: " + user);
852 if (cdd.expires.after(now)) {
853 byte[] dbcred = cdd.cred.array();
857 case CredDAO.BASIC_AUTH:
858 byte[] md5=Hash.hashMD5(cred);
859 if (Hash.compareTo(md5,dbcred)==0) {
860 checkLessThanDays(trans,cldays,now,cdd);
861 trans.setTag(cdd.tag);
862 return Result.ok(cdd.expires);
863 } else if (debug!=null) {
867 case CredDAO.BASIC_AUTH_SHA256:
868 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + cred.length);
869 bb.putInt(cdd.other);
871 byte[] hash = Hash.hashSHA256(bb.array());
873 if (Hash.compareTo(hash,dbcred)==0) {
874 checkLessThanDays(trans,cldays,now,cdd);
875 trans.setTag(cdd.tag);
876 return Result.ok(cdd.expires);
877 } else if (debug!=null) {
882 trans.error().log("Unknown Credential Type %s for %s, %s",Integer.toString(cdd.type),cdd.id, Chrono.dateTime(cdd.expires));
884 } catch (NoSuchAlgorithmException e) {
885 trans.error().log(e);
888 if (expired==null || expired.before(cdd.expires)) {
889 expired = cdd.expires;
890 trans.setTag(cdd.tag);
896 // Note: this is only returned if there are no good Credentials
897 rv = Result.err(Status.ERR_Security,
898 "Credentials expired %s",Chrono.utcStamp(expired));
900 if (debug==null && alwaysSpecial) {
901 debug = new StringBuilder();
904 debug.append(trans.env().encryptor().encrypt(new String(cred)));
905 rv = Result.err(Status.ERR_Security,String.format("invalid password - %s",debug.toString()));
910 return Result.err(result);
912 return rv == null ? Result.err(Status.ERR_Security, "Wrong credential") : rv;
916 private void load(StringBuilder debug, Data cdd) {
917 debug.append("\nDB Entry: user=");
918 debug.append(cdd.id);
919 debug.append(",type=");
920 debug.append(cdd.type);
921 debug.append(",expires=");
922 debug.append(Chrono.dateTime(cdd.expires));
923 debug.append(",tag=");
924 debug.append(cdd.tag);
929 private void checkLessThanDays(AuthzTrans trans, int days, Date now, Data cdd) {
930 long close = now.getTime() + (days * 86400000);
931 long cexp=cdd.expires.getTime();
933 int daysLeft = days-(int)((close-cexp)/86400000);
934 trans.audit().printf("user=%s,ip=%s,expires=%s,days=%d,tag=%s,msg=\"Password expires in less than %d day%s\"",
935 cdd.id,trans.ip(),Chrono.dateOnlyStamp(cdd.expires),daysLeft, cdd.tag,
936 daysLeft,daysLeft==1?"":"s");
941 public Result<CredDAO.Data> userCredSetup(AuthzTrans trans, CredDAO.Data cred) {
942 if (cred.type==CredDAO.RAW) {
943 TimeTaken tt = trans.start("Hash Cred", Env.SUB);
945 cred.type = CredDAO.BASIC_AUTH_SHA256;
946 cred.other = random.nextInt();
947 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + cred.cred.capacity());
948 bb.putInt(cred.other);
950 byte[] hash = Hash.hashSHA256(bb.array());
951 cred.cred = ByteBuffer.wrap(hash);
952 return Result.ok(cred);
953 } catch (NoSuchAlgorithmException e) {
954 return Result.err(Status.ERR_General,e.getLocalizedMessage());
959 } else if (cred.type==CredDAO.FQI) {
961 return Result.ok(cred);
963 return Result.err(Status.ERR_Security,"invalid/unreadable credential");
966 public Result<Boolean> userCredCheck(AuthzTrans trans, CredDAO.Data orig, final byte[] raw) {
967 TimeTaken tt = trans.start("CheckCred Cred", Env.SUB);
970 case CredDAO.BASIC_AUTH_SHA256:
971 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + raw.length);
972 bb.putInt(orig.other);
974 return Result.ok(Hash.compareTo(orig.cred.array(),Hash.hashSHA256(bb.array()))==0);
975 case CredDAO.BASIC_AUTH:
976 return Result.ok( Hash.compareTo(orig.cred.array(), Hash.hashMD5(raw))==0);
979 return Result.ok(false);
981 } catch (NoSuchAlgorithmException e) {
982 return Result.err(Status.ERR_General,e.getLocalizedMessage());
988 public static final String APPROVED = "APPROVE";
989 public static final String REJECT = "REJECT";
990 public static final String PENDING = "PENDING";
992 public Result<Void> canAddUser(AuthzTrans trans, UserRoleDAO.Data data,
993 List<ApprovalDAO.Data> approvals) {
994 // get the approval policy for the organization
996 // get the list of approvals with an accept status
998 // validate the approvals against the policy
1000 // for now check if all approvals are received and return
1001 // SUCCESS/FAILURE/SKIP
1002 boolean bReject = false;
1003 boolean bPending = false;
1005 for (ApprovalDAO.Data approval : approvals) {
1006 if (approval.status.equals(REJECT)) {
1008 } else if (approval.status.equals(PENDING)) {
1013 return Result.err(Status.ERR_Policy,
1014 "Approval Polocy not conformed");
1017 return Result.err(Status.ERR_ActionNotCompleted,
1018 "Required Approvals not received");
1024 private static final String NO_CACHE_NAME = "No Cache Data named %s";
1026 public Result<Void> clearCache(AuthzTrans trans, String cname) {
1027 boolean all = "all".equals(cname);
1028 Result<Void> rv = null;
1030 if (all || NsDAO.TABLE.equals(cname)) {
1031 int[] seg = series(NsDAO.CACHE_SEG);
1032 for (int i: seg) {cacheClear(trans, NsDAO.TABLE,i);}
1033 rv = cacheInfoDAO.touch(trans, NsDAO.TABLE, seg);
1035 if (all || PermDAO.TABLE.equals(cname)) {
1036 int[] seg = series(PermDAO.CACHE_SEG);
1037 for (int i: seg) {cacheClear(trans, PermDAO.TABLE,i);}
1038 rv = cacheInfoDAO.touch(trans, PermDAO.TABLE,seg);
1040 if (all || RoleDAO.TABLE.equals(cname)) {
1041 int[] seg = series(RoleDAO.CACHE_SEG);
1042 for (int i: seg) {cacheClear(trans, RoleDAO.TABLE,i);}
1043 rv = cacheInfoDAO.touch(trans, RoleDAO.TABLE,seg);
1045 if (all || UserRoleDAO.TABLE.equals(cname)) {
1046 int[] seg = series(UserRoleDAO.CACHE_SEG);
1047 for (int i: seg) {cacheClear(trans, UserRoleDAO.TABLE,i);}
1048 rv = cacheInfoDAO.touch(trans, UserRoleDAO.TABLE,seg);
1050 if (all || CredDAO.TABLE.equals(cname)) {
1051 int[] seg = series(CredDAO.CACHE_SEG);
1052 for (int i: seg) {cacheClear(trans, CredDAO.TABLE,i);}
1053 rv = cacheInfoDAO.touch(trans, CredDAO.TABLE,seg);
1055 if (all || CertDAO.TABLE.equals(cname)) {
1056 int[] seg = series(CertDAO.CACHE_SEG);
1057 for (int i: seg) {cacheClear(trans, CertDAO.TABLE,i);}
1058 rv = cacheInfoDAO.touch(trans, CertDAO.TABLE,seg);
1062 rv = Result.err(Status.ERR_BadData, NO_CACHE_NAME, cname);
1067 public Result<Void> cacheClear(AuthzTrans trans, String cname,Integer segment) {
1069 if (NsDAO.TABLE.equals(cname)) {
1070 rv = nsDAO.invalidate(segment);
1071 } else if (PermDAO.TABLE.equals(cname)) {
1072 rv = permDAO.invalidate(segment);
1073 } else if (RoleDAO.TABLE.equals(cname)) {
1074 rv = roleDAO.invalidate(segment);
1075 } else if (UserRoleDAO.TABLE.equals(cname)) {
1076 rv = userRoleDAO.invalidate(segment);
1077 } else if (CredDAO.TABLE.equals(cname)) {
1078 rv = credDAO.invalidate(segment);
1079 } else if (CertDAO.TABLE.equals(cname)) {
1080 rv = certDAO.invalidate(segment);
1082 rv = Result.err(Status.ERR_BadData, NO_CACHE_NAME, cname);
1087 private int[] series(int max) {
1088 int[] series = new int[max];
1089 for (int i = 0; i < max; ++i)
1094 public boolean isDelegated(AuthzTrans trans, String user, String approver, Map<String,Result<List<DelegateDAO.Data>>> rldd ) {
1095 Result<List<DelegateDAO.Data>> userDelegatedFor = rldd.get(user);
1096 if (userDelegatedFor==null) {
1097 userDelegatedFor=delegateDAO.readByDelegate(trans, user);
1098 rldd.put(user, userDelegatedFor);
1100 if (userDelegatedFor.isOKhasData()) {
1101 for (DelegateDAO.Data curr : userDelegatedFor.value) {
1102 if (curr.user.equals(approver) && curr.delegate.equals(user)
1103 && curr.expires.after(new Date())) {
1111 public static boolean willSpecialLog(AuthzTrans trans, String user) {
1112 Boolean b = trans.get(specialLogSlot, null);
1113 if (b==null) { // we haven't evaluated in this trans for Special Log yet
1114 if (specialLog==null) {
1117 b = specialLog.contains(user);
1118 trans.put(specialLogSlot, b);
1124 public static void logEncryptTrace(AuthzTrans trans, String data) {
1126 trans.put(transIDSlot, ti=nextTraceID());
1127 trans.trace().log("id="+Long.toHexString(ti)+",data=\""+trans.env().encryptor().encrypt(data)+'"');
1130 private synchronized static long nextTraceID() {
1134 public static synchronized boolean specialLogOn(AuthzTrans trans, String id) {
1135 if (specialLog == null) {
1136 specialLog = new HashSet<>();
1138 boolean rc = specialLog.add(id);
1140 trans.trace().printf("Trace on for %s requested by %s",id,trans.user());
1145 public static synchronized boolean specialLogOff(AuthzTrans trans, String id) {
1146 if (specialLog==null) {
1149 boolean rv = specialLog.remove(id);
1150 if (specialLog.isEmpty()) {
1154 trans.trace().printf("Trace off for %s requested by %s",id,trans.user());
1161 * Which Types can be moved
1165 public boolean canMove(NsType nsType) {
1180 public boolean isAdmin(AuthzTrans trans, String user, String ns) {
1181 Date now = new Date();
1182 Result<List<UserRoleDAO.Data>> rur = userRoleDAO.read(trans, user,ns+DOT_ADMIN);
1183 if (rur.isOKhasData()) {
1184 for (UserRoleDAO.Data urdd : rur.value){
1185 if (urdd.expires.after(now)) {
1193 public boolean isOwner(AuthzTrans trans, String user, String ns) {
1194 Result<List<UserRoleDAO.Data>> rur = userRoleDAO.read(trans, user,ns+DOT_OWNER);
1195 Date now = new Date();
1196 if (rur.isOKhasData()) {for (UserRoleDAO.Data urdd : rur.value){
1197 if (urdd.expires.after(now)) {
1204 public int countOwner(AuthzTrans trans, String ns) {
1205 Result<List<UserRoleDAO.Data>> rur = userRoleDAO.readByRole(trans,ns+DOT_OWNER);
1206 Date now = new Date();
1208 if (rur.isOKhasData()) {for (UserRoleDAO.Data urdd : rur.value){
1209 if (urdd.expires.after(now)) {
1217 * Return a Unique String, (same string, if it is already unique), with only
1218 * lowercase letters, digits and the '.' character.
1222 * @throws IOException
1224 public static String toUnique(String name) throws IOException {
1225 byte[] from = name.getBytes();
1226 StringBuilder sb = new StringBuilder();
1228 for (int i=0;i<from.length;++i) {
1229 f=(byte)(from[i]); // printables;
1230 sb.append((char)((f>>4)+0x61));
1231 sb.append((char)((f&0x0F)+0x61));
1233 return sb.toString();
1236 public static String fromUnique(String name) throws IOException {
1237 byte[] from = name.getBytes();
1238 StringBuilder sb = new StringBuilder();
1240 for (int i=0;i<from.length;++i) {
1241 c = (char)((from[i]-0x61)<<4);
1242 c |= (from[++i]-0x61);
1245 return sb.toString();