Skip to content
Merged
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
8 changes: 8 additions & 0 deletions checkstyle/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@
<allow pkg="org.apache.kafka.connect.storage" />
</subpackage>

<subpackage name="connector.policy">
<allow pkg="org.apache.kafka.connect.health" />
<allow pkg="org.apache.kafka.connect.connector" />
<!-- for testing -->
<allow pkg="org.apache.kafka.connect.runtime" />
</subpackage>

<subpackage name="rest">
<allow pkg="org.apache.kafka.connect.health" />
<allow pkg="javax.ws.rs" />
Expand Down Expand Up @@ -356,6 +363,7 @@
<allow pkg="org.apache.kafka.connect.storage" />
<allow pkg="org.apache.kafka.connect.util" />
<allow pkg="org.apache.kafka.common" />
<allow pkg="org.apache.kafka.connect.connector.policy" />
</subpackage>

<subpackage name="storage">
Expand Down
3 changes: 2 additions & 1 deletion checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
<!-- Connect -->
<suppress checks="ClassFanOutComplexity"
files="DistributedHerder(|Test).java"/>

<suppress checks="ClassFanOutComplexity"
files="Worker.java"/>
<suppress checks="MethodLength"
files="(KafkaConfigBackingStore|RequestResponseTest|WorkerSinkTaskTest).java"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ public static Set<String> configNames() {
return CONFIG.names();
}

public static ConfigDef configDef() {
return CONFIG;
}
Comment thread
rhauch marked this conversation as resolved.
Outdated

