FilePrivateKeyStore implements PrivateKeyStore { @Override public ServerPrivateKey getPrivateKey(String service, String serverHostName, String serverRegion, String algorithm) { if (!ZMS_SERVICE.equalsIgnoreCase(service) && !ZTS_SERVICE.equalsIgnoreCase(service)) { LOG.error("FilePrivateKeyStore: unknown service: {}", service); return null; } if (!ALGO_RSA.equalsIgnoreCase(algorithm) && !ALGO_EC.equalsIgnoreCase(algorithm)) { LOG.error("FilePrivateKeyStore: unknown algorithm: {}", algorithm); return null; } String privKeyName; String privKeyId; if (ALGO_RSA.equalsIgnoreCase(algorithm)) { privKeyName = System.getProperty(ATHENZ_PROP_PRIVATE_RSA_KEY); privKeyId = System.getProperty(ATHENZ_PROP_PRIVATE_RSA_KEY_ID, "0"); } else { privKeyName = System.getProperty(ATHENZ_PROP_PRIVATE_EC_KEY); privKeyId = System.getProperty(ATHENZ_PROP_PRIVATE_EC_KEY_ID, "0"); } if (LOG.isDebugEnabled()) { LOG.debug("FilePrivateKeyStore: private key file: {}, id: {}", privKeyName, privKeyId); } if (privKeyName == null) { return null; } File privKeyFile = new File(privKeyName); PrivateKey pkey = Crypto.loadPrivateKey(privKeyFile); ServerPrivateKey privateKey = null; if (pkey != null) { privateKey = new ServerPrivateKey(pkey, privKeyId); } return privateKey; } FilePrivateKeyStore(); @Override ServerPrivateKey getPrivateKey(String service, String serverHostName,
            String serverRegion, String algorithm); @Override PrivateKey getPrivateKey(String service, String serverHostName,
            StringBuilder privateKeyId); static final String ATHENZ_PROP_PRIVATE_KEY; static final String ATHENZ_PROP_PRIVATE_KEY_ID; static final String ATHENZ_PROP_PRIVATE_EC_KEY; static final String ATHENZ_PROP_PRIVATE_EC_KEY_ID; static final String ATHENZ_PROP_PRIVATE_RSA_KEY; static final String ATHENZ_PROP_PRIVATE_RSA_KEY_ID; }
@SuppressWarnings("deprecation") @Test public void testRetrievePrivateKeyValid() { FilePrivateKeyStoreFactory factory = new FilePrivateKeyStoreFactory(); PrivateKeyStore store = factory.create(); String saveProp = System.getProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY); System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY, "src/test/resources/unit_test_zts_private_k0.key"); StringBuilder keyId = new StringBuilder(256); PrivateKey privKey = store.getPrivateKey("zms", "localhost", keyId); assertNotNull(privKey); if (saveProp == null) { System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY); } else { System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY, saveProp); } }
@Test public void testRetrieveRSAPrivateKeyValid() { FilePrivateKeyStoreFactory factory = new FilePrivateKeyStoreFactory(); PrivateKeyStore store = factory.create(); String saveProp = System.getProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_RSA_KEY); System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_RSA_KEY, "src/test/resources/unit_test_zts_private_k0.key"); ServerPrivateKey privKey = store.getPrivateKey("zms", "localhost", "us-east-1", "rsa"); assertNotNull(privKey); if (saveProp == null) { System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_RSA_KEY); } else { System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_RSA_KEY, saveProp); } }
@Test public void testRetrieveECPrivateKeyValid() { FilePrivateKeyStoreFactory factory = new FilePrivateKeyStoreFactory(); PrivateKeyStore store = factory.create(); String saveProp = System.getProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY); System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY, "src/test/resources/unit_test_ec_private.key"); ServerPrivateKey privKey = store.getPrivateKey("zms", "localhost", "us-east-1", "ec"); assertNotNull(privKey); if (saveProp == null) { System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY); } else { System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY, saveProp); } }
@Test public void testRetrieveAlgoPrivateKeyInalid() { FilePrivateKeyStoreFactory factory = new FilePrivateKeyStoreFactory(); PrivateKeyStore store = factory.create(); String saveProp = System.getProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY); assertNull(store.getPrivateKey("app", "localhost", "us-east-1", "ec")); assertNull(store.getPrivateKey("zms", "localhost", "us-east-1", "unknown")); System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY); assertNull(store.getPrivateKey("zms", "localhost", "us-east-1", "ec")); System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY, "src/test/resources/ec_public_invalid.key"); assertNull(store.getPrivateKey("zms", "localhost", "us-east-1", "ec")); if (saveProp == null) { System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY); } else { System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_EC_KEY, saveProp); } }
@SuppressWarnings("deprecation") @Test public void testRetrievePrivateKeyInValid() { FilePrivateKeyStoreFactory factory = new FilePrivateKeyStoreFactory(); PrivateKeyStore store = factory.create(); String saveProp = System.getProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY); System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY, "src/test/resources/zts_private_k0_invalid.pem"); try { StringBuilder keyId = new StringBuilder(256); store.getPrivateKey("zts", "localhost", keyId); fail(); } catch (Exception ex) { assertTrue(true); } System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY); StringBuilder keyId = new StringBuilder(256); assertNull(store.getPrivateKey("zts", "localhost", keyId)); if (saveProp == null) { System.clearProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY); } else { System.setProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY, saveProp); } }