Skip to content

Commit

Permalink
fix: read and write secrets to the runtime config (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
saranyailla authored Nov 10, 2022
1 parent 18ae4aa commit 8a11ab2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import javax.inject.Inject;

import static com.aws.greengrass.lifecyclemanager.GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC;
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC;

/**
Expand Down Expand Up @@ -50,7 +51,8 @@ public FileSecretDao(KernelClient kernelClient) throws SecretManagerException {
*/
public synchronized SecretDocument getAll() throws SecretManagerException {
Topic secretResponseTopic = kernelClient.getConfig().lookup(SERVICES_NAMESPACE_TOPIC,
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, SECRET_RESPONSE_TOPIC);
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, RUNTIME_STORE_NAMESPACE_TOPIC,
SECRET_RESPONSE_TOPIC);
if (secretResponseTopic.getOnce() == null) {
throw new NoSecretFoundException("No secrets found in file");
}
Expand Down Expand Up @@ -92,7 +94,8 @@ public synchronized AWSSecretResponse get(String secretArn, String label) throws
*/
public synchronized void saveAll(SecretDocument doc) throws SecretManagerException {
Topic secretTopic = kernelClient.getConfig().lookup(SERVICES_NAMESPACE_TOPIC,
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, SECRET_RESPONSE_TOPIC);
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, RUNTIME_STORE_NAMESPACE_TOPIC,
SECRET_RESPONSE_TOPIC);
try {
secretTopic.withValue(OBJECT_MAPPER.writeValueAsString(doc));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.UUID;

import static com.aws.greengrass.lifecyclemanager.GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC;
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC;
import static com.aws.greengrass.secretmanager.FileSecretDao.SECRET_RESPONSE_TOPIC;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -105,7 +106,8 @@ void GIVEN_dao_store_WHEN_secrets_saved_THEN_get_returns_them() throws SecretMan
FileSecretDao dao = new FileSecretDao(mockKernelClient);
Topic mockTopic = mock(Topic.class);
when(mockConfiguration.lookup(SERVICES_NAMESPACE_TOPIC,
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, SECRET_RESPONSE_TOPIC)).thenReturn(mockTopic);
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, RUNTIME_STORE_NAMESPACE_TOPIC,
SECRET_RESPONSE_TOPIC)).thenReturn(mockTopic);


List<AWSSecretResponse> response = getSecrets();
Expand Down Expand Up @@ -196,7 +198,8 @@ void GIVEN_dao_store_WHEN_no_secret_saved_THEN_get_throws_exception() throws Sec
FileSecretDao dao = new FileSecretDao(mockKernelClient);
Topic mockTopic = mock(Topic.class);
when(mockConfiguration.lookup(SERVICES_NAMESPACE_TOPIC,
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, SECRET_RESPONSE_TOPIC)).thenReturn(mockTopic);
SecretManagerService.SECRET_MANAGER_SERVICE_NAME, RUNTIME_STORE_NAMESPACE_TOPIC,
SECRET_RESPONSE_TOPIC)).thenReturn(mockTopic);
when(mockTopic.getOnce()).thenReturn(null);

assertThrows(NoSecretFoundException.class, () -> dao.getAll());
Expand All @@ -208,7 +211,7 @@ void GIVEN_dao_store_WHEN_objectmapper_error_THEN_throws() throws SecretManagerE
FileSecretDao dao = new FileSecretDao(mockKernelClient);
Topic mockTopic = mock(Topic.class);
when(mockConfiguration.lookup(SERVICES_NAMESPACE_TOPIC, SecretManagerService.SECRET_MANAGER_SERVICE_NAME,
SECRET_RESPONSE_TOPIC)).thenReturn(mockTopic);
RUNTIME_STORE_NAMESPACE_TOPIC, SECRET_RESPONSE_TOPIC)).thenReturn(mockTopic);
// Make readValue() throw JsonProcessingException
when(Coerce.toString(mockTopic)).thenReturn(mockTopic.getClass().getName());
assertThrows(SecretManagerException.class, () -> dao.getAll());
Expand Down

0 comments on commit 8a11ab2

Please sign in to comment.