2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 IBM.
7 * ===========================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END====================================================
23 package org.onap.aaf.auth.cm.service;
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28 import java.nio.ByteBuffer;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.cert.Certificate;
31 import java.security.cert.X509Certificate;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Date;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.List;
40 import org.onap.aaf.auth.cm.AAF_CM;
41 import org.onap.aaf.auth.cm.ca.CA;
42 import org.onap.aaf.auth.cm.ca.X509andChain;
43 import org.onap.aaf.auth.cm.cert.BCFactory;
44 import org.onap.aaf.auth.cm.cert.CSRMeta;
45 import org.onap.aaf.auth.cm.data.CertDrop;
46 import org.onap.aaf.auth.cm.data.CertRenew;
47 import org.onap.aaf.auth.cm.data.CertReq;
48 import org.onap.aaf.auth.cm.data.CertResp;
49 import org.onap.aaf.auth.cm.validation.CertmanValidator;
50 import org.onap.aaf.auth.common.Define;
51 import org.onap.aaf.auth.dao.CassAccess;
52 import org.onap.aaf.auth.dao.cass.ArtiDAO;
53 import org.onap.aaf.auth.dao.cass.CacheInfoDAO;
54 import org.onap.aaf.auth.dao.cass.CertDAO;
55 import org.onap.aaf.auth.dao.cass.CertDAO.Data;
56 import org.onap.aaf.auth.dao.cass.CredDAO;
57 import org.onap.aaf.auth.dao.cass.HistoryDAO;
58 import org.onap.aaf.auth.dao.cass.Status;
59 import org.onap.aaf.auth.dao.hl.Question;
60 import org.onap.aaf.auth.env.AuthzTrans;
61 import org.onap.aaf.auth.layer.Result;
62 import org.onap.aaf.auth.org.Organization;
63 import org.onap.aaf.auth.org.Organization.Identity;
64 import org.onap.aaf.auth.org.OrganizationException;
65 import org.onap.aaf.cadi.Access.Level;
66 import org.onap.aaf.cadi.Hash;
67 import org.onap.aaf.cadi.Permission;
68 import org.onap.aaf.cadi.aaf.AAFPermission;
69 import org.onap.aaf.cadi.config.Config;
70 import org.onap.aaf.cadi.configure.Factory;
71 import org.onap.aaf.cadi.util.FQI;
72 import org.onap.aaf.misc.env.APIException;
73 import org.onap.aaf.misc.env.util.Chrono;
75 public class CMService {
76 // If we add more CAs, may want to parameterize
77 private static final int STD_RENEWAL = 30;
78 private static final int MAX_RENEWAL = 60;
79 private static final int MIN_RENEWAL = 10;
80 // Limit total requests
81 private static final int MAX_X509s = 200; // Need a "LIMIT Exception" DB.
82 private static final String MAX_X509S_TAG = "cm_max_x509s"; // be able to adjust limit in future
84 public static final String REQUEST = "request";
85 public static final String IGNORE_IPS = "ignoreIPs";
86 public static final String RENEW = "renew";
87 public static final String DROP = "drop";
88 public static final String DOMAIN = "domain";
89 public static final String DYNAMIC_SANS="dynamic_sans";
91 private static final String CERTMAN = "certman";
92 private static final String ACCESS = "access";
94 private static final String[] NO_NOTES = new String[0];
95 private final Permission root_read_permission;
96 private final CertDAO certDAO;
97 private final CredDAO credDAO;
98 private final ArtiDAO artiDAO;
99 private AAF_CM certManager;
100 private Boolean allowIgnoreIPs;
101 private AAFPermission limitOverridePerm;
102 private int max_509s;
104 // @SuppressWarnings("unchecked")
105 public CMService(final AuthzTrans trans, AAF_CM certman) throws APIException, IOException {
106 // Jonathan 4/2015 SessionFilter unneeded... DataStax already deals with
107 // Multithreading well
109 HistoryDAO hd = new HistoryDAO(trans, certman.cluster, CassAccess.KEYSPACE);
110 CacheInfoDAO cid = new CacheInfoDAO(trans, hd);
111 certDAO = new CertDAO(trans, hd, cid);
112 credDAO = new CredDAO(trans, hd, cid);
113 artiDAO = new ArtiDAO(trans, hd, cid);
115 this.certManager = certman;
117 root_read_permission=new AAFPermission(
118 trans.getProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF),
124 max_509s = Integer.parseInt(trans.env().getProperty(MAX_X509S_TAG,Integer.toString(MAX_X509s)));
125 } catch (Exception e) {
126 trans.env().log(e, "");
127 max_509s = MAX_X509s;
129 limitOverridePerm = new AAFPermission(Define.ROOT_NS(),"certman","quantity","override");
130 allowIgnoreIPs = Boolean.valueOf(certman.access.getProperty(Config.CM_ALLOW_IGNORE_IPS, "false"));
132 trans.env().access().log(Level.INIT, "Allowing DNS Evaluation to be turned off with <ns>.certman|<ca name>|"+IGNORE_IPS);
136 public Result<CertResp> requestCert(final AuthzTrans trans, final Result<CertReq> req, final CA ca) {
138 if (req.value.fqdns.isEmpty()) {
139 return Result.err(Result.ERR_BadData, "No Machines passed in Request");
142 String key = req.value.fqdns.get(0);
144 // Policy 6: Requester must be granted Change permission in Namespace requested
145 String mechNS = FQI.reverseDomain(req.value.mechid);
146 if (mechNS == null) {
147 return Result.err(Status.ERR_Denied, "%s does not reflect a valid AAF Namespace", req.value.mechid);
150 List<String> notes = null;
152 boolean domain_based = false;
153 boolean dynamic_sans = false;
155 if(req.value.fqdns.isEmpty()) {
156 fqdns = new ArrayList<>();
158 // Only Template or Dynamic permitted to pass in FQDNs
159 if (req.value.fqdns.get(0).startsWith("*")) { // Domain set
160 if (trans.fish(new AAFPermission(null,ca.getPermType(), ca.getName(), DOMAIN))) {
163 return Result.err(Result.ERR_Denied,
164 "Domain based Authorizations (" + req.value.fqdns.get(0) + ") requires Exception");
167 if(trans.fish(new AAFPermission(null, ca.getPermType(), ca.getName(),DYNAMIC_SANS))) {
170 return Result.err(Result.ERR_Denied,
171 "Dynamic SANs for (" + req.value.mechid + ") requires Permission");
174 fqdns = new ArrayList<>(req.value.fqdns);
180 Organization org = trans.org();
184 ignoreIPs = trans.fish(new AAFPermission(mechNS,CERTMAN, ca.getName(), IGNORE_IPS));
190 InetAddress primary = null;
191 // Organize incoming information to get to appropriate Artifact
192 if (!fqdns.isEmpty()) { // Passed in FQDNS, validated above
193 // Accept domain wild cards, but turn into real machines
194 // Need *domain.com:real.machine.domain.com:san.machine.domain.com:...
195 if (domain_based) { // Domain set
196 // check for Permission in Add Artifact?
197 String domain = fqdns.get(0).substring(1); // starts with *, see above
199 if (fqdns.isEmpty()) {
200 return Result.err(Result.ERR_Denied,
201 "Requests using domain require machine declaration");
205 InetAddress ia = InetAddress.getByName(fqdns.get(0));
207 return Result.err(Result.ERR_Denied,
208 "Request not made from matching IP matching domain");
209 } else if (ia.getHostName().endsWith(domain)) {
215 // Passed in FQDNs, but not starting with *
217 for (String cn : req.value.fqdns) {
219 InetAddress[] ias = InetAddress.getAllByName(cn);
220 Set<String> potentialSanNames = new HashSet<>();
221 for (InetAddress ia1 : ias) {
222 InetAddress ia2 = InetAddress.getByAddress(ia1.getAddress());
223 if (primary == null && ias.length == 1 && trans.ip().equals(ia1.getHostAddress())) {
225 } else if (!cn.equals(ia1.getHostName())
226 && !ia2.getHostName().equals(ia2.getHostAddress())) {
227 potentialSanNames.add(ia1.getHostName());
230 } catch (UnknownHostException e1) {
231 trans.debug().log(e1);
232 return Result.err(Result.ERR_BadData, "There is no DNS lookup for %s", cn);
241 host = req.value.fqdns.get(0);
242 } else if (primary == null) {
243 return Result.err(Result.ERR_Denied, "Request not made from matching IP (%s)", req.value.fqdns.get(0));
245 String thost = primary.getHostName();
246 host = thost==null?primary.getHostAddress():thost;
249 ArtiDAO.Data add = null;
250 Result<List<ArtiDAO.Data>> ra = artiDAO.read(trans, req.value.mechid, host);
251 if (ra.isOKhasData()) {
252 add = ra.value.get(0); // single key
253 if(dynamic_sans && (add.sans!=null && !add.sans.isEmpty())) { // do not allow both Dynamic and Artifact SANS
254 return Result.err(Result.ERR_Denied,"Authorization must not include SANS when doing Dynamic SANS (%s, %s)", req.value.mechid, key);
258 ra = artiDAO.read(trans, req.value.mechid, key);
259 if (ra.isOKhasData()) { // is the Template available?
260 add = ra.value.get(0);
262 for (String s : fqdns) {
263 if (!s.equals(add.machine)) {
264 add.sans(true).add(s);
267 Result<ArtiDAO.Data> rc = artiDAO.create(trans, add); // Create new Artifact from Template
269 return Result.err(rc);
272 return Result.err(Result.ERR_Denied,"No Authorization Template for %s, %s", req.value.mechid, key);
275 return Result.err(Result.ERR_Denied,"No Authorization found for %s, %s", req.value.mechid, key);
279 // Add Artifact listed FQDNs
281 if (add.sans != null) {
282 for (String s : add.sans) {
283 if (!fqdns.contains(s)) {
290 // Policy 2: If Config marked as Expired, do not create or renew
291 Date now = new Date();
292 if (add.expires != null && now.after(add.expires)) {
293 return Result.err(Result.ERR_Policy, "Configuration for %s %s is expired %s", add.mechid,
294 add.machine, Chrono.dateFmt.format(add.expires));
297 // Policy 3: MechID must be current
298 Identity muser = org.getIdentity(trans, add.mechid);
300 return Result.err(Result.ERR_Policy, "MechID must exist in %s", org.getName());
303 // Policy 4: Sponsor must be current
304 Identity ouser = muser.responsibleTo();
306 return Result.err(Result.ERR_Policy, "%s does not have a current sponsor at %s", add.mechid,
308 } else if (!ouser.isFound() || ouser.mayOwn() != null) {
309 return Result.err(Result.ERR_Policy, "%s reports that %s cannot be responsible for %s",
310 org.getName(), trans.user());
313 // Set Email from most current Sponsor
314 email = ouser.email();
316 // Policy 5: keep Artifact data current
317 if (!ouser.fullID().equals(add.sponsor)) {
318 add.sponsor = ouser.fullID();
319 artiDAO.update(trans, add);
322 // Policy 7: Caller must be the MechID or have specifically delegated
324 if (!(trans.user().equals(req.value.mechid)
325 || trans.fish(new AAFPermission(mechNS,CERTMAN, ca.getName(), REQUEST)))) {
326 return Result.err(Status.ERR_Denied, "%s must have access to modify x509 certs in NS %s",
327 trans.user(), mechNS);
330 // Make sure Primary is the first in fqdns
331 if (fqdns.size() > 1) {
332 for (int i = 0; i < fqdns.size(); ++i) {
333 if (primary==null && !ignoreIPs) {
334 trans.error().log("CMService var primary is null");
336 String fg = fqdns.get(i);
337 if (fg!=null && primary!=null && fg.equals(primary.getHostName())) {
339 String tmp = fqdns.get(0);
340 fqdns.set(0, primary.getHostName());
347 } catch (Exception e) {
348 trans.error().log(e);
349 return Result.err(Status.ERR_Denied,
350 "AppID Sponsorship cannot be determined at this time. Try later.");
355 csrMeta = BCFactory.createCSRMeta(ca, req.value.mechid, email, fqdns);
356 csrMeta.environment(ca.getEnv());
358 // Before creating, make sure they don't have too many
359 if(!trans.fish(limitOverridePerm)) {
360 Result<List<CertDAO.Data>> existing = certDAO.readID(trans, req.value.mechid);
361 if(existing.isOK()) {
362 String cn = "CN=" + csrMeta.cn();
364 Date now = new Date();
365 for (CertDAO.Data cdd : existing.value) {
366 Collection<? extends Certificate> certs = Factory.toX509Certificate(cdd.x509);
367 for(Iterator<? extends Certificate> iter = certs.iterator(); iter.hasNext();) {
368 X509Certificate x509 = (X509Certificate)iter.next();
369 if(x509.getNotAfter().after(now) && x509.getSubjectDN().getName().contains(cn)) {
370 if(++count>max_509s) {
377 return Result.err(Result.ERR_Denied, "There are too many Certificates generated for " + cn + " for " + req.value.mechid);
381 // Here is where we send off to CA for Signing.
382 X509andChain x509ac = ca.sign(trans, csrMeta);
383 if (x509ac == null) {
384 return Result.err(Result.ERR_ActionNotCompleted, "x509 Certificate not signed by CA");
386 trans.info().printf("X509 Subject: %s", x509ac.getX509().getSubjectDN());
388 X509Certificate x509 = x509ac.getX509();
389 CertDAO.Data cdd = new CertDAO.Data();
390 cdd.ca = ca.getName();
391 cdd.serial = x509.getSerialNumber();
392 cdd.id = req.value.mechid;
393 cdd.x500 = x509.getSubjectDN().getName();
394 cdd.x509 = Factory.toString(trans, x509);
396 certDAO.create(trans, cdd);
398 CredDAO.Data crdd = new CredDAO.Data();
399 crdd.other = Question.random.nextInt();
400 crdd.cred = getChallenge256SaltedHash(csrMeta.challenge(), crdd.other);
401 crdd.expires = x509.getNotAfter();
402 crdd.id = req.value.mechid;
403 crdd.ns = Question.domain2ns(crdd.id);
404 crdd.type = CredDAO.CERT_SHA256_RSA;
405 crdd.tag = cdd.ca + '|' + cdd.serial.toString();
406 credDAO.create(trans, crdd);
408 CertResp cr = new CertResp(trans, ca, x509, csrMeta, x509ac.getTrustChain(), compileNotes(notes));
409 return Result.ok(cr);
410 } catch (Exception e) {
411 trans.debug().log(e);
412 return Result.err(Result.ERR_ActionNotCompleted, e.getMessage());
415 return Result.err(req);
419 public Result<CertResp> renewCert(AuthzTrans trans, Result<CertRenew> renew) {
421 return Result.err(Result.ERR_NotImplemented, "Not implemented yet");
423 return Result.err(renew);
427 public Result<Void> dropCert(AuthzTrans trans, Result<CertDrop> drop) {
429 return Result.err(Result.ERR_NotImplemented, "Not implemented yet");
431 return Result.err(drop);
435 public Result<List<Data>> readCertsByMechID(AuthzTrans trans, String mechID) {
436 // Policy 1: To Read, must have NS Read or is Sponsor
437 String ns = Question.domain2ns(mechID);
439 if (trans.user().equals(mechID) || trans.fish(new AAFPermission(ns,ACCESS, "*", "read"))
440 || (trans.org().validate(trans, Organization.Policy.OWNS_MECHID, null, mechID)) == null) {
441 return certDAO.readID(trans, mechID);
443 return Result.err(Result.ERR_Denied, "%s is not the ID, Sponsor or NS Owner/Admin for %s at %s",
444 trans.user(), mechID, trans.org().getName());
446 } catch (OrganizationException e) {
447 return Result.err(e);
451 public Result<CertResp> requestPersonalCert(AuthzTrans trans, CA ca) {
452 if (ca.inPersonalDomains(trans.getUserPrincipal())) {
453 Organization org = trans.org();
455 // Policy 1: MechID must be current
458 ouser = org.getIdentity(trans, trans.user());
459 } catch (OrganizationException e1) {
460 trans.debug().log(e1);
464 return Result.err(Result.ERR_Policy, "Requesting User must exist in %s", org.getName());
467 // Set Email from most current Sponsor
471 csrMeta = BCFactory.createPersonalCSRMeta(ca, trans.user(), ouser.email());
472 X509andChain x509ac = ca.sign(trans, csrMeta);
473 if (x509ac == null) {
474 return Result.err(Result.ERR_ActionNotCompleted, "x509 Certificate not signed by CA");
476 X509Certificate x509 = x509ac.getX509();
477 CertDAO.Data cdd = new CertDAO.Data();
478 cdd.ca = ca.getName();
479 cdd.serial = x509.getSerialNumber();
480 cdd.id = trans.user();
481 cdd.x500 = x509.getSubjectDN().getName();
482 cdd.x509 = Factory.toString(trans, x509);
483 certDAO.create(trans, cdd);
485 CertResp cr = new CertResp(trans, ca, x509, csrMeta, x509ac.getTrustChain(), compileNotes(null));
486 return Result.ok(cr);
487 } catch (Exception e) {
488 trans.debug().log(e);
489 return Result.err(Result.ERR_ActionNotCompleted, e.getMessage());
492 return Result.err(Result.ERR_Denied, trans.user(), " not supported for CA", ca.getName());
499 public Result<Void> createArtifact(AuthzTrans trans, List<ArtiDAO.Data> list) {
500 CertmanValidator v = new CertmanValidator().artisRequired(list, 1);
502 return Result.err(Result.ERR_BadData, v.errs());
504 for (ArtiDAO.Data add : list) {
506 // Policy 1: MechID must exist in Org
507 Identity muser = trans.org().getIdentity(trans, add.mechid);
509 return Result.err(Result.ERR_Denied, "%s is not valid for %s", add.mechid, trans.org().getName());
512 // Policy 2: MechID must have valid Organization Owner
514 if (muser.isPerson()) {
517 Identity ouser = muser.responsibleTo();
519 return Result.err(Result.ERR_Denied, "%s is not a valid Sponsor for %s at %s", trans.user(),
520 add.mechid, trans.org().getName());
523 // Policy 3: Calling ID must be MechID Owner
524 if (!trans.user().startsWith(ouser.id())) {
525 return Result.err(Result.ERR_Denied, "%s is not the Sponsor for %s at %s", trans.user(),
526 add.mechid, trans.org().getName());
531 // Policy 4: Renewal Days are between 10 and 60 (constants, may be
533 if (add.renewDays < MIN_RENEWAL) {
534 add.renewDays = STD_RENEWAL;
535 } else if (add.renewDays > MAX_RENEWAL) {
536 add.renewDays = MAX_RENEWAL;
539 // Policy 5: If Notify is blank, set to Owner's Email
540 if (add.notify == null || add.notify.length() == 0) {
541 add.notify = "mailto:" + emailUser.email();
544 // Policy 6: Only do Domain by Exception
545 if (add.machine.startsWith("*")) { // Domain set
546 CA ca = certManager.getCA(add.ca);
547 if (!trans.fish(new AAFPermission(ca.getPermNS(),ca.getPermType(), add.ca, DOMAIN))) {
548 return Result.err(Result.ERR_Denied, "Domain Artifacts (%s) requires specific Permission",
553 // Set Sponsor from Golden Source
554 add.sponsor = emailUser.fullID();
556 } catch (OrganizationException e) {
557 return Result.err(e);
560 Result<ArtiDAO.Data> rv = artiDAO.create(trans, add);
561 // come up with Partial Reporting Scheme, or allow only one at a time.
563 return Result.err(rv);
569 public Result<List<ArtiDAO.Data>> readArtifacts(AuthzTrans trans, ArtiDAO.Data add) throws OrganizationException {
570 CertmanValidator v = new CertmanValidator().keys(add);
572 return Result.err(Result.ERR_BadData, v.errs());
574 Result<List<ArtiDAO.Data>> data = artiDAO.read(trans, add);
575 if (data.notOKorIsEmpty()) {
578 add = data.value.get(0);
579 if (trans.user().equals(add.mechid)
580 || trans.fish(root_read_permission,
581 new AAFPermission(add.ns,ACCESS, "*", "read"),
582 new AAFPermission(add.ns,CERTMAN, add.ca, "read"),
583 new AAFPermission(add.ns,CERTMAN, add.ca, REQUEST))
584 || (trans.org().validate(trans, Organization.Policy.OWNS_MECHID, null, add.mechid)) == null) {
587 return Result.err(Result.ERR_Denied,
588 "%s is not %s, is not the sponsor, and doesn't have delegated permission.", trans.user(),
589 add.mechid, add.ns + ".certman|" + add.ca + "|read or ...|request"); // note: reason is set by 2nd
590 // case, if 1st case misses
595 public Result<List<ArtiDAO.Data>> readArtifactsByMechID(AuthzTrans trans, String mechid)
596 throws OrganizationException {
597 CertmanValidator v = new CertmanValidator();
598 v.nullOrBlank("mechid", mechid);
600 return Result.err(Result.ERR_BadData, v.errs());
602 String ns = FQI.reverseDomain(mechid);
605 if (trans.fish(new AAFPermission(ns, ACCESS, "*", "read"))
606 || (reason = trans.org().validate(trans, Organization.Policy.OWNS_MECHID, null, mechid)) == null) {
607 return artiDAO.readByMechID(trans, mechid);
609 return Result.err(Result.ERR_Denied, reason); // note: reason is set by 2nd case, if 1st case misses
614 public Result<List<ArtiDAO.Data>> readArtifactsByMachine(AuthzTrans trans, String machine) {
615 CertmanValidator v = new CertmanValidator();
616 v.nullOrBlank("machine", machine);
618 return Result.err(Result.ERR_BadData, v.errs());
623 return artiDAO.readByMachine(trans, machine);
626 public Result<List<ArtiDAO.Data>> readArtifactsByNs(AuthzTrans trans, String ns) {
627 CertmanValidator v = new CertmanValidator();
628 v.nullOrBlank("ns", ns);
630 return Result.err(Result.ERR_BadData, v.errs());
634 return artiDAO.readByNs(trans, ns);
637 public Result<Void> updateArtifact(AuthzTrans trans, List<ArtiDAO.Data> list) throws OrganizationException {
638 CertmanValidator v = new CertmanValidator();
639 v.artisRequired(list, 1);
641 return Result.err(Result.ERR_BadData, v.errs());
644 // Check if requesting User is Sponsor
645 // Shall we do one, or multiples?
646 for (ArtiDAO.Data add : list) {
647 // Policy 1: MechID must exist in Org
648 Identity muser = trans.org().getIdentity(trans, add.mechid);
650 return Result.err(Result.ERR_Denied, "%s is not valid for %s", add.mechid, trans.org().getName());
653 // Policy 2: MechID must have valid Organization Owner
654 Identity ouser = muser.responsibleTo();
656 return Result.err(Result.ERR_Denied, "%s is not a valid Sponsor for %s at %s", trans.user(), add.mechid,
657 trans.org().getName());
660 // Policy 3: Renewal Days are between 10 and 60 (constants, may be
662 if (add.renewDays < MIN_RENEWAL) {
663 add.renewDays = STD_RENEWAL;
664 } else if (add.renewDays > MAX_RENEWAL) {
665 add.renewDays = MAX_RENEWAL;
668 // Policy 4: Data is always updated with the latest Sponsor
669 // Add to Sponsor, to make sure we are always up to date.
670 add.sponsor = ouser.fullID();
672 // Policy 5: If Notify is blank, set to Owner's Email
673 if (add.notify == null || add.notify.length() == 0) {
674 add.notify = "mailto:" + ouser.email();
676 // Policy 6: Only do Domain by Exception
677 if (add.machine.startsWith("*")) { // Domain set
678 CA ca = certManager.getCA(add.ca);
680 return Result.err(Result.ERR_BadData, "CA is required in Artifact");
682 if (!trans.fish(new AAFPermission(null,ca.getPermType(), add.ca, DOMAIN))) {
683 return Result.err(Result.ERR_Denied, "Domain Artifacts (%s) requires specific Permission",
688 // Policy 7: only Owner may update info
689 if (trans.user().startsWith(ouser.id())) {
690 return artiDAO.update(trans, add);
692 return Result.err(Result.ERR_Denied, "%s may not update info for %s", trans.user(), muser.fullID());
695 return Result.err(Result.ERR_BadData, "No Artifacts to update");
698 public Result<Void> deleteArtifact(AuthzTrans trans, String mechid, String machine) throws OrganizationException {
699 CertmanValidator v = new CertmanValidator();
700 v.nullOrBlank("mechid", mechid).nullOrBlank("machine", machine);
702 return Result.err(Result.ERR_BadData, v.errs());
705 Result<List<ArtiDAO.Data>> rlad = artiDAO.read(trans, mechid, machine);
706 if (rlad.notOKorIsEmpty()) {
707 return Result.err(Result.ERR_NotFound, "Artifact for %s %s does not exist.", mechid, machine);
710 return deleteArtifact(trans, rlad.value.get(0));
713 private Result<Void> deleteArtifact(AuthzTrans trans, ArtiDAO.Data add) throws OrganizationException {
714 // Policy 1: Record should be delete able only by Existing Sponsor.
715 String sponsor = null;
716 Identity muser = trans.org().getIdentity(trans, add.mechid);
718 Identity ouser = muser.responsibleTo();
720 sponsor = ouser.fullID();
723 // Policy 1.a: If Sponsorship is deleted in system of Record, then
724 // accept deletion by sponsor in Artifact Table
725 if (sponsor == null) {
726 sponsor = add.sponsor;
729 String ns = FQI.reverseDomain(add.mechid);
731 if (trans.fish(new AAFPermission(ns,ACCESS, "*", "write")) || trans.user().equals(sponsor)) {
732 return artiDAO.delete(trans, add, false);
734 return Result.err(Result.ERR_Denied, "%1 is not allowed to delete this item", trans.user());
737 public Result<Void> deleteArtifact(AuthzTrans trans, List<ArtiDAO.Data> list) {
738 CertmanValidator v = new CertmanValidator().artisRequired(list, 1);
740 return Result.err(Result.ERR_BadData, v.errs());
744 boolean partial = false;
745 Result<Void> result = null;
746 for (ArtiDAO.Data add : list) {
747 result = deleteArtifact(trans, add);
748 if (result.notOK()) {
752 if (result == null) {
753 result = Result.err(Result.ERR_BadData, "No Artifacts to delete");
754 } else if (partial) {
755 result.partialContent(true);
758 } catch (Exception e) {
759 return Result.err(e);
763 private String[] compileNotes(List<String> notes) {
768 rv = new String[notes.size()];
774 private ByteBuffer getChallenge256SaltedHash(String challenge, int salt) throws NoSuchAlgorithmException {
775 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + challenge.length());
777 bb.put(challenge.getBytes());
778 byte[] hash = Hash.hashSHA256(bb.array());
779 return ByteBuffer.wrap(hash);