Skip to content

Instantly share code, notes, and snippets.

@Zejnilovic
Created December 7, 2021 11:59
Show Gist options
  • Save Zejnilovic/d15e1fcf38e157dbd4ddea3e4de27c17 to your computer and use it in GitHub Desktop.
Save Zejnilovic/d15e1fcf38e157dbd4ddea3e4de27c17 to your computer and use it in GitHub Desktop.
SPNEGO Auth in Scala / Java
ThisBuild / name := "KerberosTest"
ThisBuild / organization := "com.example"
ThisBuild / version := "0.0.1-SNAPSHOT"
ThisBuild / scalaVersion := "2.11.12"
libraryDependencies ++= List(
"org.apache.spark" %% "spark-core" % "2.4.7" % "provided",
"org.apache.spark" %% "spark-sql" % "2.4.7" % "provided",
"org.springframework.security.kerberos" % "spring-security-kerberos-client" % "1.0.1.RELEASE"
)
mainClass in assembly := Some("com.example.SparkApp")
assemblyMergeStrategy in assembly := {
case x if Assembly.isConfigFile(x) =>
MergeStrategy.concat
case PathList(ps @ _*) if Assembly.isReadme(ps.last) || Assembly.isLicenseFile(ps.last) =>
MergeStrategy.rename
case PathList("META-INF", xs @ _*) =>
(xs map {_.toLowerCase}) match {
case ("manifest.mf" :: Nil) | ("index.list" :: Nil) | ("dependencies" :: Nil) =>
MergeStrategy.discard
case ps @ (x :: xs) if ps.last.endsWith(".sf") || ps.last.endsWith(".dsa") =>
MergeStrategy.discard
case "plexus" :: xs =>
MergeStrategy.discard
case "services" :: xs =>
MergeStrategy.filterDistinctLines
case ("spring.schemas" :: Nil) | ("spring.handlers" :: Nil) =>
MergeStrategy.filterDistinctLines
case _ => MergeStrategy.first
}
case _ => MergeStrategy.first
}
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
import org.apache.spark.sql.SparkSession
import org.slf4j.LoggerFactory
import sun.security.krb5.internal.ktab.KeyTab
import org.springframework.security.kerberos.client.KerberosRestTemplate
object SparkApp {
def main(args: Array[String]) {
// Spark session is obsolete, but I needed it to test it as a spark app
val spark = SparkSession.builder.appName("KerberosTest Spark Job").getOrCreate()
val logger = LoggerFactory.getLogger(this.getClass)
val keytabPath = args(0) // "keytab path"
logger.info(s"Keytab path $keytabPath")
val url = args(1) // SPNEGO entry point
logger.info(s"URL $url")
val keytab = KeyTab.getInstance(keytabPath) // "Validate" the keytab
val username = keytab.getOneName.getName // Get name from keytab, just to be sure
logger.info(s"Username $username")
val restTemplate = new KerberosRestTemplate(keytabPath, username)
val response = restTemplate.getForEntity(url, classOf[String]) // Pull headers from this response
val statusCode = response.getStatusCode
logger.info(response.getBody)
logger.info("--statusCode--")
logger.info(statusCode.toString)
logger.info("--statusCode--")
spark.stop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment