Search in sources :

Example 26 with ZkUtils

use of kafka.utils.ZkUtils in project testcases by coheigea.

the class KafkaTest method setup.

@org.junit.BeforeClass
public static void setup() throws Exception {
    zkServer = new TestingServer();
    tempDir = Files.createTempDirectory("kafka");
    port = Integer.parseInt(System.getProperty("kafka.port"));
    Properties props = new Properties();
    props.put("broker.id", 1);
    props.put("host.name", "localhost");
    props.put("port", port);
    props.put("log.dir", tempDir.toString());
    props.put("zookeeper.connect", zkServer.getConnectString());
    props.put("replica.socket.timeout.ms", "1500");
    props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
    props.put("offsets.topic.replication.factor", (short) 1);
    props.put("offsets.topic.num.partitions", 1);
    KafkaConfig config = new KafkaConfig(props);
    kafkaServer = new KafkaServerStartable(config);
    kafkaServer.startup();
    // Create a "test" topic
    ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
    final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
    AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ZkClient(org.I0Itec.zkclient.ZkClient) KafkaServerStartable(kafka.server.KafkaServerStartable) ZkUtils(kafka.utils.ZkUtils) Properties(java.util.Properties) ZkConnection(org.I0Itec.zkclient.ZkConnection) KafkaConfig(kafka.server.KafkaConfig)

Example 27 with ZkUtils

use of kafka.utils.ZkUtils in project testcases by coheigea.

the class KafkaAuthorizerSASLSSLTest method setup.

@org.junit.BeforeClass
public static void setup() throws Exception {
    // JAAS Config file
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    File f = new File(basedir + "/src/test/resources/kafka_plain.jaas");
    System.setProperty("java.security.auth.login.config", f.getPath());
    zkServer = new TestingServer();
    // Get a random port
    ServerSocket serverSocket = new ServerSocket(0);
    port = serverSocket.getLocalPort();
    serverSocket.close();
    final Properties props = new Properties();
    props.put("broker.id", 1);
    props.put("host.name", "localhost");
    props.put("port", port);
    props.put("log.dir", "/tmp/kafka");
    props.put("zookeeper.connect", zkServer.getConnectString());
    props.put("replica.socket.timeout.ms", "1500");
    props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
    // Enable SASL_SSL
    props.put("listeners", "SASL_SSL://localhost:" + port);
    props.put("security.inter.broker.protocol", "SASL_SSL");
    props.put("sasl.enabled.mechanisms", "PLAIN");
    props.put("sasl.mechanism.inter.broker.protocol", "PLAIN");
    props.put("ssl.keystore.location", KafkaAuthorizerTest.class.getResource("/brokerstore.jks").getPath());
    props.put("ssl.keystore.password", "bspass");
    props.put("ssl.key.password", "bkpass");
    props.put("ssl.truststore.location", KafkaAuthorizerTest.class.getResource("/truststore.jks").getPath());
    props.put("ssl.truststore.password", "security");
    // Plug in custom authorizer
    props.put("authorizer.class.name", "org.apache.coheigea.bigdata.kafka.CustomSASLSSLAuthorizer");
    KafkaConfig config = new KafkaConfig(props);
    kafkaServer = new KafkaServerStartable(config);
    kafkaServer.startup();
    // Create some topics
    ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
    final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
    AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
    AdminUtils.createTopic(zkUtils, "dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ZkClient(org.I0Itec.zkclient.ZkClient) ServerSocket(java.net.ServerSocket) KafkaServerStartable(kafka.server.KafkaServerStartable) ZkUtils(kafka.utils.ZkUtils) Properties(java.util.Properties) File(java.io.File) ZkConnection(org.I0Itec.zkclient.ZkConnection) KafkaConfig(kafka.server.KafkaConfig)

Example 28 with ZkUtils

use of kafka.utils.ZkUtils in project testcases by coheigea.

the class KafkaAuthorizerTest method setup.

@org.junit.BeforeClass
public static void setup() throws Exception {
    zkServer = new TestingServer();
    // Get a random port
    ServerSocket serverSocket = new ServerSocket(0);
    port = serverSocket.getLocalPort();
    serverSocket.close();
    final Properties props = new Properties();
    props.put("broker.id", 1);
    props.put("host.name", "localhost");
    props.put("port", port);
    props.put("log.dir", "/tmp/kafka");
    props.put("zookeeper.connect", zkServer.getConnectString());
    props.put("replica.socket.timeout.ms", "1500");
    props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
    // Enable SSL
    props.put("listeners", "SSL://localhost:" + port);
    props.put("ssl.keystore.location", KafkaAuthorizerTest.class.getResource("/brokerstore.jks").getPath());
    props.put("ssl.keystore.password", "bspass");
    props.put("ssl.key.password", "bkpass");
    props.put("ssl.truststore.location", KafkaAuthorizerTest.class.getResource("/truststore.jks").getPath());
    props.put("ssl.truststore.password", "security");
    props.put("security.inter.broker.protocol", "SSL");
    props.put("ssl.client.auth", "required");
    // Plug in custom authorizer
    props.put("authorizer.class.name", "org.apache.coheigea.bigdata.kafka.CustomAuthorizer");
    KafkaConfig config = new KafkaConfig(props);
    kafkaServer = new KafkaServerStartable(config);
    kafkaServer.startup();
    // Create some topics
    ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
    final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
    AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
    AdminUtils.createTopic(zkUtils, "dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ZkClient(org.I0Itec.zkclient.ZkClient) ServerSocket(java.net.ServerSocket) KafkaServerStartable(kafka.server.KafkaServerStartable) ZkUtils(kafka.utils.ZkUtils) Properties(java.util.Properties) ZkConnection(org.I0Itec.zkclient.ZkConnection) KafkaConfig(kafka.server.KafkaConfig)

Example 29 with ZkUtils

use of kafka.utils.ZkUtils in project testcases by coheigea.

the class KafkaRangerGSSAuthorizerTest method setup.

@org.junit.BeforeClass
public static void setup() throws Exception {
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    configureKerby(basedir);
    // JAAS Config file - We need to point to the correct keytab files
    Path path = FileSystems.getDefault().getPath(basedir, "/src/test/resources/kafka_kerberos.jaas");
    String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
    content = content.replaceAll("<basedir>", basedir);
    Path path2 = FileSystems.getDefault().getPath(basedir, "/target/test-classes/kafka_kerberos.jaas");
    Files.write(path2, content.getBytes(StandardCharsets.UTF_8));
    System.setProperty("java.security.auth.login.config", path2.toString());
    // Set up Zookeeper to require SASL
    Map<String, Object> zookeeperProperties = new HashMap<>();
    zookeeperProperties.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
    zookeeperProperties.put("requireClientAuthScheme", "sasl");
    zookeeperProperties.put("jaasLoginRenew", "3600000");
    InstanceSpec instanceSpec = new InstanceSpec(null, -1, -1, -1, true, 1, -1, -1, zookeeperProperties);
    zkServer = new TestingServer(instanceSpec, true);
    // Get a random port
    ServerSocket serverSocket = new ServerSocket(0);
    port = serverSocket.getLocalPort();
    serverSocket.close();
    final Properties props = new Properties();
    props.put("broker.id", 1);
    props.put("host.name", "localhost");
    props.put("port", port);
    props.put("log.dir", "/tmp/kafka");
    props.put("zookeeper.connect", zkServer.getConnectString());
    props.put("replica.socket.timeout.ms", "1500");
    props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
    // Enable SASL_PLAINTEXT
    props.put("listeners", "SASL_PLAINTEXT://localhost:" + port);
    props.put("security.inter.broker.protocol", "SASL_PLAINTEXT");
    props.put("sasl.enabled.mechanisms", "GSSAPI");
    props.put("sasl.mechanism.inter.broker.protocol", "GSSAPI");
    props.put("sasl.kerberos.service.name", "kafka");
    // Plug in Apache Ranger authorizer
    props.put("authorizer.class.name", "org.apache.ranger.authorization.kafka.authorizer.RangerKafkaAuthorizer");
    // Create users for testing
    UserGroupInformation.createUserForTesting("client@kafka.apache.org", new String[] { "public" });
    UserGroupInformation.createUserForTesting("kafka/localhost@kafka.apache.org", new String[] { "IT" });
    KafkaConfig config = new KafkaConfig(props);
    kafkaServer = new KafkaServerStartable(config);
    kafkaServer.startup();
    // Create some topics
    ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
    final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
    AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
    AdminUtils.createTopic(zkUtils, "dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
    AdminUtils.createTopic(zkUtils, "messages", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}
Also used : Path(java.nio.file.Path) TestingServer(org.apache.curator.test.TestingServer) ZkClient(org.I0Itec.zkclient.ZkClient) InstanceSpec(org.apache.curator.test.InstanceSpec) HashMap(java.util.HashMap) ServerSocket(java.net.ServerSocket) KafkaServerStartable(kafka.server.KafkaServerStartable) ZkUtils(kafka.utils.ZkUtils) Properties(java.util.Properties) ZkConnection(org.I0Itec.zkclient.ZkConnection) File(java.io.File) KafkaConfig(kafka.server.KafkaConfig)

Example 30 with ZkUtils

use of kafka.utils.ZkUtils in project SpinalTap by airbnb.

the class KafkaDestinationTest method createKafkaTopic.

private void createKafkaTopic(String topicName) throws Exception {
    ZkConnection zkConn = null;
    ZkClient zkClient = null;
    try {
        zkClient = new ZkClient("localhost:" + zkPort(), SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, ZK_SERIALIZER);
        zkConn = new ZkConnection("localhost:" + zkPort());
        Properties props = new Properties();
        props.setProperty("min.insync.replicas", "2");
        AdminUtils.createTopic(new ZkUtils(zkClient, zkConn, false), topicName, 1, 3, props);
    } catch (Exception ex) {
        logger().error("Kafka topic creation failed due to " + ex.getLocalizedMessage());
        // We need to abort upon topic creation failure.
        throw ex;
    } finally {
        if (zkClient != null)
            zkClient.close();
        if (zkConn != null)
            zkConn.close();
    }
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) ZkUtils(kafka.utils.ZkUtils) Properties(java.util.Properties) ZkConnection(org.I0Itec.zkclient.ZkConnection)

Aggregations

ZkUtils (kafka.utils.ZkUtils)61 ZkClient (org.I0Itec.zkclient.ZkClient)26 Properties (java.util.Properties)25 ZkConnection (org.I0Itec.zkclient.ZkConnection)22 Test (org.testng.annotations.Test)18 Configuration (org.apache.commons.configuration.Configuration)16 HashMap (java.util.HashMap)8 KafkaConfig (kafka.server.KafkaConfig)8 TestingServer (org.apache.curator.test.TestingServer)8 KafkaServerStartable (kafka.server.KafkaServerStartable)7 ServerSocket (java.net.ServerSocket)6 Test (org.junit.Test)4 File (java.io.File)3 Path (java.nio.file.Path)2 Level (java.util.logging.Level)2 TopicMetadata (kafka.api.TopicMetadata)2 TopicExistsException (kafka.common.TopicExistsException)2 InstanceSpec (org.apache.curator.test.InstanceSpec)2 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)2 Before (org.junit.Before)2