From 09128196b16368651e8fa2a20140216e086700f2 Mon Sep 17 00:00:00 2001 From: awudzins Date: Tue, 3 Mar 2020 14:51:21 +0100 Subject: Fix reload endpoint to return status Signed-off-by: Adam Wudzinski Issue-ID: AAF-997 wChange-Id: I4563428ef407b4dc8c84c3efc8ec213145806b32 Change-Id: Iee33a26feb5cc1acd39d91ee9f34a49183040e06 --- .../certservice/api/CertificationController.java | 84 +++++++++++++++++++++ .../aaf/certservice/api/CertificationService.java | 85 ---------------------- .../certservice/api/ReloadConfigController.java | 58 +++++++++++++++ .../configuration/CmpServersConfig.java | 41 ++++++----- .../configuration/CmpServersConfigLoader.java | 19 ++--- .../CmpServersConfigLoadingException.java | 32 ++++++++ .../configuration/model/Authentication.java | 3 + .../configuration/model/Cmpv2Server.java | 8 +- .../src/main/resources/application.properties | 2 +- 9 files changed, 215 insertions(+), 117 deletions(-) create mode 100644 certService/src/main/java/org/onap/aaf/certservice/api/CertificationController.java delete mode 100644 certService/src/main/java/org/onap/aaf/certservice/api/CertificationService.java create mode 100644 certService/src/main/java/org/onap/aaf/certservice/api/ReloadConfigController.java create mode 100644 certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoadingException.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 new file mode 100644 index 00000000..e663909c --- /dev/null +++ b/certService/src/main/java/org/onap/aaf/certservice/api/CertificationController.java @@ -0,0 +1,84 @@ +/* + * ============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; + +import com.google.gson.Gson; +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.exception.DecryptionException; +import org.onap.aaf.certservice.certification.model.CertificationModel; +import org.onap.aaf.certservice.certification.model.CsrModel; +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.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +public class CertificationController { + + private static final Logger LOGGER = LoggerFactory.getLogger(CertificationController.class); + + private final CsrModelFactory csrModelFactory; + private final CertificationModelFactory certificationModelFactory; + + @Autowired + CertificationController(CsrModelFactory csrModelFactory, CertificationModelFactory certificationModelFactory) { + this.csrModelFactory = csrModelFactory; + this.certificationModelFactory = certificationModelFactory; + } + + /** + * Request for signing certificate by given CA. + * + * + * @param caName the name of Certification Authority that will sign root certificate + * @param encodedCsr Certificate Sign Request encoded in Base64 form + * @param encodedPrivateKey Private key for CSR, needed for PoP, encoded in Base64 form + * @return JSON containing trusted certificates and certificate chain + */ + @GetMapping(value = "v1/certificate/{caName}", produces = "application/json; charset=utf-8") + public ResponseEntity signCertificate( + @PathVariable String caName, + @RequestHeader("CSR") String encodedCsr, + @RequestHeader("PK") String encodedPrivateKey + ) throws DecryptionException { + + caName = caName.replaceAll("[\n|\r|\t]", "_"); + LOGGER.info("Received certificate signing request for CA named: {}", caName); + CsrModel csrModel = csrModelFactory.createCsrModel( + new StringBase64(encodedCsr), + new StringBase64(encodedPrivateKey) + ); + LOGGER.debug("Received CSR meta data: \n{}", csrModel); + CertificationModel certificationModel = certificationModelFactory + .createCertificationModel(csrModel, caName); + return new ResponseEntity<>(new Gson().toJson(certificationModel), HttpStatus.OK); + + } + +} diff --git a/certService/src/main/java/org/onap/aaf/certservice/api/CertificationService.java b/certService/src/main/java/org/onap/aaf/certservice/api/CertificationService.java deleted file mode 100644 index 945fc6d1..00000000 --- a/certService/src/main/java/org/onap/aaf/certservice/api/CertificationService.java +++ /dev/null @@ -1,85 +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.api; - -import com.google.gson.Gson; -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.exception.DecryptionException; -import org.onap.aaf.certservice.certification.model.CertificationModel; -import org.onap.aaf.certservice.certification.model.CsrModel; -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.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RestController; - - -@RestController -public class CertificationService { - - private static final Logger LOGGER = LoggerFactory.getLogger(CertificationService.class); - - private final CsrModelFactory csrModelFactory; - private final CertificationModelFactory certificationModelFactory; - - @Autowired - CertificationService(CsrModelFactory csrModelFactory, CertificationModelFactory certificationModelFactory) { - this.csrModelFactory = csrModelFactory; - this.certificationModelFactory = certificationModelFactory; - } - - /** - * Request for signing certificate by given CA. - * - * - * @param caName the name of Certification Authority that will sign root certificate - * @param encodedCsr Certificate Sign Request encoded in Base64 form - * @param encodedPrivateKey Private key for CSR, needed for PoP, encoded in Base64 form - * @return JSON containing trusted certificates and certificate chain - */ - @GetMapping(value = "v1/certificate/{caName}", produces = "application/json; charset=utf-8") - public ResponseEntity signCertificate( - @PathVariable String caName, - @RequestHeader("CSR") String encodedCsr, - @RequestHeader("PK") String encodedPrivateKey - ) throws DecryptionException { - - caName = caName.replaceAll("[\n|\r|\t]", "_"); - LOGGER.info("Received certificate signing request for CA named: {}", caName); - CsrModel csrModel = csrModelFactory.createCsrModel( - new StringBase64(encodedCsr), - new StringBase64(encodedPrivateKey) - ); - LOGGER.debug("Received CSR meta data: \n{}", csrModel); - CertificationModel certificationModel = certificationModelFactory - .createCertificationModel(csrModel, caName); - return new ResponseEntity<>(new Gson().toJson(certificationModel), 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 new file mode 100644 index 00000000..5390a00c --- /dev/null +++ b/certService/src/main/java/org/onap/aaf/certservice/api/ReloadConfigController.java @@ -0,0 +1,58 @@ +/* + * ============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; + +import org.onap.aaf.certservice.certification.configuration.CmpServersConfig; +import org.onap.aaf.certservice.certification.configuration.CmpServersConfigLoadingException; +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 +public class ReloadConfigController { + + private static final Logger LOGGER = LoggerFactory.getLogger(ReloadConfigController.class); + + private final CmpServersConfig cmpServersConfig; + + @Autowired + public ReloadConfigController(CmpServersConfig cmpServersConfig) { + this.cmpServersConfig = cmpServersConfig; + } + + @GetMapping("/reload") + 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/certification/configuration/CmpServersConfig.java b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfig.java index 25e69251..a304b5a6 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfig.java +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfig.java @@ -20,34 +20,31 @@ package org.onap.aaf.certservice.certification.configuration; +import java.io.File; +import java.util.Collections; +import java.util.List; +import javax.annotation.PostConstruct; import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; import org.springframework.context.annotation.Configuration; -import javax.annotation.PostConstruct; -import java.io.File; -import java.util.Collections; -import java.util.List; -import org.springframework.context.event.EventListener; - @Configuration public class CmpServersConfig { private static final Logger LOGGER = LoggerFactory.getLogger(CmpServersConfig.class); + private static final String LOADING_SUCCESS_MESSAGE = "CMP Servers configuration successfully loaded from file {}"; private static final String CMP_SERVERS_CONFIG_FILENAME = "cmpServers.json"; + private static final String INIT_CONFIGURATION = "Loading initial configuration"; private static final String REFRESHING_CONFIGURATION = "Refreshing configuration"; - private String configPath; - private List cmpServers; + private final String configPath; private final CmpServersConfigLoader cmpServersConfigLoader; + private List cmpServers; + @Autowired public CmpServersConfig(@Value("${app.config.path}") String configPath, CmpServersConfigLoader cmpServersConfigLoader) { @@ -56,18 +53,28 @@ public class CmpServersConfig { } @PostConstruct - void loadConfiguration() { - String configFilePath = configPath + File.separator + CMP_SERVERS_CONFIG_FILENAME; - this.cmpServers = Collections.unmodifiableList(cmpServersConfigLoader.load(configFilePath)); + void init() { + LOGGER.info(INIT_CONFIGURATION); + try { + loadConfiguration(); + } catch (CmpServersConfigLoadingException e) { + LOGGER.error(e.getMessage(), e.getCause()); + } } - @EventListener - public void onRefreshScope(final RefreshScopeRefreshedEvent event) { + public void reloadConfiguration() throws CmpServersConfigLoadingException { LOGGER.info(REFRESHING_CONFIGURATION); loadConfiguration(); } + void loadConfiguration() throws CmpServersConfigLoadingException { + String configFilePath = configPath + File.separator + CMP_SERVERS_CONFIG_FILENAME; + this.cmpServers = Collections.unmodifiableList(cmpServersConfigLoader.load(configFilePath)); + LOGGER.info(LOADING_SUCCESS_MESSAGE, configFilePath); + } + public List getCmpServers() { return cmpServers; } + } diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java index 94530100..1072d630 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java @@ -24,20 +24,18 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; import java.security.InvalidParameterException; -import java.util.ArrayList; import java.util.List; import org.onap.aaf.certservice.certification.configuration.model.CmpServers; import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; import org.onap.aaf.certservice.certification.configuration.validation.Cmpv2ServerConfigurationValidator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component class CmpServersConfigLoader { - private static final Logger LOGGER = LoggerFactory.getLogger(CmpServersConfigLoader.class); + private static final String LOADING_EXCEPTION_MESSAGE = "Exception occurred during CMP Servers configuration loading"; + private static final String VALIDATION_EXCEPTION_MESSAGE = "Validation of CMPv2 servers configuration failed"; private final Cmpv2ServerConfigurationValidator validator; @@ -46,19 +44,16 @@ class CmpServersConfigLoader { this.validator = validator; } - List load(String path) { - List servers = new ArrayList<>(); + List load(String path) throws CmpServersConfigLoadingException { try { - servers = loadConfigFromFile(path).getCmpv2Servers(); + List servers = loadConfigFromFile(path).getCmpv2Servers(); servers.forEach(validator::validate); - LOGGER.info("CMP Servers configuration successfully loaded from file {}", path); + return servers; } catch (IOException e) { - LOGGER.error("Exception occurred during CMP Servers configuration loading: ", e); + throw new CmpServersConfigLoadingException(LOADING_EXCEPTION_MESSAGE, e); } catch (InvalidParameterException e) { - LOGGER.error("Validation of CMPv2 servers configuration failed:", e); + throw new CmpServersConfigLoadingException(VALIDATION_EXCEPTION_MESSAGE, e); } - - return servers; } private CmpServers loadConfigFromFile(String path) throws IOException { diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoadingException.java b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoadingException.java new file mode 100644 index 00000000..b7c3638d --- /dev/null +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoadingException.java @@ -0,0 +1,32 @@ +/* + * ============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.configuration; + +public class CmpServersConfigLoadingException extends Exception { + + public CmpServersConfigLoadingException(String message) { + super(message); + } + + public CmpServersConfigLoadingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Authentication.java b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Authentication.java index af254d61..3785cf8e 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Authentication.java +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Authentication.java @@ -20,12 +20,15 @@ package org.onap.aaf.certservice.certification.configuration.model; +import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.Length; public class Authentication { + @NotNull @Length(min = 1, max = 256) private String iak; + @NotNull @Length(min = 1, max = 256) private String rv; diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Cmpv2Server.java b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Cmpv2Server.java index 9f8f9796..20b83b82 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Cmpv2Server.java +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/model/Cmpv2Server.java @@ -20,19 +20,23 @@ package org.onap.aaf.certservice.certification.configuration.model; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; import org.bouncycastle.asn1.x500.X500Name; import org.hibernate.validator.constraints.Length; import org.onap.aaf.certservice.certification.configuration.validation.constraints.Cmpv2URL; -import javax.validation.Valid; - public class Cmpv2Server { + @NotNull @Valid private Authentication authentication; + @NotNull private CaMode caMode; + @NotNull @Length(min = 1, max = 128) private String caName; + @NotNull private X500Name issuerDN; @Cmpv2URL private String url; diff --git a/certService/src/main/resources/application.properties b/certService/src/main/resources/application.properties index ac81c3a0..aac9b96f 100644 --- a/certService/src/main/resources/application.properties +++ b/certService/src/main/resources/application.properties @@ -3,7 +3,7 @@ springdoc.show-actuator=true management.endpoints.enabled-by-default=true management.endpoint.configprops.enabled=true -management.endpoints.web.exposure.include=refresh,health +management.endpoints.web.exposure.include=health # Swagger configuration -- cgit 1.2.3-korg