From 0855846bc0ab2dcdadc5ef7ed9588f70124ce21e Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Tue, 10 Mar 2020 16:05:09 +0100 Subject: Add annotation for OpenAPI 3.0 documentation generation Issue-ID: AAF-997 Signed-off-by: Bartosz Gardziejewski Change-Id: I6dcbe2076d7e0095c210b1cae95309fe31ebf243 --- .../certservice/api/CertificationController.java | 36 ++++++-- .../aaf/certservice/api/ReadinessController.java | 15 +++- .../certservice/api/ReloadConfigController.java | 26 ++++-- .../api/advice/CertificationExceptionAdvice.java | 100 +++++++++++++++++++++ .../api/advice/ReloadConfigExceptionAdvice.java | 43 +++++++++ .../api/configuration/OpenApiConfig.java | 44 +++++++++ .../CertificationExceptionController.java | 100 --------------------- .../exception/Cmpv2ServerNotFoundException.java | 4 +- .../src/main/resources/application.properties | 6 +- 9 files changed, 249 insertions(+), 125 deletions(-) create mode 100644 certService/src/main/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdvice.java create mode 100644 certService/src/main/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdvice.java create mode 100644 certService/src/main/java/org/onap/aaf/certservice/api/configuration/OpenApiConfig.java delete mode 100644 certService/src/main/java/org/onap/aaf/certservice/certification/CertificationExceptionController.java (limited to 'certService/src/main') diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/CertificationController.java b/certService/src/main/java/org/onap/aaf/certservice/api/CertificationController.java index abb6811b..fe941f58 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/api/CertificationController.java +++ b/certService/src/main/java/org/onap/aaf/certservice/api/CertificationController.java @@ -20,16 +20,18 @@ package org.onap.aaf.certservice.api; -import com.google.gson.Gson; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; import org.onap.aaf.certservice.certification.CertificationModelFactory; -import org.onap.aaf.certservice.certification.CsrModelFactory; -import org.onap.aaf.certservice.certification.CsrModelFactory.StringBase64; -import org.onap.aaf.certservice.certification.configuration.Cmpv2ServerProvider; -import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException; import org.onap.aaf.certservice.certification.exception.DecryptionException; +import org.onap.aaf.certservice.certification.exception.ErrorResponseModel; import org.onap.aaf.certservice.certification.model.CertificationModel; -import org.onap.aaf.certservice.certification.model.CsrModel; import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,6 +45,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController +@Tag(name = "CertificationService") public class CertificationController { private static final Logger LOGGER = LoggerFactory.getLogger(CertificationController.class); @@ -64,17 +67,32 @@ public class CertificationController { * @return JSON containing trusted certificates and certificate chain */ @GetMapping(value = "v1/certificate/{caName}", produces = "application/json; charset=utf-8") - public ResponseEntity signCertificate( + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "certificate successfully signed"), + @ApiResponse(responseCode = "400", description = "given CSR or/and PK is incorrect", + content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))), + @ApiResponse(responseCode = "404", description = "CA not found for given name", + content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))), + @ApiResponse(responseCode = "500", description = "something went wrong during connecting to cmp client", + content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))) + }) + @Operation( + summary = "sign certificate", + description = "Web endpoint for requesting certificate signing. Used by system components to gain certificate signed by CA.", + tags = { "CertificationService" }) + public ResponseEntity signCertificate( + @Parameter(description="Name of certification authority that will sign CSR.") @PathVariable String caName, + @Parameter(description="Certificate signing request in form of PEM object encoded in Base64 (with header and footer).") @RequestHeader("CSR") String encodedCsr, + @Parameter(description="Private key in form of PEM object encoded in Base64 (with header and footer).") @RequestHeader("PK") String encodedPrivateKey ) throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException { caName = caName.replaceAll("[\n|\r|\t]", "_"); LOGGER.info("Received certificate signing request for CA named: {}", caName); CertificationModel certificationModel = certificationModelFactory .createCertificationModel(encodedCsr, encodedPrivateKey, caName); - return new ResponseEntity<>(new Gson().toJson(certificationModel), HttpStatus.OK); - + return new ResponseEntity<>(certificationModel, HttpStatus.OK); } } diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/ReadinessController.java b/certService/src/main/java/org/onap/aaf/certservice/api/ReadinessController.java index e33bf512..288957ca 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/api/ReadinessController.java +++ b/certService/src/main/java/org/onap/aaf/certservice/api/ReadinessController.java @@ -20,6 +20,10 @@ package org.onap.aaf.certservice.api; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; import org.onap.aaf.certservice.certification.configuration.CmpServersConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -28,6 +32,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController +@Tag(name = "CertificationService") public class ReadinessController { private final CmpServersConfig cmpServersConfig; @@ -37,7 +42,15 @@ public class ReadinessController { this.cmpServersConfig = cmpServersConfig; } - @GetMapping("/ready") + @GetMapping(value = "/ready", produces = "application/json; charset=utf-8") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "configuration is loaded and service is ready to use"), + @ApiResponse(responseCode = "503", description = "configuration loading failed and service is unavailable") + }) + @Operation( + summary = "check is container is ready", + description = "Web endpoint for checking if service is ready to be used.", + tags = { "CertificationService" }) public ResponseEntity checkReady() { if (cmpServersConfig.isReady()) { return new ResponseEntity<>(HttpStatus.OK); diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/ReloadConfigController.java b/certService/src/main/java/org/onap/aaf/certservice/api/ReloadConfigController.java index 5390a00c..b6673ce2 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/api/ReloadConfigController.java +++ b/certService/src/main/java/org/onap/aaf/certservice/api/ReloadConfigController.java @@ -20,18 +20,25 @@ package org.onap.aaf.certservice.api; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; import org.onap.aaf.certservice.certification.configuration.CmpServersConfig; import org.onap.aaf.certservice.certification.configuration.CmpServersConfigLoadingException; +import org.onap.aaf.certservice.certification.exception.ErrorResponseModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController +@Tag(name = "CertificationService") public class ReloadConfigController { private static final Logger LOGGER = LoggerFactory.getLogger(ReloadConfigController.class); @@ -43,16 +50,19 @@ public class ReloadConfigController { this.cmpServersConfig = cmpServersConfig; } - @GetMapping("/reload") + @GetMapping(value = "/reload", produces = "application/json; charset=utf-8") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "configuration has been successfully reloaded"), + @ApiResponse(responseCode = "500", description = "something went wrong during configuration loading", + content = @Content(schema = @Schema(implementation = ErrorResponseModel.class))) + }) + @Operation( + summary = "reload service configuration from file", + description = "Web endpoint for performing configuration reload. Used to reload configuration file from file.", + tags = { "CertificationService" }) public ResponseEntity reloadConfiguration() throws CmpServersConfigLoadingException { cmpServersConfig.reloadConfiguration(); return new ResponseEntity<>(HttpStatus.OK); } - @ExceptionHandler(value = CmpServersConfigLoadingException.class) - public ResponseEntity handle(CmpServersConfigLoadingException exception) { - LOGGER.error(exception.getMessage(), exception.getCause()); - return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } - } diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdvice.java b/certService/src/main/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdvice.java new file mode 100644 index 00000000..f75e93c9 --- /dev/null +++ b/certService/src/main/java/org/onap/aaf/certservice/api/advice/CertificationExceptionAdvice.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.api.advice; + +import org.onap.aaf.certservice.api.CertificationController; +import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException; +import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException; +import org.onap.aaf.certservice.certification.exception.CsrDecryptionException; +import org.onap.aaf.certservice.certification.exception.ErrorResponseModel; +import org.onap.aaf.certservice.certification.exception.KeyDecryptionException; +import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice(assignableTypes = CertificationController.class) +public class CertificationExceptionAdvice { + + private static final Logger LOGGER = LoggerFactory.getLogger(CertificationExceptionAdvice.class); + + @ExceptionHandler(value = CsrDecryptionException.class) + public ResponseEntity handle(CsrDecryptionException exception) { + LOGGER.error("Exception occurred during decoding certificate sign request:", exception); + return getErrorResponseEntity( + "Wrong certificate signing request (CSR) format", + HttpStatus.BAD_REQUEST + ); + } + + @ExceptionHandler(value = KeyDecryptionException.class) + public ResponseEntity handle(KeyDecryptionException exception) { + LOGGER.error("Exception occurred during decoding key:", exception); + return getErrorResponseEntity( + "Wrong key (PK) format", + HttpStatus.BAD_REQUEST + ); + } + + @ExceptionHandler(value = Cmpv2ServerNotFoundException.class) + public ResponseEntity handle(Cmpv2ServerNotFoundException exception) { + LOGGER.error("Exception occurred selecting CMPv2 server:", exception); + return getErrorResponseEntity( + "Certification authority not found for given CAName", + HttpStatus.NOT_FOUND + ); + } + + @ExceptionHandler(value = RuntimeException.class) + public ResponseEntity handle(RuntimeException exception) throws CmpClientException { + throw new CmpClientException("Runtime exception occurred calling cmp client business logic", exception); + } + + @ExceptionHandler(value = CmpClientException.class) + public ResponseEntity handle(CmpClientException exception) { + LOGGER.error("Exception occurred calling cmp client:", exception); + return getErrorResponseEntity( + "Exception occurred during call to cmp client", + HttpStatus.INTERNAL_SERVER_ERROR + ); + } + + @ExceptionHandler(value = Cmpv2ClientAdapterException.class) + public ResponseEntity handle(Cmpv2ClientAdapterException exception) { + LOGGER.error("Exception occurred parsing cmp client response:", exception); + return getErrorResponseEntity( + "Exception occurred parsing cmp client response", + HttpStatus.INTERNAL_SERVER_ERROR + ); + } + + private ResponseEntity getErrorResponseEntity(String errorMessage, HttpStatus status) { + ErrorResponseModel errorResponse = new ErrorResponseModel(errorMessage); + return new ResponseEntity<>( + errorResponse, + status + ); + } + +} diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdvice.java b/certService/src/main/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdvice.java new file mode 100644 index 00000000..bf83ece2 --- /dev/null +++ b/certService/src/main/java/org/onap/aaf/certservice/api/advice/ReloadConfigExceptionAdvice.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.api.advice; + +import org.onap.aaf.certservice.api.ReloadConfigController; +import org.onap.aaf.certservice.certification.configuration.CmpServersConfigLoadingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice(assignableTypes = ReloadConfigController.class) +public class ReloadConfigExceptionAdvice { + + private static final Logger LOGGER = LoggerFactory.getLogger(ReloadConfigExceptionAdvice.class); + + @ExceptionHandler(value = CmpServersConfigLoadingException.class) + public ResponseEntity handle(CmpServersConfigLoadingException exception) { + LOGGER.error(exception.getMessage(), exception.getCause()); + return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + +} diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/configuration/OpenApiConfig.java b/certService/src/main/java/org/onap/aaf/certservice/api/configuration/OpenApiConfig.java new file mode 100644 index 00000000..18327048 --- /dev/null +++ b/certService/src/main/java/org/onap/aaf/certservice/api/configuration/OpenApiConfig.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.api.configuration; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OpenApiConfig { + + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .components(new Components()) + .info( + new Info() + .title("CertService Documentation") + .description("Certification service API documentation") + .version("1.0.0") + ); + } + +} diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/CertificationExceptionController.java b/certService/src/main/java/org/onap/aaf/certservice/certification/CertificationExceptionController.java deleted file mode 100644 index d649f147..00000000 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/CertificationExceptionController.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aaf.certservice.certification; - -import com.google.gson.Gson; -import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException; -import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException; -import org.onap.aaf.certservice.certification.exception.CsrDecryptionException; -import org.onap.aaf.certservice.certification.exception.ErrorResponseModel; -import org.onap.aaf.certservice.certification.exception.KeyDecryptionException; -import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; - -@ControllerAdvice -public class CertificationExceptionController { - - private static final Logger LOGGER = LoggerFactory.getLogger(CertificationExceptionController.class); - - @ExceptionHandler(value = CsrDecryptionException.class) - public ResponseEntity handle(CsrDecryptionException exception) { - LOGGER.error("Exception occurred during decoding certificate sign request:", exception); - return getErrorResponseEntity( - "Wrong certificate signing request (CSR) format", - HttpStatus.BAD_REQUEST - ); - } - - @ExceptionHandler(value = KeyDecryptionException.class) - public ResponseEntity handle(KeyDecryptionException exception) { - LOGGER.error("Exception occurred during decoding key:", exception); - return getErrorResponseEntity( - "Wrong key (PK) format", - HttpStatus.BAD_REQUEST - ); - } - - @ExceptionHandler(value = Cmpv2ServerNotFoundException.class) - public ResponseEntity handle(Cmpv2ServerNotFoundException exception) { - LOGGER.error("Exception occurred selecting CMPv2 server:", exception); - return getErrorResponseEntity( - "Certification authority not found for given CAName", - HttpStatus.NOT_FOUND - ); - } - - @ExceptionHandler(value = CmpClientException.class) - public ResponseEntity handle(CmpClientException exception) { - LOGGER.error("Exception occurred calling cmp client:", exception); - return getErrorResponseEntity( - "Exception occurred during call to cmp client", - HttpStatus.INTERNAL_SERVER_ERROR - ); - } - - @ExceptionHandler(value = RuntimeException.class) - public ResponseEntity handle(RuntimeException exception) throws CmpClientException { - throw new CmpClientException("Runtime exception occurred calling cmp client business logic", exception); - } - - @ExceptionHandler(value = Cmpv2ClientAdapterException.class) - public ResponseEntity handle(Cmpv2ClientAdapterException exception) { - LOGGER.error("Exception occurred parsing cmp client response:", exception); - return getErrorResponseEntity( - "Exception occurred parsing cmp client response", - HttpStatus.INTERNAL_SERVER_ERROR - ); - } - - private ResponseEntity getErrorResponseEntity(String errorMessage, HttpStatus status) { - ErrorResponseModel errorResponse = new ErrorResponseModel(errorMessage); - return new ResponseEntity<>( - new Gson().toJson(errorResponse), - status - ); - } - -} diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/exception/Cmpv2ServerNotFoundException.java b/certService/src/main/java/org/onap/aaf/certservice/certification/exception/Cmpv2ServerNotFoundException.java index 95d4cd69..35acb0cd 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/exception/Cmpv2ServerNotFoundException.java +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/exception/Cmpv2ServerNotFoundException.java @@ -20,9 +20,7 @@ package org.onap.aaf.certservice.certification.exception; -import org.webjars.NotFoundException; - -public class Cmpv2ServerNotFoundException extends NotFoundException { +public class Cmpv2ServerNotFoundException extends RuntimeException { public Cmpv2ServerNotFoundException(String message) { super(message); } diff --git a/certService/src/main/resources/application.properties b/certService/src/main/resources/application.properties index aac9b96f..9ccdd326 100644 --- a/certService/src/main/resources/application.properties +++ b/certService/src/main/resources/application.properties @@ -1,13 +1,11 @@ # Actuator configuration -springdoc.show-actuator=true - management.endpoints.enabled-by-default=true management.endpoint.configprops.enabled=true management.endpoints.web.exposure.include=health - # Swagger configuration +springdoc.show-actuator=true springdoc.swagger-ui.path=/docs # AAF CertService app specific configuration -app.config.path=/etc/onap/aaf/certservice \ No newline at end of file +app.config.path=/etc/onap/aaf/certservice -- cgit 1.2.3-korg