Skip to content

Commit

Permalink
Keeping the existing values in the secret. Just updating an existing key
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Gandhe <[email protected]>
  • Loading branch information
san81 committed Jan 14, 2025
1 parent 4df4fba commit a384d85
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ public GetSecretValueRequest createGetSecretValueRequest() {
.build();
}

public PutSecretValueRequest putSecretValueRequest(String keyToUpdate, Object newValue) {
String updatedSecretString = String.format("{\"%s\": \"%s\"}", keyToUpdate, newValue);
public PutSecretValueRequest putSecretValueRequest(String secretKeyValueMapAsString) {
return PutSecretValueRequest.builder()
.secretId(awsSecretId)
.secretString(updatedSecretString)
.secretString(secretKeyValueMapAsString)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ private Object retrieveSecretsFromSecretManager(final AwsSecretManagerConfigurat

@Override
public String updateValue(String secretId, String keyToUpdate, Object newValue) {
final Map<String, Object> keyValuePairs = (Map<String, Object>) secretIdToValue.get(secretId);
keyValuePairs.put(keyToUpdate, newValue);
String secretKeyValueMapAsString = (String) retrieveValue(secretId);
AwsSecretManagerConfiguration awsSecretManagerConfiguration = awsSecretManagerConfigurationMap.get(secretId);
PutSecretValueRequest putSecretValueRequest =
awsSecretManagerConfiguration.putSecretValueRequest(keyToUpdate, newValue);
awsSecretManagerConfiguration.putSecretValueRequest(secretKeyValueMapAsString);
SecretsManagerClient secretsManagerClient = secretsManagerClientMap.get(secretId);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void testCreatePutSecretValueRequest() throws IOException {
mockStatic(PutSecretValueRequest.class)) {
putSecretValueRequestMockedStatic.when(PutSecretValueRequest::builder).thenReturn(
putSecretValueRequestBuilder);
assertThat(awsSecretManagerConfiguration.putSecretValueRequest("keyToUpdate", "newValue"), is(putSecretValueRequest));
assertThat(awsSecretManagerConfiguration.putSecretValueRequest("{\"keyToUpdate\", \"newValue\"}"), is(putSecretValueRequest));
}
verify(putSecretValueRequestBuilder).secretId("test-secret");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void testRefreshSecretsWithoutKey() {

@Test
void testUpdateValue() {
when(awsSecretManagerConfiguration.putSecretValueRequest(any(), any())).thenReturn(putSecretValueRequest);
when(awsSecretManagerConfiguration.putSecretValueRequest(any())).thenReturn(putSecretValueRequest);
when(secretsManagerClient.putSecretValue(eq(putSecretValueRequest))).thenReturn(putSecretValueResponse);
final String testValue = "{\"key\":\"oldValue\"}";
when(secretValueDecoder.decode(eq(getSecretValueResponse))).thenReturn(testValue);
Expand All @@ -188,7 +188,7 @@ void testUpdateValue() {

@Test
void testUpdateValueFailed() {
when(awsSecretManagerConfiguration.putSecretValueRequest(any(), any())).thenReturn(putSecretValueRequest);
when(awsSecretManagerConfiguration.putSecretValueRequest(any())).thenReturn(putSecretValueRequest);
when(secretsManagerClient.putSecretValue(eq(putSecretValueRequest))).thenReturn(putSecretValueResponse);
final String testValue = "{\"key\":\"oldValue\"}";
when(secretValueDecoder.decode(eq(getSecretValueResponse))).thenReturn(testValue);
Expand Down

0 comments on commit a384d85

Please sign in to comment.