Hive: Misc

This tutorial will show you some misc usage for working with Hive. If you have no installed Hive yet please follow this tutorial.

NVL:
Check if value is null then substitute other value.

  1. SELECT NVL(columnA, 'was null')
  2. FROM test;

CAST:
If columnE was a date and you wanted it to be a string.

  1. SELECT CAST(columnE AS STRING)
  2. FROM test;

Concat:
This will concat the strings together giving “Test the code!”

  1. SELECT CONCAT('Test', ' the ', 'code!');

CONCAT_WS:
This will concat the strings together starting at “Test” and use the first position as the seperator giving “Test the code”

  1. SELECT CONCAT_WS(' ', 'Test', 'the', 'code');

MIN:
This will give the minimum value of columnC

  1. SELECT MIN(columnC) AS Max_ColumnC
  2. FROM test;

MAX:
This will give the maximum value of columnC

  1. SELECT MAX(columnC) AS Max_ColumnC
  2. FROM test;

DISTINCT:
This will select the distinct columnA and columnB

  1. SELECT DISTINCT columnA, columnB
  2. FROM test;

CASE:

  1. SELECT CASE WHEN columnA = 'val' THEN 'Is Val' ELSE 'Not Val' END
  2. FROM test;

COLLECT_SET:
This will collect all values in columnC and select the first index.

  1. SELECT COLLECT_SET(columnC)[0]
  2. FROM test;

CURRENT_DATE:
This will give you the current date in the format YYYY-MM-dd.

  1. SELECT CURRENT_DATE();

UNIX_TIMESTAMP:
This will give you the current timestamp from EPOCH. IE: 1549591492

  1. SELECT UNIX_TIMESTAMP();

FROM_UNIXTIME:
This will take a timestamp and display it in the format YYYY-MM-dd HH:MM:SS.

  1. SELECT FROM_UNIXTIME(1549591492);

TO_DATE:
This will convert a date to YYYY-MM-dd.

  1. SELECT TO_DATE('2019-02-01 01:01:01');

YEAR:

  1. SELECT YEAR(columnE)
  2. FROM test;

MONTH:

  1. SELECT MONTH(columnE)
  2. FROM test;

DAY:

  1. SELECT DAY(columnE)
  2. FROM test;

DATE_ADD:

  1. SELECT DATE_ADD('2019-01-01', 4);

UPPER:
This will upper case columnA.

  1. SELECT UPPER(columnA)
  2. FROM test;

LOWER:
This will lower case columnA.

  1. SELECT LOWER(columnA)
  2. FROM test;

TRIM:
This will trim leading and trailing spaces in columnA.

  1. SELECT TRIM(columnA)
  2. FROM test;

LITERALS:
If a column contains a space you will need to use literal in order to use the AS keyword or when you are defining it in the create table command. Although I don’t recommend this it is possible.

  1. SELECT columnA AS `test column`
  2. FROM test;

 

 

 

Hive: Tables

This tutorial will show you some common usage for working with tables. If you have no installed Hive yet please follow this tutorial.

Show Tables:

  1. SHOW TABLES;
    SHOW TABLES LIKE '*test*';

Table Creation:

  1. CREATE TABLE test (
    columnA STRING,
    columnB VARCHAR(15),
    columnC INT,
    columnD TIMESTAMP,
    columnE DATE
    )
    STORED AS ORC;

Table Creation with Partitioning:

  1. CREATE TABLE test_partition (
    columnA STRING,
    columnB VARCHAR(15),
    columnC INT,
    columnD TIMESTAMP,
    columnE DATE
    )
    PARTITIONED BY (columnF INT)
    STORED AS ORC;

Inline Table Creation:

  1. CREATE TABLE test_inline STORED AS ORC AS
    SELECT *
    FROM test;

Temporary Table Creation:

  1. CREATE TEMPORARY TABLE temp (
    columnA STRING,
    columnB VARCHAR(15),
    columnC INT,
    columnD TIMESTAMP,
    columnE DATE
    )
    STORED AS ORC;

DESC Table:
This will show you the basic definition of a table.

  1. DESC test;

DESC EXTENDED Table:
This will show you the extended definition of a table.

  1. DESC EXTENDED test;

Drop Table:

  1. DROP TABLE IF EXISTS temp;

Shell: AWK Command

In this tutorial I will show you some useful awk uses.

Determine Line Number

This will find the line number of the text_to_find.

  1. lineNumber=`awk '/TEXT_TO_FIND/{print NR}' /file/path/name.extension`
Split Text By Space

If you want to split a word by a space and then select the second one

  1. val='test test2'
  2. echo $val | awk '{ print $2 }'
Split Text By Comma

If you want to split a word by a space and then select the second one

  1. val='test test2'
  2. echo $val | awk -F, '{ print $2 }'

 

Shell: SED Command

In this tutorial I will show a few useful examples of using “sed”.

-i: inline replace
-e: command to run

Add Line At End

Notice ($) and (a after $). Match last line (‘$’) and append (‘a’)

  1. sudo sed -i -e "\$amy new line" /file/path/name.extension
Match Replace

This will replace the text on the left with the text on the right. Notice the “/” at the beginning and “/c\” in the middle.

  1. sudo sed -i '/myCustomText=/c\myCustomText=newtext' /file/path/name.extension
Remove Matched Line

This will remove the line that matches the text. Notice the “/d” at the end and the “/” at the beginning.

  1. sudo sed -i '/RemoveMyCustomText/d' /file/path/name.extension
Remove All Occurrences

This will remove all occurrences of “NIFI.APACHE.ORG” and replace with “REALM.CA”. Notice “/g” at end for global replace. Notice “s/” at the beginning. If means substitute. The “/” in the middle is used to denote the separation of the text to find and the text to replace.

  1. sudo sed -i 's/NIFI.APACHE.ORG/REALM.CA/g' /file/path/name.extension
Handle ” Quotes / Handle XML / Add Tab

So the below example will go a few things. It will show you how to escape double quotes. It shows you how to add a tab and shows you how to handle xml. It also will add a line under text.

  1. sudo sed -i -e "/<property name=\"Default Realm\">REALM.CA<\/property>/a \ \t<property name=\"Kerberos Config File\">/etc/krb5.conf<\/property>" /file/path/name.extension
Delete Lines By Number
  1. sudo sed -i -e "$lineNumber,$maxLineNumber d" /file/path/name.extension

Kerberos: Commands

In this tutorial I will give you a few useful commands when using Kerberos. If you haven’t installed Kerberos yet go here. I will keep this updated as time goes on. Also note that the commands below have a variety of options. Please go check.

Admin

This will open Kerberos V5 administration system.

  1. kadmin.local
Add Principal

This will add a new principal. -randkey is optional. When specified the encrypted key will be chosen at random instead of derived from a password. Be sure to change USER to whatever your user is.

  1. addprinc -randkey USER/_HOST@REALM.CA
Create KeyTab

This will create a keytab in the directory where you generated it. You should put it in /etc/security/keytabs/ folder. You can also specify the full path (IE: /etc/security/keytabs/USER.keytab). Be sure to change USER to whatever your user is.

  1. xst -k USER.keytab USER/_HOST@REALM.CA
Kinit

When using the -kt uses the keytab to grant a ticket

  1. kinit -kt /etc/security/keytabs/USER.keytab USER/_HOST@REALM.CA
Klist

If you want to see what tickets have been granted. You can issue the below command.

  1. klist
Inline Commands

You can do inline Kerberos commands without first opening kadmin.local. To do so you must specify the “-q” option then in quotes the command to issue. See below.

  1. kadmin.local -q "addprinc -randkey USER/_HOST@REALM.CA"

 

 

 

 

 

 

 

 

NiFi: ExecuteSQL Processor

In this tutorial I will guide you through how to add a processor for querying a SQL table to NiFi.

For this tutorial you will need an AVRO schema called “dttest” and it’s contents are as follows.

  1. {
  2. "type": "record",
  3. "namespace": "com.example",
  4. "name": "FullName",
  5. "fields": [
  6. { "name": "id", "type": "int" },
  7. { "name": "name", "type": "string" }
  8. ]
  9. }

First we need to drag the processor onto the grid.

Next we need select the processor ExecuteSQLRecord.

