Skip to content

Commit ab4e147

Browse files
committed
Clean up classes for PR review
1 parent 5afde06 commit ab4e147

6 files changed

Lines changed: 27 additions & 50 deletions

File tree

sdks/java/io/kafka/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ dependencies {
108108
testRuntimeOnly library.java.slf4j_jdk14
109109
testRuntimeOnly project(path: ":runners:direct-java", configuration: "shadow")
110110
kafkaVersions.each {"kafkaVersion$it.key" "org.apache.kafka:kafka-clients:$it.value"}
111-
implementation 'com.google.cloud:google-cloud-secretmanager:2.72.0'
112-
testImplementation project(path: ":runners:google-cloud-dataflow-java")
113-
testImplementation project(path: ":sdks:java:extensions:google-cloud-platform-core")
114-
testImplementation project(path: ":sdks:java:io:google-cloud-platform")
115111
}
116112

117113
kafkaVersions.each { kv ->

sdks/java/io/kafka/file-aware-factories/src/main/java/org/apache/beam/sdk/io/kafka/file/aware/factories/FileAwareFactoryFn.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,9 @@
1717
*/
1818
package org.apache.beam.sdk.io.kafka.file.aware.factories;
1919

20-
/*
21-
* Licensed to the Apache Software Foundation (ASF) under one
22-
* or more contributor license agreements. See the NOTICE file
23-
* distributed with this work for additional information
24-
* regarding copyright ownership. The ASF licenses this file
25-
* to you under the Apache License, Version 2.0 (the
26-
* "License"); you may not use this file except in compliance
27-
* with the License. You may obtain a copy of the License at
28-
*
29-
* http://www.apache.org/licenses/LICENSE-2.0
30-
*
31-
* Unless required by applicable law or agreed to in writing, software
32-
* distributed under the License is distributed on an "AS IS" BASIS,
33-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34-
* See the License for the specific language governing permissions and
35-
* limitations under the License.
36-
*/
37-
3820
import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse;
3921
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
4022
import com.google.cloud.secretmanager.v1.SecretVersionName;
41-
import com.google.common.base.Preconditions;
4223
import java.io.File;
4324
import java.io.IOException;
4425
import java.nio.channels.FileChannel;
@@ -57,6 +38,7 @@
5738
import java.util.regex.Pattern;
5839
import org.apache.beam.sdk.io.FileSystems;
5940
import org.apache.beam.sdk.transforms.SerializableFunction;
41+
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
6042
import org.slf4j.Logger;
6143
import org.slf4j.LoggerFactory;
6244

@@ -230,8 +212,7 @@ protected byte[] getSecret(String secretVersion) {
230212
}
231213
}
232214

