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

feat: add proxy authentication to schema registry #2139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class SchemaRegistryClientConfig {

public static final String PROXY_HOST = "proxy.host";
public static final String PROXY_PORT = "proxy.port";
public static final String PROXY_USER = "proxy.user";
public static final String PROXY_PASSWORD = "proxy.password";

public static final String MISSING_CACHE_SIZE_CONFIG = "missing.cache.size";
public static final String MISSING_ID_CACHE_TTL_CONFIG = "missing.id.cache.ttl.sec";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
Expand Down Expand Up @@ -218,6 +221,27 @@ public void configure(Map<String, ?> configs) {

if (isValidProxyConfig(proxyHost, proxyPort)) {
setProxy(proxyHost, proxyPort);
String user = Optional.ofNullable((String) configs.get(SchemaRegistryClientConfig.PROXY_USER)).orElse("");
String password = Optional.ofNullable((String) configs.get(SchemaRegistryClientConfig.PROXY_PASSWORD)).orElse("");
PasswordAuthentication passwordAuthentication = new PasswordAuthentication(user, password.toCharArray());
String hostLowercase = proxyHost.toLowerCase();
// override authenticator setting since probability to have only
// one corporate proxy setting to reach internet is very high
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
if (hostLowercase.equals(getRequestingHost().toLowerCase())) {
if (proxyPort == getRequestingPort()) {
return passwordAuthentication;
}
}
}
// Don't have access to Authenticator.getDefault() in order to relay the Authenticator Provider
// to another definition because it's available from 1.9+
return null;
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ public class AbstractKafkaSchemaSerDeConfig extends AbstractConfig {
"The port number of the proxy server that will be used to connect to the schema registry "
+ "instances.";

public static final String PROXY_USER = SchemaRegistryClientConfig.PROXY_USER;
public static final String PROXY_USER_DEFAULT = "";
public static final String PROXY_USER_DOC =
"The username used to authenticate with the proxy that will be used to connect to the schema "
+ " registry instances.";

public static final String PROXY_PASSWORD = SchemaRegistryClientConfig.PROXY_PASSWORD;
public static final String PROXY_PASSWORD_DEFAULT = "";
public static final String PROXY_PASSWORD_DOC =
"The password used to authenticate with the proxy that will be used to connect to the schema "
+ " registry instances.";

public static ConfigDef baseConfigDef() {
ConfigDef configDef = new ConfigDef()
.define(SCHEMA_REGISTRY_URL_CONFIG, Type.LIST,
Expand Down Expand Up @@ -201,7 +213,11 @@ public static ConfigDef baseConfigDef() {
.define(PROXY_HOST, Type.STRING, PROXY_HOST_DEFAULT,
Importance.LOW, PROXY_HOST_DOC)
.define(PROXY_PORT, Type.INT, PROXY_PORT_DEFAULT,
Importance.LOW, PROXY_PORT_DOC);
Importance.LOW, PROXY_PORT_DOC)
.define(PROXY_USER, Type.INT, PROXY_USER_DEFAULT,
Importance.LOW, PROXY_USER_DOC)
.define(PROXY_PASSWORD, Type.INT, PROXY_PASSWORD_DEFAULT,
Importance.LOW, PROXY_PASSWORD_DOC);
SchemaRegistryClientConfig.withClientSslSupport(
configDef, SchemaRegistryClientConfig.CLIENT_NAMESPACE);
return configDef;
Expand Down