d3a83ed15be079ee4da20a97d2ee650a3ea18dd3
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / api / CertificationController.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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.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.DecryptionException;
32 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
33 import org.onap.oom.certservice.certification.model.CertificationModel;
34 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.RequestHeader;
43 import org.springframework.web.bind.annotation.RestController;
44
45
46 @RestController
47 @Tag(name = "CertificationService")
48 public class CertificationController {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationController.class);
51
52     private final CertificationModelFactory certificationModelFactory;
53
54     @Autowired
55     CertificationController(CertificationModelFactory certificationModelFactory) {
56         this.certificationModelFactory = certificationModelFactory;
57     }
58
59     /**
60      * Request for signing certificate by given CA.
61      *
62      * @param caName            the name of Certification Authority that will sign root certificate
63      * @param encodedCsr        Certificate Sign Request encoded in Base64 form
64      * @param encodedPrivateKey Private key for CSR, needed for PoP, encoded in Base64 form
65      * @return JSON containing trusted certificates and certificate chain
66      */
67     @GetMapping(value = "v1/certificate/{caName}", produces = "application/json")
68     @ApiResponses(value = {
69             @ApiResponse(responseCode = "200", description = "Certificate successfully signed"),
70             @ApiResponse(responseCode = "400", description = "Given CSR or/and PK is incorrect",
71                     content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))),
72             @ApiResponse(responseCode = "404", description = "CA not found for given name",
73                     content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))),
74             @ApiResponse(responseCode = "500", description = "Something went wrong during connectiion to CMPv2 server",
75                     content = @Content(schema = @Schema(implementation = ErrorResponseModel.class)))
76     })
77     @Operation(
78             summary = "sign certificate",
79             description = "Web endpoint for requesting certificate signing. Used by system components to gain certificate signed by CA.",
80             tags = {"CertificationService"})
81     public ResponseEntity<CertificationModel> signCertificate(
82             @Parameter(description = "Name of certification authority that will sign CSR.")
83             @PathVariable String caName,
84             @Parameter(description = "Certificate signing request in form of PEM object encoded in Base64 (with header and footer).")
85             @RequestHeader("CSR") String encodedCsr,
86             @Parameter(description = "Private key in form of PEM object encoded in Base64 (with header and footer).")
87             @RequestHeader("PK") String encodedPrivateKey
88     ) throws DecryptionException, CmpClientException {
89         caName = caName.replaceAll("[\n|\r|\t]", "_");
90         LOGGER.info("Received certificate signing request for CA named: {}", caName);
91         CertificationModel certificationModel = certificationModelFactory
92                 .createCertificationModel(encodedCsr, encodedPrivateKey, caName);
93         return new ResponseEntity<>(certificationModel, HttpStatus.OK);
94     }
95
96 }