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