233-
protected String processSecret(String originalValue, String secretId, byte[] secretValue)
234-
throws RuntimeException {
215+
protected String processSecret(String originalValue, String secretId, byte[] secretValue) {
235216
// By Default, this will return the secret value directly. This function can be overridden by
236217
// derived classes.
237218
return new String(secretValue, StandardCharsets.UTF_8);

sdks/java/io/kafka/file-aware-factories/src/main/java/org/apache/beam/sdk/io/kafka/file/aware/factories/KerberosConsumerFactoryFn.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public KerberosConsumerFactoryFn(String krb5ConfigGcsPath) {
5959
@Override
6060
protected Consumer<byte[], byte[]> createObject(Map<String, Object> config) {
6161
// This will be called after the config map processing has occurred. Therefore, we know that the
62-
// property will have
63-
// had it's value replaced with a local directory. We don't need to worry about the GCS prefix
64-
// in this case.
65-
LOG.info("config when creating the objects: {}", config);
62+
// property will have had it's value replaced with a local directory.
63+
// We don't need to worry about the GCS prefix in this case.
6664
try {
6765
String jaasConfig = (String) config.get(JAAS_CONFIG_PROPERTY);
6866
String localKeytabPath = "";
@@ -73,8 +71,7 @@ protected Consumer<byte[], byte[]> createObject(Map<String, Object> config) {
7371
}
7472

7573
// Set the permissions on the file to be as strict as possible for security reasons. The
76-
// keytab contains
77-
// sensitive information and should be as locked down as possible.
74+
// keytab contains sensitive information and should be as locked down as possible.
7875
Path path = Paths.get(localKeytabPath);
7976
Set<PosixFilePermission> perms = new HashSet<>();
8077
perms.add(PosixFilePermission.OWNER_READ);
@@ -100,8 +97,6 @@ protected void downloadAndProcessExtraFiles() throws IOException {
10097

10198
System.setProperty("java.security.krb5.conf", localKrb5ConfPath);
10299
Configuration.getConfiguration().refresh();
103-
LOG.info(
104-
"Successfully set and refreshed java.security.krb5.conf to {}", localKrb5ConfPath);
105100
}
106101
}
107102
}
@@ -119,15 +114,13 @@ protected String processSecret(String originalValue, String secretId, byte[] sec
119114
"Error matching values. Secret was discovered but its value is null");
120115
}
121116
currentSecretId = currentSecretId.substring(KEYTAB_SECRET_PREFIX.length());
122-
LOG.info("currentSecretId: {} and secretId: {}", currentSecretId, secretId);
123117
if (!currentSecretId.equals(secretId)) {
124118
// A sasl.jaas.config can contain multiple keytabs in one string. Therefore, we must assume
125119
// that there can
126120
// also be multiple keytab secrets in the same string. If the currently matched secret does
127121
// not equal
128122
// the secret that we are processing (passed in via secretId) then we do not want to create
129-
// a keytab file and
130-
// overwrite it.
123+
// a keytab file and overwrite it.
131124
continue;
132125
}
133126
String filename = "kafka-client-" + UUID.randomUUID().toString() + ".keytab";
@@ -141,7 +134,7 @@ protected String processSecret(String originalValue, String secretId, byte[] sec
141134
}
142135
Files.write(localFilePath, secretValue);
143136
if (!new File(localFileString).canRead()) {
144-
LOG.info("The file is not readable");
137+
LOG.warn("The file is not readable");
145138
}
146139
LOG.info("Successfully wrote file to path: {}", localFilePath);
147140
} catch (IOException e) {
@@ -150,7 +143,6 @@ protected String processSecret(String originalValue, String secretId, byte[] sec
150143
}
151144
// if no localFile was created, then we can assume that the secret is meant to be kept as a
152145
// value.
153-
LOG.info("LocalFilestring: {}", localFileString);
154146
return localFileString.isEmpty()
155147
? new String(secretValue, StandardCharsets.UTF_8)
156148
: localFileString;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/** ConsumerFactoryFns for file paths that exist in GCS or Google SecretManager. */
20+
package org.apache.beam.sdk.io.kafka.file.aware.factories;

sdks/java/io/kafka/file-aware-factories/src/test/java/org/apache/beam/sdk/io/kafka/file/aware/factories/FileAwareFactoryFnTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,6 @@ public void testApplyHappyPathResolvesSecretValue() {
220220
Map<String, Object> config = new HashMap<>();
221221
config.put("db.password", originalValue);
222222

223-
// FIX: Create an anonymous inner class that extends our TestFactoryFn
224-
// and overrides the resolveSecret method to return a hardcoded value.
225-
// This completely avoids the call to the real getSecret method and its
226-
// final class dependencies.
227223
TestFactoryFn factoryWithMockedSecret =
228224
new TestFactoryFn() {
229225
@Override
@@ -252,8 +248,6 @@ public void testApplyFailurePathThrowsExceptionForInvalidSecretFormat() {
252248
config.put("db.password", "password=" + invalidSecret);
253249

254250
// Act & Assert
255-
// The spy will call the real method here, which will throw an exception
256-
// because the secret path is not parsable.
257251
RuntimeException ex = Assert.assertThrows(RuntimeException.class, () -> factory.apply(config));
258252
Assert.assertEquals(IllegalArgumentException.class, ex.getCause().getClass());
259253
}

sdks/java/io/kafka/file-aware-factories/src/test/java/org/apache/beam/sdk/io/kafka/file/aware/factories/KerberosConsumerFactoryFnTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class KerberosConsumerFactoryFnTest {
5353

5454
@Before
5555
public void setup() {
56-
// Use reflection to reset the static variable before each test for isolation
5756
try {
5857
java.lang.reflect.Field field =
5958
KerberosConsumerFactoryFn.class.getDeclaredField("localKrb5ConfPath");
@@ -64,8 +63,6 @@ public void setup() {
6463
}
6564

6665
factory = spy(new KerberosConsumerFactoryFn(KRB5_GCS_PATH));
67-
68-
// Store original system property to restore it after tests
6966
originalKrb5Conf = System.getProperty("java.security.krb5.conf");
7067
}
7168

@@ -102,8 +99,6 @@ public void testHappyPath() {
10299
+ keytabGcsPath
103100
+ "\" principal=\"user@REALM\";");
104101

105-
// Use try-with-resources for all necessary static mocks
106-
107102
try (MockedStatic<FileAwareFactoryFn> mockedStaticFactory =
108103
Mockito.mockStatic(FileAwareFactoryFn.class);
109104
MockedStatic<Configuration> mockedConfiguration = Mockito.mockStatic(Configuration.class);
@@ -120,7 +115,6 @@ public void testHappyPath() {
120115
.when(() -> FileAwareFactoryFn.downloadGcsFile(keytabGcsPath, expectedKeytabLocalPath))
121116
.thenReturn(expectedKeytabLocalPath);
122117

123-
// Mock other dependencies to prevent side effects
124118
Configuration mockConf = Mockito.mock(Configuration.class);
125119
mockedConfiguration.when(Configuration::getConfiguration).thenReturn(mockConf);
126120
mockedFiles

0 commit comments

Comments
 (0)