Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package org.apache.spark.sql.kafka010

import java.io.{File, IOException}
import java.io.{File, IOException, PrintWriter}
import java.net.{InetAddress, InetSocketAddress}
import java.nio.charset.StandardCharsets
import java.util.{Collections, Properties, UUID}
import java.util.concurrent.TimeUnit
import javax.security.auth.login.Configuration

import scala.collection.JavaConverters._
import scala.io.Source
import scala.util.Random

import com.google.common.io.Files
Expand Down Expand Up @@ -136,9 +137,33 @@ class KafkaTestUtils(
kdcConf.setProperty(MiniKdc.DEBUG, "true")
kdc = new MiniKdc(kdcConf, kdcDir)
kdc.start()
rewriteKrb5Conf()
kdcReady = true
}

private def rewriteKrb5Conf(): Unit = {
val krb5Conf = Source.fromFile(kdc.getKrb5conf, "UTF-8").getLines()
val rewriteKrb5Conf = krb5Conf.map(s =>
if (s.contains("libdefaults")) {
val addedConfig =
addedKrb5Config("default_tkt_enctypes", "aes128-cts-hmac-sha1-96") +
addedKrb5Config("default_tgs_enctypes", "aes128-cts-hmac-sha1-96")
s + addedConfig
} else {
s
})
kdc.getKrb5conf.delete()
val writer = new PrintWriter(kdc.getKrb5conf)
// scalastyle:off println
rewriteKrb5Conf.foreach(writer.println)
// scalastyle:on println
writer.close()
Copy link
Member

Choose a reason for hiding this comment

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

Shall we use Files.write(content, file, StandardCharsets.UTF_8) instead of println?

Copy link
Member

Choose a reason for hiding this comment

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

-    val writer = new PrintWriter(kdc.getKrb5conf)
-    // scalastyle:off println
-    writer.println(krb5confStr)
-    // scalastyle:on println
-    writer.close()
+    Files.write(krb5confStr, kdc.getKrb5conf, StandardCharsets.UTF_8)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shall we use Files.write(content, file, StandardCharsets.UTF_8) instead of println?

Done thanks.

}

private def addedKrb5Config(key: String, value: String): String = {
System.lineSeparator() + s" $key=$value"
}

private def createKeytabsAndJaasConfigFile(): String = {
assert(kdcReady, "KDC should be set up beforehand")
val baseDir = Utils.createTempDir()
Expand Down Expand Up @@ -171,6 +196,7 @@ class KafkaTestUtils(
| useKeyTab=true
| storeKey=true
| useTicketCache=false
| refreshKrb5Config=true
| keyTab="${zkServerKeytabFile.getAbsolutePath()}"
| principal="$zkServerUser@$realm";
|};
Expand All @@ -180,6 +206,7 @@ class KafkaTestUtils(
| useKeyTab=true
| storeKey=true
| useTicketCache=false
| refreshKrb5Config=true
| keyTab="${zkClientKeytabFile.getAbsolutePath()}"
| principal="$zkClientUser@$realm";
|};
Expand Down