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.service;
24 import static org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE.force;
25 import static org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE.future;
26 import static org.onap.aaf.auth.layer.Result.OK;
27 import static org.onap.aaf.auth.rserv.HttpMethods.DELETE;
28 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
29 import static org.onap.aaf.auth.rserv.HttpMethods.POST;
30 import static org.onap.aaf.auth.rserv.HttpMethods.PUT;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.Date;
37 import java.util.GregorianCalendar;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
43 import java.util.TreeMap;
44 import java.util.UUID;
45 import java.util.concurrent.TimeUnit;
47 import javax.servlet.http.HttpServletRequest;
49 import org.onap.aaf.auth.common.Define;
50 import org.onap.aaf.auth.dao.DAOException;
51 import org.onap.aaf.auth.dao.cached.CachedPermDAO;
52 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
53 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
54 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
55 import org.onap.aaf.auth.dao.cass.CertDAO;
56 import org.onap.aaf.auth.dao.cass.CredDAO;
57 import org.onap.aaf.auth.dao.cass.DelegateDAO;
58 import org.onap.aaf.auth.dao.cass.FutureDAO;
59 import org.onap.aaf.auth.dao.cass.HistoryDAO;
60 import org.onap.aaf.auth.dao.cass.Namespace;
61 import org.onap.aaf.auth.dao.cass.NsDAO;
62 import org.onap.aaf.auth.dao.cass.NsDAO.Data;
63 import org.onap.aaf.auth.dao.cass.NsSplit;
64 import org.onap.aaf.auth.dao.cass.NsType;
65 import org.onap.aaf.auth.dao.cass.PermDAO;
66 import org.onap.aaf.auth.dao.cass.RoleDAO;
67 import org.onap.aaf.auth.dao.cass.Status;
68 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
69 import org.onap.aaf.auth.dao.hl.CassExecutor;
70 import org.onap.aaf.auth.dao.hl.Function;
71 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
72 import org.onap.aaf.auth.dao.hl.Function.Lookup;
73 import org.onap.aaf.auth.dao.hl.Function.OP_STATUS;
74 import org.onap.aaf.auth.dao.hl.PermLookup;
75 import org.onap.aaf.auth.dao.hl.Question;
76 import org.onap.aaf.auth.dao.hl.Question.Access;
77 import org.onap.aaf.auth.env.AuthzTrans;
78 import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE;
79 import org.onap.aaf.auth.layer.Result;
80 import org.onap.aaf.auth.org.Executor;
81 import org.onap.aaf.auth.org.Organization;
82 import org.onap.aaf.auth.org.Organization.Expiration;
83 import org.onap.aaf.auth.org.Organization.Identity;
84 import org.onap.aaf.auth.org.Organization.Policy;
85 import org.onap.aaf.auth.org.OrganizationException;
86 import org.onap.aaf.auth.rserv.doc.ApiDoc;
87 import org.onap.aaf.auth.service.mapper.Mapper;
88 import org.onap.aaf.auth.service.mapper.Mapper.API;
89 import org.onap.aaf.auth.service.validation.ServiceValidator;
90 import org.onap.aaf.auth.validation.Validator;
91 import org.onap.aaf.cadi.aaf.Defaults;
92 import org.onap.aaf.cadi.principal.BasicPrincipal;
93 import org.onap.aaf.cadi.util.FQI;
94 import org.onap.aaf.misc.env.Env;
95 import org.onap.aaf.misc.env.TimeTaken;
96 import org.onap.aaf.misc.env.util.Chrono;
97 import org.onap.aaf.misc.env.util.Split;
99 import aaf.v2_0.CredRequest;
102 * AuthzCassServiceImpl implements AuthzCassService for
117 public class AuthzCassServiceImpl <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS>
118 implements AuthzService <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> {
120 private static final String TWO_SPACE = " ";
121 private Mapper <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> mapper;
123 public Mapper <NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> mapper() {return mapper;}
125 private static final String ASTERIX = "*";
126 private static final String CACHE = "cache";
127 private static final String ROOT_NS = Define.ROOT_NS();
128 private static final String ROOT_COMPANY = Define.ROOT_COMPANY();
130 private final Question ques;
131 private final Function func;
133 public AuthzCassServiceImpl(AuthzTrans trans, Mapper<NSS,PERMS,PERMKEY,ROLES,USERS,USERROLES,DELGS,CERTS,KEYS,REQUEST,HISTORY,ERR,APPROVALS> mapper,Question question) {
134 this.ques = question;
135 func = new Function(trans, question);
136 this.mapper = mapper;
140 /***********************************
142 ***********************************/
145 * @throws DAOException
146 * @see org.onap.aaf.auth.service.AuthzService#createNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String, java.lang.String)
153 errorCodes = { 403,404,406,409 },
154 text = { "Namespace consists of: ",
155 "<ul><li>name - What you want to call this Namespace</li>",
156 "<li>responsible(s) - Person(s) who receive Notifications and approves Requests ",
157 "regarding this Namespace. Companies have Policies as to who may take on ",
158 "this Responsibility. Separate multiple identities with commas</li>",
159 "<li>admin(s) - Person(s) who are allowed to make changes on the namespace, ",
160 "including creating Roles, Permissions and Credentials. Separate multiple ",
161 "identities with commas</li></ul>",
162 "Note: Namespaces are dot-delimited (i.e. com.myCompany.myApp) and must be ",
163 "created with parent credentials (i.e. To create com.myCompany.myApp, you must ",
164 "be an admin of com.myCompany or com"
168 public Result<Void> createNS(final AuthzTrans trans, REQUEST from, NsType type) {
169 final Result<Namespace> rnamespace = mapper.ns(trans, from);
170 final ServiceValidator v = new ServiceValidator();
171 if (v.ns(rnamespace).err()) {
172 return Result.err(Status.ERR_BadData,v.errs());
174 final Namespace namespace = rnamespace.value;
175 final Result<NsDAO.Data> parentNs = ques.deriveNs(trans,namespace.name);
176 if (parentNs.notOK()) {
177 return Result.err(parentNs);
180 // Note: Data validate occurs in func.createNS
181 if (namespace.name.lastIndexOf('.')<0) { // Root Namespace... Function will check if allowed
182 return func.createNS(trans, namespace, false);
185 Result<FutureDAO.Data> fd = mapper.future(trans, NsDAO.TABLE,from,namespace,true,
188 public String get() {
189 return "Create Namespace [" + namespace.name + ']';
193 private Result<NsDAO.Data> rnd;
195 public Result<?> mayChange() {
197 rnd = ques.mayUser(trans, trans.user(), parentNs.value,Access.write);
204 Result<String> rfc = func.createFuture(trans, fd.value, namespace.name, trans.user(),parentNs.value, FUTURE_OP.C);
206 return Result.err(Status.ACC_Future, "NS [%s] is saved for future processing",namespace.name);
208 return Result.err(rfc);
211 return func.createNS(trans, namespace, false);
213 return Result.err(fd);
219 path = "/authz/ns/:ns/admin/:id",
220 params = { "ns|string|true",
224 errorCodes = { 403,404,406,409 },
225 text = { "Add an Identity :id to the list of Admins for the Namespace :ns",
226 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)" }
229 public Result<Void> addAdminNS(AuthzTrans trans, String ns, String id) {
230 return func.addUserRole(trans, id, ns,Question.ADMIN);
235 path = "/authz/ns/:ns/admin/:id",
236 params = { "ns|string|true",
240 errorCodes = { 403,404 },
241 text = { "Remove an Identity :id from the list of Admins for the Namespace :ns",
242 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)" }
245 public Result<Void> delAdminNS(AuthzTrans trans, String ns, String id) {
246 return func.delAdmin(trans,ns,id);
251 path = "/authz/ns/:ns/responsible/:id",
252 params = { "ns|string|true",
256 errorCodes = { 403,404,406,409 },
257 text = { "Add an Identity :id to the list of Responsibles for the Namespace :ns",
258 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)" }
261 public Result<Void> addResponsibleNS(AuthzTrans trans, String ns, String id) {
262 return func.addUserRole(trans,id,ns,Question.OWNER);
267 path = "/authz/ns/:ns/responsible/:id",
268 params = { "ns|string|true",
272 errorCodes = { 403,404 },
273 text = { "Remove an Identity :id to the list of Responsibles for the Namespace :ns",
274 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)",
275 "Note: A namespace must have at least 1 responsible party"
279 public Result<Void> delResponsibleNS(AuthzTrans trans, String ns, String id) {
280 return func.delOwner(trans,ns,id);
284 * @see org.onap.aaf.auth.service.AuthzService#applyModel(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
288 path = "/authz/ns/:ns/attrib/:key/:value",
289 params = { "ns|string|true",
291 "value|string|true"},
293 errorCodes = { 403,404,406,409 },
295 "Create an attribute in the Namespace",
296 "You must be given direct permission for key by AAF"
300 public Result<Void> createNsAttrib(AuthzTrans trans, String ns, String key, String value) {
301 TimeTaken tt = trans.start("Create NsAttrib " + ns + ':' + key + ':' + value, Env.SUB);
304 final Validator v = new ServiceValidator();
305 if (v.ns(ns).err() ||
307 v.value(value).err()) {
308 return Result.err(Status.ERR_BadData,v.errs());
311 // Check if exists already
312 Result<List<Data>> rlnsd = ques.nsDAO().read(trans, ns);
313 if (rlnsd.notOKorIsEmpty()) {
314 return Result.err(rlnsd);
316 NsDAO.Data nsd = rlnsd.value.get(0);
318 // Check for Existence
319 if (nsd.attrib.get(key)!=null) {
320 return Result.err(Status.ERR_ConflictAlreadyExists, "NS Property %s:%s exists", ns, key);
323 // Check if User may put
324 if (!ques.isGranted(trans, trans.user(), ROOT_NS, Question.ATTRIB,
325 ":"+trans.org().getDomain()+".*:"+key, Access.write.name())) {
326 return Result.err(Status.ERR_Denied, "%s may not create NS Attrib [%s:%s]", trans.user(),ns, key);
330 nsd.attrib.put(key, value);
331 ques.nsDAO().dao().attribAdd(trans,ns,key,value);
332 ques.nsDAO().invalidate(trans, nsd);
341 path = "/authz/ns/attrib/:key",
342 params = { "key|string|true" },
344 errorCodes = { 403,404 },
346 "Read Attributes for Namespace"
350 public Result<KEYS> readNsByAttrib(AuthzTrans trans, String key) {
352 final Validator v = new ServiceValidator();
353 if (v.nullOrBlank("Key",key).err()) {
354 return Result.err(Status.ERR_BadData,v.errs());
358 if (!ques.isGranted(trans, trans.user(), ROOT_NS, Question.ATTRIB,
359 ":"+trans.org().getDomain()+".*:"+key, Question.READ)) {
360 return Result.err(Status.ERR_Denied,"%s may not read NS by Attrib '%s'",trans.user(),key);
363 Result<Set<String>> rsd = ques.nsDAO().dao().readNsByAttrib(trans, key);
365 return Result.err(rsd);
367 return mapper().keys(rsd.value);
373 path = "/authz/ns/:ns/attrib/:key/:value",
374 params = { "ns|string|true",
377 errorCodes = { 403,404 },
379 "Update Value on an existing attribute in the Namespace",
380 "You must be given direct permission for key by AAF"
384 public Result<?> updateNsAttrib(AuthzTrans trans, String ns, String key, String value) {
385 TimeTaken tt = trans.start("Update NsAttrib " + ns + ':' + key + ':' + value, Env.SUB);
388 final Validator v = new ServiceValidator();
389 if (v.ns(ns).err() ||
391 v.value(value).err()) {
392 return Result.err(Status.ERR_BadData,v.errs());
395 // Check if exists already (NS must exist)
396 Result<List<Data>> rlnsd = ques.nsDAO().read(trans, ns);
397 if (rlnsd.notOKorIsEmpty()) {
398 return Result.err(rlnsd);
400 NsDAO.Data nsd = rlnsd.value.get(0);
402 // Check for Existence
403 if (nsd.attrib.get(key)==null) {
404 return Result.err(Status.ERR_NotFound, "NS Property %s:%s exists", ns, key);
407 // Check if User may put
408 if (!ques.isGranted(trans, trans.user(), ROOT_NS, Question.ATTRIB,
409 ":"+trans.org().getDomain()+".*:"+key, Access.write.name())) {
410 return Result.err(Status.ERR_Denied, "%s may not create NS Attrib [%s:%s]", trans.user(),ns, key);
414 nsd.attrib.put(key, value);
415 ques.nsDAO().invalidate(trans, nsd);
416 return ques.nsDAO().update(trans,nsd);
425 path = "/authz/ns/:ns/attrib/:key",
426 params = { "ns|string|true",
429 errorCodes = { 403,404 },
431 "Delete an attribute in the Namespace",
432 "You must be given direct permission for key by AAF"
436 public Result<Void> deleteNsAttrib(AuthzTrans trans, String ns, String key) {
437 TimeTaken tt = trans.start("Delete NsAttrib " + ns + ':' + key, Env.SUB);
440 final Validator v = new ServiceValidator();
441 if (v.nullOrBlank("NS",ns).err() ||
442 v.nullOrBlank("Key",key).err()) {
443 return Result.err(Status.ERR_BadData,v.errs());
446 // Check if exists already
447 Result<List<Data>> rlnsd = ques.nsDAO().read(trans, ns);
448 if (rlnsd.notOKorIsEmpty()) {
449 return Result.err(rlnsd);
451 NsDAO.Data nsd = rlnsd.value.get(0);
453 // Check for Existence
454 if (nsd.attrib.get(key)==null) {
455 return Result.err(Status.ERR_NotFound, "NS Property [%s:%s] does not exist", ns, key);
458 // Check if User may del
459 if (!ques.isGranted(trans, trans.user(), ROOT_NS, "attrib", ":" + ROOT_COMPANY + ".*:"+key, Access.write.name())) {
460 return Result.err(Status.ERR_Denied, "%s may not delete NS Attrib [%s:%s]", trans.user(),ns, key);
464 nsd.attrib.remove(key);
465 ques.nsDAO().dao().attribRemove(trans,ns,key);
466 ques.nsDAO().invalidate(trans, nsd);
475 path = "/authz/nss/:id",
476 params = { "id|string|true" },
478 errorCodes = { 404,406 },
480 "Lists the Owner(s), Admin(s), Description, and Attributes of Namespace :id",
484 public Result<NSS> getNSbyName(AuthzTrans trans, String ns, boolean includeExpired) {
485 final Validator v = new ServiceValidator();
486 if (v.nullOrBlank("NS", ns).err()) {
487 return Result.err(Status.ERR_BadData,v.errs());
490 Result<List<NsDAO.Data>> rlnd = ques.nsDAO().read(trans, ns);
492 if (rlnd.isEmpty()) {
493 return Result.err(Status.ERR_NotFound, "No data found for %s",ns);
495 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rlnd.value.get(0), Access.read);
497 return Result.err(rnd);
501 Namespace namespace = new Namespace(rnd.value);
502 Result<List<String>> rd = func.getOwners(trans, namespace.name, includeExpired);
504 namespace.owner = rd.value;
506 rd = func.getAdmins(trans, namespace.name, includeExpired);
508 namespace.admin = rd.value;
511 NSS nss = mapper.newInstance(API.NSS);
512 return mapper.nss(trans, namespace, nss);
514 return Result.err(rlnd);
520 path = "/authz/nss/admin/:id",
521 params = { "id|string|true" },
523 errorCodes = { 403,404 },
524 text = { "Lists all Namespaces where Identity :id is an Admin",
525 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)"
529 public Result<NSS> getNSbyAdmin(AuthzTrans trans, String user, boolean full) {
530 final Validator v = new ServiceValidator();
531 if (v.nullOrBlank("User", user).err()) {
532 return Result.err(Status.ERR_BadData, v.errs());
535 Result<Collection<Namespace>> rn = loadNamepace(trans, user, ".admin", full);
537 return Result.err(rn);
540 return Result.err(Status.ERR_NotFound, "[%s] is not an admin for any namespaces",user);
542 NSS nss = mapper.newInstance(API.NSS);
543 // Note: "loadNamespace" already validates view of Namespace
544 return mapper.nss(trans, rn.value, nss);
549 path = "/authz/nss/either/:id",
550 params = { "id|string|true" },
552 errorCodes = { 403,404 },
553 text = { "Lists all Namespaces where Identity :id is either an Admin or an Owner",
554 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)"
558 public Result<NSS> getNSbyEither(AuthzTrans trans, String user, boolean full) {
559 final Validator v = new ServiceValidator();
560 if (v.nullOrBlank("User", user).err()) {
561 return Result.err(Status.ERR_BadData, v.errs());
564 Result<Collection<Namespace>> rn = loadNamepace(trans, user, null, full);
566 return Result.err(rn);
569 return Result.err(Status.ERR_NotFound, "[%s] is not an admin or owner for any namespaces",user);
571 NSS nss = mapper.newInstance(API.NSS);
572 // Note: "loadNamespace" already validates view of Namespace
573 return mapper.nss(trans, rn.value, nss);
576 private Result<Collection<Namespace>> loadNamepace(AuthzTrans trans, String user, String endsWith, boolean full) {
577 Result<List<UserRoleDAO.Data>> urd = ques.userRoleDAO().readByUser(trans, user);
578 if (urd.notOKorIsEmpty()) {
579 return Result.err(urd);
581 Map<String, Namespace> lm = new HashMap<>();
582 Map<String, Namespace> other = full || endsWith==null?null:new TreeMap<>();
583 for (UserRoleDAO.Data urdd : urd.value) {
585 if (endsWith==null || urdd.role.endsWith(endsWith)) {
586 RoleDAO.Data rd = RoleDAO.Data.decode(urdd);
587 Result<NsDAO.Data> nsd = ques.mayUser(trans, user, rd, Access.read);
589 Namespace namespace = lm.get(nsd.value.name);
590 if (namespace==null) {
591 namespace = new Namespace(nsd.value);
592 lm.put(namespace.name,namespace);
594 Result<List<String>> rls = func.getAdmins(trans, namespace.name, false);
596 namespace.admin=rls.value;
599 rls = func.getOwners(trans, namespace.name, false);
601 namespace.owner=rls.value;
605 } else { // Shortened version. Only Namespace Info available from Role.
606 if (Question.ADMIN.equals(urdd.rname) || Question.OWNER.equals(urdd.rname)) {
607 RoleDAO.Data rd = RoleDAO.Data.decode(urdd);
608 Result<NsDAO.Data> nsd = ques.mayUser(trans, user, rd, Access.read);
610 Namespace namespace = lm.get(nsd.value.name);
611 if (namespace==null) {
613 namespace = other.remove(nsd.value.name);
615 if (namespace==null) {
616 namespace = new Namespace(nsd.value);
617 namespace.admin=new ArrayList<>();
618 namespace.owner=new ArrayList<>();
620 if (endsWith==null || urdd.role.endsWith(endsWith)) {
621 lm.put(namespace.name,namespace);
623 other.put(namespace.name,namespace);
626 if (Question.OWNER.equals(urdd.rname)) {
627 namespace.owner.add(urdd.user);
629 namespace.admin.add(urdd.user);
635 return Result.ok(lm.values());
640 path = "/authz/nss/responsible/:id",
641 params = { "id|string|true" },
643 errorCodes = { 403,404 },
644 text = { "Lists all Namespaces where Identity :id is a Responsible Party",
645 "Note: :id must be fully qualified (i.e. ab1234@people.osaaf.org)"
649 public Result<NSS> getNSbyResponsible(AuthzTrans trans, String user, boolean full) {
650 final Validator v = new ServiceValidator();
651 if (v.nullOrBlank("User", user).err()) {
652 return Result.err(Status.ERR_BadData, v.errs());
654 Result<Collection<Namespace>> rn = loadNamepace(trans, user, ".owner",full);
656 return Result.err(rn);
659 return Result.err(Status.ERR_NotFound, "[%s] is not an owner for any namespaces",user);
661 NSS nss = mapper.newInstance(API.NSS);
662 // Note: "loadNamespace" prevalidates
663 return mapper.nss(trans, rn.value, nss);
668 path = "/authz/nss/children/:id",
669 params = { "id|string|true" },
671 errorCodes = { 403,404 },
672 text = { "Lists all Child Namespaces of Namespace :id",
673 "Note: This is not a cached read"
677 public Result<NSS> getNSsChildren(AuthzTrans trans, String parent) {
678 final Validator v = new ServiceValidator();
679 if (v.nullOrBlank("NS", parent).err()) {
680 return Result.err(Status.ERR_BadData,v.errs());
683 Result<NsDAO.Data> rnd = ques.deriveNs(trans, parent);
685 return Result.err(rnd);
687 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
689 return Result.err(rnd);
692 Set<Namespace> lm = new HashSet<>();
693 Result<List<NsDAO.Data>> rlnd = ques.nsDAO().dao().getChildren(trans, parent);
695 if (rlnd.isEmpty()) {
696 return Result.err(Status.ERR_NotFound, "No data found for %s",parent);
698 for (NsDAO.Data ndd : rlnd.value) {
699 Namespace namespace = new Namespace(ndd);
700 Result<List<String>> rls = func.getAdmins(trans, namespace.name, false);
702 namespace.admin=rls.value;
705 rls = func.getOwners(trans, namespace.name, false);
707 namespace.owner=rls.value;
712 NSS nss = mapper.newInstance(API.NSS);
713 return mapper.nss(trans,lm, nss);
715 return Result.err(rlnd);
725 errorCodes = { 403,404,406 },
726 text = { "Replace the Current Description of a Namespace with a new one"
730 public Result<Void> updateNsDescription(AuthzTrans trans, REQUEST from) {
731 final Result<Namespace> nsd = mapper.ns(trans, from);
732 final ServiceValidator v = new ServiceValidator();
733 if (v.ns(nsd).err()) {
734 return Result.err(Status.ERR_BadData,v.errs());
736 if (v.nullOrBlank("description", nsd.value.description).err()) {
737 return Result.err(Status.ERR_BadData,v.errs());
740 Namespace namespace = nsd.value;
741 Result<List<NsDAO.Data>> rlnd = ques.nsDAO().read(trans, namespace.name);
743 if (rlnd.notOKorIsEmpty()) {
744 return Result.err(Status.ERR_NotFound, "Namespace [%s] does not exist",namespace.name);
747 if (ques.mayUser(trans, trans.user(), rlnd.value.get(0), Access.write).notOK()) {
748 return Result.err(Status.ERR_Denied, "You do not have approval to change %s",namespace.name);
751 Result<Void> rdr = ques.nsDAO().dao().addDescription(trans, namespace.name, namespace.description);
755 return Result.err(rdr);
761 * @throws DAOException
762 * @see org.onap.aaf.auth.service.AuthzService#deleteNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String, java.lang.String)
766 path = "/authz/ns/:ns",
767 params = { "ns|string|true" },
769 errorCodes = { 403,404,424 },
770 text = { "Delete the Namespace :ns. Namespaces cannot normally be deleted when there ",
771 "are still credentials associated with them, but they can be deleted by setting ",
772 "the \"force\" property. To do this: Add 'force=true' as a query parameter",
773 "<p>WARNING: Using force will delete all credentials attached to this namespace. Use with care.</p>"
774 + "if the \"force\" property is set to 'force=move', then Permissions and Roles are not deleted,"
775 + "but are retained, and assigned to the Parent Namespace. 'force=move' is not permitted "
776 + "at or below Application Scope"
780 public Result<Void> deleteNS(AuthzTrans trans, String ns) {
781 return func.deleteNS(trans, ns);
785 /***********************************
787 ***********************************/
791 * @see org.onap.aaf.auth.service.AuthzService#createOrUpdatePerm(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object, boolean, java.lang.String, java.lang.String, java.lang.String, java.util.List, java.util.List)
795 path = "/authz/perm",
798 errorCodes = {403,404,406,409},
799 text = { "Permission consists of:",
800 "<ul><li>type - a Namespace qualified identifier specifying what kind of resource "
801 + "is being protected</li>",
802 "<li>instance - a key, possibly multi-dimensional, that identifies a specific "
803 + " instance of the type</li>",
804 "<li>action - what kind of action is allowed</li></ul>",
805 "Note: instance and action can be an *"
809 public Result<Void> createPerm(final AuthzTrans trans,REQUEST rreq) {
810 final Result<PermDAO.Data> newPd = mapper.perm(trans, rreq);
812 final ServiceValidator v = new ServiceValidator();
813 if (v.perm(newPd).err()) {
814 return Result.err(Status.ERR_BadData,v.errs());
817 // User Permission mechanism
818 if(newPd.value.ns.indexOf('@')>0) {
819 PermDAO.Data pdd = newPd.value;
820 if(trans.user().equals(newPd.value.ns)) {
821 CachedPermDAO permDAO = ques.permDAO();
822 Result<List<PermDAO.Data>> rlpdd = permDAO.read(trans, pdd);
824 return Result.err(rlpdd);
826 if(!rlpdd.isEmpty()) {
827 return Result.err(Result.ERR_ConflictAlreadyExists,"Permission already exists");
830 RoleDAO.Data rdd = new RoleDAO.Data();
834 pdd.roles(true).add(rdd.fullName());
835 Result<PermDAO.Data> rpdd = permDAO.create(trans, pdd);
837 return Result.err(rpdd);
840 CachedRoleDAO roleDAO = ques.roleDAO();
841 Result<List<RoleDAO.Data>> rlrdd = roleDAO.read(trans, rdd);
843 return Result.err(rlrdd);
845 if(!rlrdd.isEmpty()) {
846 rdd = rlrdd.value.get(0);
850 String eperm = pdd.encode();
851 rdd.perms(true).add(eperm);
852 Result<Void> rv = roleDAO.update(trans, rdd);
857 CachedUserRoleDAO urDAO = ques.userRoleDAO();
858 UserRoleDAO.Data urdd = new UserRoleDAO.Data();
859 urdd.user = trans.user();
861 urdd.rname = rdd.name;
862 urdd.role = rdd.fullName();
863 Result<List<UserRoleDAO.Data>> rlurdd = urDAO.read(trans, urdd);
865 return Result.err(rlrdd);
866 } else if(rlurdd.isEmpty()) {
867 GregorianCalendar gc = trans.org().expiration(null, Expiration.UserInRole);
869 return Result.err(Result.ERR_Policy,"Organzation does not grant Expiration for UserRole");
871 urdd.expires = gc.getTime();
873 Result<UserRoleDAO.Data> rurdd = urDAO.create(trans, urdd);
874 return Result.err(rurdd);
878 return Result.err(Result.ERR_Security,"Only the User can create User Permissions");
881 // Does Perm Type exist as a Namespace?
882 if(newPd.value.type.isEmpty() || ques.nsDAO().read(trans, newPd.value.fullType()).isOKhasData()) {
883 return Result.err(Status.ERR_ConflictAlreadyExists,
884 "Permission Type exists as a Namespace");
887 Result<FutureDAO.Data> fd = mapper.future(trans, PermDAO.TABLE, rreq, newPd.value,false,
890 public String get() {
891 return "Create Permission [" +
892 newPd.value.fullType() + '|' +
893 newPd.value.instance + '|' +
894 newPd.value.action + ']';
898 private Result<NsDAO.Data> nsd;
900 public Result<?> mayChange() {
902 nsd = ques.mayUser(trans, trans.user(), newPd.value, Access.write);
908 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, newPd.value.ns);
909 if (nsr.notOKorIsEmpty()) {
910 return Result.err(nsr);
914 Result<String> rfc = func.createFuture(trans,fd.value,
915 newPd.value.fullType() + '|' + newPd.value.instance + '|' + newPd.value.action,
920 return Result.err(Status.ACC_Future, "Perm [%s.%s|%s|%s] is saved for future processing",
923 newPd.value.instance,
926 return Result.err(rfc);
929 return func.createPerm(trans, newPd.value, true);
931 return Result.err(fd);
938 path = "/authz/perms/:type",
939 params = {"type|string|true"},
941 errorCodes = { 404,406 },
942 text = { "List All Permissions that match the :type element of the key" }
945 public Result<PERMS> getPermsByType(AuthzTrans trans, final String permType) {
946 final Validator v = new ServiceValidator();
947 if (v.nullOrBlank("PermType", permType).err()) {
948 return Result.err(Status.ERR_BadData,v.errs());
951 Result<List<PermDAO.Data>> rlpd = ques.getPermsByType(trans, permType);
953 return Result.err(rlpd);
956 // We don't have instance & action for mayUserView... do we want to loop through all returned here as well as in mapper?
957 // Result<NsDAO.Data> r;
958 // if ((r = ques.mayUserViewPerm(trans, trans.user(), permType)).notOK())return Result.err(r);
960 PERMS perms = mapper.newInstance(API.PERMS);
961 if (!rlpd.isEmpty()) {
962 // Note: Mapper will restrict what can be viewed
963 return mapper.perms(trans, rlpd.value, perms, true);
965 return Result.ok(perms);
970 path = "/authz/perms/:type/:instance/:action",
971 params = {"type|string|true",
972 "instance|string|true",
973 "action|string|true"},
975 errorCodes = { 404,406 },
976 text = { "List Permissions that match key; :type, :instance and :action" }
979 public Result<PERMS> getPermsByName(AuthzTrans trans, String type, String instance, String action) {
980 final Validator v = new ServiceValidator();
981 if (v.nullOrBlank("PermType", type).err()
982 || v.nullOrBlank("PermInstance", instance).err()
983 || v.nullOrBlank("PermAction", action).err()) {
984 return Result.err(Status.ERR_BadData,v.errs());
987 Result<List<PermDAO.Data>> rlpd = ques.getPermsByName(trans, type, instance, action);
989 return Result.err(rlpd);
992 PERMS perms = mapper.newInstance(API.PERMS);
993 if (!rlpd.isEmpty()) {
994 // Note: Mapper will restrict what can be viewed
995 return mapper.perms(trans, rlpd.value, perms, true);
997 return Result.ok(perms);
1002 path = "/authz/perms/user/:user",
1003 params = {"user|string|true"},
1005 errorCodes = { 404,406 },
1006 text = { "List All Permissions that match user :user",
1007 "<p>'user' must be expressed as full identity (ex: id@full.domain.com)</p>"}
1010 public Result<PERMS> getPermsByUser(AuthzTrans trans, String user) {
1011 final Validator v = new ServiceValidator();
1012 if (v.nullOrBlank("User", user).err()) {
1013 return Result.err(Status.ERR_BadData,v.errs());
1016 PermLookup pl = PermLookup.get(trans,ques,user);
1017 Result<List<PermDAO.Data>> rlpd = pl.getPerms(trans.requested(force));
1019 return Result.err(rlpd);
1022 PERMS perms = mapper.newInstance(API.PERMS);
1024 if (rlpd.isEmpty()) {
1025 return Result.ok(perms);
1027 // Note: Mapper will restrict what can be viewed
1028 // if user is the same as that which is looked up, no filtering is required
1029 return mapper.perms(trans, rlpd.value,
1031 !user.equals(trans.user()));
1036 path = "/authz/perms/user/:user/scope/:scope",
1037 params = {"user|string|true","scope|string|true"},
1039 errorCodes = { 404,406 },
1040 text = { "List All Permissions that match user :user, filtered by NS (Scope)",
1041 "<p>'user' must be expressed as full identity (ex: id@full.domain.com)</p>",
1042 "<p>'scope' must be expressed as NSs separated by ':'</p>"
1046 public Result<PERMS> getPermsByUserScope(AuthzTrans trans, String user, String[] scopes) {
1047 final Validator v = new ServiceValidator();
1048 if (v.nullOrBlank("User", user).err()) {
1049 return Result.err(Status.ERR_BadData,v.errs());
1052 Result<List<PermDAO.Data>> rlpd = ques.getPermsByUser(trans, user, trans.requested(force));
1054 return Result.err(rlpd);
1057 PERMS perms = mapper.newInstance(API.PERMS);
1059 if (rlpd.isEmpty()) {
1060 return Result.ok(perms);
1062 // Note: Mapper will restrict what can be viewed
1063 // if user is the same as that which is looked up, no filtering is required
1064 return mapper.perms(trans, rlpd.value,
1067 !user.equals(trans.user()));
1072 path = "/authz/perms/user/:user",
1073 params = {"user|string|true"},
1075 errorCodes = { 404,406 },
1076 text = { "List All Permissions that match user :user",
1077 "<p>'user' must be expressed as full identity (ex: id@full.domain.com)</p>",
1079 "Present Queries as one or more Permissions (see ContentType Links below for format).",
1081 "If the Caller is Granted this specific Permission, and the Permission is valid",
1082 " for the User, it will be included in response Permissions, along with",
1083 " all the normal permissions on the 'GET' version of this call. If it is not",
1084 " valid, or Caller does not have permission to see, it will be removed from the list",
1086 " *Note: This design allows you to make one call for all expected permissions",
1087 " The permission to be included MUST be:",
1088 " <user namespace>.access|:<ns|role|perm>[:key]|<create|read|write>",
1090 " com.att.myns.access|:ns|write",
1091 " com.att.myns.access|:role:myrole|create",
1092 " com.att.myns.access|:perm:mytype:myinstance:myaction|read",
1097 public Result<PERMS> getPermsByUser(AuthzTrans trans, PERMS _perms, String user) {
1098 PERMS perms = _perms;
1099 final Validator v = new ServiceValidator();
1100 if (v.nullOrBlank("User", user).err()) {
1101 return Result.err(Status.ERR_BadData,v.errs());
1105 PermLookup pl = PermLookup.get(trans,ques,user);
1106 Result<List<PermDAO.Data>> rlpd = pl.getPerms(trans.requested(force));
1108 return Result.err(rlpd);
1112 1) See if allowed to query
1113 2) See if User is allowed
1115 Result<List<PermDAO.Data>> in = mapper.perms(trans, perms);
1116 if (in.isOKhasData()) {
1117 List<PermDAO.Data> out = rlpd.value;
1119 for (PermDAO.Data pdd : in.value) {
1121 if ("access".equals(pdd.type)) {
1122 Access access = Access.valueOf(pdd.action);
1123 String[] mdkey = Split.splitTrim(':',pdd.instance);
1124 if (mdkey.length>1) {
1125 String type = mdkey[1];
1126 if ("role".equals(type)) {
1127 if (mdkey.length>2) {
1128 RoleDAO.Data rdd = new RoleDAO.Data();
1131 ok = ques.mayUser(trans, trans.user(), rdd, Access.read).isOK() && ques.mayUser(trans, user, rdd , access).isOK();
1133 } else if ("perm".equals(type)) {
1134 if (mdkey.length>4) { // also need instance/action
1135 PermDAO.Data p = new PermDAO.Data();
1138 p.instance=mdkey[3];
1140 ok = ques.mayUser(trans, trans.user(), p, Access.read).isOK() && ques.mayUser(trans, user, p , access).isOK();
1142 } else if ("ns".equals(type)) {
1143 NsDAO.Data ndd = new NsDAO.Data();
1145 ok = ques.mayUser(trans, trans.user(), ndd, Access.read).isOK() && ques.mayUser(trans, user, ndd , access).isOK();
1155 perms = mapper.newInstance(API.PERMS);
1156 if (rlpd.isEmpty()) {
1157 return Result.ok(perms);
1159 // Note: Mapper will restrict what can be viewed
1160 // if user is the same as that which is looked up, no filtering is required
1161 return mapper.perms(trans, rlpd.value,
1163 !user.equals(trans.user()));
1168 path = "/authz/perms/role/:role",
1169 params = {"role|string|true"},
1171 errorCodes = { 404,406 },
1172 text = { "List All Permissions that are granted to :role" }
1175 public Result<PERMS> getPermsByRole(AuthzTrans trans,String role) {
1176 final Validator v = new ServiceValidator();
1177 if (v.nullOrBlank("Role", role).err()) {
1178 return Result.err(Status.ERR_BadData,v.errs());
1181 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques,role);
1183 return Result.err(rrdd);
1186 Result<NsDAO.Data> r = ques.mayUser(trans, trans.user(), rrdd.value, Access.read);
1188 return Result.err(r);
1191 PERMS perms = mapper.newInstance(API.PERMS);
1193 Result<List<PermDAO.Data>> rlpd = ques.getPermsByRole(trans, role, trans.requested(force));
1194 if (rlpd.isOKhasData()) {
1195 // Note: Mapper will restrict what can be viewed
1196 return mapper.perms(trans, rlpd.value, perms, true);
1198 return Result.ok(perms);
1203 path = "/authz/perms/ns/:ns",
1204 params = {"ns|string|true"},
1206 errorCodes = { 404,406 },
1207 text = { "List All Permissions that are in Namespace :ns" }
1210 public Result<PERMS> getPermsByNS(AuthzTrans trans,String ns) {
1211 final Validator v = new ServiceValidator();
1212 if (v.nullOrBlank("NS", ns).err()) {
1213 return Result.err(Status.ERR_BadData,v.errs());
1216 Result<NsDAO.Data> rnd = ques.deriveNs(trans, ns);
1218 return Result.err(rnd);
1221 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
1223 return Result.err(rnd);
1226 Result<List<PermDAO.Data>> rlpd = ques.permDAO().readNS(trans, ns);
1228 return Result.err(rlpd);
1231 PERMS perms = mapper.newInstance(API.PERMS);
1232 if (!rlpd.isEmpty()) {
1233 // Note: Mapper will restrict what can be viewed
1234 return mapper.perms(trans, rlpd.value,perms, true);
1236 return Result.ok(perms);
1241 path = "/authz/perm/:type/:instance/:action",
1242 params = {"type|string|true",
1243 "instance|string|true",
1244 "action|string|true"},
1246 errorCodes = { 404,406, 409 },
1247 text = { "Rename the Permission referenced by :type :instance :action, and "
1248 + "rename (copy/delete) to the Permission described in PermRequest" }
1251 public Result<Void> renamePerm(final AuthzTrans trans,REQUEST rreq, String origType, String origInstance, String origAction) {
1252 final Result<PermDAO.Data> newPd = mapper.perm(trans, rreq);
1253 final ServiceValidator v = new ServiceValidator();
1254 if (v.perm(newPd).err()) {
1255 return Result.err(Status.ERR_BadData,v.errs());
1258 if (ques.mayUser(trans, trans.user(), newPd.value,Access.write).notOK()) {
1259 return Result.err(Status.ERR_Denied, "You do not have approval to change Permission [%s.%s|%s|%s]",
1260 newPd.value.ns,newPd.value.type,newPd.value.instance,newPd.value.action);
1263 Result<NsSplit> nss = ques.deriveNsSplit(trans, origType);
1264 Result<List<PermDAO.Data>> origRlpd = ques.permDAO().read(trans, nss.value.ns, nss.value.name, origInstance, origAction);
1266 if (origRlpd.notOKorIsEmpty()) {
1267 return Result.err(Status.ERR_PermissionNotFound,
1268 "Permission [%s|%s|%s] does not exist",
1269 origType,origInstance,origAction);
1272 PermDAO.Data origPd = origRlpd.value.get(0);
1274 if (!origPd.ns.equals(newPd.value.ns)) {
1275 return Result.err(Status.ERR_Denied, "Cannot change namespace with rename command. " +
1276 "<new type> must start with [" + origPd.ns + "]");
1279 if ( origPd.type.equals(newPd.value.type) &&
1280 origPd.action.equals(newPd.value.action) &&
1281 origPd.instance.equals(newPd.value.instance) ) {
1282 return Result.err(Status.ERR_ConflictAlreadyExists, "New Permission must be different than original permission");
1285 Set<String> origRoles = origPd.roles(false);
1286 if (!origRoles.isEmpty()) {
1287 Set<String> roles = newPd.value.roles(true);
1288 for (String role : origPd.roles) {
1293 newPd.value.description = origPd.description;
1295 Result<Void> rv = null;
1297 rv = func.createPerm(trans, newPd.value, false);
1299 rv = func.deletePerm(trans, origPd, true, false);
1306 path = "/authz/perm",
1309 errorCodes = { 404,406 },
1310 text = { "Add Description Data to Perm" }
1313 public Result<Void> updatePermDescription(AuthzTrans trans, REQUEST from) {
1314 final Result<PermDAO.Data> pd = mapper.perm(trans, from);
1315 final ServiceValidator v = new ServiceValidator();
1316 if (v.perm(pd).err()) {
1317 return Result.err(Status.ERR_BadData,v.errs());
1319 if (v.nullOrBlank("description", pd.value.description).err()) {
1320 return Result.err(Status.ERR_BadData,v.errs());
1322 final PermDAO.Data perm = pd.value;
1323 if (ques.permDAO().read(trans, perm.ns, perm.type, perm.instance,perm.action).notOKorIsEmpty()) {
1324 return Result.err(Status.ERR_NotFound, "Permission [%s.%s|%s|%s] does not exist",
1325 perm.ns,perm.type,perm.instance,perm.action);
1328 if (ques.mayUser(trans, trans.user(), perm, Access.write).notOK()) {
1329 return Result.err(Status.ERR_Denied, "You do not have approval to change Permission [%s.%s|%s|%s]",
1330 perm.ns,perm.type,perm.instance,perm.action);
1333 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, pd.value.ns);
1334 if (nsr.notOKorIsEmpty()) {
1335 return Result.err(nsr);
1338 Result<Void> rdr = ques.permDAO().addDescription(trans, perm.ns, perm.type, perm.instance,
1339 perm.action, perm.description);
1343 return Result.err(rdr);
1350 path = "/authz/role/perm",
1353 errorCodes = {403,404,406,409},
1354 text = { "Set a permission's roles to roles given" }
1358 public Result<Void> resetPermRoles(final AuthzTrans trans, REQUEST rreq) {
1359 final Result<PermDAO.Data> updt = mapper.permFromRPRequest(trans, rreq);
1360 if (updt.notOKorIsEmpty()) {
1361 return Result.err(updt);
1364 final ServiceValidator v = new ServiceValidator();
1365 if (v.perm(updt).err()) {
1366 return Result.err(Status.ERR_BadData,v.errs());
1369 Result<NsDAO.Data> nsd = ques.mayUser(trans, trans.user(), updt.value, Access.write);
1371 return Result.err(nsd);
1374 // Read full set to get CURRENT values
1375 Result<List<PermDAO.Data>> rcurr = ques.permDAO().read(trans,
1378 updt.value.instance,
1381 if (rcurr.notOKorIsEmpty()) {
1382 return Result.err(Status.ERR_PermissionNotFound,
1383 "Permission [%s.%s|%s|%s] does not exist",
1384 updt.value.ns,updt.value.type,updt.value.instance,updt.value.action);
1387 // Create a set of Update Roles, which are in Internal Format
1388 Set<String> updtRoles = new HashSet<>();
1389 Result<NsSplit> nss;
1390 for (String role : updt.value.roles(false)) {
1391 nss = ques.deriveNsSplit(trans, role);
1393 updtRoles.add(nss.value.ns + '|' + nss.value.name);
1395 trans.error().log(nss.errorString());
1399 Result<Void> rv = null;
1401 for (PermDAO.Data curr : rcurr.value) {
1402 Set<String> currRoles = curr.roles(false);
1403 // must add roles to this perm, and add this perm to each role
1404 // in the update, but not in the current
1405 for (String role : updtRoles) {
1406 if (!currRoles.contains(role)) {
1407 Result<RoleDAO.Data> key = RoleDAO.Data.decode(trans, ques, role);
1408 if (key.isOKhasData()) {
1409 Result<List<RoleDAO.Data>> rrd = ques.roleDAO().read(trans, key.value);
1410 if (rrd.isOKhasData()) {
1411 for (RoleDAO.Data r : rrd.value) {
1412 rv = func.addPermToRole(trans, r, curr, false);
1413 if (rv.notOK() && rv.status!=Result.ERR_ConflictAlreadyExists) {
1414 return Result.err(rv);
1418 return Result.err(rrd);
1423 // similarly, must delete roles from this perm, and delete this perm from each role
1424 // in the update, but not in the current
1425 for (String role : currRoles) {
1426 if (!updtRoles.contains(role)) {
1427 Result<RoleDAO.Data> key = RoleDAO.Data.decode(trans, ques, role);
1428 if (key.isOKhasData()) {
1429 Result<List<RoleDAO.Data>> rdd = ques.roleDAO().read(trans, key.value);
1430 if (rdd.isOKhasData()) {
1431 for (RoleDAO.Data r : rdd.value) {
1432 rv = func.delPermFromRole(trans, r, curr, true);
1433 if (rv.notOK() && rv.status!=Status.ERR_PermissionNotFound) {
1434 return Result.err(rv);
1442 return rv==null?Result.ok():rv;
1447 path = "/authz/perm",
1450 errorCodes = { 404,406 },
1451 text = { "Delete the Permission referenced by PermKey.",
1452 "You cannot normally delete a permission which is still granted to roles,",
1453 "however the \"force\" property allows you to do just that. To do this: Add",
1454 "'force=true' as a query parameter.",
1455 "<p>WARNING: Using force will ungrant this permission from all roles. Use with care.</p>" }
1458 public Result<Void> deletePerm(final AuthzTrans trans, REQUEST from) {
1459 Result<PermDAO.Data> pd = mapper.perm(trans, from);
1461 return Result.err(pd);
1463 final ServiceValidator v = new ServiceValidator();
1464 if (v.nullOrBlank(pd.value).err()) {
1465 return Result.err(Status.ERR_BadData,v.errs());
1467 final PermDAO.Data perm = pd.value;
1468 if (ques.permDAO().read(trans, perm).notOKorIsEmpty()) {
1469 return Result.err(Status.ERR_PermissionNotFound, "Permission [%s.%s|%s|%s] does not exist",
1470 perm.ns,perm.type,perm.instance,perm.action );
1473 Result<FutureDAO.Data> fd = mapper.future(trans,PermDAO.TABLE,from,perm,false,
1476 public String get() {
1477 return "Delete Permission [" + perm.fullPerm() + ']';
1481 private Result<NsDAO.Data> nsd;
1483 public Result<?> mayChange() {
1485 nsd = ques.mayUser(trans, trans.user(), perm, Access.write);
1493 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, perm.ns);
1494 if (nsr.notOKorIsEmpty()) {
1495 return Result.err(nsr);
1498 Result<String> rfc = func.createFuture(trans, fd.value,
1499 perm.encode(), trans.user(),nsr.value.get(0),FUTURE_OP.D);
1501 return Result.err(Status.ACC_Future, "Perm Deletion [%s] is saved for future processing",perm.encode());
1503 return Result.err(rfc);
1505 case Status.ACC_Now:
1506 return func.deletePerm(trans,perm,trans.requested(force), false);
1508 return Result.err(fd);
1514 path = "/authz/perm/:name/:type/:action",
1515 params = {"type|string|true",
1516 "instance|string|true",
1517 "action|string|true"},
1519 errorCodes = { 404,406 },
1520 text = { "Delete the Permission referenced by :type :instance :action",
1521 "You cannot normally delete a permission which is still granted to roles,",
1522 "however the \"force\" property allows you to do just that. To do this: Add",
1523 "'force=true' as a query parameter",
1524 "<p>WARNING: Using force will ungrant this permission from all roles. Use with care.</p>"}
1527 public Result<Void> deletePerm(AuthzTrans trans, String type, String instance, String action) {
1528 final Validator v = new ServiceValidator();
1529 if (v.nullOrBlank("Type",type)
1530 .nullOrBlank("Instance",instance)
1531 .nullOrBlank("Action",action)
1533 return Result.err(Status.ERR_BadData,v.errs());
1536 Result<PermDAO.Data> pd = ques.permFrom(trans, type, instance, action);
1538 return func.deletePerm(trans, pd.value, trans.requested(force), false);
1540 return Result.err(pd);
1544 /***********************************
1546 ***********************************/
1549 path = "/authz/role",
1552 errorCodes = {403,404,406,409},
1555 "Roles are part of Namespaces",
1557 "<ul><li>org.onap.aaf - The team that created and maintains AAF</li>",
1558 "Roles do not include implied permissions for an App. Instead, they contain explicit Granted Permissions by any Namespace in AAF (See Permissions)",
1559 "Restrictions on Role Names:",
1560 "<ul><li>Must start with valid Namespace name, terminated by . (dot/period)</li>",
1561 "<li>Allowed Characters are a-zA-Z0-9._-</li>",
1562 "<li>role names are Case Sensitive</li></ul>",
1563 "The right questions to ask for defining and populating a Role in AAF, therefore, are:",
1564 "<ul><li>'What Job Function does this represent?'</li>",
1565 "<li>'Does this person perform this Job Function?'</li></ul>" }
1569 public Result<Void> createRole(final AuthzTrans trans, REQUEST from) {
1570 final Result<RoleDAO.Data> rd = mapper.role(trans, from);
1571 // Does Perm Type exist as a Namespace?
1572 if(rd.value.name.isEmpty() || ques.nsDAO().read(trans, rd.value.fullName()).isOKhasData()) {
1573 return Result.err(Status.ERR_ConflictAlreadyExists,
1574 "Role exists as a Namespace");
1576 final ServiceValidator v = new ServiceValidator();
1577 if (v.role(rd).err()) {
1578 return Result.err(Status.ERR_BadData,v.errs());
1580 final RoleDAO.Data role = rd.value;
1581 if (ques.roleDAO().read(trans, role.ns, role.name).isOKhasData()) {
1582 return Result.err(Status.ERR_ConflictAlreadyExists, "Role [" + role.fullName() + "] already exists");
1585 Result<FutureDAO.Data> fd = mapper.future(trans,RoleDAO.TABLE,from,role,false,
1588 public String get() {
1589 return "Create Role [" +
1590 rd.value.fullName() +
1595 private Result<NsDAO.Data> nsd;
1597 public Result<?> mayChange() {
1599 nsd = ques.mayUser(trans, trans.user(), role, Access.write);
1605 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, rd.value.ns);
1606 if (nsr.notOKorIsEmpty()) {
1607 return Result.err(nsr);
1612 Result<String> rfc = func.createFuture(trans, fd.value,
1613 role.encode(), trans.user(),nsr.value.get(0),FUTURE_OP.C);
1615 return Result.err(Status.ACC_Future, "Role [%s.%s] is saved for future processing",
1619 return Result.err(rfc);
1621 case Status.ACC_Now:
1622 Result<RoleDAO.Data> rdr = ques.roleDAO().create(trans, role);
1626 return Result.err(rdr);
1629 return Result.err(fd);
1634 * @see org.onap.aaf.auth.service.AuthzService#getRolesByName(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1638 path = "/authz/roles/:role",
1639 params = {"role|string|true"},
1641 errorCodes = {404,406},
1642 text = { "List Roles that match :role",
1643 "Note: You must have permission to see any given role"
1647 public Result<ROLES> getRolesByName(AuthzTrans trans, String role) {
1648 final Validator v = new ServiceValidator();
1649 if (v.nullOrBlank("Role", role).err()) {
1650 return Result.err(Status.ERR_BadData,v.errs());
1653 // Determine if User can ask this question
1654 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
1655 if (rrdd.isOKhasData()) {
1656 Result<NsDAO.Data> r;
1657 if ((r = ques.mayUser(trans, trans.user(), rrdd.value, Access.read)).notOK()) {
1658 return Result.err(r);
1661 return Result.err(rrdd);
1665 int query = role.indexOf('?');
1666 Result<List<RoleDAO.Data>> rlrd = ques.getRolesByName(trans, query<0?role:role.substring(0, query));
1668 // Note: Mapper will restrict what can be viewed
1669 ROLES roles = mapper.newInstance(API.ROLES);
1670 return mapper.roles(trans, rlrd.value, roles, true);
1672 return Result.err(rlrd);
1677 * @see org.onap.aaf.auth.service.AuthzService#getRolesByUser(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1681 path = "/authz/roles/user/:name",
1682 params = {"name|string|true"},
1684 errorCodes = {404,406},
1685 text = { "List all Roles that match user :name",
1686 "'user' must be expressed as full identity (ex: id@full.domain.com)",
1687 "Note: You must have permission to see any given role"
1692 public Result<ROLES> getRolesByUser(AuthzTrans trans, String user) {
1693 final Validator v = new ServiceValidator();
1694 if (v.nullOrBlank("User", user).err()) {
1695 return Result.err(Status.ERR_BadData,v.errs());
1698 ROLES roles = mapper.newInstance(API.ROLES);
1699 // Get list of roles per user, then add to Roles as we go
1700 Result<List<RoleDAO.Data>> rlrd;
1701 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByUser(trans, user);
1702 if (rlurd.isOKhasData()) {
1703 for (UserRoleDAO.Data urd : rlurd.value ) {
1704 rlrd = ques.roleDAO().read(trans, urd.ns,urd.rname);
1705 // Note: Mapper will restrict what can be viewed
1706 // if user is the same as that which is looked up, no filtering is required
1707 if (rlrd.isOKhasData()) {
1708 mapper.roles(trans, rlrd.value,roles, !user.equals(trans.user()));
1712 return Result.ok(roles);
1717 * @see org.onap.aaf.auth.service.AuthzService#getRolesByNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1721 path = "/authz/roles/ns/:ns",
1722 params = {"ns|string|true"},
1724 errorCodes = {404,406},
1725 text = { "List all Roles for the Namespace :ns",
1726 "Note: You must have permission to see any given role"
1731 public Result<ROLES> getRolesByNS(AuthzTrans trans, String ns) {
1732 final Validator v = new ServiceValidator();
1733 if (v.nullOrBlank("NS", ns).err()) {
1734 return Result.err(Status.ERR_BadData,v.errs());
1737 // check if user is allowed to view NS
1738 Result<NsDAO.Data> rnsd = ques.deriveNs(trans, ns);
1740 return Result.err(rnsd);
1742 rnsd = ques.mayUser(trans, trans.user(), rnsd.value, Access.read);
1744 return Result.err(rnsd);
1747 TimeTaken tt = trans.start("MAP Roles by NS to Roles", Env.SUB);
1749 ROLES roles = mapper.newInstance(API.ROLES);
1750 // Get list of roles per user, then add to Roles as we go
1751 Result<List<RoleDAO.Data>> rlrd = ques.roleDAO().readNS(trans, ns);
1753 if (!rlrd.isEmpty()) {
1754 // Note: Mapper doesn't need to restrict what can be viewed, because we did it already.
1755 mapper.roles(trans,rlrd.value,roles,false);
1757 return Result.ok(roles);
1759 return Result.err(rlrd);
1768 * @see org.onap.aaf.auth.service.AuthzService#getRolesByNS(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
1772 path = "/authz/roles/name/:name",
1773 params = {"name|string|true"},
1775 errorCodes = {404,406},
1776 text = { "List all Roles for only the Name of Role (without Namespace)",
1777 "Note: You must have permission to see any given role"
1781 public Result<ROLES> getRolesByNameOnly(AuthzTrans trans, String name) {
1782 final Validator v = new ServiceValidator();
1783 if (v.nullOrBlank("Name", name).err()) {
1784 return Result.err(Status.ERR_BadData,v.errs());
1787 // User Mapper to make sure user is allowed to view NS
1789 TimeTaken tt = trans.start("MAP Roles by Name to Roles", Env.SUB);
1791 ROLES roles = mapper.newInstance(API.ROLES);
1792 // Get list of roles per user, then add to Roles as we go
1793 Result<List<RoleDAO.Data>> rlrd = ques.roleDAO().readName(trans, name);
1795 if (!rlrd.isEmpty()) {
1796 // Note: Mapper will restrict what can be viewed
1797 mapper.roles(trans,rlrd.value,roles,true);
1799 return Result.ok(roles);
1801 return Result.err(rlrd);
1810 path = "/authz/roles/perm/:type/:instance/:action",
1811 params = {"type|string|true",
1812 "instance|string|true",
1813 "action|string|true"},
1815 errorCodes = {404,406},
1816 text = { "Find all Roles containing the given Permission." +
1817 "Permission consists of:",
1818 "<ul><li>type - a Namespace qualified identifier specifying what kind of resource "
1819 + "is being protected</li>",
1820 "<li>instance - a key, possibly multi-dimensional, that identifies a specific "
1821 + " instance of the type</li>",
1822 "<li>action - what kind of action is allowed</li></ul>",
1823 "Notes: instance and action can be an *",
1824 " You must have permission to see any given role"
1829 public Result<ROLES> getRolesByPerm(AuthzTrans trans, String type, String instance, String action) {
1830 final Validator v = new ServiceValidator();
1831 if (v.permType(type)
1832 .permInstance(instance)
1835 return Result.err(Status.ERR_BadData,v.errs());
1838 TimeTaken tt = trans.start("Map Perm Roles Roles", Env.SUB);
1840 ROLES roles = mapper.newInstance(API.ROLES);
1841 // Get list of roles per user, then add to Roles as we go
1842 Result<NsSplit> nsSplit = ques.deriveNsSplit(trans, type);
1843 if (nsSplit.isOK()) {
1844 PermDAO.Data pdd = new PermDAO.Data(nsSplit.value, instance, action);
1846 if ((res=ques.mayUser(trans, trans.user(), pdd, Question.Access.read)).notOK()) {
1847 return Result.err(res);
1850 Result<List<PermDAO.Data>> pdlr = ques.permDAO().read(trans, pdd);
1851 if (pdlr.isOK())for (PermDAO.Data pd : pdlr.value) {
1852 Result<List<RoleDAO.Data>> rlrd;
1853 for (String r : pd.roles) {
1854 Result<String[]> rs = RoleDAO.Data.decodeToArray(trans, ques, r);
1856 rlrd = ques.roleDAO().read(trans, rs.value[0],rs.value[1]);
1857 // Note: Mapper will restrict what can be viewed
1858 if (rlrd.isOKhasData()) {
1859 mapper.roles(trans,rlrd.value,roles,true);
1865 return Result.ok(roles);
1873 path = "/authz/role",
1876 errorCodes = {404,406},
1877 text = { "Add Description Data to a Role" }
1881 public Result<Void> updateRoleDescription(AuthzTrans trans, REQUEST from) {
1882 final Result<RoleDAO.Data> rd = mapper.role(trans, from);
1883 final ServiceValidator v = new ServiceValidator();
1884 if (v.role(rd).err()) {
1885 return Result.err(Status.ERR_BadData,v.errs());
1887 if (v.nullOrBlank("description", rd.value.description).err()) {
1888 return Result.err(Status.ERR_BadData,v.errs());
1891 final RoleDAO.Data role = rd.value;
1892 if (ques.roleDAO().read(trans, role.ns, role.name).notOKorIsEmpty()) {
1893 return Result.err(Status.ERR_NotFound, "Role [" + role.fullName() + "] does not exist");
1896 if (ques.mayUser(trans, trans.user(), role, Access.write).notOK()) {
1897 return Result.err(Status.ERR_Denied, "You do not have approval to change " + role.fullName());
1900 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, rd.value.ns);
1901 if (nsr.notOKorIsEmpty()) {
1902 return Result.err(nsr);
1905 Result<Void> rdr = ques.roleDAO().addDescription(trans, role.ns, role.name, role.description);
1909 return Result.err(rdr);
1916 path = "/authz/role/perm",
1919 errorCodes = {403,404,406,409},
1920 text = { "Grant a Permission to a Role",
1921 "Permission consists of:",
1922 "<ul><li>type - a Namespace qualified identifier specifying what kind of resource "
1923 + "is being protected</li>",
1924 "<li>instance - a key, possibly multi-dimensional, that identifies a specific "
1925 + " instance of the type</li>",
1926 "<li>action - what kind of action is allowed</li></ul>",
1927 "Note: instance and action can be an *",
1928 "Note: Using the \"force\" property will create the Permission, if it doesn't exist AND the requesting " +
1929 " ID is allowed to create. It will then grant",
1930 " the permission to the role in one step. To do this: add 'force=true' as a query parameter."
1935 public Result<Void> addPermToRole(final AuthzTrans trans, REQUEST rreq) {
1936 // Translate Request into Perm and Role Objects
1937 final Result<PermDAO.Data> rpd = mapper.permFromRPRequest(trans, rreq);
1938 if (rpd.notOKorIsEmpty()) {
1939 return Result.err(rpd);
1941 final Result<RoleDAO.Data> rrd = mapper.roleFromRPRequest(trans, rreq);
1942 if (rrd.notOKorIsEmpty()) {
1943 return Result.err(rrd);
1946 // Validate Role and Perm values
1947 final ServiceValidator v = new ServiceValidator();
1948 if (v.perm(rpd.value)
1951 return Result.err(Status.ERR_BadData,v.errs());
1954 Result<List<RoleDAO.Data>> rlrd = ques.roleDAO().read(trans, rrd.value.ns, rrd.value.name);
1955 if (rlrd.notOKorIsEmpty()) {
1956 return Result.err(Status.ERR_RoleNotFound, "Role [%s] does not exist", rrd.value.fullName());
1959 // Check Status of Data in DB (does it exist)
1960 Result<List<PermDAO.Data>> rlpd = ques.permDAO().read(trans, rpd.value.ns,
1961 rpd.value.type, rpd.value.instance, rpd.value.action);
1962 PermDAO.Data createPerm = null; // if not null, create first
1963 if (rlpd.notOKorIsEmpty()) { // Permission doesn't exist
1964 if (trans.requested(force)) {
1965 // Remove roles from perm data object so we just create the perm here
1966 createPerm = rpd.value;
1967 createPerm.roles.clear();
1969 return Result.err(Status.ERR_PermissionNotFound,"Permission [%s.%s|%s|%s] does not exist",
1970 rpd.value.ns,rpd.value.type,rpd.value.instance,rpd.value.action);
1973 if (rlpd.value.get(0).roles(false).contains(rrd.value.encode())) {
1974 return Result.err(Status.ERR_ConflictAlreadyExists,
1975 "Permission [%s.%s|%s|%s] already granted to Role [%s.%s]",
1976 rpd.value.ns,rpd.value.type,rpd.value.instance,rpd.value.action,
1977 rrd.value.ns,rrd.value.name
1983 Result<FutureDAO.Data> fd = mapper.future(trans, PermDAO.TABLE, rreq, rpd.value,true, // Allow grants to create Approvals
1986 public String get() {
1987 return "Grant Permission [" + rpd.value.fullPerm() + ']' +
1988 " to Role [" + rrd.value.fullName() + "]";
1992 private Result<NsDAO.Data> nsd;
1994 public Result<?> mayChange() {
1996 nsd = ques.mayUser(trans, trans.user(), rpd.value, Access.write);
1998 trans.requested(REQD_TYPE.future,true);
2004 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, rpd.value.ns);
2005 if (nsr.notOKorIsEmpty()) {
2006 return Result.err(nsr);
2010 Result<String> rfc = func.createFuture(trans,fd.value,
2011 rpd.value.fullPerm(),
2016 return Result.err(Status.ACC_Future, "Perm [%s.%s|%s|%s] is saved for future processing",
2022 return Result.err(rfc);
2024 case Status.ACC_Now:
2025 Result<Void> rv = null;
2026 if (createPerm!=null) {// has been validated for creating
2027 rv = func.createPerm(trans, createPerm, false);
2029 if (rv==null || rv.isOK()) {
2030 rv = func.addPermToRole(trans, rrd.value, rpd.value, false);
2034 return Result.err(fd);
2040 * Delete Perms from Roles (UnGrant)
2042 * @param roleFullName
2047 path = "/authz/role/:role/perm",
2048 params = {"role|string|true"},
2050 errorCodes = {404,406},
2051 text = { "Ungrant a permission from Role :role" }
2055 public Result<Void> delPermFromRole(final AuthzTrans trans, REQUEST rreq) {
2056 final Result<PermDAO.Data> updt = mapper.permFromRPRequest(trans, rreq);
2057 if (updt.notOKorIsEmpty()) {
2058 return Result.err(updt);
2060 final Result<RoleDAO.Data> rrd = mapper.roleFromRPRequest(trans, rreq);
2061 if (rrd.notOKorIsEmpty()) {
2062 return Result.err(rrd);
2065 final ServiceValidator v = new ServiceValidator();
2066 if (v.nullOrBlank(updt.value)
2067 .nullOrBlank(rrd.value)
2069 return Result.err(Status.ERR_BadData,v.errs());
2072 return delPermFromRole(trans, updt.value,rrd.value, rreq);
2075 private Result<Void> delPermFromRole(final AuthzTrans trans, PermDAO.Data pdd, RoleDAO.Data rdd, REQUEST rreq) {
2076 Result<List<PermDAO.Data>> rlpd = ques.permDAO().read(trans, pdd.ns, pdd.type,
2077 pdd.instance, pdd.action);
2079 if (rlpd.notOKorIsEmpty()) {
2080 return Result.err(Status.ERR_PermissionNotFound,
2081 "Permission [%s.%s|%s|%s] does not exist",
2082 pdd.ns,pdd.type,pdd.instance,pdd.action);
2085 Result<FutureDAO.Data> fd = mapper.future(trans, PermDAO.TABLE, rreq, pdd,true, // allow ungrants requests
2088 public String get() {
2089 return "Ungrant Permission [" + pdd.fullPerm() + ']' +
2090 " from Role [" + rdd.fullName() + "]";
2094 private Result<NsDAO.Data> nsd;
2096 public Result<?> mayChange() {
2098 nsd = ques.mayUser(trans, trans.user(), pdd, Access.write);
2103 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, pdd.ns);
2104 if (nsr.notOKorIsEmpty()) {
2105 return Result.err(nsr);
2109 Result<String> rfc = func.createFuture(trans,fd.value,
2116 return Result.err(Status.ACC_Future, "Perm [%s.%s|%s|%s] is saved for future processing",
2122 return Result.err(rfc);
2124 case Status.ACC_Now:
2125 return func.delPermFromRole(trans, rdd, pdd, false);
2127 return Result.err(fd);
2134 path = "/authz/role/:role/perm/:type/:instance/:action",
2135 params = {"role|string|true",
2136 "perm type|string|true",
2137 "perm instance|string|true",
2138 "perm action|string|true"
2141 errorCodes = {404,406},
2142 text = { "Ungrant a single permission from Role :role with direct key" }
2146 public Result<Void> delPermFromRole(AuthzTrans trans, String role, String type, String instance, String action) {
2147 Result<Data> rpns = ques.deriveNs(trans, type);
2148 if (rpns.notOKorIsEmpty()) {
2149 return Result.err(rpns);
2152 final Validator v = new ServiceValidator();
2154 .permType(rpns.value.name,rpns.value.parent)
2155 .permInstance(instance)
2158 return Result.err(Status.ERR_BadData,v.errs());
2161 Result<Data> rrns = ques.deriveNs(trans, role);
2162 if (rrns.notOKorIsEmpty()) {
2163 return Result.err(rrns);
2166 final Result<List<RoleDAO.Data>> rrd = ques.roleDAO().read(trans, rrns.value.parent, rrns.value.name);
2167 if (rrd.notOKorIsEmpty()) {
2168 return Result.err(rrd);
2171 final Result<List<PermDAO.Data>> rpd = ques.permDAO().read(trans, rpns.value.parent, rpns.value.name, instance, action);
2172 if (rpd.notOKorIsEmpty()) {
2173 return Result.err(rpd);
2177 return delPermFromRole(trans,rpd.value.get(0), rrd.value.get(0), mapper.ungrantRequest(trans, role, type, instance, action));
2182 path = "/authz/role/:role",
2183 params = {"role|string|true"},
2185 errorCodes = {404,406},
2186 text = { "Delete the Role named :role"}
2190 public Result<Void> deleteRole(AuthzTrans trans, String role) {
2191 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans,ques,role);
2192 if (rrdd.isOKhasData()) {
2193 final ServiceValidator v = new ServiceValidator();
2194 if (v.nullOrBlank(rrdd.value).err()) {
2195 return Result.err(Status.ERR_BadData,v.errs());
2197 return func.deleteRole(trans, rrdd.value, false, false);
2199 return Result.err(rrdd);
2205 path = "/authz/role",
2208 errorCodes = { 404,406 },
2209 text = { "Delete the Role referenced by RoleKey",
2210 "You cannot normally delete a role which still has permissions granted or users assigned to it,",
2211 "however the \"force\" property allows you to do just that. To do this: Add 'force=true'",
2212 "as a query parameter.",
2213 "<p>WARNING: Using force will remove all users and permission from this role. Use with care.</p>"}
2217 public Result<Void> deleteRole(final AuthzTrans trans, REQUEST from) {
2218 final Result<RoleDAO.Data> rd = mapper.role(trans, from);
2219 final ServiceValidator v = new ServiceValidator();
2221 return Result.err(Status.ERR_BadData,"Request does not contain Role");
2223 if (v.nullOrBlank(rd.value).err()) {
2224 return Result.err(Status.ERR_BadData,v.errs());
2226 final RoleDAO.Data role = rd.value;
2227 if (ques.roleDAO().read(trans, role).notOKorIsEmpty() && !trans.requested(force)) {
2228 return Result.err(Status.ERR_RoleNotFound, "Role [" + role.fullName() + "] does not exist");
2231 Result<FutureDAO.Data> fd = mapper.future(trans,RoleDAO.TABLE,from,role,false,
2232 () -> "Delete Role [" + role.fullName() + ']'
2233 + " and all attached user roles",
2235 private Result<NsDAO.Data> nsd;
2237 public Result<?> mayChange() {
2239 nsd = ques.mayUser(trans, trans.user(), role, Access.write);
2247 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, rd.value.ns);
2248 if (nsr.notOKorIsEmpty()) {
2249 return Result.err(nsr);
2252 Result<String> rfc = func.createFuture(trans, fd.value,
2253 role.encode(), trans.user(),nsr.value.get(0),FUTURE_OP.D);
2255 return Result.err(Status.ACC_Future, "Role Deletion [%s.%s] is saved for future processing",
2259 return Result.err(rfc);
2261 case Status.ACC_Now:
2262 return func.deleteRole(trans,role,trans.requested(force), true /*preapproved*/);
2264 return Result.err(fd);
2269 /***********************************
2271 ***********************************/
2272 private class MayCreateCred implements MayChange {
2273 private Result<NsDAO.Data> nsd;
2274 private AuthzTrans trans;
2275 private CredDAO.Data cred;
2276 private Executor exec;
2278 public MayCreateCred(AuthzTrans trans, CredDAO.Data cred, Executor exec) {
2285 public Result<?> mayChange() {
2287 nsd = ques.validNSOfDomain(trans, cred.id);
2289 // is Ns of CredID valid?
2293 if (trans.org().validate(trans,Policy.CREATE_MECHID, exec, cred.id)==null) {
2296 Result<?> rmc = ques.mayUser(trans, trans.user(), nsd.value, Access.write);
2297 if (rmc.isOKhasData()) {
2301 } catch (Exception e) {
2302 trans.warn().log(e);
2305 trans.warn().log(nsd.errorString());
2307 return Result.err(Status.ERR_Denied,"%s is not allowed to create %s in %s",trans.user(),cred.id,cred.ns);
2311 private class MayChangeCred implements MayChange {
2312 private static final String EXTEND = "extend";
2313 private static final String RESET = "reset";
2314 private static final String DELETE = "delete";
2315 private Result<NsDAO.Data> nsd;
2316 private AuthzTrans trans;
2317 private CredDAO.Data cred;
2318 private String action;
2319 public MayChangeCred(AuthzTrans trans, CredDAO.Data cred, String action) {
2322 this.action = action;
2326 public Result<?> mayChange() {
2327 // User can change himself (but not create)
2329 nsd = ques.validNSOfDomain(trans, cred.id);
2331 // Get the Namespace
2333 String ns = nsd.value.name;
2334 String user = trans.user();
2336 String temp[] = Split.split('.',ns);
2337 switch(temp.length) {
2339 company = Defaults.AAF_NS;
2345 company = temp[0] + '.' + temp[1];
2349 if(ques.isOwner(trans, user,ns) ||
2350 ques.isAdmin(trans, user,ns) ||
2351 ques.isGranted(trans, user, ROOT_NS,"password",company,DELETE)) {
2357 if (ques.isGranted(trans, trans.user(), ROOT_NS,"password",company,action)) {
2363 return Result.err(Status.ERR_Denied,"%s is not allowed to %s %s in %s",trans.user(),action,cred.id,cred.ns);
2367 private final long DAY_IN_MILLIS = 24*3600*1000L;
2371 path = "/authn/cred",
2374 errorCodes = {403,404,406,409},
2375 text = { "A credential consists of:",
2376 "<ul><li>id - the ID to create within AAF. The domain is in reverse",
2377 "order of Namespace (i.e. Users of Namespace com.att.myapp would be",
2378 "AB1234@myapp.att.com</li>",
2379 "<li>password - Company Policy Compliant Password</li></ul>",
2380 "Note: AAF does support multiple credentials with the same ID.",
2381 "Check with your organization if you have this implemented."
2385 public Result<Void> createUserCred(final AuthzTrans trans, REQUEST from) {
2386 final String cmdDescription = ("Create User Credential");
2387 TimeTaken tt = trans.start(cmdDescription, Env.SUB);
2390 Result<CredDAO.Data> rcred = mapper.cred(trans, from, true);
2391 if (rcred.isOKhasData()) {
2392 rcred = ques.userCredSetup(trans, rcred.value);
2394 final ServiceValidator v = new ServiceValidator();
2396 if (v.cred(trans, trans.org(),rcred,true).err()) { // Note: Creates have stricter Validations
2397 return Result.err(Status.ERR_BadData,v.errs());
2401 // 2016-4 Jonathan, New Behavior - If MechID is not registered with Org, deny creation
2402 Identity mechID = null;
2403 Organization org = trans.org();
2405 mechID = org.getIdentity(trans, rcred.value.id);
2406 } catch (Exception e1) {
2407 trans.error().log(e1,rcred.value.id,"cannot be validated at this time");
2409 if (mechID==null || !mechID.isFound()) {
2410 return Result.err(Status.ERR_Policy,"MechIDs must be registered with %s before provisioning in AAF",org.getName());
2413 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, rcred.value.ns);
2414 if (nsr.notOKorIsEmpty()) {
2415 return Result.err(Status.ERR_NsNotFound,"Cannot provision %s on non-existent Namespace %s",mechID.id(),rcred.value.ns);
2419 boolean firstID = false;
2422 CassExecutor exec = new CassExecutor(trans, func);
2423 Result<List<CredDAO.Data>> rlcd = ques.credDAO().readID(trans, rcred.value.id);
2424 if (rlcd.isOKhasData()) {
2425 if (!org.canHaveMultipleCreds(rcred.value.id)) {
2426 return Result.err(Status.ERR_ConflictAlreadyExists, "Credential exists");
2429 for (CredDAO.Data curr : rlcd.value) {
2430 // May not use the same password in the list
2431 // Note: ASPR specifies character differences, but we don't actually store the
2432 // password to validate char differences.
2434 // byte[] rawCred = rcred.value.type==CredDAO.RAW?null:; return Result.err(Status.ERR_ConflictAlreadyExists, "Credential with same Expiration Date exists");
2435 if(rcred.value.type==CredDAO.FQI ) {
2436 if(curr.type==CredDAO.FQI) {
2437 return Result.err(Status.ERR_ConflictAlreadyExists, "Credential with same Expiration Date exists");
2441 rb = ques.userCredCheck(trans, curr, rcred.value.cred!=null?rcred.value.cred.array():null);
2443 return Result.err(rb);
2444 } else if (rb.value){
2445 return Result.err(Status.ERR_Policy, "Credential content cannot be reused.");
2446 } else if(Chrono.dateOnlyStamp(curr.expires).equals(Chrono.dateOnlyStamp(rcred.value.expires))
2447 && curr.type==rcred.value.type
2449 // Allow if expiring differential is greater than 1 day (for TEMP)
2450 // Unless expiring in 1 day
2451 if(System.currentTimeMillis() - rcred.value.expires.getTime() > TimeUnit.DAYS.toMillis(1)) {
2452 return Result.err(Status.ERR_ConflictAlreadyExists, "Credential with same Expiration Date exists");
2459 // 2016-04-12 Jonathan If Caller is the Sponsor and is also an Owner of NS, allow without special Perm
2460 String theMechID = rcred.value.id;
2461 Boolean otherMechIDs = false;
2462 // find out if this is the only mechID. other MechIDs mean special handling (not automated)
2463 for (CredDAO.Data cd : ques.credDAO().readNS(trans,nsr.value.get(0).name).value) {
2464 if (!cd.id.equals(theMechID)) {
2465 otherMechIDs = true;
2470 // We can say "ID does not exist" here
2471 if ((reason=org.validate(trans, Policy.CREATE_MECHID, exec, theMechID,trans.user(),otherMechIDs.toString()))!=null) {
2472 return Result.err(Status.ERR_Denied, reason);
2475 } catch (Exception e) {
2476 return Result.err(e);
2480 mc = new MayCreateCred(trans, rcred.value, exec);
2482 final CredDAO.Data cdd = rcred.value;
2483 Result<FutureDAO.Data> fd = mapper.future(trans,CredDAO.TABLE,from, rcred.value,false, // may want to enable in future.
2486 public String get() {
2487 return cmdDescription + " [" +
2490 + cdd.expires + ']';
2497 Result<String> rfc = func.createFuture(trans, fd.value,
2498 rcred.value.id + '|' + rcred.value.type.toString() + '|' + rcred.value.expires,
2499 trans.user(), nsr.value.get(0), FUTURE_OP.C);
2501 return Result.err(Status.ACC_Future, "Credential Request [%s|%s|%s] is saved for future processing",
2503 Integer.toString(rcred.value.type),
2504 rcred.value.expires.toString());
2506 return Result.err(rfc);
2508 case Status.ACC_Now:
2511 // OK, it's a first ID, and not by NS Owner
2512 if(!ques.isOwner(trans,trans.user(),cdd.ns)) {
2513 // Admins are not allowed to set first Cred, but Org has already
2514 // said entity MAY create, typically by Permission
2515 // We can't know which reason they are allowed here, so we
2516 // have to assume that any with Special Permission would not be
2518 if(ques.isAdmin(trans, trans.user(), cdd.ns)) {
2519 return Result.err(Result.ERR_Denied,
2520 "Only Owners may create first passwords in their Namespace. Admins may modify after one exists" );
2522 // Allow IDs that AREN'T part of NS with Org Onboarding Permission (see Org object) to create Temp Passwords.
2523 rcred.value.expires = org.expiration(null, Expiration.TempPassword).getTime();
2527 } catch (Exception e) {
2528 trans.error().log(e, "While setting expiration to TempPassword");
2531 Result<?>udr = ques.credDAO().create(trans, rcred.value);
2535 return Result.err(udr);
2537 return Result.err(fd);
2541 return Result.err(rcred);
2550 path = "/authn/creds/ns/:ns",
2551 params = {"ns|string|true"},
2553 errorCodes = {403,404,406},
2554 text = { "Return all IDs in Namespace :ns"
2558 public Result<USERS> getCredsByNS(AuthzTrans trans, String ns) {
2559 final Validator v = new ServiceValidator();
2560 if (v.ns(ns).err()) {
2561 return Result.err(Status.ERR_BadData,v.errs());
2564 // check if user is allowed to view NS
2565 Result<NsDAO.Data> rnd = ques.deriveNs(trans,ns);
2567 return Result.err(rnd);
2569 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
2571 return Result.err(rnd);
2574 TimeTaken tt = trans.start("MAP Creds by NS to Creds", Env.SUB);
2576 USERS users = mapper.newInstance(API.USERS);
2577 Result<List<CredDAO.Data>> rlcd = ques.credDAO().readNS(trans, ns);
2580 if (!rlcd.isEmpty()) {
2581 return mapper.cred(rlcd.value, users);
2583 return Result.ok(users);
2585 return Result.err(rlcd);
2595 path = "/authn/creds/id/:ns",
2596 params = {"id|string|true"},
2598 errorCodes = {403,404,406},
2599 text = { "Return all IDs in for ID"
2600 ,"(because IDs are multiple, due to multiple Expiration Dates)"
2604 public Result<USERS> getCredsByID(AuthzTrans trans, String id) {
2605 final Validator v = new ServiceValidator();
2606 if (v.nullOrBlank("ID",id).err()) {
2607 return Result.err(Status.ERR_BadData,v.errs());
2610 String ns = Question.domain2ns(id);
2611 // check if user is allowed to view NS
2612 Result<NsDAO.Data> rnd = ques.deriveNs(trans,ns);
2614 return Result.err(rnd);
2616 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
2618 return Result.err(rnd);
2621 TimeTaken tt = trans.start("MAP Creds by ID to Creds", Env.SUB);
2623 USERS users = mapper.newInstance(API.USERS);
2624 Result<List<CredDAO.Data>> rlcd = ques.credDAO().readID(trans, id);
2627 if (!rlcd.isEmpty()) {
2628 return mapper.cred(rlcd.value, users);
2630 return Result.ok(users);
2632 return Result.err(rlcd);
2642 path = "/authn/certs/id/:id",
2643 params = {"id|string|true"},
2645 errorCodes = {403,404,406},
2646 text = { "Return Cert Info for ID"
2650 public Result<CERTS> getCertInfoByID(AuthzTrans trans, HttpServletRequest req, String id) {
2651 TimeTaken tt = trans.start("Get Cert Info by ID", Env.SUB);
2653 CERTS certs = mapper.newInstance(API.CERTS);
2654 Result<List<CertDAO.Data>> rlcd = ques.certDAO().readID(trans, id);
2657 if (!rlcd.isEmpty()) {
2658 return mapper.cert(rlcd.value, certs);
2660 return Result.ok(certs);
2662 return Result.err(rlcd);
2672 path = "/authn/cred",
2675 errorCodes = {300,403,404,406},
2676 text = { "Reset a Credential Password. If multiple credentials exist for this",
2677 "ID, you will need to specify which entry you are resetting in the",
2678 "CredRequest object"
2682 public Result<Void> resetUserCred(final AuthzTrans trans, REQUEST from) {
2683 final String cmdDescription = "Update User Credential";
2684 TimeTaken tt = trans.start(cmdDescription, Env.SUB);
2686 Result<CredDAO.Data> rcred = mapper.cred(trans, from, true);
2687 if (rcred.isOKhasData()) {
2688 rcred = ques.userCredSetup(trans, rcred.value);
2690 final ServiceValidator v = new ServiceValidator();
2692 if (v.cred(trans, trans.org(),rcred,false).err()) {// Note: Creates have stricter Validations
2693 return Result.err(Status.ERR_BadData,v.errs());
2695 Result<List<CredDAO.Data>> rlcd = ques.credDAO().readID(trans, rcred.value.id);
2696 if (rlcd.notOKorIsEmpty()) {
2697 return Result.err(Status.ERR_UserNotFound, "Credential does not exist");
2700 MayChange mc = new MayChangeCred(trans, rcred.value,MayChangeCred.RESET);
2701 Result<?> rmc = mc.mayChange();
2703 return Result.err(rmc);
2706 List<CredDAO.Data> lcdd = filterList(rlcd.value,CredDAO.BASIC_AUTH, CredDAO.BASIC_AUTH_SHA256);
2708 Result<Integer> ri = selectEntryIfMultiple((CredRequest)from, lcdd, MayChangeCred.RESET);
2710 return Result.err(ri);
2712 int entry = ri.value;
2715 final CredDAO.Data cred = rcred.value;
2717 Result<FutureDAO.Data> fd = mapper.future(trans,CredDAO.TABLE,from, rcred.value,false,
2720 public String get() {
2721 return cmdDescription + " [" +
2724 + cred.expires + ']';
2729 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, rcred.value.ns);
2730 if (nsr.notOKorIsEmpty()) {
2731 return Result.err(nsr);
2736 Result<String> rfc = func.createFuture(trans, fd.value,
2737 rcred.value.id + '|' + rcred.value.type.toString() + '|' + rcred.value.expires,
2738 trans.user(), nsr.value.get(0), FUTURE_OP.U);
2740 return Result.err(Status.ACC_Future, "Credential Request [%s|%s|%s]",
2742 Integer.toString(rcred.value.type),
2743 rcred.value.expires.toString());
2745 return Result.err(rfc);
2747 case Status.ACC_Now:
2748 Result<?>udr = null;
2749 // If we are Resetting Password on behalf of someone else (am not the Admin)
2750 // use TempPassword Expiration time.
2752 if (ques.isAdmin(trans, trans.user(), nsr.value.get(0).name)) {
2753 exp = Expiration.Password;
2755 exp = Expiration.TempPassword;
2758 Organization org = trans.org();
2759 CredDAO.Data current = rlcd.value.get(entry);
2760 // If user resets password in same day, we will have a primary key conflict, so subtract 1 day
2761 if (current.expires.equals(rcred.value.expires)
2762 && rlcd.value.get(entry).type==rcred.value.type) {
2763 GregorianCalendar gc = org.expiration(null, exp,rcred.value.id);
2764 gc = Chrono.firstMomentOfDay(gc);
2765 gc.set(GregorianCalendar.HOUR_OF_DAY, org.startOfDay());
2766 rcred.value.expires = new Date(gc.getTimeInMillis() - DAY_IN_MILLIS);
2768 rcred.value.expires = org.expiration(null,exp).getTime();
2771 udr = ques.credDAO().create(trans, rcred.value);
2773 udr = ques.credDAO().delete(trans, rlcd.value.get(entry),false);
2779 return Result.err(udr);
2781 return Result.err(fd);
2784 return Result.err(rcred);
2793 path = "/authn/cred/:days",
2794 params = {"days|string|true"},
2796 errorCodes = {300,403,404,406},
2797 text = { "Extend a Credential Expiration Date. The intention of this API is",
2798 "to avoid an outage in PROD due to a Credential expiring before it",
2799 "can be configured correctly. Measures are being put in place ",
2800 "so that this is not abused."
2804 public Result<Void> extendUserCred(final AuthzTrans trans, REQUEST from, String days) {
2805 TimeTaken tt = trans.start("Extend User Credential", Env.SUB);
2807 Result<CredDAO.Data> cred = mapper.cred(trans, from, false);
2808 Organization org = trans.org();
2809 final ServiceValidator v = new ServiceValidator();
2810 if (v.notOK(cred).err() ||
2811 v.nullOrBlank(cred.value.id, "Invalid ID").err() ||
2812 v.user(org,cred.value.id).err()) {
2813 return Result.err(Status.ERR_BadData,v.errs());
2818 if ((reason=org.validate(trans, Policy.MAY_EXTEND_CRED_EXPIRES, new CassExecutor(trans,func)))!=null) {
2819 return Result.err(Status.ERR_Policy,reason);
2821 } catch (Exception e) {
2823 trans.error().log(e, msg="Could not contact Organization for User Validation");
2824 return Result.err(Status.ERR_Denied, msg);
2827 // Get the list of Cred Entries
2828 Result<List<CredDAO.Data>> rlcd = ques.credDAO().readID(trans, cred.value.id);
2829 if (rlcd.notOKorIsEmpty()) {
2830 return Result.err(Status.ERR_UserNotFound, "Credential does not exist");
2833 // Only Passwords can be extended
2834 List<CredDAO.Data> lcdd = filterList(rlcd.value,CredDAO.BASIC_AUTH, CredDAO.BASIC_AUTH_SHA256);
2836 //Need to do the "Pick Entry" mechanism
2838 Result<Integer> ri = selectEntryIfMultiple((CredRequest)from, lcdd, MayChangeCred.EXTEND);
2840 return Result.err(ri);
2843 CredDAO.Data found = lcdd.get(ri.value);
2844 CredDAO.Data cd = cred.value;
2845 // Copy over the cred
2847 cd.cred = found.cred;
2848 cd.other = found.other;
2849 cd.type = found.type;
2851 cd.notes = "Extended";
2853 cd.expires = org.expiration(null, Expiration.ExtendPassword,days).getTime();
2854 if(cd.expires.before(found.expires)) {
2855 return Result.err(Result.ERR_BadData,String.format("Credential's expiration date is more than %s days in the future",days));
2858 cred = ques.credDAO().create(trans, cd);
2862 return Result.err(cred);
2870 path = "/authn/cred",
2873 errorCodes = {300,403,404,406},
2874 text = { "Delete a Credential. If multiple credentials exist for this",
2875 "ID, you will need to specify which entry you are deleting in the",
2876 "CredRequest object."
2880 public Result<Void> deleteUserCred(AuthzTrans trans, REQUEST from) {
2881 final Result<CredDAO.Data> cred = mapper.cred(trans, from, false);
2882 final Validator v = new ServiceValidator();
2883 if (v.nullOrBlank("cred", cred.value.id).err()) {
2884 return Result.err(Status.ERR_BadData,v.errs());
2887 MayChange mc = new MayChangeCred(trans,cred.value,MayChangeCred.DELETE);
2888 Result<?> rmc = mc.mayChange();
2890 return Result.err(rmc);
2893 boolean doForce = trans.requested(force);
2894 Result<List<CredDAO.Data>> rlcd = ques.credDAO().readID(trans, cred.value.id);
2895 if (rlcd.notOKorIsEmpty()) {
2896 // Empty Creds should not have user_roles.
2897 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByUser(trans, cred.value.id);
2898 if (rlurd.isOKhasData()) {
2899 for (UserRoleDAO.Data data : rlurd.value) {
2900 ques.userRoleDAO().delete(trans, data, false);
2903 return Result.err(Status.ERR_UserNotFound, "Credential does not exist");
2905 boolean isLastCred = rlcd.value.size()==1;
2908 CredRequest cr = (CredRequest)from;
2910 if(cr.getEntry()==null || "1".equals(cr.getEntry())) {
2913 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
2918 if(cred.value.type==CredDAO.FQI) {
2920 for(CredDAO.Data cdd : rlcd.value) {
2922 if(cdd.type == CredDAO.FQI) {
2929 if (rlcd.value.size() > 1) {
2930 String inputOption = cr.getEntry();
2931 if (inputOption == null) {
2932 List<CredDAO.Data> list = filterList(rlcd.value,CredDAO.BASIC_AUTH,CredDAO.BASIC_AUTH_SHA256,CredDAO.CERT_SHA256_RSA);
2933 String message = selectCredFromList(list, MayChangeCred.DELETE);
2934 Object[] variables = buildVariables(list);
2935 return Result.err(Status.ERR_ChoiceNeeded, message, variables);
2938 if (inputOption.length()>5) { // should be a date
2939 Date d = Chrono.xmlDatatypeFactory.newXMLGregorianCalendar(inputOption).toGregorianCalendar().getTime();
2940 for (CredDAO.Data cd : rlcd.value) {
2942 if (cd.type.equals(cr.getType()) && cd.expires.equals(d)) {
2948 entry = Integer.parseInt(inputOption) - 1;
2950 for (CredDAO.Data cd : rlcd.value) {
2951 if(cd.type!=CredDAO.BASIC_AUTH && cd.type!=CredDAO.BASIC_AUTH_SHA256 && cd.type!=CredDAO.CERT_SHA256_RSA) {
2959 } catch (NullPointerException e) {
2960 return Result.err(Status.ERR_BadData, "Invalid Date Format for Entry");
2961 } catch (NumberFormatException e) {
2962 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
2965 isLastCred = (entry==-1)?true:false;
2969 if (entry < -1 || entry >= rlcd.value.size()) {
2970 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
2976 Result<FutureDAO.Data> fd = mapper.future(trans,CredDAO.TABLE,from,cred.value,false,
2977 () -> "Delete Credential [" +
2982 Result<List<NsDAO.Data>> nsr = ques.nsDAO().read(trans, cred.value.ns);
2983 if (nsr.notOKorIsEmpty()) {
2984 return Result.err(nsr);
2989 Result<String> rfc = func.createFuture(trans, fd.value, cred.value.id,
2990 trans.user(), nsr.value.get(0), FUTURE_OP.D);
2993 return Result.err(Status.ACC_Future, "Credential Delete [%s] is saved for future processing",cred.value.id);
2995 return Result.err(rfc);
2997 case Status.ACC_Now:
2998 Result<?>udr = null;
2999 if (!trans.requested(force)) {
3000 if (entry<0 || entry >= rlcd.value.size()) {
3001 if(cred.value.type==CredDAO.FQI) {
3002 return Result.err(Status.ERR_BadData,"FQI does not exist");
3004 return Result.err(Status.ERR_BadData,"Invalid Choice [" + entry + "] chosen for Delete [%s] is saved for future processing",cred.value.id);
3007 udr = ques.credDAO().delete(trans, rlcd.value.get(entry),false);
3009 for (CredDAO.Data curr : rlcd.value) {
3010 udr = ques.credDAO().delete(trans, curr, false);
3012 return Result.err(udr);
3017 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByUser(trans, cred.value.id);
3019 for (UserRoleDAO.Data data : rlurd.value) {
3020 ques.userRoleDAO().delete(trans, data, false);
3025 return Result.err(Result.ERR_NotFound,"No User Data found");
3030 return Result.err(udr);
3032 return Result.err(fd);
3038 * Codify the way to get Either Choice Needed or actual Integer from Credit Request
3040 private Result<Integer> selectEntryIfMultiple(final CredRequest cr, List<CredDAO.Data> lcd, String action) {
3042 if (lcd.size() > 1) {
3043 String inputOption = cr.getEntry();
3044 if (inputOption == null) {
3045 String message = selectCredFromList(lcd, action);
3046 Object[] variables = buildVariables(lcd);
3047 return Result.err(Status.ERR_ChoiceNeeded, message, variables);
3049 if(MayChangeCred.EXTEND.equals(action)) {
3051 if(inputOption.length()>4) { //Tag is at least 12
3053 CredDAO.Data last = null;
3055 for(CredDAO.Data cdd : lcd) {
3056 if(inputOption.equals(cdd.tag)) {
3061 if(last.expires.before(cdd.expires)) {
3070 return Result.ok(lastIdx);
3072 return Result.err(Status.ERR_BadData, "User chose unknown Tag");
3075 entry = Integer.parseInt(inputOption) - 1;
3077 if (entry < 0 || entry >= lcd.size()) {
3078 return Result.err(Status.ERR_BadData, "User chose invalid credential selection");
3081 return Result.ok(entry);
3084 private List<CredDAO.Data> filterList(List<CredDAO.Data> orig, Integer ... types) {
3085 List<CredDAO.Data> rv = new ArrayList<>();
3086 for(CredDAO.Data cdd : orig) {
3088 for(int t : types) {
3095 Collections.sort(rv, (o1,o2) -> {
3096 if(o1.type==o2.type) {
3097 return o1.expires.compareTo(o2.expires);
3099 return o1.type.compareTo(o2.type);
3105 private String[] buildVariables(List<CredDAO.Data> value) {
3106 String [] vars = new String[value.size()];
3109 for (int i = 0; i < value.size(); i++) {
3111 vars[i] = cdd.id + TWO_SPACE + Define.getCredType(cdd.type) + TWO_SPACE + Chrono.niceUTCStamp(cdd.expires) + TWO_SPACE + cdd.tag;
3116 private String selectCredFromList(List<CredDAO.Data> value, String action) {
3117 StringBuilder errMessage = new StringBuilder();
3118 String userPrompt = MayChangeCred.DELETE.equals(action)?
3119 "Select which cred to delete (set force=true to delete all):":
3120 "Select which cred to " + action + ':';
3121 int numSpaces = value.get(0).id.length() - "Id".length();
3123 errMessage.append(userPrompt + '\n');
3124 errMessage.append(" ID");
3125 for (int i = 0; i < numSpaces; i++) {
3126 errMessage.append(' ');
3128 errMessage.append(" Type Expires Tag " + '\n');
3129 for (int i=0;i<value.size();++i) {
3130 errMessage.append(" %s\n");
3132 if(MayChangeCred.EXTEND.equals(action)) {
3133 errMessage.append("Run same command again with chosen entry or Tag as last parameter");
3135 errMessage.append("Run same command again with chosen entry as last parameter");
3137 return errMessage.toString();
3142 public Result<Date> doesCredentialMatch(AuthzTrans trans, REQUEST credReq) {
3143 TimeTaken tt = trans.start("Does Credential Match", Env.SUB);
3145 // Note: Mapper assigns RAW type
3146 Result<CredDAO.Data> data = mapper.cred(trans, credReq,false);
3147 if (data.notOKorIsEmpty()) {
3148 return Result.err(data);
3150 CredDAO.Data cred = data.value; // of the Mapped Cred
3151 if (cred.cred==null) {
3152 return Result.err(Result.ERR_BadData,"No Password");
3154 return ques.doesUserCredMatch(trans, cred.id, cred.cred.array());
3157 } catch (DAOException e) {
3158 trans.error().log(e,"Error looking up cred");
3159 return Result.err(Status.ERR_Denied,"Credential does not match");
3167 path = "/authn/validate",
3170 errorCodes = { 403 },
3171 text = { "Validate a Credential given a Credential Structure. This is a more comprehensive validation, can "
3172 + "do more than BasicAuth as Credential types exp" }
3175 public Result<Date> validateBasicAuth(AuthzTrans trans, String basicAuth) {
3176 //TODO how to make sure people don't use this in browsers? Do we care?
3177 TimeTaken tt = trans.start("Validate Basic Auth", Env.SUB);
3179 BasicPrincipal bp = new BasicPrincipal(basicAuth,trans.org().getRealm());
3180 Result<Date> rq = ques.doesUserCredMatch(trans, bp.getName(), bp.getCred());
3181 // Note: Only want to log problem, don't want to send back to end user
3185 trans.audit().log(rq.errorString());
3187 } catch (Exception e) {
3188 trans.warn().log(e);
3192 return Result.err(Status.ERR_Denied,"Bad Basic Auth");
3197 path = "/authn/basicAuth",
3200 errorCodes = { 403 },
3201 text = { "!!!! DEPRECATED without X509 Authentication STOP USING THIS API BY DECEMBER 2017, or use Certificates !!!!\n"
3202 + "Use /authn/validate instead\n"
3203 + "Note: Validate a Password using BasicAuth Base64 encoded Header. This HTTP/S call is intended as a fast"
3204 + " User/Password lookup for Security Frameworks, and responds 200 if it passes BasicAuth "
3205 + "security, and 403 if it does not." }
3207 private void basicAuth() {
3208 // This is a place holder for Documentation. The real BasicAuth API does not call Service.
3211 /***********************************
3213 ***********************************/
3216 path = "/authz/userRole",
3219 errorCodes = {403,404,406,409},
3220 text = { "Create a UserRole relationship (add User to Role)",
3221 "A UserRole is an object Representation of membership of a Role for limited time.",
3222 "If a shorter amount of time for Role ownership is required, use the 'End' field.",
3223 "** Note: Owners of Namespaces will be required to revalidate users in these roles ",
3224 "before Expirations expire. Namespace owners will be notified by email."
3228 public Result<Void> createUserRole(final AuthzTrans trans, REQUEST from) {
3229 TimeTaken tt = trans.start("Create UserRole", Env.SUB);
3231 Result<UserRoleDAO.Data> urr = mapper.userRole(trans, from);
3232 if (urr.notOKorIsEmpty()) {
3233 return Result.err(urr);
3235 final UserRoleDAO.Data userRole = urr.value;
3237 final ServiceValidator v = new ServiceValidator();
3238 if (v.user_role(trans.user(),userRole).err() ||
3239 v.user(trans.org(), userRole.user).err()) {
3240 return Result.err(Status.ERR_BadData,v.errs());
3245 // Check if user can change first
3246 Result<FutureDAO.Data> fd = mapper.future(trans,UserRoleDAO.TABLE,from,urr.value,true, // may request Approvals
3247 () -> "Add User [" + userRole.user + "] to Role [" +
3251 private Result<NsDAO.Data> nsd;
3253 public Result<?> mayChange() {
3254 if(urr.value.role.startsWith(urr.value.user)) {
3255 return Result.ok((NsDAO.Data)null);
3258 RoleDAO.Data r = RoleDAO.Data.decode(userRole);
3259 nsd = ques.mayUser(trans, trans.user(), r, Access.write);
3266 if(userRole.role.startsWith(userRole.user)) {
3267 userRole.ns=userRole.user;
3268 userRole.rname="user";
3271 Result<NsDAO.Data> nsr = ques.deriveNs(trans, userRole.role);
3273 return Result.err(nsr);
3280 Result<String> rfc = func.createFuture(trans, fd.value, userRole.user+'|'+userRole.ns + '.' + userRole.rname,
3281 userRole.user, ndd, FUTURE_OP.C);
3283 return Result.err(Status.ACC_Future, "UserRole [%s - %s.%s] is saved for future processing",
3288 return Result.err(rfc);
3290 case Status.ACC_Now:
3291 return func.addUserRole(trans, userRole);
3293 return Result.err(fd);
3301 * getUserRolesByRole
3305 path = "/authz/userRoles/role/:role",
3306 params = {"role|string|true"},
3308 errorCodes = {404,406},
3309 text = { "List all Users that are attached to Role specified in :role",
3313 public Result<USERROLES> getUserRolesByRole(AuthzTrans trans, String role) {
3314 final Validator v = new ServiceValidator();
3315 if (v.nullOrBlank("Role",role).err()) {
3316 return Result.err(Status.ERR_BadData,v.errs());
3319 Result<RoleDAO.Data> rrdd;
3320 rrdd = RoleDAO.Data.decode(trans,ques,role);
3322 return Result.err(rrdd);
3324 // May Requester see result?
3325 Result<NsDAO.Data> ns = ques.mayUser(trans,trans.user(), rrdd.value,Access.read);
3327 return Result.err(ns);
3330 // boolean filter = true;
3331 // if (ns.value.isAdmin(trans.user()) || ns.value.isResponsible(trans.user()))
3334 // Get list of roles per user, then add to Roles as we go
3335 HashSet<UserRoleDAO.Data> userSet = new HashSet<>();
3336 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByRole(trans, role);
3338 for (UserRoleDAO.Data data : rlurd.value) {
3343 @SuppressWarnings("unchecked")
3344 USERROLES users = (USERROLES) mapper.newInstance(API.USER_ROLES);
3345 // Checked for permission
3346 mapper.userRoles(trans, userSet, users);
3347 return Result.ok(users);
3350 * getUserRolesByRole
3354 path = "/authz/userRoles/user/:user",
3355 params = {"role|string|true"},
3357 errorCodes = {404,406},
3358 text = { "List all UserRoles for :user",
3362 public Result<USERROLES> getUserRolesByUser(AuthzTrans trans, String user) {
3363 final Validator v = new ServiceValidator();
3364 if (v.nullOrBlank("User",user).err()) {
3365 return Result.err(Status.ERR_BadData,v.errs());
3368 // Get list of roles per user, then add to Roles as we go
3369 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByUser(trans, user);
3370 if (rlurd.notOK()) {
3371 return Result.err(rlurd);
3376 * 2) is User's Supervisor
3377 * 3) Has special global access =read permission
3379 * If none of the 3, then filter results to NSs in which Calling User has Ns.access * read
3382 String callingUser = trans.getUserPrincipal().getName();
3383 NsDAO.Data ndd = new NsDAO.Data();
3385 if (user.equals(callingUser)) {
3388 Organization org = trans.org();
3390 Identity orgID = org.getIdentity(trans, user);
3391 Identity manager = orgID==null?null:orgID.responsibleTo();
3392 if (orgID!=null && (manager!=null && callingUser.equals(manager.fullID()))) {
3394 } else if (ques.isGranted(trans, callingUser, ROOT_NS, Question.ACCESS, "*", Access.read.name())) {
3399 } catch (OrganizationException e) {
3405 List<UserRoleDAO.Data> content;
3407 content = new ArrayList<>(rlurd.value.size()); // avoid multi-memory redos
3409 for (UserRoleDAO.Data data : rlurd.value) {
3411 Result<Data> mur = ques.mayUser(trans, callingUser, ndd, Access.read);
3418 content = rlurd.value;
3422 @SuppressWarnings("unchecked")
3423 USERROLES users = (USERROLES) mapper.newInstance(API.USER_ROLES);
3424 // Checked for permission
3425 mapper.userRoles(trans, content, users);
3426 return Result.ok(users);
3434 path = "/authz/userRole/extend/:user/:role",
3435 params = { "user|string|true",
3439 errorCodes = {403,404,406},
3440 text = { "Extend the Expiration of this User Role by the amount set by Organization",
3441 "Requestor must be allowed to modify the role"
3445 public Result<Void> extendUserRole(AuthzTrans trans, String user, String role) {
3446 Organization org = trans.org();
3447 final ServiceValidator v = new ServiceValidator();
3448 if (v.user(org, user)
3451 return Result.err(Status.ERR_BadData,v.errs());
3454 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans,ques,role);
3456 return Result.err(rrdd);
3459 Result<NsDAO.Data> rcr = ques.mayUser(trans, trans.user(), rrdd.value, Access.write);
3460 boolean mayNotChange;
3461 if ((mayNotChange = rcr.notOK()) && !trans.requested(future)) {
3462 return Result.err(rcr);
3465 Result<List<UserRoleDAO.Data>> rr = ques.userRoleDAO().read(trans, user,role);
3467 return Result.err(rr);
3469 for (UserRoleDAO.Data userRole : rr.value) {
3470 if (mayNotChange) { // Function exited earlier if !trans.futureRequested
3471 FutureDAO.Data fto = new FutureDAO.Data();
3472 fto.target=UserRoleDAO.TABLE;
3473 fto.memo = "Extend User ["+userRole.user+"] in Role ["+userRole.role+"]";
3474 GregorianCalendar now = new GregorianCalendar();
3475 fto.start = now.getTime();
3476 fto.expires = org.expiration(now, Expiration.Future).getTime();
3478 fto.construct = userRole.bytify();
3479 } catch (IOException e) {
3480 trans.error().log(e, "Error while bytifying UserRole for Future");
3481 return Result.err(e);
3484 Result<String> rfc = func.createFuture(trans, fto,
3485 userRole.user+'|'+userRole.role, userRole.user, rcr.value, FUTURE_OP.U);
3487 return Result.err(Status.ACC_Future, "UserRole [%s - %s] is saved for future processing",
3491 return Result.err(rfc);
3494 return func.extendUserRole(trans, userRole, false);
3497 return Result.err(Result.ERR_NotFound,"This user and role doesn't exist");
3502 path = "/authz/userRole/:user/:role",
3503 params = { "user|string|true",
3507 errorCodes = {403,404,406},
3508 text = { "Remove Role :role from User :user."
3512 public Result<Void> deleteUserRole(AuthzTrans trans, String usr, String role) {
3513 Validator val = new ServiceValidator();
3514 if (val.nullOrBlank("User", usr)
3515 .nullOrBlank("Role", role).err()) {
3516 return Result.err(Status.ERR_BadData, val.errs());
3519 boolean mayNotChange;
3520 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans,ques,role);
3522 return Result.err(rrdd);
3525 RoleDAO.Data rdd = rrdd.value;
3526 Result<NsDAO.Data> rns = ques.mayUser(trans, trans.user(), rdd, Access.write);
3528 // Make sure we don't delete the last owner of valid NS
3529 if (rns.isOKhasData() && Question.OWNER.equals(rdd.name) && ques.countOwner(trans,rdd.ns)<=1) {
3530 return Result.err(Status.ERR_Denied,"You may not delete the last Owner of " + rdd.ns );
3533 if (mayNotChange=rns.notOK()) {
3534 if (!trans.requested(future)) {
3535 return Result.err(rns);
3539 Result<List<UserRoleDAO.Data>> rulr;
3540 if ((rulr=ques.userRoleDAO().read(trans, usr, role)).notOKorIsEmpty()) {
3541 return Result.err(Status.ERR_UserRoleNotFound, "User [ "+usr+" ] is not "
3542 + "Assigned to the Role [ " + role + " ]");
3545 UserRoleDAO.Data userRole = rulr.value.get(0);
3546 if (mayNotChange) { // Function exited earlier if !trans.futureRequested
3547 FutureDAO.Data fto = new FutureDAO.Data();
3548 fto.target=UserRoleDAO.TABLE;
3549 fto.memo = "Remove User ["+userRole.user+"] from Role ["+userRole.role+"]";
3550 GregorianCalendar now = new GregorianCalendar();
3551 fto.start = now.getTime();
3552 fto.expires = trans.org().expiration(now, Expiration.Future).getTime();
3554 Result<String> rfc = func.createFuture(trans, fto,
3555 userRole.user+'|'+userRole.role, userRole.user, rns.value, FUTURE_OP.D);
3557 return Result.err(Status.ACC_Future, "UserRole [%s - %s] is saved for future processing",
3561 return Result.err(rfc);
3564 return ques.userRoleDAO().delete(trans, rulr.value.get(0), false);
3570 path = "/authz/userRole/:user/:role",
3571 params = {"user|string|true",
3572 "role|string|true"},
3574 errorCodes = {403,404,406},
3575 text = { "Returns the User (with Expiration date from listed User/Role) if it exists"
3579 public Result<USERS> getUserInRole(AuthzTrans trans, String user, String role) {
3580 final Validator v = new ServiceValidator();
3581 if (v.role(role).nullOrBlank("User", user).err()) {
3582 return Result.err(Status.ERR_BadData,v.errs());
3585 // Result<NsDAO.Data> ns = ques.deriveNs(trans, role);
3586 // if (ns.notOK()) return Result.err(ns);
3588 // Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), ns.value, Access.write);
3589 // May calling user see by virtue of the Role
3590 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3592 return Result.err(rrdd);
3594 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rrdd.value,Access.read);
3596 return Result.err(rnd);
3599 HashSet<UserRoleDAO.Data> userSet = new HashSet<>();
3600 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readUserInRole(trans, user, role);
3602 for (UserRoleDAO.Data data : rlurd.value) {
3607 @SuppressWarnings("unchecked")
3608 USERS users = (USERS) mapper.newInstance(API.USERS);
3609 mapper.users(trans, userSet, users);
3610 return Result.ok(users);
3615 path = "/authz/users/role/:role",
3616 params = {"user|string|true",
3617 "role|string|true"},
3619 errorCodes = {403,404,406},
3620 text = { "Returns the User (with Expiration date from listed User/Role) if it exists"
3624 public Result<USERS> getUsersByRole(AuthzTrans trans, String role) {
3625 final Validator v = new ServiceValidator();
3626 if (v.nullOrBlank("Role",role).err()) {
3627 return Result.err(Status.ERR_BadData,v.errs());
3630 // Result<NsDAO.Data> ns = ques.deriveNs(trans, role);
3631 // if (ns.notOK()) return Result.err(ns);
3633 // Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), ns.value, Access.write);
3634 // May calling user see by virtue of the Role
3635 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3637 return Result.err(rrdd);
3640 boolean contactOnly = false;
3641 // Allow the request of any valid user to find the contact of the NS (Owner)
3642 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rrdd.value,Access.read);
3644 if (Question.OWNER.equals(rrdd.value.name)) {
3647 return Result.err(rnd);
3651 HashSet<UserRoleDAO.Data> userSet = new HashSet<>();
3652 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByRole(trans, role);
3654 for (UserRoleDAO.Data data : rlurd.value) {
3655 if (contactOnly) { //scrub data
3656 // Can't change actual object, or will mess up the cache.
3657 UserRoleDAO.Data scrub = new UserRoleDAO.Data();
3659 scrub.rname = data.rname;
3660 scrub.role = data.role;
3661 scrub.user = data.user;
3669 @SuppressWarnings("unchecked")
3670 USERS users = (USERS) mapper.newInstance(API.USERS);
3671 mapper.users(trans, userSet, users);
3672 return Result.ok(users);
3676 * getUsersByPermission
3680 path = "/authz/users/perm/:type/:instance/:action",
3681 params = { "type|string|true",
3682 "instance|string|true",
3683 "action|string|true"
3686 errorCodes = {404,406},
3687 text = { "List all Users that have Permission specified by :type :instance :action",
3691 public Result<USERS> getUsersByPermission(AuthzTrans trans, String type, String instance, String action) {
3692 final Validator v = new ServiceValidator();
3693 if (v.nullOrBlank("Type",type)
3694 .nullOrBlank("Instance",instance)
3695 .nullOrBlank("Action",action)
3697 return Result.err(Status.ERR_BadData,v.errs());
3700 Result<NsSplit> nss = ques.deriveNsSplit(trans, type);
3702 return Result.err(nss);
3705 Result<List<NsDAO.Data>> nsd = ques.nsDAO().read(trans, nss.value.ns);
3707 return Result.err(nsd);
3710 boolean allInstance = ASTERIX.equals(instance);
3711 boolean allAction = ASTERIX.equals(action);
3712 // Get list of roles per Permission,
3713 // Then loop through Roles to get Users
3714 // Note: Use Sets to avoid processing or responding with Duplicates
3715 Set<String> roleUsed = new HashSet<>();
3716 Set<UserRoleDAO.Data> userSet = new HashSet<>();
3718 if (!nss.isEmpty()) {
3719 Result<List<PermDAO.Data>> rlp = ques.permDAO().readByType(trans, nss.value.ns, nss.value.name);
3720 if (rlp.isOKhasData()) {
3721 for (PermDAO.Data pd : rlp.value) {
3722 if ((allInstance || pd.instance.equals(instance)) &&
3723 (allAction || pd.action.equals(action))) {
3724 if (ques.mayUser(trans, trans.user(),pd,Access.read).isOK()) {
3725 for (String role : pd.roles) {
3726 if (!roleUsed.contains(role)) { // avoid evaluating Role many times
3728 Result<List<UserRoleDAO.Data>> rlurd = ques.userRoleDAO().readByRole(trans, role.replace('|', '.'));
3729 if (rlurd.isOKhasData()) {
3730 for (UserRoleDAO.Data urd : rlurd.value) {
3741 @SuppressWarnings("unchecked")
3742 USERS users = (USERS) mapper.newInstance(API.USERS);
3743 mapper.users(trans, userSet, users);
3744 return Result.ok(users);
3747 /***********************************
3749 ***********************************/
3751 public Result<HISTORY> getHistoryByUser(final AuthzTrans trans, String user, final int[] yyyymm, final int sort) {
3752 final Validator v = new ServiceValidator();
3753 if (v.nullOrBlank("User",user).err()) {
3754 return Result.err(Status.ERR_BadData,v.errs());
3757 Result<NsDAO.Data> rnd;
3758 // Users may look at their own data
3759 if (trans.user().equals(user)) {
3760 // Users may look at their own data
3762 int at = user.indexOf('@');
3763 if (at>=0 && trans.org().getRealm().equals(user.substring(at+1))) {
3764 NsDAO.Data nsd = new NsDAO.Data();
3765 nsd.name = Question.domain2ns(user);
3766 rnd = ques.mayUser(trans, trans.user(), nsd, Access.read);
3768 return Result.err(rnd);
3771 rnd = ques.validNSOfDomain(trans, user);
3773 return Result.err(rnd);
3776 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
3778 return Result.err(rnd);
3782 Result<List<HistoryDAO.Data>> resp = ques.historyDAO().readByUser(trans, user, yyyymm);
3784 return Result.err(resp);
3786 return mapper.history(trans, resp.value,sort);
3790 public Result<HISTORY> getHistoryByRole(AuthzTrans trans, String role, int[] yyyymm, final int sort) {
3791 final Validator v = new ServiceValidator();
3792 if (v.nullOrBlank("Role",role).err()) {
3793 return Result.err(Status.ERR_BadData,v.errs());
3796 Result<RoleDAO.Data> rrdd = RoleDAO.Data.decode(trans, ques, role);
3798 return Result.err(rrdd);
3801 Result<NsDAO.Data> rnd = ques.mayUser(trans, trans.user(), rrdd.value, Access.read);
3803 return Result.err(rnd);
3805 Result<List<HistoryDAO.Data>> resp = ques.historyDAO().readBySubject(trans, role, "role", yyyymm);
3807 return Result.err(resp);
3809 return mapper.history(trans, resp.value,sort);
3813 public Result<HISTORY> getHistoryByPerm(AuthzTrans trans, String type, int[] yyyymm, final int sort) {
3814 final Validator v = new ServiceValidator();
3815 if (v.nullOrBlank("Type",type)
3817 return Result.err(Status.ERR_BadData,v.errs());
3820 // May user see Namespace of Permission (since it's only one piece... we can't check for "is permission part of")
3821 Result<List<HistoryDAO.Data>> resp;
3822 if(type.startsWith(trans.user())) {
3823 resp = ques.historyDAO().readBySubject(trans, type, "perm", yyyymm);
3825 Result<NsDAO.Data> rnd = ques.deriveNs(trans,type);
3827 return Result.err(rnd);
3829 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
3831 return Result.err(rnd);
3833 resp = ques.historyDAO().readBySubject(trans, type, "perm", yyyymm);
3837 return Result.err(resp);
3839 return mapper.history(trans, resp.value,sort);
3843 public Result<HISTORY> getHistoryByNS(AuthzTrans trans, String ns, int[] yyyymm, final int sort) {
3844 final Validator v = new ServiceValidator();
3845 if (v.nullOrBlank("NS",ns).err()) {
3846 return Result.err(Status.ERR_BadData,v.errs());
3849 Result<NsDAO.Data> rnd = ques.deriveNs(trans,ns);
3851 return Result.err(rnd);
3853 rnd = ques.mayUser(trans, trans.user(), rnd.value, Access.read);
3855 return Result.err(rnd);
3858 Result<List<HistoryDAO.Data>> resp = ques.historyDAO().readBySubject(trans, ns, "ns", yyyymm);
3860 return Result.err(resp);
3862 return mapper.history(trans, resp.value,sort);
3866 public Result<HISTORY> getHistoryBySubject(AuthzTrans trans, String subject, String target, int[] yyyymm, final int sort) {
3867 NsDAO.Data ndd = new NsDAO.Data();
3868 ndd.name = FQI.reverseDomain(subject);
3869 Result<Data> rnd = ques.mayUser(trans, trans.user(), ndd, Access.read);
3871 return Result.err(rnd);
3874 Result<List<HistoryDAO.Data>> resp = ques.historyDAO().readBySubject(trans, subject, target, yyyymm);
3876 return Result.err(resp);
3878 return mapper.history(trans, resp.value,sort);
3881 /***********************************
3883 ***********************************/
3885 public Result<Void> createDelegate(final AuthzTrans trans, REQUEST base) {
3886 return createOrUpdateDelegate(trans, base, Question.Access.create);
3890 public Result<Void> updateDelegate(AuthzTrans trans, REQUEST base) {
3891 return createOrUpdateDelegate(trans, base, Question.Access.write);
3895 private Result<Void> createOrUpdateDelegate(final AuthzTrans trans, REQUEST base, final Access access) {
3896 final Result<DelegateDAO.Data> rd = mapper.delegate(trans, base);
3897 final ServiceValidator v = new ServiceValidator();
3898 if (v.delegate(trans.org(),rd).err()) {
3899 return Result.err(Status.ERR_BadData,v.errs());
3902 final DelegateDAO.Data dd = rd.value;
3904 Result<List<DelegateDAO.Data>> ddr = ques.delegateDAO().read(trans, dd);
3905 if (access==Access.create && ddr.isOKhasData()) {
3906 return Result.err(Status.ERR_ConflictAlreadyExists, "[%s] already delegates to [%s]", dd.user, ddr.value.get(0).delegate);
3907 } else if (access!=Access.create && ddr.notOKorIsEmpty()) {
3908 return Result.err(Status.ERR_NotFound, "[%s] does not have a Delegate Record to [%s].",dd.user,access.name());
3910 Result<Void> rv = ques.mayUser(trans, dd, access);
3915 Result<FutureDAO.Data> fd = mapper.future(trans,DelegateDAO.TABLE,base, dd, false,
3917 StringBuilder sb = new StringBuilder();
3918 sb.append(access.name());
3919 sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
3920 sb.append("Delegate ");
3921 sb.append(access==Access.create?"[":"to [");
3922 sb.append(rd.value.delegate);
3923 sb.append("] for [");
3924 sb.append(rd.value.user);
3926 return sb.toString();
3929 return Result.ok(); // Validate in code above
3934 Result<String> rfc = func.createFuture(trans, fd.value,
3935 dd.user, trans.user(),null, access==Access.create?FUTURE_OP.C:FUTURE_OP.U);
3937 return Result.err(Status.ACC_Future, "Delegate for [%s]",
3940 return Result.err(rfc);
3942 case Status.ACC_Now:
3943 if (access==Access.create) {
3944 Result<DelegateDAO.Data> rdr = ques.delegateDAO().create(trans, dd);
3948 return Result.err(rdr);
3951 return ques.delegateDAO().update(trans, dd);
3954 return Result.err(fd);
3959 public Result<Void> deleteDelegate(AuthzTrans trans, REQUEST base) {
3960 final Result<DelegateDAO.Data> rd = mapper.delegate(trans, base);
3961 final Validator v = new ServiceValidator();
3962 if (v.notOK(rd).nullOrBlank("User", rd.value.user).err()) {
3963 return Result.err(Status.ERR_BadData,v.errs());
3966 Result<List<DelegateDAO.Data>> ddl;
3967 if ((ddl=ques.delegateDAO().read(trans, rd.value)).notOKorIsEmpty()) {
3968 return Result.err(Status.ERR_DelegateNotFound,"Cannot delete non-existent Delegate");
3970 final DelegateDAO.Data dd = ddl.value.get(0);
3971 Result<Void> rv = ques.mayUser(trans, dd, Access.write);
3976 return ques.delegateDAO().delete(trans, dd, false);
3980 public Result<Void> deleteDelegate(AuthzTrans trans, String userName) {
3981 DelegateDAO.Data dd = new DelegateDAO.Data();
3982 final Validator v = new ServiceValidator();
3983 if (v.nullOrBlank("User", userName).err()) {
3984 return Result.err(Status.ERR_BadData,v.errs());
3987 Result<List<DelegateDAO.Data>> ddl;
3988 if ((ddl=ques.delegateDAO().read(trans, dd)).notOKorIsEmpty()) {
3989 return Result.err(Status.ERR_DelegateNotFound,"Cannot delete non-existent Delegate");
3991 dd = ddl.value.get(0);
3992 Result<Void> rv = ques.mayUser(trans, dd, Access.write);
3997 return ques.delegateDAO().delete(trans, dd, false);
4001 public Result<DELGS> getDelegatesByUser(AuthzTrans trans, String user) {
4002 final Validator v = new ServiceValidator();
4003 if (v.nullOrBlank("User", user).err()) {
4004 return Result.err(Status.ERR_BadData,v.errs());
4007 DelegateDAO.Data ddd = new DelegateDAO.Data();
4009 ddd.delegate = null;
4010 Result<Void> rv = ques.mayUser(trans, ddd, Access.read);
4012 return Result.err(rv);
4015 TimeTaken tt = trans.start("Get delegates for a user", Env.SUB);
4017 Result<List<DelegateDAO.Data>> dbDelgs = ques.delegateDAO().read(trans, user);
4019 if (dbDelgs.isOKhasData()) {
4020 return mapper.delegate(dbDelgs.value);
4022 return Result.err(Status.ERR_DelegateNotFound,"No Delegate found for [%s]",user);
4030 public Result<DELGS> getDelegatesByDelegate(AuthzTrans trans, String delegate) {
4031 final Validator v = new ServiceValidator();
4032 if (v.nullOrBlank("Delegate", delegate).err()) {
4033 return Result.err(Status.ERR_BadData,v.errs());
4036 DelegateDAO.Data ddd = new DelegateDAO.Data();
4037 ddd.user = delegate;
4038 Result<Void> rv = ques.mayUser(trans, ddd, Access.read);
4040 return Result.err(rv);
4043 TimeTaken tt = trans.start("Get users for a delegate", Env.SUB);
4045 Result<List<DelegateDAO.Data>> dbDelgs = ques.delegateDAO().readByDelegate(trans, delegate);
4047 if (dbDelgs.isOKhasData()) {
4048 return mapper.delegate(dbDelgs.value);
4050 return Result.err(Status.ERR_DelegateNotFound,"Delegate [%s] is not delegating for anyone.",delegate);
4057 /***********************************
4059 ***********************************/
4060 private static final String APPR_FMT = "actor=%s, action=%s, operation=\"%s\", requestor=%s, delegator=%s";
4062 public Result<Void> updateApproval(AuthzTrans trans, APPROVALS approvals) {
4063 Result<List<ApprovalDAO.Data>> rlad = mapper.approvals(approvals);
4065 return Result.err(rlad);
4067 int numApprs = rlad.value.size();
4069 return Result.err(Status.ERR_NoApprovals,"No Approvals sent for Updating");
4071 int numProcessed = 0;
4072 String user = trans.user();
4074 Result<List<ApprovalDAO.Data>> curr;
4075 Lookup<List<ApprovalDAO.Data>> apprByTicket=null;
4076 for (ApprovalDAO.Data updt : rlad.value) {
4077 if (updt.ticket!=null) {
4078 curr = ques.approvalDAO().readByTicket(trans, updt.ticket);
4079 if (curr.isOKhasData()) {
4080 final List<ApprovalDAO.Data> add = curr.value;
4081 // Store a Pre-Lookup
4082 apprByTicket = (trans1, noop) -> add;
4084 } else if (updt.id!=null) {
4085 curr = ques.approvalDAO().read(trans, updt);
4086 } else if (updt.approver!=null) {
4087 curr = ques.approvalDAO().readByApprover(trans, updt.approver);
4089 return Result.err(Status.ERR_BadData,"Approvals need ID, Ticket or Approval data to update");
4092 if (curr.isOKhasData()) {
4093 Map<String, Result<List<DelegateDAO.Data>>> delegateCache = new HashMap<>();
4094 Map<UUID, FutureDAO.Data> futureCache = new HashMap<>();
4095 FutureDAO.Data hasDeleted = new FutureDAO.Data();
4097 for (ApprovalDAO.Data cd : curr.value) {
4098 if ("pending".equals(cd.status)) {
4099 // Check for right record. Need ID, or (Ticket&Trans.User==Appr)
4101 boolean delegatedAction = ques.isDelegated(trans, user, cd.approver, delegateCache);
4102 String delegator = cd.approver;
4103 if (updt.id!=null ||
4104 (updt.ticket!=null && user.equals(cd.approver)) ||
4105 (updt.ticket!=null && delegatedAction)) {
4106 if (updt.ticket.equals(cd.ticket)) {
4107 Changed ch = new Changed();
4108 cd.id = ch.changed(cd.id,updt.id);
4109 // cd.ticket = changed(cd.ticket,updt.ticket);
4110 cd.user = ch.changed(cd.user,updt.user);
4111 cd.approver = ch.changed(cd.approver,updt.approver);
4112 cd.type = ch.changed(cd.type,updt.type);
4113 cd.status = ch.changed(cd.status,updt.status);
4114 cd.memo = ch.changed(cd.memo,updt.memo);
4115 cd.operation = ch.changed(cd.operation,updt.operation);
4116 cd.updated = ch.changed(cd.updated,updt.updated==null?new Date():updt.updated);
4117 // if (updt.status.equals("denied")) {
4118 // cd.last_notified = null;
4120 if (cd.ticket!=null) {
4121 FutureDAO.Data fdd = futureCache.get(cd.ticket);
4122 if (fdd==null) { // haven't processed ticket yet
4123 Result<FutureDAO.Data> rfdd = ques.futureDAO().readPrimKey(trans, cd.ticket);
4125 fdd = rfdd.value; // null is ok
4129 futureCache.put(cd.ticket, fdd); // processed this Ticket... don't do others on this ticket
4131 if (fdd==hasDeleted) { // YES, by Object
4133 cd.status = "ticketDeleted";
4134 ch.hasChanged(true);
4136 FUTURE_OP fop = FUTURE_OP.toFO(cd.operation);
4138 trans.info().printf("Approval Status %s is not actionable",cd.status);
4139 } else if (apprByTicket!=null) {
4140 Result<OP_STATUS> rv = func.performFutureOp(trans, fop, fdd, apprByTicket,func.urDBLookup);
4144 if (delegatedAction) {
4145 trans.audit().printf(APPR_FMT,user,updt.status,cd.memo,cd.user,delegator);
4147 futureCache.put(cd.ticket, hasDeleted);
4151 ch.hasChanged(true);
4152 trans.audit().printf(APPR_FMT,user,rv.value.desc(),cd.memo,cd.user,delegator);
4153 futureCache.put(cd.ticket, hasDeleted);
4158 trans.info().log(rv.toString());
4165 if (ch.hasChanged()) {
4166 ques.approvalDAO().update(trans, cd, true);
4175 if (numApprs==numProcessed) {
4178 return Result.err(Status.ERR_ActionNotCompleted,numProcessed + " out of " + numApprs + " completed");
4182 private static class Changed {
4183 private boolean hasChanged = false;
4185 public<T> T changed(T src, T proposed) {
4186 if (proposed==null || (src!=null && src.equals(proposed))) {
4193 public void hasChanged(boolean b) {
4197 public boolean hasChanged() {
4203 public Result<APPROVALS> getApprovalsByUser(AuthzTrans trans, String user) {
4204 final Validator v = new ServiceValidator();
4205 if (v.nullOrBlank("User", user).err()) {
4206 return Result.err(Status.ERR_BadData,v.errs());
4209 Result<List<ApprovalDAO.Data>> rapd = ques.approvalDAO().readByUser(trans, user);
4211 return mapper.approvals(rapd.value);
4213 return Result.err(rapd);
4218 public Result<APPROVALS> getApprovalsByTicket(AuthzTrans trans, String ticket) {
4219 final Validator v = new ServiceValidator();
4220 if (v.nullOrBlank("Ticket", ticket).err()) {
4221 return Result.err(Status.ERR_BadData,v.errs());
4225 uuid = UUID.fromString(ticket);
4226 } catch (IllegalArgumentException e) {
4227 return Result.err(Status.ERR_BadData,e.getMessage());
4230 Result<List<ApprovalDAO.Data>> rapd = ques.approvalDAO().readByTicket(trans, uuid);
4232 return mapper.approvals(rapd.value);
4234 return Result.err(rapd);
4239 public Result<APPROVALS> getApprovalsByApprover(AuthzTrans trans, String approver) {
4240 final Validator v = new ServiceValidator();
4241 if (v.nullOrBlank("Approver", approver).err()) {
4242 return Result.err(Status.ERR_BadData,v.errs());
4245 List<ApprovalDAO.Data> listRapds = new ArrayList<>();
4247 Result<List<ApprovalDAO.Data>> myRapd = ques.approvalDAO().readByApprover(trans, approver);
4248 if (myRapd.notOK()) {
4249 return Result.err(myRapd);
4252 listRapds.addAll(myRapd.value);
4254 Result<List<DelegateDAO.Data>> delegatedFor = ques.delegateDAO().readByDelegate(trans, approver);
4255 if (delegatedFor.isOK()) {
4256 for (DelegateDAO.Data dd : delegatedFor.value) {
4257 if (dd.expires.after(new Date())) {
4258 String delegator = dd.user;
4259 Result<List<ApprovalDAO.Data>> rapd = ques.approvalDAO().readByApprover(trans, delegator);
4261 for (ApprovalDAO.Data d : rapd.value) {
4262 if (!d.user.equals(trans.user())) {
4271 return mapper.approvals(listRapds);
4275 * @see org.onap.aaf.auth.service.AuthzService#clearCache(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String)
4278 public Result<Void> cacheClear(AuthzTrans trans, String cname) {
4279 if (ques.isGranted(trans,trans.user(),ROOT_NS,CACHE,cname,"clear")) {
4280 return ques.clearCache(trans,cname);
4282 return Result.err(Status.ERR_Denied, "%s does not have AAF Permission '%s.%s|%s|clear",
4283 trans.user(),ROOT_NS,CACHE,cname);
4287 * @see org.onap.aaf.auth.service.AuthzService#cacheClear(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.String, java.lang.Integer)
4290 public Result<Void> cacheClear(AuthzTrans trans, String cname, int[] segment) {
4291 if (ques.isGranted(trans,trans.user(),ROOT_NS,CACHE,cname,"clear")) {
4292 Result<Void> v=null;
4293 for (int i: segment) {
4294 v=ques.cacheClear(trans,cname,i);
4300 return Result.err(Status.ERR_Denied, "%s does not have AAF Permission '%s.%s|%s|clear",
4301 trans.user(),ROOT_NS,CACHE,cname);
4305 * @see org.onap.aaf.auth.service.AuthzService#dbReset(org.onap.aaf.auth.env.test.AuthzTrans)
4308 public void dbReset(AuthzTrans trans) {
4309 ques.historyDAO().reportPerhapsReset(trans, null);