[BUG] Can not use FIC in CosmosDB with scala
Describe the bug When I use the ManagedIdentity to get token for Federate Credential in scala, the below throw exception: block()/blockFirst()/blockLast() are blocking, which is not supported in thread cosmos-parallel-1
The code snip is:
def initCosmosClient(): CosmosClient = {
println(s"Getting CosmosClient")
val managedIdentityCredential = new ManagedIdentityCredentialBuilder()
.clientId(miClientId)
.build()
val clientAssertionCredential = new ClientAssertionCredentialBuilder()
.tenantId(tenantId)
.clientId(appId)
.clientAssertion(() => {
try {
val token = managedIdentityCredential.getTokenSync(new TokenRequestContext().addScopes("api://AzureADTokenExchange/.default"))
if (token != null){
token.getToken
} else {
""
}
} catch {
case e: Exception => {
println(s"Getting CosmosClient with Exception ${e.getMessage}")
""
}
}
})
.additionallyAllowedTenants("*")
.build()
new CosmosClientBuilder()
.endpoint(configs.host)
.credential(clientAssertionCredential)
.buildClient()
}
@billwert @g2vinay
Thank you for your feedback. Tagging and routing to the team member best able to assist.
Hello @Danieladu!
Can you try using the asynchronous getToken() method instead of getTokenSync()? Something like:
val token = managedIdentityCredential.getToken(new TokenRequestContext().addScopes("api://AzureADTokenExchange/.default")).block()
Let me know if that still hits the exception.
Hi @Danieladu. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
Same issue. @billwert
Got it. You'll have to swap to using getToken instead of getTokenSync. Try this:
Supplier<String> supplier2 = () -> {
CompletableFuture<String> future = new CompletableFuture<>();
credential.getToken(request).subscribe(t -> future.complete(t.getToken()));
return future.join();
};
Hi @Danieladu. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
Hi @Danieladu, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!