c06734f4b5b5af864ae67d6e54ddeb3d6b4a4101
[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
28 import org.onap.aaf.auth.cm.data.CertDrop;
29 import org.onap.aaf.auth.cm.data.CertRenew;
30 import org.onap.aaf.auth.cm.data.CertReq;
31 import org.onap.aaf.auth.cm.data.CertResp;
32 import org.onap.aaf.auth.cm.validation.CertmanValidator;
33 import org.onap.aaf.auth.dao.cass.ArtiDAO;
34 import org.onap.aaf.auth.dao.cass.ArtiDAO.Data;
35 import org.onap.aaf.auth.dao.cass.CertDAO;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.layer.Result;
38 import org.onap.aaf.cadi.util.FQI;
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, String... 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(String s : var) {
90                         err.getVariables().add(s);
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 = arti.getMechid();
223                         data.machine = arti.getMachine();
224                         data.type(true).addAll(arti.getType());
225                         data.ca = arti.getCa();
226                         data.dir = arti.getDir();
227                         data.os_user = arti.getOsUser();
228                         // Optional (on way in)
229                         data.ns = arti.getNs();
230                         data.renewDays = arti.getRenewDays();
231                         data.notify = arti.getNotification();
232                         
233                         // Ignored on way in for create/update
234                         data.sponsor = arti.getSponsor();
235                         data.expires = null;
236                         
237                         // Derive Optional Data from Machine (Domain) if exists
238                         if(data.machine!=null) {
239                                 if(data.ca==null && data.machine.endsWith(".att.com")) {
240                                                 data.ca = "aaf"; // default
241                                 }
242                                 if(data.ns==null ) {
243                                         data.ns=FQI.reverseDomain(data.machine);
244                                 }
245                         }
246                         data.sans(true).addAll(arti.getSans());
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(arti.mechid);
262                                 a.setMachine(arti.machine);
263                                 a.setSponsor(arti.sponsor);
264                                 a.setNs(arti.ns);
265                                 a.setCa(arti.ca);
266                                 a.setDir(arti.dir);
267                                 a.getType().addAll(arti.type(false));
268                                 a.setOsUser(arti.os_user);
269                                 a.setRenewDays(arti.renewDays);
270                                 a.setNotification(arti.notify);
271                                 a.getSans().addAll(arti.sans(false));
272                                 artis.getArtifact().add(a);
273                         }
274                         return Result.ok(artis);
275                 } else {
276                         return Result.err(lArtiDAO);
277                 }
278         }
279         
280         
281
282 }