Client credential flow to interact with API secured with Azure AD #513
Replies: 4 comments 10 replies
-
Sure. Propose a PR and we'll discuss it further. |
Beta Was this translation helpful? Give feedback.
-
Yeah, it's not easy to implement that use-case in |
Beta Was this translation helpful? Give feedback.
-
/*
* Copyright 2020 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.abnamro.dmp.mdc.splinecustomhttpdispatcher
import org.apache.spark.internal.Logging
import org.apache.http.HttpHeaders
import scalaj.http.{HttpRequest, HttpResponse}
import za.co.absa.commons.lang.ARM.using
import za.co.absa.spline.harvester.dispatcher.httpdispatcher.HttpConstants.Encoding
import org.abnamro.dmp.mdc.splinecustomhttpdispatcher.AzureRestEndpoint._
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPOutputStream
import javax.ws.rs.HttpMethod
import scalaj.http._
class AzureRestEndpoint(val request: HttpRequest,val clientId: String, val clientSecret: String, val scope:String, val tenantId: String) extends Logging {
def getAccessToken(clientId: String, clientSecret: String, tenantId: String,scope: String): String = {
val tokenEndpoint = s"https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
val response = Http(tokenEndpoint)
.postForm(Seq(
"client_id" -> clientId,
"client_secret" -> clientSecret,
"grant_type" -> "client_credentials",
"scope" -> scope.concat("/.default")
)).options(HttpOptions.allowUnsafeSSL,HttpOptions.readTimeout(5000))
.asString
val responseJson = response.body
import scala.util.matching.Regex
val pattern: Regex = """"access_token"\s*:\s*"([^"]+)"""".r
val accessToken = "Bearer ".concat(pattern.findFirstMatchIn(responseJson).map(_.group(1)).getOrElse(""))
logWarning(accessToken)
accessToken
}
def head(): HttpResponse[String] = request.header("Authorization", getAccessToken(clientId, clientSecret, tenantId, scope)).method(HttpMethod.HEAD).asString
def post(data: String, contentType: String, enableRequestCompression: Boolean): HttpResponse[String] = {
val jsonRequest = request
.header(HttpHeaders.CONTENT_TYPE, contentType)
.header("Authorization", getAccessToken(clientId, clientSecret, tenantId, scope))
if (enableRequestCompression && data.length > GzipCompressionLengthThreshold) {
jsonRequest
.header(HttpHeaders.CONTENT_ENCODING, Encoding.GZIP)
.postData(gzipContent(data.getBytes("UTF-8")))
.asString
} else {
jsonRequest
.postData(data)
.asString
}
}
}
object AzureRestEndpoint {
private val GzipCompressionLengthThreshold: Int = 2048
private def gzipContent(bytes: Array[Byte]): Array[Byte] = {
val byteStream = new ByteArrayOutputStream(bytes.length)
using(new GZIPOutputStream(byteStream))(_.write(bytes))
byteStream.toByteArray
}
} |
Beta Was this translation helpful? Give feedback.
-
Spline Agent 1.1.0 has been released. spline.lineageDispatcher.http.authentication.type=OAUTH
spline.lineageDispatcher.http.authentication.grantType=client_credentials
spline.lineageDispatcher.http.authentication.clientId=<client_id>
spline.lineageDispatcher.http.authentication.clientSecret=<secret>
spline.lineageDispatcher.http.authentication.scope=<scope>
spline.lineageDispatcher.http.authentication.tokenUrl=<token_url> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Team,
I am aware that as new PR is merged, we will be able to add requests headers to Http request created by HttpDispatcher.
We need to send lineage to a endpoint which is secured by Azure AD. Can't we make a functionality to get access token using client credential flow and send that in a header?
I think that would be great.
Beta Was this translation helpful? Give feedback.
All reactions