9f877788744cfb746a93ce870ea5d0a35f8337a6
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / api / CertificationController.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.oom.certservice.api;
22
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.media.Content;
26 import io.swagger.v3.oas.annotations.media.Schema;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
28 import io.swagger.v3.oas.annotations.responses.ApiResponses;
29 import io.swagger.v3.oas.annotations.tags.Tag;
30 import org.onap.oom.certservice.certification.CertificationModelFactory;
31 import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
32 import org.onap.oom.certservice.certification.exception.DecryptionException;
33 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
34 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
35 import org.onap.oom.certservice.certification.model.CertificationModel;
36 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestHeader;
45 import org.springframework.web.bind.annotation.RestController;
46
47
48 @RestController
49 @Tag(name = "CertificationService")
50 public class CertificationController {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationController.class);
53
54     private final CertificationModelFactory certificationModelFactory;
55
56     @Autowired
57     CertificationController(CertificationModelFactory certificationModelFactory) {
58         this.certificationModelFactory = certificationModelFactory;
59     }
60
61     /**
62      * Request for signing certificate by given CA.
63      *
64      * @param caName            the name of Certification Authority that will sign root certificate
65      * @param encodedCsr        Certificate Sign Request encoded in Base64 form
66      * @param encodedPrivateKey Private key for CSR, needed for PoP, encoded in Base64 form
67      * @return JSON containing trusted certificates and certificate chain
68      */
69     @GetMapping(value = "v1/certificate/{caName}", produces = "application/json")
70     @ApiResponses(value = {
71             @ApiResponse(responseCode = "200", description = "Certificate successfully signed"),
72             @ApiResponse(responseCode = "400", description = "Given CSR or/and PK is incorrect",
73                     content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))),
74             @ApiResponse(responseCode = "404", description = "CA not found for given name",
75                     content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))),
76             @ApiResponse(responseCode = "500", description = "Something went wrong during connectiion to CMPv2 server",
77                     content = @Content(schema = @Schema(implementation = ErrorResponseModel.class)))
78     })
79     @Operation(
80             summary = "sign certificate",
81             description = "Web endpoint for requesting certificate signing. Used by system components to gain certificate signed by CA.",
82             tags = {"CertificationService"})
83     public ResponseEntity<CertificationModel> signCertificate(
84             @Parameter(description = "Name of certification authority that will sign CSR.")
85             @PathVariable String caName,
86             @Parameter(description = "Certificate signing request in form of PEM object encoded in Base64 (with header and footer).")
87             @RequestHeader("CSR") String encodedCsr,
88             @Parameter(description = "Private key in form of PEM object encoded in Base64 (with header and footer).")
89             @RequestHeader("PK") String encodedPrivateKey
90     ) throws DecryptionException, CmpClientException {
91         caName = replaceWhiteSpaceChars(caName);
92         LOGGER.info("Received certificate signing request for CA named: {}", caName);
93         CertificationModel certificationModel = certificationModelFactory
94                 .createCertificationModel(encodedCsr, encodedPrivateKey, caName);
95         return new ResponseEntity<>(certificationModel, HttpStatus.OK);
96     }
97
98     /**
99      * Request for updating certificate by given CA.
100      *
101      * @param caName                the name of Certification Authority that will sign root certificate
102      * @param encodedCsr            Certificate Sign Request encoded in Base64 form
103      * @param encodedPrivateKey     Private key for CSR, needed for PoP, encoded in Base64 form
104      * @param encodedOldCert        Certificate (signed by Certification Authority) that should be renewed
105      * @param encodedOldPrivateKey  Old private key corresponding with old certificate
106      * @return JSON containing trusted certificates and certificate chain
107      */
108     @GetMapping(value = "v1/certificate-update/{caName}", produces = "application/json")
109     public ResponseEntity<CertificationModel> updateCertificate(
110             @PathVariable String caName,
111             @RequestHeader("CSR") String encodedCsr,
112             @RequestHeader("PK") String encodedPrivateKey,
113             @RequestHeader("OLD_CERT") String encodedOldCert,
114             @RequestHeader("OLD_PK") String encodedOldPrivateKey
115     ) throws DecryptionException, CmpClientException, CertificateDecryptionException {
116         caName = replaceWhiteSpaceChars(caName);
117         LOGGER.info("Received certificate update request for CA named: {}", caName);
118         CertificateUpdateModel certificateUpdateModel = new CertificateUpdateModel.CertificateUpdateModelBuilder()
119                 .setEncodedCsr(encodedCsr)
120                 .setEncodedPrivateKey(encodedPrivateKey)
121                 .setEncodedOldCert(encodedOldCert)
122                 .setEncodedOldPrivateKey(encodedOldPrivateKey)
123                 .setCaName(caName)
124                 .build();
125         CertificationModel certificationModel = certificationModelFactory
126                 .createCertificationModel(certificateUpdateModel);
127         return new ResponseEntity<>(certificationModel, HttpStatus.OK);
128     }
129
130     private String replaceWhiteSpaceChars(String text) {
131         return text.replaceAll("[\n\r\t]", "_");
132     }
133 }