2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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.facade;
25 import static org.onap.aaf.auth.layer.Result.ERR_ActionNotCompleted;
26 import static org.onap.aaf.auth.layer.Result.ERR_BadData;
27 import static org.onap.aaf.auth.layer.Result.ERR_ConflictAlreadyExists;
28 import static org.onap.aaf.auth.layer.Result.ERR_Denied;
29 import static org.onap.aaf.auth.layer.Result.ERR_NotFound;
30 import static org.onap.aaf.auth.layer.Result.ERR_NotImplemented;
31 import static org.onap.aaf.auth.layer.Result.ERR_Policy;
32 import static org.onap.aaf.auth.layer.Result.ERR_Security;
33 import static org.onap.aaf.auth.layer.Result.OK;
35 import java.io.IOException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
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.data.CertResp;
43 import org.onap.aaf.auth.cm.mapper.Mapper;
44 import org.onap.aaf.auth.cm.mapper.Mapper.API;
45 import org.onap.aaf.auth.cm.service.CMService;
46 import org.onap.aaf.auth.dao.cass.ArtiDAO;
47 import org.onap.aaf.auth.dao.cass.Status;
48 import org.onap.aaf.auth.env.AuthzEnv;
49 import org.onap.aaf.auth.env.AuthzTrans;
50 import org.onap.aaf.auth.layer.Result;
51 import org.onap.aaf.cadi.aaf.AAFPermission;
52 import org.onap.aaf.misc.env.APIException;
53 import org.onap.aaf.misc.env.Data;
54 import org.onap.aaf.misc.env.Env;
55 import org.onap.aaf.misc.env.TimeTaken;
56 import org.onap.aaf.misc.env.util.Split;
57 import org.onap.aaf.misc.rosetta.env.RosettaDF;
58 import org.onap.aaf.misc.rosetta.env.RosettaData;
63 * This Service Facade encapsulates the essence of the API Service can do, and provides
64 * a single created object for elements such as RosettaDF.
66 * The Responsibilities of this class are to:
67 * 1) Interact with the Service Implementation (which might be supported by various kinds of Backend Storage)
68 * 2) Validate incoming data (if applicable)
69 * 3) Convert the Service response into the right Format, and mark the Content Type
70 * a) In the future, we may support multiple Response Formats, aka JSON or XML, based on User Request.
71 * 4) Log Service info, warnings and exceptions as necessary
72 * 5) When asked by the API layer, this will create and write Error content to the OutputStream
74 * Note: This Class does NOT set the HTTP Status Code. That is up to the API layer, so that it can be
75 * clearly coordinated with the API Documentation
80 public abstract class FacadeImpl<REQ,CERT,ARTIFACTS,ERROR> extends org.onap.aaf.auth.layer.FacadeImpl implements Facade<REQ,CERT,ARTIFACTS,ERROR>
82 private static final String TRUE = "TRUE";
83 private static final String REQUEST_CERT = "Request New Certificate";
84 private static final String RENEW_CERT = "Renew Certificate";
85 private static final String DROP_CERT = "Drop Certificate";
86 private static final String READ_CERTS_MECHID = "Read Certificates by MechID";
87 private static final String CREATE_ARTIFACTS = "Create Deployment Artifact";
88 private static final String READ_ARTIFACTS = "Read Deployment Artifact";
89 private static final String UPDATE_ARTIFACTS = "Update Deployment Artifact";
90 private static final String DELETE_ARTIFACTS = "Delete Deployment Artifact";
92 private CMService service;
94 private final RosettaDF<ERROR> errDF;
95 private final RosettaDF<REQ> certRequestDF, certRenewDF, certDropDF;
96 private final RosettaDF<CERT> certDF;
97 private final RosettaDF<ARTIFACTS> artiDF;
98 private Mapper<REQ, CERT, ARTIFACTS, ERROR> mapper;
99 // private Slot sCertAuth;
100 private final String voidResp;
102 public FacadeImpl(AAF_CM certman,
104 Mapper<REQ,CERT,ARTIFACTS,ERROR> mapper,
105 Data.TYPE dataType) throws APIException {
106 this.service = service;
107 this.mapper = mapper;
108 AuthzEnv env = certman.env;
109 //TODO: Gabe [JUnit] Static issue, talk to Jonathan
110 (errDF = env.newDataFactory(mapper.getClass(API.ERROR))).in(dataType).out(dataType);
111 (certRequestDF = env.newDataFactory(mapper.getClass(API.CERT_REQ))).in(dataType).out(dataType);
112 (certRenewDF = env.newDataFactory(mapper.getClass(API.CERT_RENEW))).in(dataType).out(dataType);
113 (certDropDF = env.newDataFactory(mapper.getClass(API.CERT_DROP))).in(dataType).out(dataType);
114 (certDF = env.newDataFactory(mapper.getClass(API.CERT))).in(dataType).out(dataType);
115 (artiDF = env.newDataFactory(mapper.getClass(API.ARTIFACTS))).in(dataType).out(dataType);
116 // sCertAuth = env.slot(API_Cert.CERT_AUTH);
117 if (artiDF.getOutType().name().contains("xml")) {
118 voidResp = "application/Void+xml;charset=utf-8;version=1.0,application/xml;version=1.0,*/*";
120 voidResp = "application/Void+json;charset=utf-8;version=1.0,application/json;version=1.0,*/*";
124 public Mapper<REQ,CERT,ARTIFACTS,ERROR> mapper() {
129 * @see com.att.authz.facade.AuthzFacade#error(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletResponse, int)
131 * Note: Conforms to AT&T TSS RESTful Error Structure
134 public void error(AuthzTrans trans, HttpServletResponse response, Result<?> result) {
135 error(trans, response, result.status,
136 result.details==null?"":result.details.trim(),
137 result.variables==null?Result.EMPTY_VARS:result.variables);
141 public void error(AuthzTrans trans, HttpServletResponse response, int status, final String _msg, final Object ... _detail) {
144 boolean hidemsg=false;
147 case ERR_ActionNotCompleted:
149 prefix = "Accepted, Action not complete";
150 response.setStatus(/*httpstatus=*/202);
158 prefix = "Forbidden";
159 response.setStatus(/*httpstatus=*/403);
165 prefix = "Not Found";
166 response.setStatus(/*httpstatus=*/404);
172 prefix = "Not Acceptable";
173 response.setStatus(/*httpstatus=*/406);
177 case ERR_ConflictAlreadyExists:
179 prefix = "Conflict Already Exists";
180 response.setStatus(/*httpstatus=*/409);
184 case ERR_NotImplemented:
186 prefix = "Not Implemented";
187 response.setStatus(/*httpstatus=*/501);
193 prefix = "General Service Error";
194 response.setStatus(/*httpstatus=*/500);
200 StringBuilder holder = new StringBuilder();
201 ERROR em = mapper().errorFromMessage(holder, msgId,prefix + ": " + _msg,_detail);
210 em = mapper().errorFromMessage(holder, msgId, "Server had an issue processing this request");
212 errDF.newData(trans).load(em).to(response.getOutputStream());
214 } catch (Exception e) {
215 trans.error().log(e,"unable to send response for",_msg);
220 public Result<Void> check(AuthzTrans trans, HttpServletResponse resp, String perm) throws IOException {
221 String[] p = Split.split('|',perm);
225 ap = new AAFPermission(null, p[0],p[1],p[2]);
228 ap = new AAFPermission(p[0],p[1],p[2],p[3]);
231 return Result.err(Result.ERR_BadData,"Invalid Perm String");
233 if (AAF_CM.aafLurPerm.fish(trans.getUserPrincipal(), ap)) {
234 resp.setContentType(voidResp);
235 resp.getOutputStream().write(0);
238 return Result.err(Result.ERR_Denied,"%s does not have %s",trans.user(),ap.getKey());
243 * @see com.att.auth.certman.facade.Facade#requestCert(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
246 public Result<Void> requestCert(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp, CA ca) {
247 TimeTaken tt = trans.start(REQUEST_CERT, Env.SUB|Env.ALWAYS);
249 boolean withTrust=(wt=req.getParameter("withTrust"))!=null || TRUE.equalsIgnoreCase(wt);
253 Data<REQ> rd = certRequestDF.newData().load(req.getInputStream());
254 request = rd.asObject();
255 } catch (APIException e) {
256 trans.error().log("Invalid Input",IN,REQUEST_CERT);
257 return Result.err(Result.ERR_BadData,"Invalid Input");
260 Result<CertResp> rcr = service.requestCert(trans,mapper.toReq(trans,request), ca);
262 return Result.err(rcr);
265 Result<CERT> rc = mapper.toCert(trans, rcr, withTrust);
266 if (rc.status == OK) {
267 RosettaData<CERT> data = certDF.newData(trans).load(rc.value);
268 data.to(resp.getOutputStream());
270 setContentType(resp, certDF.getOutType());
273 return Result.err(rc);
275 } catch (Exception e) {
276 trans.error().log(e,IN,REQUEST_CERT);
277 return Result.err(e);
284 * @see org.onap.aaf.auth.cm.facade.Facade#requestPersonalCert(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, boolean)
287 public Result<Void> requestPersonalCert(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp, CA ca) {
288 return Result.err(Result.ERR_NotImplemented, "not implemented yet");
292 public Result<Void> renewCert(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp, boolean withTrust) {
293 TimeTaken tt = trans.start(RENEW_CERT, Env.SUB|Env.ALWAYS);
297 Data<REQ> rd = certRenewDF.newData().load(req.getInputStream());
298 request = rd.asObject();
299 } catch (APIException e) {
300 trans.error().log("Invalid Input",IN,RENEW_CERT);
301 return Result.err(Result.ERR_BadData,"Invalid Input");
304 Result<CertResp> rcr = service.renewCert(trans,mapper.toRenew(trans,request));
305 Result<CERT> rc = mapper.toCert(trans, rcr, withTrust);
307 if (rc.status == OK) {
308 RosettaData<CERT> data = certDF.newData(trans).load(rc.value);
309 data.to(resp.getOutputStream());
311 setContentType(resp, certDF.getOutType());
314 return Result.err(rc);
315 } catch (Exception e) {
316 trans.error().log(e,IN,RENEW_CERT);
317 return Result.err(e);
325 public Result<Void> dropCert(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) {
326 TimeTaken tt = trans.start(DROP_CERT, Env.SUB|Env.ALWAYS);
330 Data<REQ> rd = certDropDF.newData().load(req.getInputStream());
331 request = rd.asObject();
332 } catch (APIException e) {
333 trans.error().log("Invalid Input",IN,DROP_CERT);
334 return Result.err(Result.ERR_BadData,"Invalid Input");
337 Result<Void> rv = service.dropCert(trans,mapper.toDrop(trans, request));
338 if (rv.status == OK) {
339 setContentType(resp, certRequestDF.getOutType());
342 return Result.err(rv);
343 } catch (Exception e) {
344 trans.error().log(e,IN,DROP_CERT);
345 return Result.err(e);
352 * @see org.onap.aaf.auth.cm.facade.Facade#readCertsByMechID(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletResponse, java.lang.String)
355 public Result<Void> readCertsByMechID(AuthzTrans trans, HttpServletResponse resp, String mechID) {
356 TimeTaken tt = trans.start(READ_CERTS_MECHID, Env.SUB|Env.ALWAYS);
358 Result<CERT> rc = mapper.toCert(trans, service.readCertsByMechID(trans,mechID));
359 if (rc.status == OK) {
360 RosettaData<CERT> data = certDF.newData(trans).load(rc.value);
361 data.to(resp.getOutputStream());
363 setContentType(resp, certDF.getOutType());
366 return Result.err(rc);
367 } catch (Exception e) {
368 trans.error().log(e,IN,READ_CERTS_MECHID);
369 return Result.err(e);
375 ////////////////////////////
377 ////////////////////////////
379 public Result<Void> createArtifacts(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) {
380 TimeTaken tt = trans.start(CREATE_ARTIFACTS, Env.SUB);
384 Data<ARTIFACTS> rd = artiDF.newData().load(req.getInputStream());
385 arti = rd.asObject();
386 } catch (APIException e) {
387 trans.error().log("Invalid Input",IN,CREATE_ARTIFACTS);
388 return Result.err(Result.ERR_BadData,"Invalid Input");
391 return service.createArtifact(trans,mapper.toArtifact(trans,arti));
392 } catch (Exception e) {
394 trans.error().log(e,IN,CREATE_ARTIFACTS);
395 return Result.err(e);
402 public Result<Void> readArtifacts(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) {
403 TimeTaken tt = trans.start(READ_ARTIFACTS, Env.SUB);
405 String mechid = req.getParameter("mechid");
406 String machine = req.getParameter("machine");
407 String ns = req.getParameter("ns");
409 Result<ARTIFACTS> ra;
410 if ( machine !=null && mechid == null) {
411 ra = mapper.fromArtifacts(service.readArtifactsByMachine(trans, machine));
412 } else if (mechid!=null && machine==null) {
413 ra = mapper.fromArtifacts(service.readArtifactsByMechID(trans, mechid));
414 } else if (mechid!=null && machine!=null) {
415 ArtiDAO.Data add = new ArtiDAO.Data();
417 add.machine = machine;
419 ra = mapper.fromArtifacts(service.readArtifacts(trans,add));
420 } else if (ns!=null) {
421 ra = mapper.fromArtifacts(service.readArtifactsByNs(trans, ns));
423 ra = Result.err(Status.ERR_BadData,"Invalid request inputs");
427 RosettaData<ARTIFACTS> data = artiDF.newData(trans).load(ra.value);
428 data.to(resp.getOutputStream());
429 setContentType(resp,artiDF.getOutType());
432 return Result.err(ra);
435 } catch (Exception e) {
436 trans.error().log(e,IN,READ_ARTIFACTS);
437 return Result.err(e);
444 public Result<Void> readArtifacts(AuthzTrans trans, HttpServletResponse resp, String mechid, String machine) {
445 TimeTaken tt = trans.start(READ_ARTIFACTS, Env.SUB);
447 ArtiDAO.Data add = new ArtiDAO.Data();
449 add.machine = machine;
450 Result<ARTIFACTS> ra = mapper.fromArtifacts(service.readArtifacts(trans,add));
452 RosettaData<ARTIFACTS> data = artiDF.newData(trans).load(ra.value);
453 data.to(resp.getOutputStream());
454 setContentType(resp,artiDF.getOutType());
457 return Result.err(ra);
459 } catch (Exception e) {
460 trans.error().log(e,IN,READ_ARTIFACTS);
461 return Result.err(e);
469 public Result<Void> updateArtifacts(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) {
470 TimeTaken tt = trans.start(UPDATE_ARTIFACTS, Env.SUB);
474 Data<ARTIFACTS> rd = artiDF.newData().load(req.getInputStream());
475 arti = rd.asObject();
476 } catch (APIException e) {
477 trans.error().log("Invalid Input",IN,UPDATE_ARTIFACTS);
478 return Result.err(Result.ERR_BadData,"Invalid Input");
481 return service.updateArtifact(trans,mapper.toArtifact(trans,arti));
482 } catch (Exception e) {
483 trans.error().log(e,IN,UPDATE_ARTIFACTS);
484 return Result.err(e);
491 public Result<Void> deleteArtifacts(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) {
492 TimeTaken tt = trans.start(DELETE_ARTIFACTS, Env.SUB);
496 Data<ARTIFACTS> rd = artiDF.newData().load(req.getInputStream());
497 arti = rd.asObject();
498 } catch (APIException e) {
499 trans.error().log("Invalid Input",IN,DELETE_ARTIFACTS);
500 return Result.err(Result.ERR_BadData,"Invalid Input");
503 Result<Void> rv = service.deleteArtifact(trans,mapper.toArtifact(trans,arti));
504 if (rv.status == OK) {
505 setContentType(resp, artiDF.getOutType());
508 } catch (Exception e) {
509 trans.error().log(e,IN,DELETE_ARTIFACTS);
510 return Result.err(e);
517 public Result<Void> deleteArtifacts(AuthzTrans trans, HttpServletResponse resp, String mechid, String machine) {
518 TimeTaken tt = trans.start(DELETE_ARTIFACTS, Env.SUB);
520 Result<Void> rv = service.deleteArtifact(trans, mechid, machine);
521 if (rv.status == OK) {
522 setContentType(resp, artiDF.getOutType());
525 } catch (Exception e) {
526 trans.error().log(e,IN,DELETE_ARTIFACTS);
527 return Result.err(e);