KeyStoreCertSignerFactory implements CertSignerFactory { @Override public CertSigner create() { final String keyStorePassword = System.getProperty(ZTS_PROP_CERT_SIGNER_KEYSTORE_PASSWORD); final String keyStorePath = System.getProperty(ZTS_PROP_CERT_SIGNER_KEYSTORE_PATH); final String keyStoreType = System.getProperty(ZTS_PROP_CERT_SIGNER_KEYSTORE_TYPE, "PKCS12"); final String caAlias = System.getProperty(ZTS_PROP_CERT_SIGNER_CA_ALIAS, "1"); final int maxCertExpiryTimeMins = Integer.parseInt(System.getProperty(ZTSConsts.ZTS_PROP_CERTSIGN_MAX_EXPIRY_TIME, "43200")); if (keyStorePassword == null || keyStorePassword.isEmpty()) { throw CREATE_ILLEGAL_ARGUMENT_EXCEPTION.apply("keyStorePassword", ZTS_PROP_CERT_SIGNER_KEYSTORE_PASSWORD); } if (keyStorePath == null || keyStorePath.isEmpty()) { throw CREATE_ILLEGAL_ARGUMENT_EXCEPTION.apply("keyStorePath", ZTS_PROP_CERT_SIGNER_KEYSTORE_PATH); } try (FileInputStream fis = new FileInputStream(keyStorePath)) { KeyStore ks = KeyStore.getInstance(keyStoreType); ks.load(fis, keyStorePassword.toCharArray()); PrivateKey caPrivateKey = (PrivateKey) ks.getKey(caAlias, keyStorePassword.toCharArray()); X509Certificate caCertificate = (X509Certificate) ks.getCertificate(caAlias); if (caPrivateKey == null || caCertificate == null) { throw CREATE_ILLEGAL_ARGUMENT_EXCEPTION.apply("caPrivateKey/caCertificate", ZTS_PROP_CERT_SIGNER_CA_ALIAS); } return new KeyStoreCertSigner(caCertificate, caPrivateKey, maxCertExpiryTimeMins); } catch (Exception ex) { throw new RuntimeException(ex); } }  @Override CertSigner create();  }
@Test(expectedExceptions = { IllegalArgumentException.class }, expectedExceptionsMessageRegExp = "Failed to get keyStorePassword from athenz.zts.keystore_signer.keystore_password property.") public void testCreateEmptyPath() { final BooleanSupplier sysPropRestoreFunc = this.getSysPropRestoreLambda( "athenz.zts.keystore_signer.keystore_password" ); System.clearProperty("athenz.zts.keystore_signer.keystore_password"); try (KeyStoreCertSigner keyStoreCertSigner = (KeyStoreCertSigner) new KeyStoreCertSignerFactory().create()) { } finally { sysPropRestoreFunc.getAsBoolean(); } }
@Test(expectedExceptions = { IllegalArgumentException.class }, expectedExceptionsMessageRegExp = "Failed to get keyStorePath from athenz.zts.keystore_signer.keystore_path property.") public void testCreateEmptyPassword() { final BooleanSupplier sysPropRestoreFunc = this.getSysPropRestoreLambda( "athenz.zts.keystore_signer.keystore_password", "athenz.zts.keystore_signer.keystore_path" ); System.setProperty("athenz.zts.keystore_signer.keystore_password", "dummy"); System.clearProperty("athenz.zts.keystore_signer.keystore_path"); try (KeyStoreCertSigner keyStoreCertSigner = (KeyStoreCertSigner) new KeyStoreCertSignerFactory().create()) { } finally { sysPropRestoreFunc.getAsBoolean(); } }
@Test(expectedExceptions = { RuntimeException.class, FileNotFoundException.class }, expectedExceptionsMessageRegExp = ".*/keystore.pkcs12.not_exist \\(No such file or directory\\)") public void testCreateFileNotFound() { final BooleanSupplier sysPropRestoreFunc = this.getSysPropRestoreLambda( "athenz.zts.keystore_signer.keystore_password", "athenz.zts.keystore_signer.keystore_path" ); String filePath = Resources.getResource("keystore.pkcs12").getFile(); System.setProperty("athenz.zts.keystore_signer.keystore_password", "dummy"); System.setProperty("athenz.zts.keystore_signer.keystore_path", filePath + ".not_exist"); try (KeyStoreCertSigner keyStoreCertSigner = (KeyStoreCertSigner) new KeyStoreCertSignerFactory().create()) { } finally { sysPropRestoreFunc.getAsBoolean(); } }
@Test(expectedExceptions = { RuntimeException.class, IOException.class }, expectedExceptionsMessageRegExp = ".* keystore password was incorrect") public void testCreateWrongPassword() { final BooleanSupplier sysPropRestoreFunc = this.getSysPropRestoreLambda( "athenz.zts.keystore_signer.keystore_password", "athenz.zts.keystore_signer.keystore_path" ); String filePath = Resources.getResource("keystore.pkcs12").getFile(); System.setProperty("athenz.zts.keystore_signer.keystore_password", "dummy"); System.setProperty("athenz.zts.keystore_signer.keystore_path", filePath); try (KeyStoreCertSigner keyStoreCertSigner = (KeyStoreCertSigner) new KeyStoreCertSignerFactory().create()) { } finally { sysPropRestoreFunc.getAsBoolean(); } }
@Test(expectedExceptions = { RuntimeException.class, IllegalArgumentException.class }, expectedExceptionsMessageRegExp = ".* Failed to get caPrivateKey/caCertificate from athenz.zts.keystore_signer.keystore_ca_alias property.") public void testCreateAliasNotFound() { final BooleanSupplier sysPropRestoreFunc = this.getSysPropRestoreLambda( "athenz.zts.keystore_signer.keystore_password", "athenz.zts.keystore_signer.keystore_path", "athenz.zts.keystore_signer.keystore_ca_alias" ); String filePath = Resources.getResource("keystore.pkcs12").getFile(); System.setProperty("athenz.zts.keystore_signer.keystore_password", "123456"); System.setProperty("athenz.zts.keystore_signer.keystore_path", filePath); System.setProperty("athenz.zts.keystore_signer.keystore_ca_alias", "dummy"); try (KeyStoreCertSigner keyStoreCertSigner = (KeyStoreCertSigner) new KeyStoreCertSignerFactory().create()) { } finally { sysPropRestoreFunc.getAsBoolean(); } }
@Test public void testCreate() { final BooleanSupplier sysPropRestoreFunc = this.getSysPropRestoreLambda( "athenz.zts.keystore_signer.keystore_password", "athenz.zts.keystore_signer.keystore_path", "athenz.zts.keystore_signer.keystore_type", "athenz.zts.keystore_signer.keystore_ca_alias", ZTSConsts.ZTS_PROP_CERTSIGN_MAX_EXPIRY_TIME ); String filePath = Resources.getResource("keystore.pkcs12").getFile(); System.setProperty("athenz.zts.keystore_signer.keystore_password", "123456"); System.setProperty("athenz.zts.keystore_signer.keystore_path", filePath); System.setProperty("athenz.zts.keystore_signer.keystore_type", "JKS"); System.setProperty("athenz.zts.keystore_signer.keystore_ca_alias", "unit"); System.setProperty(ZTSConsts.ZTS_PROP_CERTSIGN_MAX_EXPIRY_TIME, "36000"); try (KeyStoreCertSigner keyStoreCertSigner = (KeyStoreCertSigner) new KeyStoreCertSignerFactory().create()) { Function<String, Object> getFieldValue = (String fieldName) -> { try { Field field = keyStoreCertSigner.getClass().getDeclaredField(fieldName); field.setAccessible(true); return field.get(keyStoreCertSigner); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { return null; } }; Assert.assertEquals(((X509Certificate) getFieldValue.apply("caCertificate")).getIssuerX500Principal().getName(), "CN=unit.test.athenz,OU=Athenz,O=Oath,L=PlayaVista,ST=CA,C=US"); Assert.assertNotNull(getFieldValue.apply("caPrivateKey")); Assert.assertEquals(getFieldValue.apply("maxCertExpiryTimeMins"), 36000); } finally { sysPropRestoreFunc.getAsBoolean(); } }