Update Fixes from testing
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / mapper / Mapper1_0.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cm.mapper;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.onap.aaf.auth.cm.data.CertDrop;
30 import org.onap.aaf.auth.cm.data.CertRenew;
31 import org.onap.aaf.auth.cm.data.CertReq;
32 import org.onap.aaf.auth.cm.data.CertResp;
33 import org.onap.aaf.auth.cm.validation.CertmanValidator;
34 import org.onap.aaf.auth.dao.cass.ArtiDAO;
35 import org.onap.aaf.auth.dao.cass.ArtiDAO.Data;
36 import org.onap.aaf.auth.dao.cass.CertDAO;
37 import org.onap.aaf.auth.env.AuthzTrans;
38 import org.onap.aaf.auth.layer.Result;
39 import org.onap.aaf.cadi.util.Vars;
40
41 import aaf.v2_0.Error;
42 import certman.v1_0.Artifacts;
43 import certman.v1_0.Artifacts.Artifact;
44 import certman.v1_0.BaseRequest;
45 import certman.v1_0.CertInfo;
46 import certman.v1_0.CertificateDrop;
47 import certman.v1_0.CertificateRenew;
48 import certman.v1_0.CertificateRequest;
49
50
51 public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
52     
53     @Override
54     public Class<?> getClass(API api) {
55         switch(api) {
56             case CERT_REQ: return CertificateRequest.class;
57             case CERT_RENEW: return CertificateRenew.class;
58             case CERT_DROP: return CertificateDrop.class;
59             case CERT: return CertInfo.class;
60             case ARTIFACTS: return Artifacts.class;
61             case ERROR: return Error.class;
62             case VOID: return Void.class;
63         }
64         return null;
65     }
66
67     @SuppressWarnings("unchecked")
68     @Override
69     public <A> A newInstance(API api) {
70         switch(api) {
71             case CERT_REQ: return (A) new CertificateRequest();
72             case CERT_RENEW: return (A) new CertificateRenew();
73             case CERT_DROP: return (A) new CertificateDrop();
74             case CERT: return (A) new CertInfo();
75             case ARTIFACTS: return (A) new Artifacts();
76             case ERROR: return (A)new Error();
77             case VOID: return null;
78         }
79         return null;
80     }
81
82     //////////////  Mapping Functions /////////////
83     @Override
84     public Error errorFromMessage(StringBuilder holder, String msgID, String text, Object ... var) {
85         Error err = new Error();
86         err.setMessageId(msgID);
87         // AT&T Restful Error Format requires numbers "%" placements
88         err.setText(Vars.convert(holder, text, var));
89         for (Object s : var) {
90             err.getVariables().add(s.toString());
91         }
92         return err;
93     }
94
95     /* (non-Javadoc)
96      * @see com.att.authz.certman.mapper.Mapper#toCert(org.onap.aaf.auth.env.test.AuthzTrans, org.onap.aaf.auth.layer.test.Result)
97      */
98     @Override
99     public Result<CertInfo> toCert(AuthzTrans trans, Result<CertResp> in, boolean withTrustChain) throws IOException {
100         if (!in.isOK()) {
101             return Result.err(in);
102         }
103
104         CertResp cin = in.value;
105         CertInfo cout = newInstance(API.CERT);
106         cout.setPrivatekey(cin.privateString());
107         String value;
108         if ((value=cin.challenge())!=null) {
109             cout.setChallenge(value);
110         }
111         // In Version 1, Cert is always first
112         cout.getCerts().add(cin.asCertString());
113         // Follow with Trust Chain
114         if (cin.trustChain()!=null) {
115             for (String c : cin.trustChain()) {
116                 if (c!=null) {
117                     cout.getCerts().add(c);
118                 }
119             }
120         }
121
122         // Adding all the Certs in one response is a mistake.  Makes it very hard for Agent to setup
123         // Certs in keystore versus Truststore.  Separate in Version 2_0
124         if (cin.trustCAs()!=null) {
125             for (String c : cin.trustCAs()) {
126                 if (c!=null) {
127                     if (!cout.getCerts().contains(c)) {
128                         cout.getCerts().add(c);
129                     }
130                 }
131             }
132         }
133         if (cin.notes()!=null) {
134             boolean first = true;
135             StringBuilder sb = new StringBuilder();
136             for (String n : cin.notes()) {
137                 if (first) {
138                     first = false;
139                 } else {
140                     sb.append('\n');
141                 }
142                 sb.append(n);
143             }
144             cout.setNotes(sb.toString());
145         }
146         List<String> caIssuerDNs = cout.getCaIssuerDNs();
147         for (String s : cin.caIssuerDNs()) {
148             caIssuerDNs.add(s);
149         }
150         cout.setEnv(cin.env());
151         return Result.ok(cout);
152
153     }
154
155     @Override
156     public Result<CertInfo> toCert(AuthzTrans trans, Result<List<CertDAO.Data>> in) {
157         if (in.isOK()) {
158             CertInfo cout = newInstance(API.CERT);
159             List<String> certs = cout.getCerts();
160             for (CertDAO.Data cdd : in.value) {
161                 certs.add(cdd.x509);
162             }
163             return Result.ok(cout);
164         } else {
165             return Result.err(in);
166         }
167     }
168
169     /* (non-Javadoc)
170      * @see com.att.authz.certman.mapper.Mapper#toReq(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
171      */
172     @Override
173     public Result<CertReq> toReq(AuthzTrans trans, BaseRequest req) {
174         CertificateRequest in;
175         try {
176             in = (CertificateRequest)req;
177         } catch (ClassCastException e) {
178             return Result.err(Result.ERR_BadData,"Request is not a CertificateRequest");
179         }
180
181         CertReq out = new CertReq();
182         CertmanValidator v = new CertmanValidator();
183         out.mechid=in.getMechid();
184         out.fqdns=in.getFqdns();
185         v.isNull("CertRequest", req).nullOrBlank("MechID", out.mechid);
186         v.nullBlankMin("FQDNs", out.fqdns,1);
187         if (v.err()) {
188             return Result.err(Result.ERR_BadData, v.errs());
189         }
190         out.emails = in.getEmail();
191         out.sponsor=in.getSponsor();
192         out.start = in.getStart();
193         out.end = in.getEnd();
194         out.fqdns = in.getFqdns();
195         return Result.ok(out);
196     }
197
198     /* (non-Javadoc)
199      * @see com.att.authz.certman.mapper.Mapper#toRenew(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
200      */
201     @Override
202     public Result<CertRenew> toRenew(AuthzTrans trans, BaseRequest req) {
203         return Result.err(Result.ERR_NotImplemented,"Not Implemented... yet");
204     }
205
206     /* (non-Javadoc)
207      * @see com.att.authz.certman.mapper.Mapper#toDrop(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
208      */
209     @Override
210     public Result<CertDrop> toDrop(AuthzTrans trans, BaseRequest req) {
211         return Result.err(Result.ERR_NotImplemented,"Not Implemented... yet");
212     }
213
214     /* (non-Javadoc)
215      * @see org.onap.aaf.auth.cm.mapper.Mapper#toArtifact(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
216      */
217     @Override
218     public List<ArtiDAO.Data> toArtifact(AuthzTrans trans, Artifacts artifacts) {
219         List<ArtiDAO.Data> ladd = new ArrayList<>();
220         for (Artifact arti : artifacts.getArtifact()) {
221             ArtiDAO.Data data = new ArtiDAO.Data();
222             data.mechid = trim(arti.getMechid());
223             data.machine = trim(arti.getMachine());
224             if(arti.getType()!=null) {
225                 Set<String> ss = data.type(true);
226                 for(String t : arti.getType()) {
227                     ss.add(trim(t));
228                 }
229             }
230             data.ca = trim(arti.getCa());
231             data.dir = trim(arti.getDir());
232             data.os_user = trim(arti.getOsUser());
233             // Optional (on way in)
234             data.ns = trim(arti.getNs());
235             data.renewDays = arti.getRenewDays();
236             data.notify = trim(arti.getNotification());
237             
238             // Ignored on way in for create/update
239             data.sponsor = (arti.getSponsor());
240             if(arti.getSans()!=null) {
241                 Set<String> ls = data.sans(true);
242                 for(String t : arti.getSans()) {
243                     ls.add(trim(t));
244                 }
245             }
246             data.expires = null;
247             ladd.add(data);
248         }
249         return ladd;
250     }
251
252     /* (non-Javadoc)
253      * @see org.onap.aaf.auth.cm.mapper.Mapper#fromArtifacts(org.onap.aaf.auth.layer.test.Result)
254      */
255     @Override
256     public Result<Artifacts> fromArtifacts(Result<List<Data>> lArtiDAO) {
257         if (lArtiDAO.isOK()) {
258             Artifacts artis = new Artifacts();
259             for (ArtiDAO.Data arti : lArtiDAO.value) {
260                 Artifact a = new Artifact();
261                 a.setMechid(trim(arti.mechid));
262                 a.setMachine(trim(arti.machine));
263                 a.setSponsor(trim(arti.sponsor));
264                 a.setNs(trim(arti.ns));
265                 a.setCa(trim(arti.ca));
266                 a.setDir(trim(arti.dir));
267                 for(String t : arti.type(false)) {
268                     a.getType().add(trim(t));
269                 }
270                 a.setOsUser(trim(arti.os_user));
271                 a.setRenewDays(arti.renewDays);
272                 a.setNotification(trim(arti.notify));
273                 for(String t : arti.sans(false)) {
274                     a.getSans().add(trim(t));
275                 }
276                 artis.getArtifact().add(a);
277             }
278             return Result.ok(artis);
279         } else {
280             return Result.err(lArtiDAO);
281         }
282     }
283     
284     
285
286     private String trim(String s) {
287         if(s==null) {
288             return s;
289         } else {
290             return s.trim();
291         }
292     }
293 }