Skip to content

Commit

Permalink
[#590] Implement the check for existing file also for the trust store
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Apr 23, 2018
1 parent 75a45d8 commit c418256
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
18 changes: 12 additions & 6 deletions core/src/main/java/org/eclipse/hono/config/AbstractConfig.java
Expand Up @@ -127,14 +127,20 @@ public final void setTrustStorePassword(final String trustStorePassword) {
* Gets the trust options derived from the trust store properties.
*
* @return The trust options or {@code null} if trust store path is not set or not supported.
* @throws IllegalArgumentException In the case the configured trust store is not present in the file system.
*/
public final TrustOptions getTrustOptions() {

if (trustStorePath == null) {
if (this.trustStorePath == null) {
return null;
}

final FileFormat format = FileFormat.orDetect(trustStoreFormat, trustStorePath);
if (!Files.exists(Paths.get(this.trustStorePath))) {
throw new IllegalArgumentException(
String.format("Configured trust store file does not exist: %s", this.trustStorePath));
}

final FileFormat format = FileFormat.orDetect(this.trustStoreFormat, this.trustStorePath);

if (format == null) {
LOG.debug("unsupported trust store format");
Expand All @@ -143,15 +149,15 @@ public final TrustOptions getTrustOptions() {

switch (format) {
case PEM:
LOG.debug("using certificates from file [{}] as trust anchor", trustStorePath);
return new PemTrustOptions().addCertPath(trustStorePath);
LOG.debug("using certificates from file [{}] as trust anchor", this.trustStorePath);
return new PemTrustOptions().addCertPath(this.trustStorePath);
case PKCS12:
LOG.debug("using certificates from PKCS12 key store [{}] as trust anchor", trustStorePath);
LOG.debug("using certificates from PKCS12 key store [{}] as trust anchor", this.trustStorePath);
return new PfxOptions()
.setPath(getTrustStorePath())
.setPassword(getTrustStorePassword());
case JKS:
LOG.debug("using certificates from JKS key store [{}] as trust anchor", trustStorePath);
LOG.debug("using certificates from JKS key store [{}] as trust anchor", this.trustStorePath);
return new JksOptions()
.setPath(getTrustStorePath())
.setPassword(getTrustStorePassword());
Expand Down
Expand Up @@ -128,4 +128,13 @@ public void testMissingFile3() {

cfg.getKeyCertOptions();
}

/**
* Specify a non existing trust store file.
*/
@Test(expected=IllegalArgumentException.class)
public void testMissingFile4() {
cfg.setTrustStorePath(PREFIX_KEY_PATH + "doest-not-exist");
cfg.getTrustOptions();
}
}

0 comments on commit c418256

Please sign in to comment.