forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor secrets cache to sdk client
Signed-off-by: Tanner Lewis <[email protected]>
- Loading branch information
Showing
4 changed files
with
34 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 16 additions & 12 deletions
28
...apture/trafficReplayer/src/main/java/org/opensearch/migrations/replay/AWSAuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,47 @@ | ||
package org.opensearch.migrations.replay; | ||
|
||
import com.amazonaws.secretsmanager.caching.SecretCache; | ||
import lombok.extern.slf4j.Slf4j; | ||
import software.amazon.awssdk.services.secretsmanager.SecretsManagerAsyncClient; | ||
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.Base64; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@Slf4j | ||
public class AWSAuthService implements AutoCloseable { | ||
|
||
private final SecretCache secretCache; | ||
private final SecretsManagerAsyncClient secretsManagerClient; | ||
|
||
public AWSAuthService(SecretCache secretCache) { | ||
this.secretCache = secretCache; | ||
public AWSAuthService(SecretsManagerAsyncClient secretsManagerClient) { | ||
this.secretsManagerClient = secretsManagerClient; | ||
} | ||
|
||
public AWSAuthService() { | ||
this(new SecretCache()); | ||
this(SecretsManagerAsyncClient.builder().build()); | ||
} | ||
|
||
// SecretId here can be either the unique name of the secret or the secret ARN | ||
public String getSecret(String secretId) { | ||
return secretCache.getSecretString(secretId); | ||
public CompletableFuture<GetSecretValueResponse> getSecret(String secretId) { | ||
return secretsManagerClient.getSecretValue(builder -> builder.secretId(secretId)); | ||
} | ||
|
||
/** | ||
* This method returns a Basic Auth header string, with the username:password Base64 encoded | ||
* This method synchronously returns a Basic Auth header string, with the username:password Base64 encoded | ||
* @param username The plaintext username | ||
* @param secretId The unique name of the secret or the secret ARN from AWS Secrets Manager. Its retrieved value | ||
* will fill the password part of the Basic Auth header | ||
* @return Basic Auth header string | ||
*/ | ||
public String getBasicAuthHeaderFromSecret(String username, String secretId) { | ||
String authHeaderString = username + ":" + getSecret(secretId); | ||
public String getBasicAuthHeaderFromSecret(String username, String secretId) throws ExecutionException, InterruptedException { | ||
String secretValue = getSecret(secretId).get().secretString(); | ||
String authHeaderString = username + ":" + secretValue; | ||
return "Basic " + Base64.getEncoder().encodeToString(authHeaderString.getBytes(Charset.defaultCharset())); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
secretCache.close(); | ||
secretsManagerClient.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 15 additions & 6 deletions
21
...re/trafficReplayer/src/test/java/org/opensearch/migrations/replay/AWSAuthServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
package org.opensearch.migrations.replay; | ||
|
||
import com.amazonaws.secretsmanager.caching.SecretCache; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import software.amazon.awssdk.services.secretsmanager.SecretsManagerAsyncClient; | ||
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.function.Consumer; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class AWSAuthServiceTest { | ||
|
||
@Mock | ||
private SecretCache secretCache; | ||
private SecretsManagerAsyncClient secretsManagerClient; | ||
|
||
@Test | ||
public void testBasicAuthHeaderFromSecret() { | ||
public void testBasicAuthHeaderFromSecret() throws ExecutionException, InterruptedException { | ||
String testSecretId = "testSecretId"; | ||
String testUsername = "testAdmin"; | ||
String expectedResult = "Basic dGVzdEFkbWluOmFkbWluUGFzcw=="; | ||
|
||
when(secretCache.getSecretString(testSecretId)).thenReturn("adminPass"); | ||
GetSecretValueResponse response = GetSecretValueResponse.builder().secretString("adminPass").build(); | ||
CompletableFuture<GetSecretValueResponse> responseFuture = CompletableFuture.completedFuture(response); | ||
|
||
when(secretsManagerClient.getSecretValue(any(Consumer.class))).thenReturn(responseFuture); | ||
|
||
AWSAuthService awsAuthService = new AWSAuthService(secretCache); | ||
AWSAuthService awsAuthService = new AWSAuthService(secretsManagerClient); | ||
String header = awsAuthService.getBasicAuthHeaderFromSecret(testUsername, testSecretId); | ||
Assertions.assertEquals(expectedResult, header); | ||
} | ||
|
||
|
||
} | ||
} |