@Test public void getSecretThrowsExceptionIfNoDataRetrievedFromVault() { HashicorpGetSecretData getSecretData = new HashicorpGetSecretData("engine", "secretName", "id", 0); Versioned versionedResponse = mock(Versioned.class); when(versionedResponse.hasData()).thenReturn(false); when(delegate.get(getSecretData)).thenReturn(versionedResponse); Throwable ex = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(ex).isExactlyInstanceOf(HashicorpVaultException.class); assertThat(ex).hasMessage("No data found at engine/secretName"); }
@Override public String getSecret(HashicorpGetSecretData hashicorpGetSecretData) { KeyValueOperationsDelegate keyValueOperationsDelegate = keyValueOperationsDelegateFactory.create(hashicorpGetSecretData.getSecretEngineName()); Versioned<Map<String, Object>> versionedResponse = keyValueOperationsDelegate.get(hashicorpGetSecretData); if (versionedResponse == null || !versionedResponse.hasData()) { throw new HashicorpVaultException("No data found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } if (!versionedResponse.getData().containsKey(hashicorpGetSecretData.getValueId())) { throw new HashicorpVaultException("No value with id " + hashicorpGetSecretData.getValueId() + " found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } return versionedResponse.getData().get(hashicorpGetSecretData.getValueId()).toString(); }
HashicorpKeyVaultService implements KeyVaultService<HashicorpSetSecretData, HashicorpGetSecretData> { @Override public String getSecret(HashicorpGetSecretData hashicorpGetSecretData) { KeyValueOperationsDelegate keyValueOperationsDelegate = keyValueOperationsDelegateFactory.create(hashicorpGetSecretData.getSecretEngineName()); Versioned<Map<String, Object>> versionedResponse = keyValueOperationsDelegate.get(hashicorpGetSecretData); if (versionedResponse == null || !versionedResponse.hasData()) { throw new HashicorpVaultException("No data found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } if (!versionedResponse.getData().containsKey(hashicorpGetSecretData.getValueId())) { throw new HashicorpVaultException("No value with id " + hashicorpGetSecretData.getValueId() + " found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } return versionedResponse.getData().get(hashicorpGetSecretData.getValueId()).toString(); } }
HashicorpKeyVaultService implements KeyVaultService<HashicorpSetSecretData, HashicorpGetSecretData> { @Override public String getSecret(HashicorpGetSecretData hashicorpGetSecretData) { KeyValueOperationsDelegate keyValueOperationsDelegate = keyValueOperationsDelegateFactory.create(hashicorpGetSecretData.getSecretEngineName()); Versioned<Map<String, Object>> versionedResponse = keyValueOperationsDelegate.get(hashicorpGetSecretData); if (versionedResponse == null || !versionedResponse.hasData()) { throw new HashicorpVaultException("No data found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } if (!versionedResponse.getData().containsKey(hashicorpGetSecretData.getValueId())) { throw new HashicorpVaultException("No value with id " + hashicorpGetSecretData.getValueId() + " found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } return versionedResponse.getData().get(hashicorpGetSecretData.getValueId()).toString(); } HashicorpKeyVaultService(KeyValueOperationsDelegateFactory keyValueOperationsDelegateFactory); }
HashicorpKeyVaultService implements KeyVaultService<HashicorpSetSecretData, HashicorpGetSecretData> { @Override public String getSecret(HashicorpGetSecretData hashicorpGetSecretData) { KeyValueOperationsDelegate keyValueOperationsDelegate = keyValueOperationsDelegateFactory.create(hashicorpGetSecretData.getSecretEngineName()); Versioned<Map<String, Object>> versionedResponse = keyValueOperationsDelegate.get(hashicorpGetSecretData); if (versionedResponse == null || !versionedResponse.hasData()) { throw new HashicorpVaultException("No data found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } if (!versionedResponse.getData().containsKey(hashicorpGetSecretData.getValueId())) { throw new HashicorpVaultException("No value with id " + hashicorpGetSecretData.getValueId() + " found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } return versionedResponse.getData().get(hashicorpGetSecretData.getValueId()).toString(); } HashicorpKeyVaultService(KeyValueOperationsDelegateFactory keyValueOperationsDelegateFactory); @Override String getSecret(HashicorpGetSecretData hashicorpGetSecretData); @Override Object setSecret(HashicorpSetSecretData hashicorpSetSecretData); }
HashicorpKeyVaultService implements KeyVaultService<HashicorpSetSecretData, HashicorpGetSecretData> { @Override public String getSecret(HashicorpGetSecretData hashicorpGetSecretData) { KeyValueOperationsDelegate keyValueOperationsDelegate = keyValueOperationsDelegateFactory.create(hashicorpGetSecretData.getSecretEngineName()); Versioned<Map<String, Object>> versionedResponse = keyValueOperationsDelegate.get(hashicorpGetSecretData); if (versionedResponse == null || !versionedResponse.hasData()) { throw new HashicorpVaultException("No data found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } if (!versionedResponse.getData().containsKey(hashicorpGetSecretData.getValueId())) { throw new HashicorpVaultException("No value with id " + hashicorpGetSecretData.getValueId() + " found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } return versionedResponse.getData().get(hashicorpGetSecretData.getValueId()).toString(); } HashicorpKeyVaultService(KeyValueOperationsDelegateFactory keyValueOperationsDelegateFactory); @Override String getSecret(HashicorpGetSecretData hashicorpGetSecretData); @Override Object setSecret(HashicorpSetSecretData hashicorpSetSecretData);  }