Resolve all checkstyle warnings
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / certification / adapter / CsrMetaBuilder.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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 package org.onap.aaf.certservice.certification.adapter;
22
23 import java.security.KeyPair;
24 import java.util.Arrays;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27
28 import org.bouncycastle.asn1.x500.AttributeTypeAndValue;
29 import org.bouncycastle.asn1.x500.style.BCStyle;
30 import org.bouncycastle.asn1.x500.style.IETFUtils;
31 import org.bouncycastle.cert.CertException;
32 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
33 import org.onap.aaf.certservice.certification.model.CsrModel;
34 import org.onap.aaf.certservice.cmpv2client.external.CsrMeta;
35 import org.onap.aaf.certservice.cmpv2client.external.Rdn;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 class CsrMetaBuilder {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(CsrMetaBuilder.class);
44
45     /**
46      * Creates CSRMeta from CsrModel and Cmpv2Server
47      *
48      * @param csrModel Certificate Signing Request from Service external  API
49      * @param server   Cmp Server configuration from cmpServers.json
50      * @return AAF native model  for CSR metadata
51      */
52     CsrMeta build(CsrModel csrModel, Cmpv2Server server) {
53         CsrMeta csrMeta = createCsrMeta(csrModel);
54         addSans(csrModel, csrMeta);
55         csrMeta.setKeyPair(new KeyPair(csrModel.getPublicKey(), csrModel.getPrivateKey()));
56         csrMeta.setPassword(server.getAuthentication().getIak());
57         csrMeta.setIssuerName(server.getIssuerDN());
58         csrMeta.setCaUrl(server.getUrl());
59         csrMeta.setName(csrModel.getSubjectData());
60         csrMeta.setSenderKid(server.getAuthentication().getRv());
61         return csrMeta;
62     }
63
64     private CsrMeta createCsrMeta(CsrModel csrModel) {
65         return new CsrMeta((Arrays.stream(csrModel.getSubjectData().getRDNs()).map(this::convertFromBcRdn)
66                                     .filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList())));
67     }
68
69     private void addSans(CsrModel csrModel, CsrMeta csrMeta) {
70         csrModel.getSans().forEach(csrMeta::addSan);
71     }
72
73     private Optional<Rdn> convertFromBcRdn(org.bouncycastle.asn1.x500.RDN rdn) {
74         Rdn result = null;
75         try {
76             result = convertRdn(rdn);
77         } catch (CertException e) {
78             LOGGER.error("Exception occurred during convert of RDN", e);
79         }
80         return Optional.ofNullable(result);
81     }
82
83     private Rdn convertRdn(org.bouncycastle.asn1.x500.RDN rdn) throws CertException {
84         AttributeTypeAndValue rdnData = rdn.getFirst();
85         String tag = BCStyle.INSTANCE.oidToDisplayName(rdnData.getType());
86         String value = IETFUtils.valueToString(rdnData.getValue());
87         return new Rdn(tag, value);
88     }
89
90 }