Next we must configure the processor.

 

 

 

 

 

 

 

 

Now we must create the JsonRecordWriter service.

Now we name the JsonRecordWriter

Configure the JsonWriter

Next we create the DB Connection Service

Next we name the DB Connection Service

Configure the DB Service

Now validate all the settings are as below

Now you are all done. It will now query your table.

NiFi: Kerberized Kafka Consumer Processor

In this tutorial I will guide you through how to add a Kafka consumer to NiFi which is Kerberized.

For this tutorial you will need an AVRO schema called “person” and it’s contents are as follows.

  1. {
  2. "type": "record",
  3. "namespace": "com.example",
  4. "name": "FullName",
  5. "fields": [
  6. { "name": "first_name", "type": "string" },
  7. { "name": "last_name", "type": "string" }
  8. ]
  9. }

When ready you can publish this record to Kafka using the Kafka Producer.

  1. { "first_name": "John", "last_name": "Smith" }

First we need to drag the processor onto the grid.

Next we need select the Kafka Consumer.

Next we configure the processor

 

 

 

 

 

 

 

We will need to create 5 controller services.
First is the Kerberos Service

Next is the SSL Service

Next is the Json Record Reader

Next is the Avro Registry

Next is the Json Record Writer

Now you have finished configuring the services. Ensure your final Kafka Consumer configuration looks like this and you are ready.

Next we need to enable all the controller services

We need to start the processor to start receiving data

Now the record i gave you earlier you can now put to the queue. As you can see the data starts flowing in.

You can now view the queue to see the data.

We are done now and you can start using the consumer.

Phoenix & Java: Connecting Secure

In this tutorial I will show you how to connect to an Secure Phoenix using Java. It’s rather straight forward.

POM.xml

  1. <dependency>
  2. <groupId>org.apache.phoenix</groupId>
  3. <artifactId>phoenix-queryserver</artifactId>
  4. <version>5.0.0-HBase-2.0</version>
  5. </dependency>

Imports:

  1. import java.io.IOException;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;

