Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add secret manager module and sample it tests to native image CI #2206

Merged
merged 8 commits into from
Oct 9, 2023
2 changes: 2 additions & 0 deletions .github/workflows/NativeTests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ jobs:
- vision
- spanner
- storage
- secretmanager
- logging-sample
- storage-sample
- trace-sample
- vision-sample
- secretmanager-sample
steps:
- name: Get current date
id: date
Expand Down
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@
<module>spring-cloud-gcp-storage</module>
<module>spring-cloud-gcp-vision</module>
<module>spring-cloud-gcp-data-spanner</module>
<module>spring-cloud-gcp-secretmanager</module>
</modules>

<dependencies>
Expand All @@ -381,9 +382,10 @@
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<systemPropertyVariables>
<!--integration tests are not invoked unless the relevant system property is set to true here. -->
<it.storage>true</it.storage>
<it.storage>true</it.storage>
<it.vision>true</it.vision>
<it.spanner>true</it.spanner>
<it.secretmanager>true</it.secretmanager>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
4 changes: 3 additions & 1 deletion spring-cloud-gcp-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<module>spring-cloud-gcp-integration-storage-sample</module>
<module>spring-cloud-gcp-trace-sample</module>
<module>spring-cloud-gcp-vision-api-sample</module>
<module>spring-cloud-gcp-secretmanager-sample</module>
<module>spring-cloud-gcp-vision-ocr-demo</module>
</modules>
<build>
Expand Down Expand Up @@ -138,9 +139,10 @@
</includes>
<systemPropertyVariables>
<it.logging>true</it.logging>
<it.storage>true</it.storage>
<it.vision>true</it.vision>
<it.trace>true</it.trace>
<it.storage>true</it.storage>
<it.secretmanager>true</it.secretmanager>
<gcs-resource-test-bucket>gcp-storage-resource-bucket-sample</gcs-resource-test-bucket>
<gcs-read-bucket>gcp-storage-bucket-sample-input</gcs-read-bucket>
<gcs-write-bucket>gcp-storage-bucket-sample-output</gcs-write-bucket>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.cloud.spring.secretmanager.SecretManagerTemplate;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
* Tests sample application endpoint that loads secrets as properties into the application context.
* Application secret named "application-secret" must exist.
*/
@EnabledIfSystemProperty(named = "it.secretmanager", matches = "true")
@ExtendWith(SpringExtension.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = SecretManagerApplication.class)
class SecretManagerSampleLoadSecretsIntegrationTests {

@Autowired private SecretManagerTemplate secretManagerTemplate;

@Autowired private TestRestTemplate testRestTemplate;

private static final String SECRET_CONTENT = "Hello world.";

@Test
void testApplicationStartupSecretLoadsCorrectly() {
ResponseEntity<String> response = this.testRestTemplate.getForEntity("/", String.class);
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
assertThat(response.getBody())
.contains("<b>Application secret from @Value:</b> <i>" + SECRET_CONTENT + "</i>");
assertThat(response.getBody())
.contains(
"<b>Application secret from @ConfigurationProperties:</b> <i>"
+ SECRET_CONTENT
+ "</i>");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.google.cloud.spring.secretmanager.SecretManagerTemplate;
import java.util.UUID;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -32,59 +35,59 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
* Application secret named "application-secret" must exist and have a value of "Hello world.".
*/
/** Test for sample application web endpoints using SecretManagerTemplate. */
@EnabledIfSystemProperty(named = "it.secretmanager", matches = "true")
@ExtendWith(SpringExtension.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = SecretManagerApplication.class)
class SecretManagerSampleIntegrationTests {
class SecretManagerSampleTemplateIntegrationTests {

private static final String SECRET_TO_DELETE = "secret-manager-sample-delete-secret";
@Autowired private SecretManagerTemplate secretManagerTemplate;

@Autowired
private SecretManagerTemplate secretManagerTemplate;
@Autowired private TestRestTemplate testRestTemplate;

@Autowired
private TestRestTemplate testRestTemplate;
private String secretName;

@Test
void testApplicationStartup() {
ResponseEntity<String> response = this.testRestTemplate.getForEntity("/", String.class);
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
assertThat(response.getBody()).contains(
"<b>Application secret from @Value:</b> <i>Hello world.</i>");
assertThat(response.getBody()).contains(
"<b>Application secret from @ConfigurationProperties:</b> <i>Hello world.</i>");
@BeforeEach
void createSecret() {
this.secretName = String.format("secret-manager-sample-secret-%s", UUID.randomUUID());
secretManagerTemplate.createSecret(this.secretName, "12345");
}

@AfterEach
void deleteSecret() {
if (secretManagerTemplate.secretExists(this.secretName)) {
secretManagerTemplate.deleteSecret(this.secretName);
}
}

@Test
void testCreateReadSecret() {
void testCreateSecret() {
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("secretId", "secret-manager-sample-secret");
params.add("secretId", this.secretName);
params.add("projectId", "");
params.add("secretPayload", "12345");
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(params, new HttpHeaders());

ResponseEntity<String> response =
this.testRestTemplate.postForEntity("/createSecret", request, String.class);
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
}

response =
this.testRestTemplate.getForEntity(
"/getSecret?secretId=secret-manager-sample-secret", String.class);
@Test
void testReadSecret() {
String getSecretUrl = String.format("/getSecret?secretId=%s", this.secretName);
ResponseEntity<String> response =
this.testRestTemplate.getForEntity(getSecretUrl, String.class);
assertThat(response.getBody())
.contains("Secret ID: secret-manager-sample-secret | Value: 12345");
.contains(String.format("Secret ID: %s | Value: 12345", this.secretName));
}

@Test
void testDeleteSecret() {
secretManagerTemplate.createSecret(SECRET_TO_DELETE, "test");

MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("secretId", SECRET_TO_DELETE);
params.add("secretId", this.secretName);
params.add("projectId", "");
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(params, new HttpHeaders());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.awaitility.Awaitility.await;

import com.google.cloud.spring.secretmanager.SecretManagerTemplate;
import java.time.Duration;
import java.util.UUID;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -38,18 +40,37 @@ class SecretManagerTemplateIntegrationTests {

@Autowired SecretManagerTemplate secretManagerTemplate;

private String secretName;

@BeforeEach
void createSecret() {
this.secretName = String.format("test-secret-%s", UUID.randomUUID());

secretManagerTemplate.createSecret(secretName, "1234");
await()
.atMost(Duration.ofSeconds(5))
.untilAsserted(
() -> {
String secretString = secretManagerTemplate.getSecretString(secretName);
assertThat(secretString).isEqualTo("1234");
});
}

@AfterEach
void deleteSecret() {
secretManagerTemplate.deleteSecret(this.secretName);
}

@Test
void testReadWriteSecrets() {
secretManagerTemplate.createSecret("test-secret-1234", "1234");

await()
.atMost(Duration.ofSeconds(5))
.untilAsserted(
() -> {
String secretString = secretManagerTemplate.getSecretString("test-secret-1234");
String secretString = secretManagerTemplate.getSecretString(this.secretName);
assertThat(secretString).isEqualTo("1234");

byte[] secretBytes = secretManagerTemplate.getSecretBytes("test-secret-1234");
byte[] secretBytes = secretManagerTemplate.getSecretBytes(this.secretName);
assertThat(secretBytes).isEqualTo("1234".getBytes());
});
}
Expand All @@ -63,16 +84,8 @@ void testReadMissingSecret() {

@Test
void testUpdateSecrets() {
secretManagerTemplate.createSecret("test-update-secret", "5555");
await()
.atMost(Duration.ofSeconds(5))
.untilAsserted(
() -> {
String secretString = secretManagerTemplate.getSecretString("test-update-secret");
assertThat(secretString).isEqualTo("5555");
});

secretManagerTemplate.createSecret("test-update-secret", "6666");
secretManagerTemplate.createSecret(this.secretName, "6666");
await()
.atMost(Duration.ofSeconds(10))
.untilAsserted(
Expand Down