-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...wanalytics.snowplow.collectors.scalastream/sinks/AzureAuthenticationCallbackHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright (c) 2013-present Snowplow Analytics Ltd. | ||
* All rights reserved. | ||
* | ||
* This program is licensed to you under the Snowplow Community License Version 1.0, | ||
* and you may not use this file except in compliance with the Snowplow Community License Version 1.0. | ||
* You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 | ||
*/ | ||
package com.snowplowanalytics.snowplow.collectors.scalastream | ||
package sinks | ||
|
||
import java.net.URI | ||
import java.{lang, util} | ||
|
||
import javax.security.auth.callback.Callback | ||
import javax.security.auth.callback.UnsupportedCallbackException | ||
import javax.security.auth.login.AppConfigurationEntry | ||
|
||
import org.apache.kafka.clients.producer.ProducerConfig | ||
import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler | ||
import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken | ||
import org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback | ||
|
||
import com.microsoft.azure.credentials.MSICredentials | ||
|
||
import com.nimbusds.jwt.JWTParser | ||
|
||
class AzureAuthenticationCallbackHandler extends AuthenticateCallbackHandler { | ||
|
||
val CREDENTIALS: MSICredentials = new MSICredentials() | ||
|
||
var sbUri: String = "" | ||
|
||
override def configure( | ||
configs: util.Map[String, _], | ||
saslMechanism: String, | ||
jaasConfigEntries: util.List[AppConfigurationEntry] | ||
): Unit = { | ||
val bootstrapServer = configs.get(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG).toString.replaceAll("\\[|\\]", "") | ||
val uri = URI.create("https://" + bootstrapServer) | ||
this.sbUri = uri.getScheme + "://" + uri.getHost | ||
} | ||
|
||
override def handle(callbacks: Array[Callback]): Unit = | ||
callbacks.foreach { | ||
case callback: OAuthBearerTokenCallback => | ||
try { | ||
val token = getOAuthBearerToken() | ||
callback.token(token) | ||
} catch { | ||
case e: Throwable => e.printStackTrace() | ||
} | ||
case callback => throw new UnsupportedCallbackException(callback) | ||
} | ||
|
||
def getOAuthBearerToken(): OAuthBearerToken = { | ||
val accessToken = CREDENTIALS.getToken(sbUri) | ||
val jwt = JWTParser.parse(accessToken) | ||
val claims = jwt.getJWTClaimsSet | ||
|
||
new OAuthBearerToken { | ||
override def value(): String = accessToken | ||
|
||
override def lifetimeMs(): Long = claims.getExpirationTime.getTime | ||
|
||
override def scope(): util.Set[String] = null | ||
|
||
override def principalName(): String = null | ||
|
||
override def startTimeMs(): lang.Long = null | ||
} | ||
} | ||
|
||
override def close(): Unit = () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters