in common/scala/src/main/scala/org/apache/openwhisk/core/yarn/YARNRESTUtil.scala [105:181]
def submitRequestWithAuth(authType: String, httpMethod: HttpMethod, URL: String, body: String): httpresponse = {
var client: CloseableHttpClient = null
var updatedURL = URL
authType match {
case SIMPLEAUTH =>
if (URL.contains("?"))
updatedURL = URL + "&user.name=" + System.getProperty("user.name")
else
updatedURL = URL + "?user.name=" + System.getProperty("user.name")
client = HttpClientBuilder.create.build
case KERBEROSAUTH =>
//System.setProperty("sun.security.krb5.debug", "true")
//System.setProperty("sun.security.spnego.debug", "true")
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false")
//Null credentials result in jaas login
val useJaaS = new CredentialsProvider {
override def setCredentials(authscope: AuthScope, credentials: Credentials): Unit = {}
override def getCredentials(authscope: AuthScope): Credentials = new Credentials() {
def getPassword: String = null
def getUserPrincipal: Principal = null
}
override def clear(): Unit = {}
}
val authSchemeRegistry = RegistryBuilder
.create[AuthSchemeProvider]()
.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
.build()
client = HttpClients
.custom()
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.setDefaultCredentialsProvider(useJaaS)
.build()
}
var request: HttpRequestBase = null
httpMethod match {
case HttpMethods.GET =>
request = new HttpGet(updatedURL)
case HttpMethods.POST =>
request = new HttpPost(updatedURL)
request.asInstanceOf[HttpPost].setEntity(new StringEntity(body.toString, StandardCharsets.UTF_8.toString))
case HttpMethods.PUT =>
request = new HttpPut(updatedURL)
request.asInstanceOf[HttpPut].setEntity(new StringEntity(body.toString, StandardCharsets.UTF_8.toString))
case HttpMethods.DELETE =>
request = new HttpDelete(updatedURL)
case _ =>
throw new IllegalArgumentException(s"Unsupported HTTP method: $httpMethod")
}
request.addHeader("content-type", "application/json")
try {
val response = client.execute(request)
val responseBody = IOUtils.toString(response.getEntity.getContent, StandardCharsets.UTF_8)
val statusCode: Int = response.getStatusLine.getStatusCode
httpresponse(statusCode, responseBody)
} catch {
case e: Exception =>
e.printStackTrace()
httpresponse(500, e.getMessage)
}
}