Search code examples
authenticationtestingapache-kafkakerberos

Changing the kerberos principal


I am writing a test which checks that we can create a kafka producer and send messages to a kafka cluster that uses kerberos for authentication. This works fine. (test1)

I also need to write a test which checks that a non authenticated user cannot create producer/send messages (the principal in Jaas is not right). (test2)

The tests look like :

public void test(){
        System.setProperty("java.security.auth.login.config", **jaas_path**);
        System.setProperty("java.security.krb5.conf", **krb5_path**);

        createKafkaProducer();
        sendMessages();
}

The test1 has the jaas_path of the right jaas file and the test2 the path of a jaas file with wrong principal.

My problem is that if the test1 run first, the test2 still uses the principal of the jaas of the test1. I mean that the test2 has still loaded the jaas file (principal & keytab) of the test1 and uses them.

Is there any way to clean the Kerberos credentials and change the principal?


Solution

  • Configuration.getConfiguration().refresh() resolved my issue.

    public void test(){
            System.setProperty("java.security.auth.login.config", **jaas_path**);
            System.setProperty("java.security.krb5.conf", **krb5_path**);
    
            Configuration.getConfiguration().refresh();
    
            createKafkaProducer();
            sendMessages();
    }