-
-
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.
Browse files
Browse the repository at this point in the history
* #245 Refactor & extract GCP secrets service from H2 data src * #245 Update build.gradle * #245 Update IronocDbConfigTest.java
- Loading branch information
1 parent
d7dff90
commit 5eea677
Showing
6 changed files
with
158 additions
and
43 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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.ironoc.db.service; | ||
|
||
public interface GoogleCloudClient { | ||
String getSecret(String secretVersion); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/ironoc/db/service/GoogleCloudClientImpl.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.ironoc.db.service; | ||
|
||
import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; | ||
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.Marker; | ||
import org.slf4j.MarkerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
public class GoogleCloudClientImpl implements GoogleCloudClient { | ||
|
||
@Override | ||
public String getSecret(String secretVersion) { | ||
String secretValue = ""; | ||
try { | ||
SecretManagerServiceClient client = SecretManagerServiceClient.create(); | ||
AccessSecretVersionResponse response = client.accessSecretVersion(secretVersion); | ||
secretValue = response.getPayload().getData().toStringUtf8(); | ||
log.info("Extracting GCP secret key={}", secretVersion); | ||
} catch (Exception e) { | ||
log.error("Unexpected error occurred extracting GCP secret.", e); | ||
} | ||
return secretValue; | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
src/test/java/com/ironoc/db/service/GoogleCloudClientServiceTest.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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.ironoc.db.service; | ||
|
||
import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; | ||
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; | ||
import com.google.cloud.secretmanager.v1.SecretPayload; | ||
import com.google.protobuf.ByteString; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.BDDMockito; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.emptyString; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.doThrow; | ||
|
||
@RunWith(MockitoJUnitRunner.Silent.class) | ||
public class GoogleCloudClientServiceTest { | ||
|
||
@InjectMocks | ||
private GoogleCloudClientImpl googleCloudClient; | ||
|
||
@Mock | ||
private AccessSecretVersionResponse accessSecretVersionResponseMock; | ||
|
||
@Mock | ||
private SecretPayload secretPayloadMock; | ||
|
||
@Mock | ||
private SecretManagerServiceClient secretManagerServiceClientMock; | ||
|
||
private MockedStatic<SecretManagerServiceClient> secretManagerServiceClientMockedStatic = | ||
mockStatic(SecretManagerServiceClient.class); | ||
|
||
private static final String TEST_SECRET_DATA = "TeSt_SeCr3t_V0l"; | ||
private static final String TEST_SECRET_KEY = "projects/123/secrets/<TEST_SECRET_NAME>/versions/33"; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
BDDMockito.given(SecretManagerServiceClient.create()).willReturn(secretManagerServiceClientMock); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
secretManagerServiceClientMockedStatic.close(); | ||
} | ||
|
||
@Test | ||
public void test_getSecret_success() { | ||
// given | ||
when(secretManagerServiceClientMock.accessSecretVersion(anyString())) | ||
.thenReturn(accessSecretVersionResponseMock); | ||
when(accessSecretVersionResponseMock.getPayload()).thenReturn(secretPayloadMock); | ||
when(secretPayloadMock.getData()).thenReturn(ByteString.copyFromUtf8(TEST_SECRET_DATA)); | ||
|
||
// when | ||
String result = googleCloudClient.getSecret(TEST_SECRET_KEY); | ||
|
||
// then | ||
secretManagerServiceClientMockedStatic.verify(() -> SecretManagerServiceClient.create()); | ||
verify(secretManagerServiceClientMock).accessSecretVersion(anyString()); | ||
verify(accessSecretVersionResponseMock).getPayload(); | ||
verify(secretPayloadMock).getData(); | ||
|
||
assertThat(result, is(TEST_SECRET_DATA)); | ||
} | ||
|
||
@Test | ||
public void test_getSecret_fail() { | ||
// given | ||
doThrow(new UnsupportedOperationException("test exception.")) | ||
.when(secretManagerServiceClientMock).accessSecretVersion(TEST_SECRET_KEY); | ||
|
||
// when | ||
String result = googleCloudClient.getSecret(TEST_SECRET_KEY); | ||
|
||
// then | ||
secretManagerServiceClientMockedStatic.verify(() -> SecretManagerServiceClient.create()); | ||
verify(secretManagerServiceClientMock).accessSecretVersion(anyString()); | ||
verify(accessSecretVersionResponseMock, never()).getPayload(); | ||
verify(secretPayloadMock, never()).getData(); | ||
|
||
assertThat(result, is(emptyString())); | ||
} | ||
} |