Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Stoia committed May 6, 2021
2 parents 1d10ab1 + 333abc0 commit 24af7c9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Once you add the AIS client library as a dependency to your project, you can con
RestClientConfiguration restConfig = RestClientConfiguration.builder()
.withServiceSignUrl("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign")
.withServicePendingUrl("https://ais.swisscom.com/AIS-Server/rs/v1.0/pending")
// the server certificate file is optional, in case it is omitted the CA must be a trusted one
.withServerCertificateFile("/home/user/ais-server.crt")
.withClientKeyFile("/home/user/ais-client.key")
.withClientKeyPassword("secret")
Expand Down
4 changes: 2 additions & 2 deletions docs/configure-the-AIS-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license.file=${ITEXT_LICENSE_FILE_PATH}
server.rest.signUrl=https://ais.swisscom.com/AIS-Server/rs/v1.0/sign
# The AIS server REST URL for sending the Signature status poll requests (Pending requests)
server.rest.pendingUrl=https://ais.swisscom.com/AIS-Server/rs/v1.0/pending
# The AIS server trusted CA certificate file
# The AIS server trusted CA certificate file. The configuration parameter is optional and in case it is omitted the CA must be a trusted one.
server.cert.file=/home/user/ais-server.crt
# --
# The client's private key file (corresponding to the public key attached to the client's certificate)
Expand Down Expand Up @@ -192,7 +192,7 @@ swisscom:
rest.signUrl: https://ais.swisscom.com/AIS-Server/rs/v1.0/sign
# The AIS server REST URL for sending the Signature status poll requests (Pending requests)
rest.pendingUrl: https://ais.swisscom.com/AIS-Server/rs/v1.0/pending
# The AIS server trusted CA certificate file
# The AIS server trusted CA certificate file. The configuration parameter is optional and in case it is omitted the CA must be a trusted one.
cert.file: /home/user/ais-server.crt
client:
# The client's private key file (corresponding to the public key attached to the client's certificate)
Expand Down
1 change: 1 addition & 0 deletions docs/use-the-AIS-client-programmatically.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Configure the REST client:
RestClientConfiguration restConfig = RestClientConfiguration.builder()
.withServiceSignUrl("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign")
.withServicePendingUrl("https://ais.swisscom.com/AIS-Server/rs/v1.0/pending")
// the server certificate file is optional, in case it is omitted the CA must be a trusted one
.withServerCertificateFile("/home/user/ais-server.crt")
.withClientKeyFile("/home/user/ais-client.key")
.withClientKeyPassword("secret")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void main(String[] args) throws IOException {
RestClientConfiguration restConfig = RestClientConfiguration.builder()
.withServiceSignUrl("https://ais.swisscom.com/AIS-Server/rs/v1.0/sign")
.withServicePendingUrl("https://ais.swisscom.com/AIS-Server/rs/v1.0/pending")
// the server certificate file is optional, in case it is omitted the CA must be a trusted one
.withServerCertificateFile("/home/user/ais-server.crt")
.withClientKeyFile("/home/user/ais-client.key")
.withClientKeyPassword("secret")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Function;

@SuppressWarnings("unused")
public abstract class PropertiesLoader<T> {

private static final String ENV_VARIABLE_PREFIX = "${";
Expand All @@ -34,12 +34,6 @@ public abstract class PropertiesLoader<T> {
protected PropertiesLoader() {
}

private static void validateProperty(String propertyName, String propertyValue) {
if (StringUtils.isBlank(propertyValue)) {
throw new IllegalStateException(String.format("Invalid configuration. The [%s] property is missing or is empty.", propertyName));
}
}

protected abstract T fromConfigurationProvider(ConfigurationProvider provider);

public T fromPropertiesClasspathFile(String fileName) {
Expand All @@ -64,24 +58,18 @@ public Properties loadPropertiesFromClasspathFile(Class<?> clazz, String filepat
}
}

public String extractStringProperty(ConfigurationProvider provider, String propertyName, boolean mandatory) {
return extractProperty(provider, propertyName, mandatory);
public String extractStringProperty(ConfigurationProvider provider, String propertyName, boolean isMandatory) {
return extractProperty(provider, propertyName, isMandatory).orElse(null);
}

public Integer extractIntProperty(ConfigurationProvider provider, String propertyName, boolean mandatory) {
String property = extractProperty(provider, propertyName, mandatory);
if (property == null && !mandatory) {
return null;
}
return Integer.parseInt(property);
public Integer extractIntProperty(ConfigurationProvider provider, String propertyName, boolean isMandatory) {
Optional<String> property = extractProperty(provider, propertyName, isMandatory);
return property.isPresent() ? Integer.parseInt(property.get()) : null;
}

public String extractSecretProperty(ConfigurationProvider provider, String propertyName, boolean mandatory) {
String property = extractProperty(provider, propertyName, mandatory);
if (property == null && !mandatory) {
return null;
}
return shouldExtractFromEnvVariable(property) ? System.getenv(extractEnvPropertyName(property)) : property;
public String extractSecretProperty(ConfigurationProvider provider, String propertyName, boolean isMandatory) {
Optional<String> property = extractProperty(provider, propertyName, isMandatory);
return property.isPresent() && shouldExtractFromEnvVariable(property.get()) ? System.getenv(extractEnvPropertyName(property.get())) : null;
}

private boolean shouldExtractFromEnvVariable(String property) {
Expand All @@ -97,12 +85,14 @@ public <E> E extractProperty(ConfigurationProvider provider, String propertyName
return StringUtils.isNotBlank(propertyValue) ? mapperFunction.apply(propertyValue) : defaultValue;
}

private String extractProperty(ConfigurationProvider provider, String propertyName, boolean mandatory) {
private Optional<String> extractProperty(ConfigurationProvider provider, String propertyName, boolean isMandatory) {
String propertyValue = provider.getProperty(propertyName);
if (StringUtils.isBlank(propertyValue) && !mandatory) {
return null;
if (StringUtils.isBlank(propertyValue) && !isMandatory) {
return Optional.empty();
}
if (StringUtils.isBlank(propertyValue)) {
throw new IllegalStateException(String.format("Invalid configuration. The [%s] property is missing or is empty.", propertyName));
}
validateProperty(propertyName, propertyValue);
return propertyValue;
return Optional.of(propertyValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected RestClientConfiguration.Builder fromConfigurationProvider(Configuratio
.withClientKeyFile(extractStringProperty(provider, "client.auth.keyFile", true))
.withClientKeyPassword(extractSecretProperty(provider, "client.auth.keyPassword", false))
.withClientCertificateFile(extractStringProperty(provider, "client.cert.file", true))
.withServerCertificateFile(extractStringProperty(provider, "server.cert.file", true))
.withServerCertificateFile(extractStringProperty(provider, "server.cert.file", false))
.withMaxTotalConnections(extractIntProperty(provider, "client.http.maxTotalConnections", true))
.withMaxConnectionsPerRoute(extractIntProperty(provider, "client.http.maxConnectionsPerRoute", true))
.withConnectionTimeoutInSec(extractIntProperty(provider, "client.http.connectionTimeoutInSeconds", true))
Expand All @@ -122,7 +122,6 @@ private void validate() {
ValidationUtils.notBlank(servicePendingUrl, "The servicePendingUrl parameter of the REST client configuration must not be empty");
ValidationUtils.notBlank(clientKeyFile, "The clientKeyFile parameter of the REST client configuration must not be empty");
ValidationUtils.notBlank(clientCertificateFile, "The clientCertificateFile parameter of the REST client configuration must not be empty");
ValidationUtils.notBlank(serverCertificateFile, "The serverCertificateFile parameter of the REST client configuration must not be empty");
ValidationUtils.isPositive(maxTotalConnections, "The maxTotalConnections parameter of the REST client configuration must not be empty");
ValidationUtils.isPositive(maxConnectionsPerRoute, "The maxConnectionsPerRoute parameter of the REST client configuration must not be empty");
ValidationUtils.isPositive(connectionTimeoutInSec, "The connectionTimeoutInSec parameter of the REST client configuration must not be empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public SignatureRestClientImpl withConfiguration(RestClientConfiguration config)
try {
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadKeyMaterial(produceTheKeyStore(config), keyToCharArray(config.getClientKeyPassword()), producePrivateKeyStrategy());
sslContextBuilder.loadTrustMaterial(produceTheTrustStore(config), null);
if (StringUtils.isNotBlank(config.getServerCertificateFile())) {
sslContextBuilder.loadTrustMaterial(produceTheTrustStore(config.getServerCertificateFile()), null);
}
sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
} catch (Exception e) {
throw new AisClientException("Failed to configure the TLS/SSL connection factory for the AIS client", e);
Expand Down Expand Up @@ -231,8 +233,8 @@ private KeyStore produceTheKeyStore(RestClientConfiguration config) {
}
}

private KeyStore produceTheTrustStore(RestClientConfiguration config) {
try (FileInputStream is = new FileInputStream(config.getServerCertificateFile())) {
private KeyStore produceTheTrustStore(String serverCertificateFile) {
try (FileInputStream is = new FileInputStream(serverCertificateFile)) {
CertificateFactory fact = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) fact.generateCertificate(is);

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/cli/sign-pdf-help.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license.file=${ITEXT_LICENSE_FILE_PATH}
server.rest.signUrl=https://ais.swisscom.com/AIS-Server/rs/v1.0/sign
# The AIS server REST URL for sending the Signature status poll requests (Pending requests)
server.rest.pendingUrl=https://ais.swisscom.com/AIS-Server/rs/v1.0/pending
# The AIS server trusted CA certificate file
# The AIS server trusted CA certificate file. The configuration parameter is optional and in case it is omitted the CA must be a trusted one.
server.cert.file=/home/user/ais-server.crt
# --
# The client's private key file (corresponding to the public key attached to the client's certificate)
Expand Down

0 comments on commit 24af7c9

Please sign in to comment.