Skip to content

Commit a694195

Browse files
authored
feat: add secretmanager sample for ConsumeEventNotification (#8138)
* feat: add secretmanager sample for ConsumeEventNotification * lint fix * update acc to review * update acc to review
1 parent bc85669 commit a694195

3 files changed

Lines changed: 113 additions & 5 deletions

File tree

secretmanager/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
<artifactId>protobuf-java-util</artifactId>
6262
</dependency>
6363

64+
<dependency>
65+
<groupId>org.projectlombok</groupId>
66+
<artifactId>lombok</artifactId>
67+
<version>1.18.26</version>
68+
<scope>provided</scope>
69+
</dependency>
70+
6471
<!-- test dependencies -->
6572
<dependency>
6673
<groupId>junit</groupId>
@@ -75,4 +82,26 @@
7582
<scope>test</scope>
7683
</dependency>
7784
</dependencies>
85+
86+
<build>
87+
<plugins>
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-compiler-plugin</artifactId>
91+
<version>3.10.1</version>
92+
<configuration>
93+
<source>11</source> <!-- depending on your project -->
94+
<target>11</target> <!-- depending on your project -->
95+
<annotationProcessorPaths>
96+
<path>
97+
<groupId>org.projectlombok</groupId>
98+
<artifactId>lombok</artifactId>
99+
<version>1.18.26</version>
100+
</path>
101+
</annotationProcessorPaths>
102+
</configuration>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
78107
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_consume_event_notification]
20+
21+
import java.util.Base64;
22+
import java.util.Map;
23+
import java.util.logging.Logger;
24+
import lombok.Data;
25+
26+
// Demonstrates how to consume and process a Pub/Sub notification from Secret Manager. Triggered
27+
// by a message on a Cloud Pub/Sub topic.
28+
// Ideally the class should implement a background function that accepts a Pub/Sub message.
29+
// public class ConsumeEventNotification implements BackgroundFunction<PubSubMessage> { }
30+
public class ConsumeEventNotification {
31+
32+
// You can configure the logs to print the message in Cloud Logging.
33+
private static final Logger logger = Logger.getLogger(ConsumeEventNotification.class.getName());
34+
35+
// Accepts a message from a Pub/Sub topic and writes it to logger.
36+
public static String accept(PubSubMessage message) {
37+
String eventType = message.attributes.get("eventType");
38+
String secretId = message.attributes.get("secretId");
39+
String data = new String(Base64.getDecoder().decode(message.data));
40+
String log = String.format("Received %s for %s. New metadata: %s", eventType, secretId, data);
41+
logger.info(log);
42+
return log;
43+
}
44+
45+
// Event payload. Mock of the actual Pub/Sub message.
46+
@Data
47+
public static class PubSubMessage {
48+
49+
byte[] data;
50+
Map<String, String> attributes;
51+
String messageId;
52+
String publishTime;
53+
String orderingKey;
54+
}
55+
}
56+
// [END secretmanager_consume_event_notification]

secretmanager/src/test/java/secretmanager/SnippetsIT.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import com.google.cloud.secretmanager.v1.AddSecretVersionRequest;
2222
import com.google.cloud.secretmanager.v1.CreateSecretRequest;
2323
import com.google.cloud.secretmanager.v1.DeleteSecretRequest;
24-
import com.google.cloud.secretmanager.v1.DestroySecretVersionRequest;
2524
import com.google.cloud.secretmanager.v1.DisableSecretVersionRequest;
26-
import com.google.cloud.secretmanager.v1.EnableSecretVersionRequest;
2725
import com.google.cloud.secretmanager.v1.ProjectName;
2826
import com.google.cloud.secretmanager.v1.Replication;
2927
import com.google.cloud.secretmanager.v1.Secret;
@@ -37,8 +35,12 @@
3735
import java.io.ByteArrayOutputStream;
3836
import java.io.IOException;
3937
import java.io.PrintStream;
38+
import java.nio.charset.StandardCharsets;
4039
import java.util.Arrays;
40+
import java.util.Base64;
41+
import java.util.HashMap;
4142
import java.util.List;
43+
import java.util.Map;
4244
import java.util.Random;
4345
import org.junit.After;
4446
import org.junit.AfterClass;
@@ -48,11 +50,15 @@
4850
import org.junit.Test;
4951
import org.junit.runner.RunWith;
5052
import org.junit.runners.JUnit4;
53+
import secretmanager.ConsumeEventNotification.PubSubMessage;
5154

52-
/** Integration (system) tests for {@link Snippets}. */
55+
/**
56+
* Integration (system) tests for {@link Snippets}.
57+
*/
5358
@RunWith(JUnit4.class)
5459
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
5560
public class SnippetsIT {
61+
5662
private static final String IAM_USER =
5763
"serviceAccount:iam-samples@java-docs-samples-testing.iam.gserviceaccount.com";
5864
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
@@ -122,7 +128,7 @@ public static void afterAll() throws IOException {
122128

123129
private static String randomSecretId() {
124130
Random random = new Random();
125-
return "java-" + String.valueOf(random.nextLong());
131+
return "java-" + random.nextLong();
126132
}
127133

128134
private static Secret createSecret() throws IOException {
@@ -376,12 +382,29 @@ public void testUpdateSecret() throws IOException {
376382

377383
assertThat(stdOut.toString()).contains("Updated secret");
378384
}
379-
385+
380386
@Test
381387
public void testUpdateSecretWithAlias() throws IOException {
382388
SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName());
383389
UpdateSecretWithAlias.updateSecret(name.getProject(), name.getSecret());
384390

385391
assertThat(stdOut.toString()).contains("test");
386392
}
393+
394+
@Test
395+
public void testConsumeEventNotification() {
396+
String message = "hello!";
397+
byte[] base64Bytes = Base64.getEncoder().encode(message.getBytes(StandardCharsets.UTF_8));
398+
Map<String, String> attributes = new HashMap<>();
399+
attributes.put("eventType", "SECRET_UPDATE");
400+
attributes.put("secretId", "projects/p/secrets/s");
401+
402+
PubSubMessage pubSubMessage = new PubSubMessage();
403+
pubSubMessage.setData(base64Bytes);
404+
pubSubMessage.setAttributes(attributes);
405+
406+
String log = ConsumeEventNotification.accept(pubSubMessage);
407+
assertThat(log).isEqualTo(
408+
"Received SECRET_UPDATE for projects/p/secrets/s. New metadata: hello!");
409+
}
387410
}

0 commit comments

Comments
 (0)