diff options
Diffstat (limited to 'certServiceClient/src/main')
9 files changed, 196 insertions, 116 deletions
diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/CertServiceClient.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/CertServiceClient.java index d115380f..d26be8d3 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/CertServiceClient.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/CertServiceClient.java @@ -19,6 +19,12 @@ package org.onap.oom.certservice.client; +import static org.onap.oom.certservice.client.api.ExitStatus.SUCCESS; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM; + +import java.security.KeyPair; +import javax.net.ssl.SSLContext; import org.onap.oom.certservice.client.api.ExitableException; import org.onap.oom.certservice.client.certification.ArtifactsCreatorProvider; import org.onap.oom.certservice.client.certification.CsrFactory; @@ -33,19 +39,13 @@ import org.onap.oom.certservice.client.configuration.factory.CsrConfigurationFac import org.onap.oom.certservice.client.configuration.factory.SslContextFactory; import org.onap.oom.certservice.client.configuration.model.ClientConfiguration; import org.onap.oom.certservice.client.configuration.model.CsrConfiguration; +import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory; import org.onap.oom.certservice.client.httpclient.CloseableHttpsClientProvider; import org.onap.oom.certservice.client.httpclient.HttpClient; import org.onap.oom.certservice.client.httpclient.model.CertServiceResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.net.ssl.SSLContext; -import java.security.KeyPair; - -import static org.onap.oom.certservice.client.api.ExitStatus.SUCCESS; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM; - public class CertServiceClient { private static final Logger LOGGER = LoggerFactory.getLogger(CertServiceClient.class); @@ -60,29 +60,32 @@ public class CertServiceClient { KeyPairFactory keyPairFactory = new KeyPairFactory(RSA_ENCRYPTION_ALGORITHM, KEY_SIZE); PrivateKeyToPemEncoder pkEncoder = new PrivateKeyToPemEncoder(); Base64Encoder base64Encoder = new Base64Encoder(); + ValidatorsFactory validatorsFactory = new ValidatorsFactory(); try { - ClientConfiguration clientConfiguration = new ClientConfigurationFactory(new EnvsForClient()).create(); - CsrConfiguration csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr()).create(); + ClientConfiguration clientConfiguration = new ClientConfigurationFactory(new EnvsForClient(), + validatorsFactory).create(); + CsrConfiguration csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr(), validatorsFactory) + .create(); KeyPair keyPair = keyPairFactory.create(); CsrFactory csrFactory = new CsrFactory(csrConfiguration); SSLContext sslContext = new SslContextFactory(new EnvsForTls()).create(); CloseableHttpsClientProvider provider = new CloseableHttpsClientProvider( - sslContext, clientConfiguration.getRequestTimeout()); + sslContext, clientConfiguration.getRequestTimeoutInMs()); HttpClient httpClient = new HttpClient(provider, clientConfiguration.getUrlToCertService()); CertServiceResponse certServiceData = - httpClient.retrieveCertServiceData( - clientConfiguration.getCaName(), - base64Encoder.encode(csrFactory.createCsrInPem(keyPair)), - base64Encoder.encode(pkEncoder.encodePrivateKeyToPem(keyPair.getPrivate()))); + httpClient.retrieveCertServiceData( + clientConfiguration.getCaName(), + base64Encoder.encode(csrFactory.createCsrInPem(keyPair)), + base64Encoder.encode(pkEncoder.encodePrivateKeyToPem(keyPair.getPrivate()))); ArtifactsCreatorProvider - .get(clientConfiguration.getOutputType(), - clientConfiguration.getCertsOutputPath()) - .create(certServiceData.getCertificateChain(), - certServiceData.getTrustedCertificates(), - keyPair.getPrivate()); + .get(clientConfiguration.getOutputType(), + clientConfiguration.getCertsOutputPath()) + .create(certServiceData.getCertificateChain(), + certServiceData.getTrustedCertificates(), + keyPair.getPrivate()); } catch (ExitableException e) { LOGGER.error("Cert Service Client fails in execution: ", e); diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java index ac517be4..04216ff4 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java @@ -19,6 +19,21 @@ package org.onap.oom.certservice.client.certification; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.COMMON_NAME; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.COUNTRY; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.LOCATION; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.ORGANIZATION; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.ORGANIZATION_UNIT; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.SIGN_ALGORITHM; +import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.STATE; + +import java.io.IOException; +import java.io.StringWriter; +import java.security.KeyPair; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import javax.security.auth.x500.X500Principal; import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; import org.bouncycastle.asn1.x509.Extension; import org.bouncycastle.asn1.x509.Extensions; @@ -36,25 +51,10 @@ import org.onap.oom.certservice.client.configuration.model.CsrConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.security.auth.x500.X500Principal; -import java.io.IOException; -import java.io.StringWriter; -import java.security.KeyPair; -import java.util.Optional; - -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.COMMON_NAME; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.COUNTRY; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.LOCATION; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.ORGANIZATION; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.ORGANIZATION_UNIT; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.SIGN_ALGORITHM; -import static org.onap.oom.certservice.client.certification.EncryptionAlgorithmConstants.STATE; - public class CsrFactory { private static final Logger LOGGER = LoggerFactory.getLogger(CsrFactory.class); - private static final String SANS_DELIMITER = ":"; private final CsrConfiguration configuration; @@ -75,27 +75,29 @@ public class CsrFactory { private StringBuilder getMandatoryParameters() { return new StringBuilder(String.format("%s=%s, %s=%s, %s=%s, %s=%s", - COMMON_NAME, configuration.getCommonName(), - COUNTRY, configuration.getCountry(), - STATE, configuration.getState(), - ORGANIZATION, configuration.getOrganization())); + COMMON_NAME, configuration.getCommonName(), + COUNTRY, configuration.getCountry(), + STATE, configuration.getState(), + ORGANIZATION, configuration.getOrganization())); } private String getOptionalParameters() { StringBuilder optionalParameters = new StringBuilder(); Optional.ofNullable(configuration.getOrganizationUnit()) - .filter(CsrFactory::isParameterPresent) - .map(unit -> optionalParameters.append(String.format(", %s=%s", ORGANIZATION_UNIT, unit))); + .filter(CsrFactory::isParameterPresent) + .map(unit -> optionalParameters.append(String.format(", %s=%s", ORGANIZATION_UNIT, unit))); Optional.ofNullable(configuration.getLocation()) - .filter(CsrFactory::isParameterPresent) - .map(location -> optionalParameters.append(String.format(", %s=%s", LOCATION, location))); + .filter(CsrFactory::isParameterPresent) + .map(location -> optionalParameters.append(String.format(", %s=%s", LOCATION, location))); return optionalParameters.toString(); } - private PKCS10CertificationRequest createPkcs10Csr(X500Principal subject, KeyPair keyPair) throws CsrGenerationException { - JcaPKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subject, keyPair.getPublic()); + private PKCS10CertificationRequest createPkcs10Csr(X500Principal subject, KeyPair keyPair) + throws CsrGenerationException { + JcaPKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subject, + keyPair.getPublic()); - if (isParameterPresent(configuration.getSans())) { + if (!configuration.getSans().isEmpty()) { builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, generateSansExtension()); } @@ -138,12 +140,11 @@ public class CsrFactory { } private GeneralNames createGeneralNames() { - String[] sansTable = this.configuration.getSans().split(SANS_DELIMITER); - int length = sansTable.length; - GeneralName[] generalNames = new GeneralName[length]; - for (int i = 0; i < length; i++) { - generalNames[i] = new GeneralName(GeneralName.dNSName, sansTable[i]); - } + List<String> sans = this.configuration.getSans(); + GeneralName[] generalNames = sans.stream() + .map(san -> new GeneralName(GeneralName.dNSName, san)) + .collect(Collectors.toList()) + .toArray(GeneralName[]::new); return new GeneralNames(generalNames); } diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactory.java index 17cb2cc4..9f3ae9cc 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ClientConfigurationFactory.java @@ -20,22 +20,26 @@ package org.onap.oom.certservice.client.configuration.factory; +import java.util.Optional; import org.onap.oom.certservice.client.configuration.ClientConfigurationEnvs; import org.onap.oom.certservice.client.configuration.EnvsForClient; import org.onap.oom.certservice.client.configuration.exception.ClientConfigurationException; import org.onap.oom.certservice.client.configuration.model.ClientConfiguration; +import org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions; +import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Optional; - -public class ClientConfigurationFactory extends AbstractConfigurationFactory<ClientConfiguration> { +public class ClientConfigurationFactory implements ConfigurationFactory<ClientConfiguration> { private static final Logger LOGGER = LoggerFactory.getLogger(ClientConfigurationFactory.class); private final EnvsForClient envsForClient; + private final ValidatorsFactory validatorsFactory; - public ClientConfigurationFactory(EnvsForClient envsForClient) { + + public ClientConfigurationFactory(EnvsForClient envsForClient, ValidatorsFactory validatorsFactory) { this.envsForClient = envsForClient; + this.validatorsFactory = validatorsFactory; } @Override @@ -43,29 +47,29 @@ public class ClientConfigurationFactory extends AbstractConfigurationFactory<Cli ClientConfiguration configuration = new ClientConfiguration(); - envsForClient.getUrlToCertService() - .map(configuration::setUrlToCertService); + .map(configuration::setUrlToCertService); envsForClient.getRequestTimeOut() - .map(timeout -> configuration.setRequestTimeout(Integer.valueOf(timeout))); + .map(timeout -> configuration.setRequestTimeoutInMs(Integer.valueOf(timeout))); envsForClient.getOutputPath() - .filter(this::isPathValid) - .map(configuration::setCertsOutputPath) - .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.")); + .filter(BasicValidationFunctions::isPathValid) + .map(configuration::setCertsOutputPath) + .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.")); envsForClient.getCaName() - .filter(this::isAlphaNumeric) - .map(configuration::setCaName) - .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.CA_NAME + " is invalid.")); + .filter(BasicValidationFunctions::isAlphaNumeric) + .map(configuration::setCaName) + .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.CA_NAME + " is invalid.")); Optional<String> outputType = envsForClient.getOutputType(); if (outputType.isPresent()) { - outputType.filter(this::isOutputTypeValid) - .map(configuration::setOutputType) - .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_TYPE + " is invalid.")); + outputType.filter(validatorsFactory.outputTypeValidator()) + .map(configuration::setOutputType) + .orElseThrow( + () -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_TYPE + " is invalid.")); } LOGGER.info("Successful validation of Client configuration. Configuration data: {}", configuration.toString()); diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ConfigurationFactory.java new file mode 100644 index 00000000..43caf55c --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/ConfigurationFactory.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * oom-certservice-client + * ================================================================================ + * 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.oom.certservice.client.configuration.factory; + + +import org.onap.oom.certservice.client.configuration.exception.ClientConfigurationException; +import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException; +import org.onap.oom.certservice.client.configuration.model.ConfigurationModel; + +interface ConfigurationFactory<T extends ConfigurationModel> { + + T create() throws ClientConfigurationException, CsrConfigurationException; + +} diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactory.java index 0a88252c..3f87be52 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/CsrConfigurationFactory.java @@ -20,21 +20,30 @@ package org.onap.oom.certservice.client.configuration.factory; +import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isSpecialCharPresent; + +import java.util.Arrays; import org.onap.oom.certservice.client.configuration.CsrConfigurationEnvs; import org.onap.oom.certservice.client.configuration.EnvsForCsr; import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.oom.certservice.client.configuration.model.CsrConfiguration; +import org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions; +import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class CsrConfigurationFactory extends AbstractConfigurationFactory<CsrConfiguration> { +public class CsrConfigurationFactory implements ConfigurationFactory<CsrConfiguration> { private static final Logger LOGGER = LoggerFactory.getLogger(CsrConfigurationFactory.class); + private static final String SANS_DELIMITER = ":"; + private final EnvsForCsr envsForCsr; + private final ValidatorsFactory validatorsFactory; - public CsrConfigurationFactory(EnvsForCsr envsForCsr) { + public CsrConfigurationFactory(EnvsForCsr envsForCsr, ValidatorsFactory validatorsFactory) { this.envsForCsr = envsForCsr; + this.validatorsFactory = validatorsFactory; } @Override @@ -43,12 +52,12 @@ public class CsrConfigurationFactory extends AbstractConfigurationFactory<CsrCon CsrConfiguration configuration = new CsrConfiguration(); envsForCsr.getCommonName() - .filter(this::isCommonNameValid) + .filter(validatorsFactory.commonNameValidator()) .map(configuration::setCommonName) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")); envsForCsr.getOrganization() - .filter(org -> !isSpecialCharsPresent(org)) + .filter(org -> !isSpecialCharPresent(org)) .map(configuration::setOrganization) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")); @@ -57,7 +66,7 @@ public class CsrConfigurationFactory extends AbstractConfigurationFactory<CsrCon .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.STATE + " is invalid.")); envsForCsr.getCountry() - .filter(this::isCountryValid) + .filter(BasicValidationFunctions::isCountryValid) .map(configuration::setCountry) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COUNTRY + " is invalid.")); @@ -68,10 +77,12 @@ public class CsrConfigurationFactory extends AbstractConfigurationFactory<CsrCon .map(configuration::setLocation); envsForCsr.getSubjectAlternativesName() + .map(sans -> Arrays.asList(sans.split(SANS_DELIMITER))) .map(configuration::setSubjectAlternativeNames); LOGGER.info("Successful validation of CSR configuration. Configuration data: {}", configuration.toString()); return configuration; } + } diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/ClientConfiguration.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/ClientConfiguration.java index cd8a5c1e..f87b525d 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/ClientConfiguration.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/ClientConfiguration.java @@ -29,7 +29,7 @@ public class ClientConfiguration implements ConfigurationModel { private static final String DEFAULT_OUTPUT_TYPE = "P12"; private String urlToCertService; - private Integer requestTimeout; + private Integer requestTimeoutInMs; private String certsOutputPath; private String caName; private String outputType; @@ -37,7 +37,7 @@ public class ClientConfiguration implements ConfigurationModel { public ClientConfiguration() { urlToCertService = DEFAULT_REQUEST_URL; - requestTimeout = DEFAULT_TIMEOUT_MS; + requestTimeoutInMs = DEFAULT_TIMEOUT_MS; outputType = DEFAULT_OUTPUT_TYPE; } @@ -51,12 +51,12 @@ public class ClientConfiguration implements ConfigurationModel { return this; } - public Integer getRequestTimeout() { - return requestTimeout; + public Integer getRequestTimeoutInMs() { + return requestTimeoutInMs; } - public ClientConfiguration setRequestTimeout(Integer requestTimeout) { - this.requestTimeout = requestTimeout; + public ClientConfiguration setRequestTimeoutInMs(Integer requestTimeoutInMs) { + this.requestTimeoutInMs = requestTimeoutInMs; return this; } @@ -91,7 +91,7 @@ public class ClientConfiguration implements ConfigurationModel { public String toString() { return String.format("%s: %s, %s: %s, %s: %s, %s: %s, %s: %s", ClientConfigurationEnvs.REQUEST_URL, urlToCertService, - ClientConfigurationEnvs.REQUEST_TIMEOUT, requestTimeout, + ClientConfigurationEnvs.REQUEST_TIMEOUT, requestTimeoutInMs, ClientConfigurationEnvs.OUTPUT_PATH, certsOutputPath, ClientConfigurationEnvs.CA_NAME, caName, ClientConfigurationEnvs.OUTPUT_TYPE, outputType); diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/CsrConfiguration.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/CsrConfiguration.java index c5aefd97..382d1ffc 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/CsrConfiguration.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/model/CsrConfiguration.java @@ -21,6 +21,7 @@ package org.onap.oom.certservice.client.configuration.model; +import java.util.List; import org.onap.oom.certservice.client.configuration.CsrConfigurationEnvs; public class CsrConfiguration implements ConfigurationModel { @@ -31,7 +32,7 @@ public class CsrConfiguration implements ConfigurationModel { private String country; private String organizationUnit; private String location; - private String sans; + private List<String> sans; public String getCommonName() { @@ -88,12 +89,12 @@ public class CsrConfiguration implements ConfigurationModel { return this; } - public String getSans() { + public List<String> getSans() { return sans; } - public CsrConfiguration setSubjectAlternativeNames(String subjectAlternativeNames) { - this.sans = subjectAlternativeNames; + public CsrConfiguration setSubjectAlternativeNames(List<String> subjectAlternativeNames) { + this.sans = List.copyOf(subjectAlternativeNames); return this; } diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/AbstractConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/BasicValidationFunctions.java index 293ac2d1..e5044c26 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/factory/AbstractConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/BasicValidationFunctions.java @@ -18,60 +18,40 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.certservice.client.configuration.factory; - - -import org.onap.oom.certservice.client.certification.ArtifactsCreatorProvider; -import org.onap.oom.certservice.client.configuration.exception.ClientConfigurationException; -import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException; -import org.onap.oom.certservice.client.configuration.model.ConfigurationModel; +package org.onap.oom.certservice.client.configuration.validation; import java.util.Arrays; import java.util.Locale; import java.util.regex.Pattern; -public abstract class AbstractConfigurationFactory<T extends ConfigurationModel> { +public class BasicValidationFunctions { - abstract T create() throws ClientConfigurationException, CsrConfigurationException; - - public boolean isPathValid(String path) { + public static boolean isPathValid(String path) { return path.matches("^/|(/[a-zA-Z0-9_-]+)+/?$"); } - public boolean isAlphaNumeric(String caName) { + public static boolean isAlphaNumeric(String caName) { return caName.matches("^[a-zA-Z0-9]*$"); } - public boolean isCommonNameValid(String commonName) { - return !isSpecialCharsPresent(commonName) - && !isHttpProtocolsPresent(commonName) - && !isIpAddressPresent(commonName) - && !isPortNumberPresent(commonName); - } - - public boolean isSpecialCharsPresent(String stringToCheck) { + public static boolean isSpecialCharPresent(String stringToCheck) { return Pattern.compile("[~#@*$+%!()?/{}<>\\|_^]").matcher(stringToCheck).find(); } - public boolean isCountryValid(String country) { - return Arrays.asList(Locale.getISOCountries()).contains(country); - } - - public boolean isOutputTypeValid(String outputType) { - return Arrays.stream(ArtifactsCreatorProvider.values()) - .map(ArtifactsCreatorProvider::toString) - .anyMatch(name -> name.equals(outputType)); - } - - private boolean isPortNumberPresent(String stringToCheck) { + public static boolean isPortNumberPresent(String stringToCheck) { return Pattern.compile(":[0-9]{1,5}").matcher(stringToCheck).find(); } - private boolean isIpAddressPresent(String stringToCheck) { + public static boolean isIpAddressPresent(String stringToCheck) { return Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}").matcher(stringToCheck).find(); } - private boolean isHttpProtocolsPresent(String stringToCheck) { + public static boolean isHttpProtocolsPresent(String stringToCheck) { return Pattern.compile("[h][t][t][p][:][/][/]|[h][t][t][p][s][:][/][/]").matcher(stringToCheck).find(); } + + public static boolean isCountryValid(String country) { + return Arrays.asList(Locale.getISOCountries()).contains(country); + } + } diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/ValidatorsFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/ValidatorsFactory.java new file mode 100644 index 00000000..771ce3f2 --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/configuration/validation/ValidatorsFactory.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * oom-certservice-client + * ================================================================================ + * 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.oom.certservice.client.configuration.validation; + +import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isHttpProtocolsPresent; +import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isIpAddressPresent; +import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isPortNumberPresent; +import static org.onap.oom.certservice.client.configuration.validation.BasicValidationFunctions.isSpecialCharPresent; + +import java.util.Arrays; +import java.util.function.Predicate; +import org.onap.oom.certservice.client.certification.ArtifactsCreatorProvider; + +public class ValidatorsFactory { + + public Predicate<String> commonNameValidator() { + return commonName -> + !isSpecialCharPresent(commonName) + && !isHttpProtocolsPresent(commonName) + && !isIpAddressPresent(commonName) + && !isPortNumberPresent(commonName); + } + + public Predicate<String> outputTypeValidator() { + return outputType -> Arrays.stream(ArtifactsCreatorProvider.values()) + .map(ArtifactsCreatorProvider::toString) + .anyMatch(name -> name.equals(outputType)); + } + +} |