azure-sdk-for-java icon indicating copy to clipboard operation
azure-sdk-for-java copied to clipboard

[BUG] Can not use FIC in CosmosDB with scala

Open Danieladu opened this issue 1 year ago • 8 comments

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()
  }

Danieladu avatar Sep 23 '24 09:09 Danieladu

@billwert @g2vinay

github-actions[bot] avatar Sep 23 '24 09:09 github-actions[bot]

Thank you for your feedback. Tagging and routing to the team member best able to assist.

github-actions[bot] avatar Sep 23 '24 09:09 github-actions[bot]

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.

billwert avatar Sep 23 '24 20:09 billwert

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.

github-actions[bot] avatar Sep 23 '24 20:09 github-actions[bot]

Same issue. @billwert

Danieladu avatar Sep 24 '24 03:09 Danieladu

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();
};

billwert avatar Oct 07 '24 17:10 billwert

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.

github-actions[bot] avatar Oct 07 '24 17:10 github-actions[bot]

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!

github-actions[bot] avatar Oct 14 '24 21:10 github-actions[bot]