Skip to content

Commit

Permalink
MINOR: Small cleanups in Connect (apache#15128)
Browse files Browse the repository at this point in the history
Reviewers: Divij Vaidya <[email protected]>
  • Loading branch information
mimaison authored Jan 5, 2024
1 parent c8d61a5 commit eccfb03
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.slf4j.LoggerFactory;

import javax.security.auth.login.Configuration;
import java.io.IOException;
import java.util.Map;
import java.util.function.Supplier;

Expand Down Expand Up @@ -100,7 +99,7 @@ public void register(ConnectRestExtensionContext restPluginContext) {
}

@Override
public void close() throws IOException {
public void close() {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.ws.rs.Priorities;
Expand Down Expand Up @@ -87,7 +85,7 @@ public JaasBasicAuthFilter(Configuration configuration) {
}

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
public void filter(ContainerRequestContext requestContext) {
if (isInternalRequest(requestContext)) {
log.trace("Skipping authentication for internal request");
return;
Expand Down Expand Up @@ -119,16 +117,16 @@ private boolean isInternalRequest(ContainerRequestContext requestContext) {

public static class BasicAuthCallBackHandler implements CallbackHandler {

private String username;
private String password;
private final String username;
private final String password;

public BasicAuthCallBackHandler(BasicAuthCredentials credentials) {
username = credentials.username();
password = credentials.password();
}

@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
public void handle(Callback[] callbacks) {
List<Callback> unsupportedCallbacks = new ArrayList<>();
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PropertyFileLoginModule implements LoginModule {
private String fileName;
private boolean authenticated;

private static Map<String, Properties> credentialPropertiesMap = new ConcurrentHashMap<>();
private static final Map<String, Properties> CREDENTIAL_PROPERTIES = new ConcurrentHashMap<>();

@Override
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
Expand All @@ -64,15 +64,15 @@ public void initialize(Subject subject, CallbackHandler callbackHandler, Map<Str
throw new ConfigException("Property Credentials file must be specified");
}

if (!credentialPropertiesMap.containsKey(fileName)) {
if (!CREDENTIAL_PROPERTIES.containsKey(fileName)) {
log.trace("Opening credential properties file '{}'", fileName);
Properties credentialProperties = new Properties();
try {
try (InputStream inputStream = Files.newInputStream(Paths.get(fileName))) {
log.trace("Parsing credential properties file '{}'", fileName);
credentialProperties.load(inputStream);
}
credentialPropertiesMap.putIfAbsent(fileName, credentialProperties);
CREDENTIAL_PROPERTIES.putIfAbsent(fileName, credentialProperties);
if (credentialProperties.isEmpty())
log.warn("Credential properties file '{}' is empty; all requests will be permitted",
fileName);
Expand Down Expand Up @@ -101,7 +101,7 @@ public boolean login() throws LoginException {
String username = ((NameCallback) callbacks[0]).getName();
char[] passwordChars = ((PasswordCallback) callbacks[1]).getPassword();
String password = passwordChars != null ? new String(passwordChars) : null;
Properties credentialProperties = credentialPropertiesMap.get(fileName);
Properties credentialProperties = CREDENTIAL_PROPERTIES.get(fileName);

if (credentialProperties.isEmpty()) {
log.trace("Not validating credentials for user '{}' as credential properties file '{}' is empty",
Expand Down Expand Up @@ -132,17 +132,17 @@ public boolean login() throws LoginException {
}

@Override
public boolean commit() throws LoginException {
public boolean commit() {
return authenticated;
}

@Override
public boolean abort() throws LoginException {
public boolean abort() {
return true;
}

@Override
public boolean logout() throws LoginException {
public boolean logout() {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testUnknownLoginModule() throws IOException {
}

@Test
public void testUnknownCredentialsFile() throws IOException {
public void testUnknownCredentialsFile() {
JaasBasicAuthFilter jaasBasicAuthFilter = setupJaasFilter("KafkaConnect", "/tmp/testcrednetial");
ContainerRequestContext requestContext = setMock("Basic", "user", "password");
jaasBasicAuthFilter.filter(requestContext);
Expand All @@ -142,7 +142,7 @@ public void testUnknownCredentialsFile() throws IOException {
}

@Test
public void testNoFileOption() throws IOException {
public void testNoFileOption() {
JaasBasicAuthFilter jaasBasicAuthFilter = setupJaasFilter("KafkaConnect", null);
ContainerRequestContext requestContext = setMock("Basic", "user", "password");
jaasBasicAuthFilter.filter(requestContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class FileStreamSinkTaskTest {

private FileStreamSinkTask task;
private ByteArrayOutputStream os;
private PrintStream printStream;

@TempDir
public Path topDir;
Expand All @@ -49,7 +48,7 @@ public class FileStreamSinkTaskTest {
@BeforeEach
public void setup() {
os = new ByteArrayOutputStream();
printStream = new PrintStream(os);
PrintStream printStream = new PrintStream(os);
task = new FileStreamSinkTask(printStream);
outputFile = topDir.resolve("connect.output").toAbsolutePath().toString();
}
Expand Down

0 comments on commit eccfb03

Please sign in to comment.