LunaEncryptionService extends InternalEncryptionService { @Override public KeyProxy createKeyProxy(final EncryptionKeyMetadata encryptionKeyMetadata) { return new LunaKeyProxy(createKey(encryptionKeyMetadata, lunaConnection), this); } LunaEncryptionService(
    final LunaConnection lunaConnection,
    @Value("${encryption.key_creation_enabled}") final
    boolean keyCreationEnabled,
    final TimedRetry timedRetry
  ); @Override SecureRandom getSecureRandom(); @Override CipherWrapper getCipher(); @Override EncryptedValue encrypt(final UUID canaryUuid, final Key key, final String value); @Override AlgorithmParameterSpec generateParameterSpec(final byte[] iv); @Override KeyProxy createKeyProxy(final EncryptionKeyMetadata encryptionKeyMetadata); @Override synchronized void reconnect(final Exception reasonForReconnect); boolean isLoggedIn(); static final int KEY_POPULATION_WAIT_SEC; }
@Test public void createKeyProxy_createsKeyIfNoKeyExists() throws Exception { setupNoKeyExists(); final EncryptionKeyMetadata keyMetadata = new EncryptionKeyMetadata(); keyMetadata.setEncryptionKeyName("fake_key_name"); subject = new LunaEncryptionService(connection, true, timedRetry); assertThat(subject.createKeyProxy(keyMetadata).getKey(), equalTo(aesKey)); verify(connection).setKeyEntry("fake_key_name", aesKey); }
@Test public void createKeyProxy_getsKeyIfKeyExists() throws Exception { setupKeyExists(); final EncryptionKeyMetadata keyMetadata = new EncryptionKeyMetadata(); keyMetadata.setEncryptionKeyName("fake_key_name"); subject = new LunaEncryptionService(connection, true, timedRetry); assertThat(subject.createKeyProxy(keyMetadata).getKey(), equalTo(aesKey)); verify(connection, never()).setKeyEntry("fake_key_name", aesKey); }
@Test public void createKeyProxy_waitsForKeyIfCreationIsDisabled() throws Exception { setupAnotherProcessCreatesKey(); final EncryptionKeyMetadata keyMetadata = new EncryptionKeyMetadata(); keyMetadata.setEncryptionKeyName("fake_key_name"); subject = new LunaEncryptionService(connection, false, timedRetry); assertThat(subject.createKeyProxy(keyMetadata).getKey(), equalTo(aesKey)); verify(connection, never()).generateKey(); }