Initiate Kerberos Authentication

  1. System.setProperty("java.security.krb5.conf", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\krb5.conf");
  2. System.setProperty("java.security.krb5.realm", "REALM.CA");
  3. System.setProperty("java.security.krb5.kdc", "REALM.CA");
  4. System.setProperty("sun.security.krb5.debug", "true");
  5. System.setProperty("javax.net.debug", "all");

Connect:

Now we create the connection.

  1. Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
  2. String url = "jdbc:phoenix:hadoop:2181:/hbase-secure:hbase/hadoop@REALM.CA:\\data\\hbase.service.keytab";
  3. Connection connection = DriverManager.getConnection(url);
  4.  
  5. System.out.println("Connected");
  6.  
  7. Statement statement = connection.createStatement();
  8.  
  9. //Drop table
  10. String deleteTableSql = "DROP TABLE IF EXISTS employee";
  11. System.out.println("Deleting Table: " + deleteTableSql);
  12. statement.executeUpdate(deleteTableSql);
  13. System.out.println("Created Table");
  14. //Create a table
  15. String createTableSql = "CREATE TABLE employee ( eid bigint primary key, name varchar)";
  16. System.out.println("Creating Table: " + createTableSql);
  17. statement.executeUpdate(createTableSql);
  18. System.out.println("Created Table");
  19.  
  20. //Insert Data
  21. String insertTableSql = "UPSERT INTO employee VALUES(1, 'Oliver')";
  22. System.out.println("Inserting Data: " + insertTableSql);
  23. statement.executeUpdate(insertTableSql);
  24. System.out.println("Inserted Data");
  25.  
  26. connection.commit();
  27.  
  28. //Select Data
  29. String selectTablesSql = "select * from employee";
  30. System.out.println("Show records: " + selectTablesSql);
  31. ResultSet res = statement.executeQuery(selectTablesSql);
  32. while (res.next()) {
  33. System.out.println(String.format("id: %s name: %s", res.getInt("eid"), res.getString("name")));
  34. }

 

 

 

 

 

Phoenix: Kerberize Installation

In this tutorial I will show you how to use Kerberos with Phoenix. Before you begin ensure you have installed Kerberos Server, Hadoop, HBase and Zookeeper.

This assumes your hostname is “hadoop”

Install Phoenix

  1. wget http://apache.forsale.plus/phoenix/apache-phoenix-5.0.0-HBase-2.0/bin/apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz
  2. tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz
  3. sudo mv apache-phoenix-5.0.0-HBase-2.0-bin /usr/local/phoenix/
  4. cd /usr/local/phoenix/

Setup .bashrc:

  1. sudo nano ~/.bashrc

Add the following to the end of the file.

#PHOENIX VARIABLES START
export PHOENIX_HOME=/usr/local/phoenix
export PHOENIX_CLASSPATH=$PHOENIX_HOME/*
export PATH=$PATH:$PHOENIX_HOME/bin
#PHOENIX VARIABLES END

  1. source ~/.bashrc

Link Files

  1. ln -sf $HBASE_CONF_DIR/hbase-site.xml $PHOENIX_HOME/bin/hbase-site.xml
  2. ln -sf $HADOOP_CONF_DIR/core-site.xml $PHOENIX_HOME/bin/core-site.xml
  3. ln -sf $PHOENIX_HOME/phoenix-5.0.0-HBase-2.0-server.jar $HBASE_HOME/lib/phoenix-5.0.0-HBase-2.0-server.jar

hbase-env.sh

  1. nano /usr/local/hbase/conf/hbase-env.sh
  2.  
  3. #Ensure the following env variables are set
  4.  
  5. export HADOOP_CONF_DIR=${HADOOP_CONF_DIR:-/usr/local/hadoop/etc/hadoop}
  6. export PHOENIX_CLASSPATH=${PHOENIX_CLASSPATH:-/usr/local/phoenix}
  7. export HBASE_CLASSPATH="$HBASE_CLASSPATH:$CLASSPATH:$HADOOP_CONF_DIR:$PHOENIX_CLASSPATH/phoenix-5.0.0-HBase-2.0-server.jar:$PHOENIX_CLASSPATH/phoenix-core-5.0.0-HBase-2.0.jar:$PHOENIX_CLASSPATH/phoenix-5.0.0-HBase-2.0-client.jar"

hbase-site.xml

  1. nano /usr/local/hbase/conf/hbase-site.xml
  2.  
  3. #Add the following properties
  4.  
  5. <property>
  6. <name>phoenix.functions.allowUserDefinedFunctions</name>
  7. <value>true</value>
  8. <description>enable UDF functions</description>
  9. </property>
  10. <property>
  11. <name>hbase.regionserver.wal.codec</name>
  12. <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
  13. </property>
  14. <property>
  15. <name>hbase.region.server.rpc.scheduler.factory.class</name>
  16. <value>org.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactory</value>
  17. <description>Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates</description>
  18. </property>
  19. <property>
  20. <name>hbase.rpc.controllerfactory.class</name>
  21. <value>org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory</value>
  22. <description>Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates</description>
  23. </property>
  24. <property>
  25. <name>hbase.defaults.for.version.skip</name>
  26. <value>true</value>
  27. </property>
  28. <property>
  29. <name>phoenix.queryserver.http.port</name>
  30. <value>8765</value>
  31. </property>
  32. <property>
  33. <name>phoenix.queryserver.serialization</name>
  34. <value>PROTOBUF</value>
  35. </property>
  36. <property>
  37. <name>phoenix.queryserver.keytab.file</name>
  38. <value>/etc/security/keytabs/hbase.service.keytab</value>
  39. </property>
  40. <property>
  41. <name>phoenix.queryserver.kerberos.principal</name>
  42. <value>hbase/hadoop@REALM.CA</value>
  43. </property>
  44. <property>
  45. <name>hoenix.queryserver.http.keytab.file</name>
  46. <value>/etc/security/keytabs/hbaseHTTP.service.keytab</value>
  47. </property>
  48. <property>
  49. <name>phoenix.queryserver.http.kerberos.principal</name>
  50. <value>hbaseHTTP/hadoop@REALM.CA</value>
  51. </property>
  52. <property>
  53. <name>phoenix.queryserver.dns.nameserver</name>
  54. <value>hadoop</value>
  55. </property>
  56. <property>
  57. <name>phoenix.queryserver.dns.interface</name>
  58. <value>enp0s3</value>
  59. </property>
  60. <property>
  61. <name>phoenix.schema.mapSystemTablesToNamespace</name>
  62. <value>true</value>
  63. </property>
  64. <property>
  65. <name>phoenix.schema.isNamespaceMappingEnabled</name>
  66. <value>true</value>
  67. </property>

sqlline.py

  1. sqlline.py hadoop:2181:/hbase-secure:hbase/hadoop@GAUDREAULT_KDC.CA:/etc/security/keytabs/hbase.service.keytab

 

HBASE & Java: Connecting Secure

In this tutorial I will show you how to connect to an Secure HBASE using Java. It’s rather straight forward.

Import SSL Cert to Java:

Follow this tutorial to “Installing unlimited strength encryption Java libraries

If on Windows do the following

  1. #Import it
  2. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -import -file hadoop.csr -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts" -alias "hadoop"
  3.  
  4. #Check it
  5. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -list -v -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts"
  6.  
  7. #If you want to delete it
  8. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -delete -alias hadoop -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts"

POM.xml

  1. <dependency>
  2. <groupId>org.apache.hbase</groupId>
  3. <artifactId>hbase-client</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hbase</groupId>
  8. <artifactId>hbase</artifactId>
  9. <version>2.1.0</version>
  10. <type>pom</type>
  11. </dependency>

Imports:

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.hbase.client.Admin;
  4. import org.apache.hadoop.hbase.client.Connection;
  5. import org.apache.hadoop.hbase.client.ConnectionFactory;
  6. import org.apache.hadoop.security.UserGroupInformation;

Initiate Kerberos Authentication

  1. System.setProperty("java.security.auth.login.config", "C:\\data\\kafkaconnect\\kafka\\src\\main\\resources\\client_jaas.conf");
  2. System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
  3. System.setProperty("java.security.krb5.conf", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\krb5.conf");
  4. System.setProperty("java.security.krb5.realm", "REALM.CA");
  5. System.setProperty("java.security.krb5.kdc", "REALM.CA");
  6. System.setProperty("sun.security.krb5.debug", "false");
  7. System.setProperty("javax.net.debug", "false");
  8. System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
  9. System.setProperty("javax.net.ssl.keyStore", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\cacerts");
  10. System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\cacerts");
  11. System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
  12. System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");

Config:

We will use the basic configuration here. You should secure the cluster and use appropriate settings for that.

  1. // Setup the configuration object.
  2. final Configuration config = HBaseConfiguration.create();
  3. config.set("hbase.zookeeper.quorum", "hadoop");
  4. config.set("hbase.zookeeper.property.clientPort", "2181");
  5. config.set("hadoop.security.authentication", "kerberos");
  6. config.set("hbase.security.authentication", "kerberos");
  7. config.set("hbase.cluster.distributed", "true");
  8. config.set("hbase.rpc.protection", "integrity");
  9. config.set("zookeeper.znode.parent", "/hbase-secure");
  10. config.set("hbase.master.kerberos.principal", "hbase/hadoop@REALM.CA");
  11. config.set("hbase.regionserver.kerberos.principal", "hbase/hadoop@REALM.CA");

Connect:

Now we create the connection.

  1. UserGroupInformation.setConfiguration(config);
  2. UserGroupInformation.setLoginUser(UserGroupInformation.loginUserFromKeytabAndReturnUGI("hbase/hadoop@REALM.CA", "c:\\data\\hbase.service.keytab"));
  3.  
  4. System.out.println(UserGroupInformation.getLoginUser());
  5. System.out.println(UserGroupInformation.getCurrentUser());
  6.  
  7. Connection conn = ConnectionFactory.createConnection(config);
  8.  
  9. //Later when we are done we will want to close the connection.
  10. conn.close();

Hbase Admin:

Retrieve an Admin implementation to administer an HBase cluster. If you need it.

  1. Admin admin = conn.getAdmin();
  2. //Later when we are done we will want to close the connection.
  3. admin.close();

HBase: Kerberize/SSL Installation

In this tutorial I will show you how to use Kerberos/SSL with HBase. I will use self signed certs for this example. Before you begin ensure you have installed Kerberos Server, Hadoop and Zookeeper.

This assumes your hostname is “hadoop”

We will install a Master, RegionServer and Rest Client

Create Kerberos Principals

  1. cd /etc/security/keytabs/
  2.  
  3. sudo kadmin.local
  4.  
  5. #You can list princepals
  6. listprincs
  7.  
  8. #Create the following principals
  9. addprinc -randkey hbase/hadoop@REALM.CA
  10. addprinc -randkey hbaseHTTP/hadoop@REALM.CA
  11.  
  12. #Create the keytab files.
  13. #You will need these for Hadoop to be able to login
  14. xst -k hbase.service.keytab hbase/hadoop@REALM.CA
  15. xst -k hbaseHTTP.service.keytab hbaseHTTP/hadoop@REALM.CA

Set Keytab Permissions/Ownership

  1. sudo chown root:hadoopuser /etc/security/keytabs/*
  2. sudo chmod 750 /etc/security/keytabs/*

Install HBase

  1. wget http://apache.forsale.plus/hbase/2.1.0/hbase-2.1.0-bin.tar.gz
  2. tar -zxvf hbase-2.1.0-bin.tar.gz
  3. sudo mv hbase-2.1.0 /usr/local/hbase/
  4. cd /usr/local/hbase/conf/

Setup .bashrc:

  1. sudo nano ~/.bashrc

Add the following to the end of the file.

#HBASE VARIABLES START
export HBASE_HOME=/usr/local/hbase
export PATH=$PATH:$HBASE_HOME/bin
export HBASE_CONF_DIR=$HBASE_HOME/conf
#HBASE VARIABLES END

  1. source ~/.bashrc

hbase_client_jaas.conf

  1. Client {
  2. com.sun.security.auth.module.Krb5LoginModule required
  3. useKeyTab=false
  4. useTicketCache=true;
  5. };

hbase_server_jaas.conf

  1. Client {
  2. com.sun.security.auth.module.Krb5LoginModule required
  3. useKeyTab=true
  4. useTicketCache=false
  5. keyTab="/etc/security/keytabs/hbase.service.keytab"
  6. principal="hbase/hadoop@REALM.CA";
  7. };

regionservers

  1. hadoop

hbase-env.sh

Add or modify the following settings.

  1. export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
  2. export HBASE_CONF_DIR=${HBASE_CONF_DIR:-/usr/local/hbase/conf}
  3. export HADOOP_CONF_DIR=${HADOOP_CONF_DIR:-/usr/local/hadoop/etc/hadoop}
  4. export HBASE_CLASSPATH="$CLASSPATH:$HADOOP_CONF_DIR"
  5. export HBASE_REGIONSERVERS=${HBASE_CONF_DIR}/regionservers
  6. export HBASE_LOG_DIR=${HBASE_HOME}/logs
  7. export HBASE_PID_DIR=/home/hadoopuser
  8. export HBASE_MANAGES_ZK=false
  9. export HBASE_OPTS="-Djava.security.auth.login.config=$HBASE_CONF_DIR/hbase_client_jaas.conf"
  10. export HBASE_MASTER_OPTS="-Djava.security.auth.login.config=$HBASE_CONF_DIR/hbase_server_jaas.conf"
  11. export HBASE_REGIONSERVER_OPTS="-Djava.security.auth.login.config=$HBASE_CONF_DIR/hbase_server_jaas.conf"

hbase-site.xml

  1. <configuration>
  2. <property>
  3. <name>hbase.rootdir</name>
  4. <value>hdfs://hadoop:54310/hbase</value>
  5. </property>
  6. <property>
  7. <name>hbase.zookeeper.property.dataDir</name>
  8. <value>/usr/local/zookeeper/data</value>
  9. </property>
  10. <property>
  11. <name>hbase.cluster.distributed</name>
  12. <value>true</value>
  13. </property>
  14. <property>
  15. <name>hbase.regionserver.kerberos.principal</name>
  16. <value>hbase/_HOST@REALM.CA</value>
  17. </property>
  18. <property>
  19. <name>hbase.regionserver.keytab.file</name>
  20. <value>/etc/security/keytabs/hbase.service.keytab</value>
  21. </property>
  22. <property>
  23. <name>hbase.master.kerberos.principal</name>
  24. <value>hbase/_HOST@REALM.CA</value>
  25. </property>
  26. <property>
  27. <name>hbase.master.keytab.file</name>
  28. <value>/etc/security/keytabs/hbase.service.keytab</value>
  29. </property>
  30. <property>
  31. <name>hbase.security.authentication.spnego.kerberos.principal</name>
  32. <value>hbaseHTTP/_HOST@REALM.CA</value>
  33. </property>
  34. <property>
  35. <name>hbase.security.authentication.spnego.kerberos.keytab</name>
  36. <value>/etc/security/keytabs/hbaseHTTP.service.keytab</value>
  37. </property>
  38. <property>
  39. <name>hbase.security.authentication</name>
  40. <value>kerberos</value>
  41. </property>
  42. <property>
  43. <name>hbase.security.authorization</name>
  44. <value>true</value>
  45. </property>
  46. <property>
  47. <name>hbase.coprocessor.region.classes</name>
  48. <value>org.apache.hadoop.hbase.security.token.TokenProvider</value>
  49. </property>
  50. <property>
  51. <name>hbase.rpc.protection</name>
  52. <value>integrity</value>
  53. </property>
  54. <property>
  55. <name>hbase.rpc.engine</name>
  56. <value>org.apache.hadoop.hbase.ipc.SecureRpcEngine</value>
  57. </property>
  58. <property>
  59. <name>hbase.coprocessor.master.classes</name>
  60. <value>org.apache.hadoop.hbase.security.access.AccessController</value>
  61. </property>
  62. <property>
  63. <name>hbase.coprocessor.region.classes</name>
  64. <value>org.apache.hadoop.hbase.security.token.TokenProvider,org.apache.hadoop.hbase.security.access.AccessController</value>
  65. </property>
  66. <property>
  67. <name>hbase.security.authentication.ui</name>
  68. <value>kerberos</value>
  69. <description>Controls what kind of authentication should be used for the HBase web UIs.</description>
  70. </property>
  71. <property>
  72. <name>hbase.master.port</name>
  73. <value>16000</value>
  74. </property>
  75. <property>
  76. <name>hbase.master.info.bindAddress</name>
  77. <value>0.0.0.0</value>
  78. </property>
  79. <property>
  80. <name>hbase.master.info.port</name>
  81. <value>16010</value>
  82. </property>
  83. <property>
  84. <name>hbase.regionserver.hostname</name>
  85. <value>hadoop</value>
  86. </property>
  87. <property>
  88. <name>hbase.regionserver.port</name>
  89. <value>16020</value>
  90. </property>
  91. <property>
  92. <name>hbase.regionserver.info.port</name>
  93. <value>16030</value>
  94. </property>
  95. <property>
  96. <name>hbase.regionserver.info.bindAddress</name>
  97. <value>0.0.0.0</value>
  98. </property>
  99. <property>
  100. <name>hbase.master.ipc.address</name>
  101. <value>0.0.0.0</value>
  102. </property>
  103. <property>
  104. <name>hbase.regionserver.ipc.address</name>
  105. <value>0.0.0.0</value>
  106. </property>
  107. <property>
  108. <name>hbase.ssl.enabled</name>
  109. <value>true</value>
  110. </property>
  111. <property>
  112. <name>hadoop.ssl.enabled</name>
  113. <value>true</value>
  114. </property>
  115. <property>
  116. <name>ssl.server.keystore.keypassword</name>
  117. <value>startrek</value>
  118. </property>
  119. <property>
  120. <name>ssl.server.keystore.password</name>
  121. <value>startrek</value>
  122. </property>
  123. <property>
  124. <name>ssl.server.keystore.location</name>
  125. <value>/etc/security/serverKeys/keystore.jks</value>
  126. </property>
  127. <property>
  128. <name>hbase.rest.ssl.enabled</name>
  129. <value>true</value>
  130. </property>
  131. <property>
  132. <name>hbase.rest.ssl.keystore.store</name>
  133. <value>/etc/security/serverKeys/keystore.jks</value>
  134. </property>
  135. <property>
  136. <name>hbase.rest.ssl.keystore.password</name>
  137. <value>startrek</value>
  138. </property>
  139. <property>
  140. <name>hbase.rest.ssl.keystore.keypassword</name>
  141. <value>startrek</value>
  142. </property>
  143. <property>
  144. <name>hbase.superuser</name>
  145. <value>hduser</value>
  146. </property>
  147. <property>
  148. <name>hbase.tmp.dir</name>
  149. <value>/tmp/hbase-${user.name}</value>
  150. </property>
  151. <property>
  152. <name>hbase.local.dir</name>
  153. <value>${hbase.tmp.dir}/local</value>
  154. </property>
  155. <property>
  156. <name>hbase.zookeeper.property.clientPort</name>
  157. <value>2181</value>
  158. </property>
  159. <property>
  160. <name>hbase.unsafe.stream.capability.enforce</name>
  161. <value>false</value>
  162. </property>
  163. <property>
  164. <name>hbase.zookeeper.quorum</name>
  165. <value>hadoop</value>
  166. </property>
  167. <property>
  168. <name>zookeeper.znode.parent</name>
  169. <value>/hbase-secure</value>
  170. </property>
  171. <property>
  172. <name>hbase.regionserver.dns.interface</name>
  173. <value>enp0s3</value>
  174. </property>
  175. <property>
  176. <name>hbase.rest.authentication.type</name>
  177. <value>kerberos</value>
  178. </property>
  179. <property>
  180. <name>hadoop.proxyuser.HTTP.groups</name>
  181. <value>*</value>
  182. </property>
  183. <property>
  184. <name>hadoop.proxyuser.HTTP.hosts</name>
  185. <value>*</value>
  186. </property>
  187. <property>
  188. <name>hbase.rest.authentication.kerberos.keytab</name>
  189. <value>/etc/security/keytabs/hbaseHTTP.service.keytab</value>
  190. </property>
  191. <property>
  192. <name>hbase.rest.authentication.kerberos.principal</name>
  193. <value>hbaseHTTP/_HOST@REALM.CA</value>
  194. </property>
  195. <property>
  196. <name>hbase.rest.kerberos.principal</name>
  197. <value>hbase/_HOST@REALM.CA</value>
  198. </property>
  199. <property>
  200. <name>hbase.rest.keytab.file</name>
  201. <value>/etc/security/keytabs/hbase.service.keytab</value>
  202. </property>
  203. </configuration>

Change Ownership of HBase files

  1. sudo chown hadoopuser:hadoopuser -R /usr/local/hbase/*

Hadoop HDFS Config Changes

You will need to add two properties into the core-site.xml file of Hadoop.

  1. nano /usr/local/hadoop/etc/hadoop/core-site.xml
  2.  
  3. <property>
  4. <name>hadoop.proxyuser.hbase.hosts</name>
  5. <value>*</value>
  6. </property>
  7. <property>
  8. <name>hadoop.proxyuser.hbase.groups</name>
  9. <value>*</value>
  10. </property>
  11. <property>
  12. <name>hadoop.proxyuser.HTTP.hosts</name>
  13. <value>*</value>
  14. </property>
  15. <property>
  16. <name>hadoop.proxyuser.HTTP.groups</name>
  17. <value>*</value>
  18. </property>

AutoStart

  1. crontab -e
  2.  
  3. @reboot /usr/local/hbase/bin/hbase-daemon.sh --config /usr/local/hbase/conf/ start master
  4. @reboot /usr/local/hbase/bin/hbase-daemon.sh --config /usr/local/hbase/conf/ start regionserver
  5. @reboot /usr/local/hbase/bin/hbase-daemon.sh --config /usr/local/hbase/conf/ start rest --infoport 17001 -p 17000

Validation

  1. kinit -kt /etc/security/keytabs/hbase.service.keytab hbase/hadoop@REALM.ca
  2. hbase shell
  3. status 'detailed'
  4. whoami
  5. kdestroy

References

https://hbase.apache.org/0.94/book/security.html
https://pivotalhd-210.docs.pivotal.io/doc/2100/webhelp/topics/ConfiguringSecureHBase.html
https://ambari.apache.org/1.2.5/installing-hadoop-using-ambari/content/ambari-kerb-2-3-2-1.html
https://hbase.apache.org/book.html#_using_secure_http_https_for_the_web_ui

Zookeeper Kerberos Installation

We are going to install Zookeeper. Ensure you install Kerberos.

This assumes your hostname is “hadoop”

Install Java JDK

  1. apt-get update
  2. apt-get upgrade
  3. apt-get install default-jdk

Download Zookeeper:

  1. wget http://apache.forsale.plus/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
  2. tar -zxvf zookeeper-3.4.13.tar.gz
  3. sudo mv zookeeper-3.4.13 /usr/local/zookeeper/
  4. sudo chown -R root:hadoopuser /usr/local/zookeeper/

Setup .bashrc:

  1. sudo nano ~/.bashrc

Add the following to the end of the file.

#ZOOKEEPER VARIABLES START
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
#ZOOKEEPER VARIABLES STOP

  1. source ~/.bashrc

Create Kerberos Principals

  1. cd /etc/security/keytabs
  2. sudo kadmin.local
  3. addprinc -randkey zookeeper/hadoop@REALM.CA
  4. xst -kt zookeeper.service.keytab zookeeper/hadoop@REALM.CA
  5. q

Set Keytab Permissions/Ownership

  1. sudo chown root:hadoopuser /etc/security/keytabs/*
  2. sudo chmod 750 /etc/security/keytabs/*

zoo.cfg

  1. cd /usr/local/zookeeper/conf/
  2. cp zoo_sample.cfg zoo.cfg
  3. nano zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to “0” to disable auto purge feature
#autopurge.purgeInterval=1

server.1=hadoop:2888:3888

authProvider.1 = org.apache.zookeeper.server.auth.SASLAuthenticationProvider
kerberos.removeHostFromPrincipal = true
kerberos.removeRealmFromPrincipal = true
jaasLoginRenew=3600000

java.env

  1. cd /usr/local/zookeeper/conf/
  2. touch java.env
  3. nano java.env

ZOO_LOG4J_PROP=”INFO,ROLLINGFILE”
ZOO_LOG_DIR=”/usr/local/zookeeper/logs”

zookeeper_client_jaas.conf

  1. cd /usr/local/zookeeper/conf/
  2. touch zookeeper_client_jaas.conf
  3. nano zookeeper_client_jaas.conf

Client {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=false
useTicketCache=true;
};

zookeeper_jaas.conf

  1. cd /usr/local/zookeeper/conf/
  2. touch zookeeper_jaas.conf
  3. nano zookeeper_jaas.conf

Server {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
storeKey=true
useTicketCache=false
keyTab=”/etc/security/keytabs/zookeeper.service.keytab”
principal=”zookeeper/hadoop@REALM.CA”;
};

zkServer.sh

  1. cd /usr/local/zookeeper/bin/
  2. nano zkServer.sh
  3.  
  4. #Add the following at the top
  5.  
  6. export CLIENT_JVMFLAGS="-Djava.security.auth.login.config=/usr/local/zookeeper/conf/zookeeper_client_jaas.conf"
  7. export SERVER_JVMFLAGS="-Xmx1024m -Djava.security.auth.login.config=/usr/local/zookeeper/conf/zookeeper_jaas.conf"

zkCli.sh

  1. cd /usr/local/zookeeper/bin/
  2. nano zkCli.sh
  3.  
  4. #Add the following at the top
  5.  
  6. export CLIENT_JVMFLAGS="-Djava.security.auth.login.config=/usr/local/zookeeper/conf/zookeeper_client_jaas.conf"
  7. export SERVER_JVMFLAGS="-Xmx1024m -Djava.security.auth.login.config=/usr/local/zookeeper/conf/zookeeper_jaas.conf"

MkDir

  1. mkdir /usr/local/zookeeper/data/
  2. mkdir /usr/local/zookeeper/logs/
  3.  
  4. echo "1" > /usr/local/zookeeper/data/myid
  5.  
  6. sudo chown -R hduser:hduser /usr/local/zookeeper

Auto Start

  1. crontab -e
  2.  
  3. #Add the following
  4. @reboot /usr/local/zookeeper/bin/zkServer.sh start

Run Client

  1. kinit -kt /etc/security/keytabs/zookeeper.service.keytab zookeeper/hadoop@REALM.CA
  2. ./zkCli.sh -server 127.0.0.1:2181
  3.  
  4. #Now you can list all directories
  5. ls /
  6.  
  7. #Or delete directories
  8.  
  9. rmr /folder

References

https://my-bigdata-blog.blogspot.com/2017/07/apache-Zookeeper-install-Ubuntu.html
https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.2/bk_command-line-installation/content/zookeeper_configuration.html
https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.2/bk_command-line-installation/content/securing_zookeeper_with_kerberos.html

 

 

 

Kafka & Java: Secured Consumer Read Record

In this tutorial I will show you how to read a record to Kafka. Before you begin you will need Maven/Eclipse all setup and a project ready to go. If you haven’t installed Kafka Kerberos yet please do so.

Import SSL Cert to Java:

Follow this tutorial to “Installing unlimited strength encryption Java libraries

If on Windows do the following

  1. #Import it
  2. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -import -file hadoop.csr -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts" -alias "hadoop"
  3.  
  4. #Check it
  5. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -list -v -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts"
  6.  
  7. #If you want to delete it
  8. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -delete -alias hadoop -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts"

POM.xml

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>1.1.0</version>
  5. </dependency>

Imports

  1. import org.apache.kafka.clients.consumer.*;
  2. import java.util.Properties;
  3. import java.io.InputStream;
  4. import java.util.Arrays;

Consumer JAAS Conf (client_jaas.conf)

  1. KafkaClient {
  2. com.sun.security.auth.module.Krb5LoginModule required
  3. useTicketCache=false
  4. refreshKrb5Config=true
  5. debug=true
  6. useKeyTab=true
  7. storeKey=true
  8. keyTab="c:\\data\\kafka.service.keytab"
  9. principal="kafka/hadoop@REALM.CA";
  10. };

Consumer Props File

You can go here to view all the options for consumer properties.

  1. bootstrap.servers=hadoop:9094
  2. group.id=test
  3.  
  4. security.protocol=SASL_SSL
  5. sasl.kerberos.service.name=kafka
  6.  
  7. #offset will be periodically committed in the background
  8. enable.auto.commit=true
  9.  
  10. # The serializer for the key
  11. key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
  12.  
  13. # The serializer for the value
  14. value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
  15.  
  16. # heartbeat to detect worker failures
  17. session.timeout.ms=10000
  18.  
  19. #Automatically reset offset to earliest offset
  20. auto.offset.reset=earliest

Initiate Kerberos Authentication

  1. System.setProperty("java.security.auth.login.config", "C:\\data\\kafkaconnect\\kafka\\src\\main\\resources\\client_jaas.conf");
  2. System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
  3. System.setProperty("java.security.krb5.conf", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\krb5.conf");
  4. System.setProperty("java.security.krb5.realm", "REALM.CA");
  5. System.setProperty("java.security.krb5.kdc", "REALM.CA");
  6. System.setProperty("sun.security.krb5.debug", "false");
  7. System.setProperty("javax.net.debug", "false");
  8. System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
  9. System.setProperty("javax.net.ssl.keyStore", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\cacerts");
  10. System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\cacerts");
  11. System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
  12. System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");

Consumer Connection/Send

The record we will read will just be a string for both key and value.

  1. Consumer<String, String> consumer = null;
  2.  
  3. try {
  4. ClassLoader classLoader = getClass().getClassLoader();
  5.  
  6. try (InputStream props = classLoader.getResourceAsStream("consumer.props")) {
  7. Properties properties = new Properties();
  8. properties.load(props);
  9. consumer = new KafkaConsumer<>(properties);
  10. }
  11. System.out.println("Consumer Created");
  12.  
  13. // Subscribe to the topic.
  14. consumer.subscribe(Arrays.asList("testTopic"));
  15.  
  16. while (true) {
  17. final ConsumerRecords<String, String> consumerRecords = consumer.poll(1000);
  18. if (consumerRecords.count() == 0) {
  19. //Keep reading till no records
  20. break;
  21. }
  22.  
  23. consumerRecords.forEach(record -> {
  24. System.out.printf("Consumer Record:(%s, %s, %d, %d)\n", record.key(), record.value(), record.partition(), record.offset());
  25. });
  26.  
  27. //Commit offsets returned on the last poll() for all the subscribed list of topics and partition
  28. consumer.commitAsync();
  29. }
  30. } finally {
  31. consumer.close();
  32. }
  33. System.out.println("Consumer Closed");

References

I used kafka-sample-programs as a guide for setting up props.

Kafka: Kerberize/SSL

In this tutorial I will show you how to use Kerberos/SSL with NiFi. I will use self signed certs for this example. Before you begin ensure you have installed Kerberos Server and Kafka.

If you don’t want to use the built in Zookeeper you can setup your own. To do that following this tutorial.

This assumes your hostname is “hadoop”

Create Kerberos Principals

  1. cd /etc/security/keytabs/
  2.  
  3. sudo kadmin.local
  4.  
  5. #You can list princepals
  6. listprincs
  7.  
  8. #Create the following principals
  9. addprinc -randkey kafka/hadoop@REALM.CA
  10. addprinc -randkey zookeeper/hadoop@REALM.CA
  11.  
  12. #Create the keytab files.
  13. #You will need these for Hadoop to be able to login
  14. xst -k kafka.service.keytab kafka/hadoop@REALM.CA
  15. xst -k zookeeper.service.keytab zookeeper/hadoop@REALM.CA

Set Keytab Permissions/Ownership

  1. sudo chown root:hadoopuser /etc/security/keytabs/*
  2. sudo chmod 750 /etc/security/keytabs/*

Hosts Update

  1. sudo nano /etc/hosts
  2.  
  3. #Remove 127.0.1.1 line
  4.  
  5. #Change 127.0.0.1 to the following
  6. 127.0.0.1 realm.ca hadoop localhost

Ubuntu Firewall

  1. sudo ufw disable

SSL

Setup SSL Directories if you have not previously done so.

  1. sudo mkdir -p /etc/security/serverKeys
  2. sudo chown -R root:hadoopuser /etc/security/serverKeys/
  3. sudo chmod 755 /etc/security/serverKeys/
  4.  
  5. cd /etc/security/serverKeys

Setup Keystore

  1. sudo keytool -genkey -alias NAMENODE -keyalg RSA -keysize 1024 -dname "CN=NAMENODE,OU=ORGANIZATION_UNIT,C=canada" -keypass PASSWORD -keystore /etc/security/serverKeys/keystore.jks -storepass PASSWORD
  2. sudo keytool -export -alias NAMENODE -keystore /etc/security/serverKeys/keystore.jks -rfc -file /etc/security/serverKeys/NAMENODE.csr -storepass PASSWORD

Setup Truststore

  1. sudo keytool -import -noprompt -alias NAMENODE -file /etc/security/serverKeys/NAMENODE.csr -keystore /etc/security/serverKeys/truststore.jks -storepass PASSWORD

Generate Self Signed Certifcate

  1. sudo openssl genrsa -out /etc/security/serverKeys/NAMENODE.key 2048
  2.  
  3. sudo openssl req -x509 -new -key /etc/security/serverKeys/NAMENODE.key -days 300 -out /etc/security/serverKeys/NAMENODE.pem
  4.  
  5. sudo keytool -keystore /etc/security/serverKeys/keystore.jks -alias NAMENODE -certreq -file /etc/security/serverKeys/NAMENODE.cert -storepass PASSWORD -keypass PASSWORD
  6.  
  7. sudo openssl x509 -req -CA /etc/security/serverKeys/NAMENODE.pem -CAkey /etc/security/serverKeys/NAMENODE.key -in /etc/security/serverKeys/NAMENODE.cert -out /etc/security/serverKeys/NAMENODE.signed -days 300 -CAcreateserial

Setup File Permissions

  1. sudo chmod 440 /etc/security/serverKeys/*
  2. sudo chown root:hadoopuser /etc/security/serverKeys/*

Edit server.properties Config

  1. cd /usr/local/kafka/config
  2.  
  3. sudo nano server.properties
  4.  
  5. #Edit or Add the following properties.
  6. ssl.endpoint.identification.algorithm=HTTPS
  7. ssl.enabled.protocols=TLSv1.2,TLSv1.1,TLSv1
  8. ssl.key.password=PASSWORD
  9. ssl.keystore.location=/etc/security/serverKeys/keystore.jks
  10. ssl.keystore.password=PASSWORD
  11. ssl.truststore.location=/etc/security/serverKeys/truststore.jks
  12. ssl.truststore.password=PASSWORD
  13. listeners=SASL_SSL://:9094
  14. security.inter.broker.protocol=SASL_SSL
  15. ssl.client.auth=required
  16. authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
  17. ssl.keystore.type=JKS
  18. ssl.truststore.type=JKS
  19. sasl.kerberos.service.name=kafka
  20. zookeeper.connect=hadoop:2181
  21. sasl.mechanism.inter.broker.protocol=GSSAPI
  22. sasl.enabled.mechanisms=GSSAPI

Edit zookeeper.properties Config

  1. sudo nano zookeeper.properties
  2.  
  3. #Edit or Add the following properties.
  4.  
  5. server.1=hadoop:2888:3888
  6. clientPort=2181
  7. authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
  8. requireClientAuthScheme=SASL
  9. jaasLoginRenew=3600000

Edit producer.properties Config

  1. sudo nano producer.properties
  2.  
  3. bootstrap.servers=hadoop:9094
  4. security.protocol=SASL_SSL
  5. sasl.kerberos.service.name=kafka
  6. ssl.truststore.location=/etc/security/serverKeys/truststore.jks
  7. ssl.truststore.password=PASSWORD
  8. ssl.keystore.location=/etc/security/serverKeys/keystore.jks
  9. ssl.keystore.password=PASSWORD
  10. ssl.key.password=PASSWORD
  11. sasl.mechanism=GSSAPI

Edit consumer.properties Config

  1. sudo nano consumer.properties
  2.  
  3. zookeeper.connect=hadoop:2181
  4. bootstrap.servers=hadoop:9094
  5. group.id=securing-kafka-group
  6. security.protocol=SASL_SSL
  7. sasl.kerberos.service.name=kafka
  8. ssl.truststore.location=/etc/security/serverKeys/truststore.jks
  9. ssl.truststore.password=PASSWORD
  10. sasl.mechanism=GSSAPI

Add zookeeper_jass.conf Config

  1. sudo nano zookeeper_jass.conf
  2.  
  3. Server {
  4. com.sun.security.auth.module.Krb5LoginModule required
  5. debug=true
  6. useKeyTab=true
  7. keyTab="/etc/security/keytabs/zookeeper.service.keytab"
  8. storeKey=true
  9. useTicketCache=true
  10. refreshKrb5Config=true
  11. principal="zookeeper/hadoop@REALM.CA";
  12. };

Add kafkaserver_jass.conf Config

  1. sudo nano kafkaserver_jass.conf
  2.  
  3. KafkaServer {
  4. com.sun.security.auth.module.Krb5LoginModule required
  5. debug=true
  6. useKeyTab=true
  7. storeKey=true
  8. refreshKrb5Config=true
  9. keyTab="/etc/security/keytabs/kafka.service.keytab"
  10. principal="kafka/hadoop@REALM.CA";
  11. };
  12.  
  13. kafkaClient {
  14. com.sun.security.auth.module.Krb5LoginModule required
  15. useTicketCache=true
  16. refreshKrb5Config=true
  17. debug=true
  18. useKeyTab=true
  19. storeKey=true
  20. keyTab="/etc/security/keytabs/kafka.service.keytab"
  21. principal="kafka/hadoop@REALM.CA";
  22. };

Edit kafka-server-start.sh

  1. cd /usr/local/kafka/bin/
  2.  
  3. sudo nano kafka-server-start.sh
  4.  
  5. jaas="$base_dir/../config/kafkaserver_jaas.conf"
  6.  
  7. export KAFKA_OPTS="-Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=$jaas"

Edit zookeeper-server-start.sh

  1. sudo nano zookeeper-server-start.sh
  2.  
  3. jaas="$base_dir/../config/zookeeper_jaas.conf"
  4.  
  5. export KAFKA_OPTS="-Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=$jaas"

Kafka-ACL

  1. cd /usr/local/kafka/bin/
  2.  
  3. #Grant topic access and cluster access
  4. ./kafka-acls.sh --operation All --allow-principal User:kafka --authorizer-properties zookeeper.connect=hadoop:2181 --add --cluster
  5. ./kafka-acls.sh --operation All --allow-principal User:kafka --authorizer-properties zookeeper.connect=hadoop:2181 --add --topic TOPIC
  6.  
  7. #Grant all groups for a specific topic
  8. ./kafka-acls.sh --operation All --allow-principal User:kafka --authorizer-properties zookeeper.connect=hadoop:2181 --add --topic TOPIC --group *
  9.  
  10. #If you want to remove cluster access
  11. ./kafka-acls.sh --authorizer-properties zookeeper.connect=hadoop:2181 --remove --cluster
  12.  
  13. #If you want to remove topic access
  14. ./kafka-acls.sh --authorizer-properties zookeeper.connect=hadoop:2181 --remove --topic TOPIC
  15.  
  16. #List access for cluster
  17. ./kafka-acls.sh --list --authorizer-properties zookeeper.connect=hadoop:2181 --cluster
  18.  
  19. #List access for topic
  20. ./kafka-acls.sh --list --authorizer-properties zookeeper.connect=hadoop:2181 --topic TOPIC

kafka-console-producer.sh

If you want to test using the console producer you need to make these changes.

  1. cd /usr/local/kafka/bin/
  2. nano kafka-console-producer.sh
  3.  
  4. #Add the below before the last line
  5.  
  6. base_dir=$(dirname $0)
  7. jaas="$base_dir/../config/kafkaserver_jaas.conf"
  8. export KAFKA_OPTS="-Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=$jaas"
  9.  
  10.  
  11. #Now you can run the console producer
  12. ./kafka-console-producer.sh --broker-list hadoop:9094 --topic TOPIC -producer.config ../config/producer.properties

kafka-console-consumer.sh

If you want to test using the console consumer you need to make these changes.

  1. cd /usr/local/kafka/bin/
  2. nano kafka-console-consumer.sh
  3.  
  4. #Add the below before the last line
  5.  
  6. base_dir=$(dirname $0)
  7. jaas="$base_dir/../config/kafkaserver_jaas.conf"
  8. export KAFKA_OPTS="-Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=$jaas"
  9.  
  10.  
  11. #Now you can run the console consumer
  12. ./kafka-console-consumer.sh --bootstrap-server hadoop:9094 --topic TOPIC --consumer.config ../config/consumer.properties --from-beginning

References

https://www.confluent.io/blog/apache-kafka-security-authorization-authentication-encryption/
https://github.com/confluentinc/securing-kafka-blog/blob/master/manifests/default.pp

Kafka & Java: Consumer Seek To Beginning

This is a quick tutorial on how to seek to beginning using a Kafka consumer. If you haven’t setup the consumer yet follow this tutorial.

This is all that is required once you have setup the consumer. This will put the kafka offset for the topic of your choice to the beginning so once you start reading you will get all records.

  1. consumer.seekToBeginning(consumer.assignment());

Hive & Java: Connect to Remote Kerberos Hive using KeyTab

In this tutorial I will show you how to connect to remote Kerberos Hive cluster using Java. If you haven’t install Hive yet follow the tutorial.

Import SSL Cert to Java:

Follow this tutorial to “Installing unlimited strength encryption Java libraries

If on Windows do the following

  1. #Import it
  2. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -import -file hadoop.csr -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts" -alias "hadoop"
  3.  
  4. #Check it
  5. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -list -v -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts"
  6.  
  7. #If you want to delete it
  8. "C:\Program Files\Java\jdk1.8.0_171\bin\keytool" -delete -alias hadoop -keystore "C:\Program Files\Java\jdk1.8.0_171\jre\lib\security\cacerts"

POM.xml:

  1. <dependency>
  2. <groupId>org.apache.hive</groupId>
  3. <artifactId>hive-jdbc</artifactId>
  4. <version>2.3.3</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>jdk.tools</groupId>
  8. <artifactId>jdk.tools</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>

Imports:

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.security.UserGroupInformation;
  3. import java.sql.SQLException;
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import java.sql.DriverManager;

Connect:

  1. // Setup the configuration object.
  2. final Configuration config = new Configuration();
  3.  
  4. config.set("fs.defaultFS", "swebhdfs://hadoop:50470");
  5. config.set("hadoop.security.authentication", "kerberos");
  6. config.set("hadoop.rpc.protection", "integrity");
  7.  
  8. System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
  9. System.setProperty("java.security.krb5.conf", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\krb5.conf");
  10. System.setProperty("java.security.krb5.realm", "REALM.CA");
  11. System.setProperty("java.security.krb5.kdc", "REALM.CA");
  12. System.setProperty("sun.security.krb5.debug", "true");
  13. System.setProperty("javax.net.debug", "all");
  14. System.setProperty("javax.net.ssl.keyStorePassword","changeit");
  15. System.setProperty("javax.net.ssl.keyStore","C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\cacerts");
  16. System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\Java\\jdk1.8.0_171\\jre\\lib\\security\\cacerts");
  17. System.setProperty("javax.net.ssl.trustStorePassword","changeit");
  18. System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  19.  
  20. UserGroupInformation.setConfiguration(config);
  21. UserGroupInformation.setLoginUser(UserGroupInformation.loginUserFromKeytabAndReturnUGI("hive/hadoop@REALM.CA", "c:\\data\\hive.service.keytab"));
  22.  
  23. System.out.println(UserGroupInformation.getLoginUser());
  24. System.out.println(UserGroupInformation.getCurrentUser());
  25.  
  26. //Add the hive driver
  27. Class.forName("org.apache.hive.jdbc.HiveDriver");
  28.  
  29. //Connect to hive jdbc
  30. Connection connection = DriverManager.getConnection("jdbc:hive2://hadoop:10000/default;principal=hive/hadoop@REALM.CA");
  31. Statement statement = connection.createStatement();
  32.  
  33. //Create a table
  34. String createTableSql = "CREATE TABLE IF NOT EXISTS "
  35. +" employee ( eid int, name String, "
  36. +" salary String, designation String)"
  37. +" COMMENT 'Employee details'"
  38. +" ROW FORMAT DELIMITED"
  39. +" FIELDS TERMINATED BY '\t'"
  40. +" LINES TERMINATED BY '\n'"
  41. +" STORED AS TEXTFILE";
  42.  
  43. System.out.println("Creating Table: " + createTableSql);
  44. statement.executeUpdate(createTableSql);
  45.  
  46. //Show all the tables to ensure we successfully added the table
  47. String showTablesSql = "show tables";
  48. System.out.println("Show All Tables: " + showTablesSql);
  49. ResultSet res = statement.executeQuery(showTablesSql);
  50.  
  51. while (res.next()) {
  52. System.out.println(res.getString(1));
  53. }
  54.  
  55. //Drop the table
  56. String dropTablesSql = "DROP TABLE IF EXISTS employee";
  57.  
  58. System.out.println("Dropping Table: " + dropTablesSql);
  59. statement.executeUpdate(dropTablesSql);
  60.  
  61. System.out.println("Finish!");

NiFi: Kerberize/SSL

In this tutorial I will show you how to use Kerberos/SSL with NiFi. I will use self signed certs for this example. Before you begin ensure you have installed Kerberos Server and NiFi.

This assumes your hostname is “hadoop”

Create Kerberos Principals

  1. cd /etc/security/keytabs/
  2.  
  3. sudo kadmin.local
  4.  
  5. #You can list principals
  6. listprincs
  7.  
  8. #Create the following principals
  9. addprinc -randkey nifi/hadoop@REALM.CA
  10. addprinc -randkey nifi-spnego/hadoop@REALM.CA
  11. #Notice this user does not have -randkey because we are a login user
  12. #Also notice that this user does not have a keytab created
  13. addprinc admin/hadoop@REALM.CA
  14.  
  15.  
  16. #Create the keytab files.
  17. #You will need these for Hadoop to be able to login
  18. xst -k nifi.service.keytab nifi/hadoop@REALM.CA
  19. xst -k nifi-spnego.service.keytab nifi-spnego/hadoop@REALM.CA

Set Keytab Permissions/Ownership

  1. sudo chown root:hadoopuser /etc/security/keytabs/*
  2. sudo chmod 750 /etc/security/keytabs/*

Stop NiFi

  1. sudo service nifi stop

Hosts Update

  1. sudo nano /etc/hosts
  2.  
  3. #Remove 127.0.1.1 line
  4.  
  5. #Change 127.0.0.1 to the following
  6. 127.0.0.1 gaudreault_kdc.ca hadoop localhost

Ubuntu Firewall

  1. sudo ufw disable

sysctl.conf

Disable ipv6 as it causes issues in getting your server up and running.

  1. nano /etc/sysctl.conf

Add the following to the end and save

  1. net.ipv6.conf.all.disable_ipv6 = 1
  2. net.ipv6.conf.default.disable_ipv6 = 1
  3. net.ipv6.conf.lo.disable_ipv6 = 1
  4. #Change eth0 to what ifconfig has
  5. net.ipv6.conf.eth0.disable_ipv6 = 1

Close sysctl

  1. sysctl -p
  2. cat /proc/sys/net/ipv6/conf/all/disable_ipv6
  3. reboot

TrustStore / KeyStore

  1. #Creating your Certificate Authority
  2. sudo mkdir -p /etc/security/serverKeys
  3. sudo chown -R root:hduser /etc/security/serverKeys/
  4. sudo chmod 750 /etc/security/serverKeys/
  5. cd /etc/security/serverKeys
  6.  
  7. sudo openssl genrsa -aes128 -out nifi.key 4096
  8. sudo openssl req -x509 -new -key nifi.key -days 1095 -out nifi.pem
  9. sudo openssl rsa -check -in nifi.key #check it
  10. sudo openssl x509 -outform der -in nifi.pem -out nifi.der
  11. sudo keytool -import -keystore truststore.jks -file nifi.der -alias nifi
  12. #***You must type 'yes' to trust this certificate.
  13. sudo keytool -v -list -keystore truststore.jks
  14.  
  15. #Creating your Server Keystore
  16. sudo keytool -genkey -alias nifi -keyalg RSA -keystore keystore.jks -keysize 2048
  17. sudo keytool -certreq -alias nifi -keystore keystore.jks -file nifi.csr
  18. sudo openssl x509 -sha256 -req -in nifi.csr -CA nifi.pem -CAkey nifi.key -CAcreateserial -out nifi.crt -days 730
  19. sudo keytool -import -keystore keystore.jks -file nifi.pem
  20. sudo keytool -import -trustcacerts -alias nifi -file nifi.crt -keystore keystore.jks
  21.  
  22. sudo chown -R root:hduser /etc/security/serverKeys/*
  23. sudo chmod 750 /etc/security/serverKeys/*

nifi.properties

  1. cd /usr/local/nifi/conf/
  2. nano nifi.properties
  3.  
  4. #Find "# Site to Site properties" and change the following properties to what is below
  5.  
  6. nifi.remote.input.host=
  7. nifi.remote.input.secure=true
  8. nifi.remote.input.socket.port=9096
  9. nifi.remote.input.http.enabled=false
  10.  
  11. #Find "# web properties #" and change the following properties to what is below
  12.  
  13. nifi.web.http.host=
  14. nifi.web.http.port=
  15. nifi.web.https.host=0.0.0.0
  16. nifi.web.https.port=9095
  17.  
  18. #Find "# security properties #" and change the following properties to what is below
  19.  
  20. nifi.security.keystore=/etc/security/serverKeys/keystore.jks
  21. nifi.security.keystoreType=JKS
  22. nifi.security.keystorePasswd=PASSWORD
  23. nifi.security.keyPasswd=PASSWORD
  24. nifi.security.truststore=/etc/security/serverKeys/truststore.jks
  25. nifi.security.truststoreType=JKS
  26. nifi.security.truststorePasswd=PASSWORD
  27. nifi.security.needClientAuth=true
  28. nifi.security.user.authorizer=managed-authorizer
  29. nifi.security.user.login.identity.provider=kerberos-provider
  30.  
  31. #Find "# Core Properties #" and change the following properties to what is below
  32.  
  33. nifi.authorizer.configuration.file=./conf/authorizers.xml
  34. nifi.login.identity.provider.configuration.file=./conf/login-identity-providers.xml
  35.  
  36. #Find "# kerberos #" and change the following properties to what is below
  37.  
  38. nifi.kerberos.krb5.file=/etc/krb5.conf
  39.  
  40. #Find "# kerberos service principal #" and change the following properties to what is below
  41.  
  42. nifi.kerberos.service.principal=nifi/hadoop@REALM.CA
  43. nifi.kerberos.service.keytab.location=/etc/security/keytabs/nifi.service.keytab
  44.  
  45. #Find "# kerberos spnego principal #" and change the following properties to what is below
  46.  
  47. nifi.kerberos.spnego.principal=nifi-spnego/hadoop@REALM.CA
  48. nifi.kerberos.spnego.keytab.location=/etc/security/keytabs/nifi-spnego.service.keytab
  49. nifi.kerberos.spnego.authentication.expiration=12 hours
  50.  
  51. #Find "# cluster common properties (all nodes must have same values) #" and change the following properties to what is below
  52.  
  53. nifi.cluster.protocol.is.secure=true

login-identity-providers.xml

  1. nano login-identity-providers.xml
  2.  
  3. #Find "kerberos-provider"
  4. <provider>
  5. <identifier>kerberos-provider</identifier>
  6. <class>org.apache.nifi.kerberos.KerberosProvider</class>
  7. <property name="Default Realm">REALM.CA</property>
  8. <property name="Kerberos Config File">/etc/krb5.conf</property>
  9. <property name="Authentication Expiration">12 hours</property>
  10. </provider>

authorizers.xml

  1. nano authorizers.xml
  2.  
  3. #Find "file-provider"
  4. <authorizer>
  5. <identifier>file-provider</identifier>
  6. <class>org.apache.nifi.authorization.FileAuthorizer</class>
  7. <property name="Authorizations File">./conf/authorizations.xml</property>
  8. <property name="Users File">./conf/users.xml</property>
  9. <property name="Initial Admin Identity">admin/hadoop@REALM.CA</property>
  10. <property name="Legacy Authorized Users File"></property>
  11.  
  12. <property name="Node Identity 1"></property>
  13. </authorizer>

Start Nifi

  1. sudo service nifi start

NiFi Web Login

Issues:

  • If you get the error “No applicable policies could be found” after logging in and no GUI is shown stop the NiFi service and restart. Then you should be good.
  • If you can then login but you don’t have any policies still you will need to update “authorizations.xml” and add the below lines. Making sure to change the resource process group id to the root process group id and the user id to the user id
  1. nano /usr/local/nifi/conf/authorizations.xml
  2.  
  3. <policy identifier="1c897e9d-3dd5-34ca-ae3d-75fb5ee3e1a5" resource="/data/process-groups/##CHANGE TO ROOT ID##" action="R">
  4. <user identifier="##CHANGE TO USER ID##"/>
  5. </policy>
  6. <policy identifier="91c64c2d-7848-371d-9d5f-db71138b152f" resource="/data/process-groups/##CHANGE TO ROOT ID##" action="W">
  7. <user identifier="##CHANGE TO USER ID##"/>
  8. </policy>
  9. <policy identifier="7aeb4d67-e2e1-3a3e-a8fa-94576f35539e" resource="/process-groups/##CHANGE TO ROOT ID##" action="R">
  10. <user identifier="##CHANGE TO USER ID##"/>
  11. </policy>
  12. <policy identifier="f5b620e0-b094-3f70-9542-dd6920ad5bd9" resource="/process-groups/##CHANGE TO ROOT ID##" action="W">
  13. <user identifier="##CHANGE TO USER ID##"/>
  14. </policy>

References

https://community.hortonworks.com/articles/34147/nifi-security-user-authentication-with-kerberos.html

https://community.hortonworks.com/content/supportkb/151106/nifi-how-to-create-your-own-certs-for-securing-nif.html