private KeyPair getOrInitKeyPair() { LOG.debug( "Check if key pair exists {}, {}", clientPublicKeyFileLocation, clientPrivateKeyFileLocation); if (keyPair != null) { return keyPair; } if (storage.exists(clientPublicKeyFileLocation) && storage.exists(clientPrivateKeyFileLocation)) { InputStream publicKeyInput = null; InputStream privateKeyInput = null; try { publicKeyInput = storage.openForRead(clientPublicKeyFileLocation); privateKeyInput = storage.openForRead(clientPrivateKeyFileLocation); PublicKey publicKey = KeyUtil.getPublic(publicKeyInput); PrivateKey privateKey = KeyUtil.getPrivate(privateKeyInput); if (publicKey != null && privateKey != null) { keyPair = new KeyPair(publicKey, privateKey); if (!KeyUtil.validateKeyPair(keyPair)) { throw new InvalidKeyException(); } } } catch (InvalidKeyException e) { keyPair = null; LOG.error("Unable to parse client RSA keypair. Generating new keys.. Reason {}", e); } catch (Exception e) { LOG.error("Error loading client RSA keypair. Reason {}", e); throw new RuntimeException(e); // NOSONAR } finally { IOUtils.closeQuietly(publicKeyInput); IOUtils.closeQuietly(privateKeyInput); } } if (keyPair == null) { LOG.debug("Generating Client Key pair"); OutputStream privateKeyOutput = null; OutputStream publicKeyOutput = null; try { privateKeyOutput = storage.openForWrite(clientPrivateKeyFileLocation); publicKeyOutput = storage.openForWrite(clientPublicKeyFileLocation); keyPair = KeyUtil.generateKeyPair(privateKeyOutput, publicKeyOutput); } catch (IOException e) { LOG.error("Error generating Client Key pair", e); throw new RuntimeException(e); } finally { IOUtils.closeQuietly(privateKeyOutput); IOUtils.closeQuietly(publicKeyOutput); } } return keyPair; }
@Override public void persist() { if (hasUpdate) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null); SpecificDatumWriter<Topic> datumWriter = new SpecificDatumWriter<>(Topic.class); try { for (Topic topic : topicMap.values()) { datumWriter.write(topic, encoder); LOG.info("Persisted {}", topic); } encoder.flush(); String base64Str = new String(base64.encodeBase64(baos.toByteArray()), Charset.forName("UTF-8")); state.setProperty(TOPIC_LIST, base64Str); } catch (IOException e) { LOG.error("Can't persist topic list info", e); } baos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(nfSubscriptions); String base64Str = new String(base64.encodeBase64(baos.toByteArray()), Charset.forName("UTF-8")); state.setProperty(NF_SUBSCRIPTIONS, base64Str); } catch (IOException e) { LOG.error("Can't persist notification subscription info", e); } StringBuilder attachedEndpointsString = new StringBuilder(); for (Map.Entry<EndpointAccessToken, EndpointKeyHash> attached : attachedEndpoints.entrySet()) { attachedEndpointsString .append(attached.getKey().getToken()) .append(":") .append(attached.getValue().getKeyHash()) .append(','); } state.setProperty(ATTACHED_ENDPOINTS, attachedEndpointsString.toString()); state.setProperty(EVENT_SEQ_NUM, "" + eventSequence.get()); if (topicListHash != null) { state.setProperty(TOPIC_LIST_HASH, "" + topicListHash); } OutputStream os = null; try { storage.renameTo(stateFileLocation, stateFileLocation + "_bckp"); os = storage.openForWrite(stateFileLocation); state.store(os, null); hasUpdate = false; } catch (IOException e) { LOG.error("Can't persist state file", e); } finally { IOUtils.closeQuietly(os); } } }