public static void main(String[] args) {
System.out.println(CONFIG.toHtmlTable());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ public static Set<String> configNames() {
return CONFIG.names();
}

public static ConfigDef configDef() {
return CONFIG;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I saw this late @rhauch and @mageshn. We should not do this as these types are mutable and anyone could change the shared definition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point @ijuma. What do you think about returning a copy like
public static ConfigDef configDef() {return new ConfigDef(CONFIG); }

}

public static void main(String[] args) {
System.out.println(CONFIG.toHtmlTable());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ public static Set<String> configNames() {
return CONFIG.names();
}

public static ConfigDef configDef() {
return CONFIG;
}

public static void main(String[] args) {
System.out.println(CONFIG.toHtmlTable());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.kafka.connect.connector.policy;

import org.apache.kafka.common.Configurable;
import org.apache.kafka.common.config.ConfigValue;

import java.util.List;

/**
* <p>An interface for enforcing a policy on overriding of client configs via the connector configs.
*
* <p>Common use cases are ability to provide principal per connector, <code>sasl.jaas.config</code>
* and/or enforcing that the producer/consumer configurations for optimizations are within acceptable ranges.
*/
public interface ConnectorClientConfigOverridePolicy extends Configurable, AutoCloseable {


/**
* Worker will invoke this while constructing the producer for the SourceConnectors, DLQ for SinkConnectors and the consumer for the
* SinkConnectors to validate if all of the overridden client configurations are allowed per the
* policy implementation. This would also be invoked during the validate of connector configs via the Rest API.
*
* If there are any policy violations, the connector will not be started.
*
* @param connectorClientConfigRequest an instance of {@code ConnectorClientConfigRequest} that provides the configs to overridden and
* its context; never {@code null}
* @return list of {@link ConfigValue} instances that describe each client configuration in the request and includes an
{@link ConfigValue#errorMessages error} if the configuration is not allowed by the policy; never null
*/
List<ConfigValue> validate(ConnectorClientConfigRequest connectorClientConfigRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.kafka.connect.connector.policy;

import org.apache.kafka.connect.connector.Connector;
import org.apache.kafka.connect.health.ConnectorType;

import java.util.Map;

public class ConnectorClientConfigRequest {

private Map<String, Object> clientProps;
private ClientType clientType;
private String connectorName;
private ConnectorType connectorType;
private Class<? extends Connector> connectorClass;

public ConnectorClientConfigRequest(
String connectorName,
ConnectorType connectorType,
Class<? extends Connector> connectorClass,
Map<String, Object> clientProps,
ClientType clientType) {
this.clientProps = clientProps;
this.clientType = clientType;
this.connectorName = connectorName;
this.connectorType = connectorType;
this.connectorClass = connectorClass;
}

/**
* Provides Config with prefix {@code producer.override.} for {@link ConnectorType#SOURCE}.
* Provides Config with prefix {@code consumer.override.} for {@link ConnectorType#SINK}.
* Provides Config with prefix {@code producer.override.} for {@link ConnectorType#SINK} for DLQ.
* Provides Config with prefix {@code admin.override.} for {@link ConnectorType#SINK} for DLQ.
*
* @return The client properties specified in the Connector Config with prefix {@code producer.override.} ,
* {@code consumer.override.} and {@code admin.override.}. The configs don't include the prefixes.
*/
public Map<String, Object> clientProps() {
return clientProps;
}

/**
* {@link ClientType#PRODUCER} for {@link ConnectorType#SOURCE}
* {@link ClientType#CONSUMER} for {@link ConnectorType#SINK}
* {@link ClientType#PRODUCER} for DLQ in {@link ConnectorType#SINK}
* {@link ClientType#ADMIN} for DLQ Topic Creation in {@link ConnectorType#SINK}
*
* @return enumeration specifying the client type that is being overriden by the worker; never null.
*/
public ClientType clientType() {
return clientType;
}

/**
* Name of the connector specified in the connector config.
*
* @return name of the connector; never null.
*/
public String connectorName() {
return connectorName;
}

/**
* Type of the Connector.
*
* @return enumeration specifying the type of the connector {@link ConnectorType#SINK} or {@link ConnectorType#SOURCE}.
*/
public ConnectorType connectorType() {
return connectorType;
}

/**
* The class of the Connector.
*
* @return the class of the Connector being created; never null
*/
public Class<? extends Connector> connectorClass() {
return connectorClass;
}

public enum ClientType {
PRODUCER, CONSUMER, ADMIN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.connect.connector.policy.ConnectorClientConfigOverridePolicy;
import org.apache.kafka.connect.runtime.Connect;
import org.apache.kafka.connect.runtime.Worker;
import org.apache.kafka.connect.runtime.WorkerConfig;
import org.apache.kafka.connect.runtime.WorkerConfigTransformer;
import org.apache.kafka.connect.runtime.WorkerInfo;
import org.apache.kafka.connect.runtime.distributed.DistributedConfig;
Expand Down Expand Up @@ -102,7 +104,11 @@ public Connect startConnect(Map<String, String> workerProps) {
KafkaOffsetBackingStore offsetBackingStore = new KafkaOffsetBackingStore();
offsetBackingStore.configure(config);

Worker worker = new Worker(workerId, time, plugins, config, offsetBackingStore);
ConnectorClientConfigOverridePolicy connectorClientConfigOverridePolicy = plugins.newPlugin(
config.getString(WorkerConfig.CONNECTOR_CLIENT_POLICY_CLASS_CONFIG),
config, ConnectorClientConfigOverridePolicy.class);

Worker worker = new Worker(workerId, time, plugins, config, offsetBackingStore, connectorClientConfigOverridePolicy);
WorkerConfigTransformer configTransformer = worker.configTransformer();

Converter internalValueConverter = worker.getInternalValueConverter();
Expand All @@ -116,7 +122,7 @@ public Connect startConnect(Map<String, String> workerProps) {

DistributedHerder herder = new DistributedHerder(config, time, worker,
kafkaClusterId, statusBackingStore, configBackingStore,
advertisedUrl.toString());
advertisedUrl.toString(), connectorClientConfigOverridePolicy);

final Connect connect = new Connect(herder, rest);
log.info("Kafka Connect distributed worker initialization took {}ms", time.hiResClockMs() - initStart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.connect.connector.policy.ConnectorClientConfigOverridePolicy;
import org.apache.kafka.connect.runtime.Connect;
import org.apache.kafka.connect.runtime.ConnectorConfig;
import org.apache.kafka.connect.runtime.Herder;
import org.apache.kafka.connect.runtime.Worker;
import org.apache.kafka.connect.runtime.WorkerConfig;
import org.apache.kafka.connect.runtime.WorkerInfo;
import org.apache.kafka.connect.runtime.isolation.Plugins;
import org.apache.kafka.connect.runtime.rest.RestServer;
Expand Down Expand Up @@ -87,9 +89,13 @@ public static void main(String[] args) {
URI advertisedUrl = rest.advertisedUrl();
String workerId = advertisedUrl.getHost() + ":" + advertisedUrl.getPort();

Worker worker = new Worker(workerId, time, plugins, config, new FileOffsetBackingStore());
ConnectorClientConfigOverridePolicy connectorClientConfigOverridePolicy = plugins.newPlugin(
config.getString(WorkerConfig.CONNECTOR_CLIENT_POLICY_CLASS_CONFIG),
config, ConnectorClientConfigOverridePolicy.class);
Worker worker = new Worker(workerId, time, plugins, config, new FileOffsetBackingStore(),
connectorClientConfigOverridePolicy);

Herder herder = new StandaloneHerder(worker, kafkaClusterId);
Herder herder = new StandaloneHerder(worker, kafkaClusterId, connectorClientConfigOverridePolicy);
final Connect connect = new Connect(herder, rest);
log.info("Kafka Connect standalone worker initialization took {}ms", time.hiResClockMs() - initStart);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.kafka.connect.connector.policy;

import org.apache.kafka.common.config.ConfigValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class AbstractConnectorClientConfigOverridePolicy implements ConnectorClientConfigOverridePolicy {

@Override
public void close() throws Exception {

}

@Override
public final List<ConfigValue> validate(ConnectorClientConfigRequest connectorClientConfigRequest) {
Map<String, Object> inputConfig = connectorClientConfigRequest.clientProps();
return inputConfig.entrySet().stream().map(this::configValue).collect(Collectors.toList());
}

protected ConfigValue configValue(Map.Entry<String, Object> configEntry) {
ConfigValue configValue =
new ConfigValue(configEntry.getKey(), configEntry.getValue(), new ArrayList<>(), new ArrayList<String>());
validate(configValue);
return configValue;
}

protected void validate(ConfigValue configValue) {
if (!isAllowed(configValue)) {
configValue.addErrorMessage("The '" + policyName() + "' policy does not allow '" + configValue.name()
+ "' to be overridden in the connector configuration.");
}
}

protected abstract String policyName();

protected abstract boolean isAllowed(ConfigValue configValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.kafka.connect.connector.policy;

import org.apache.kafka.common.config.ConfigValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
* Allows all client configurations to be overridden via the connector configs by setting {@code client.config.policy} to {@code All}
*/
public class AllConnectorClientConfigOverridePolicy extends AbstractConnectorClientConfigOverridePolicy {
private static final Logger log = LoggerFactory.getLogger(AllConnectorClientConfigOverridePolicy.class);

@Override
protected String policyName() {
return "All";
}

@Override
protected boolean isAllowed(ConfigValue configValue) {
return true;
}

@Override
public void configure(Map<String, ?> configs) {
log.info("Setting up All Policy for ConnectorClientConfigOverride. This will allow all client configurations to be overridden");
}
}
Loading