Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows users to provide their own TokenCredential auth from azure-identity #392

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions data/src/main/java/com/microsoft/azure/kusto/data/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

package com.microsoft.azure.kusto.data;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpRequest;
import com.azure.core.util.BinaryData;
import com.microsoft.azure.kusto.data.auth.CloudInfo;
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
import com.microsoft.azure.kusto.data.auth.TokenProviderBase;
import com.microsoft.azure.kusto.data.auth.TokenProviderFactory;
import com.microsoft.azure.kusto.data.auth.*;
import com.microsoft.azure.kusto.data.auth.endpoints.KustoTrustedEndpoints;
import com.microsoft.azure.kusto.data.exceptions.*;
import com.microsoft.azure.kusto.data.http.*;
Expand Down Expand Up @@ -47,12 +45,24 @@ public ClientImpl(ConnectionStringBuilder csb, HttpClientProperties properties)
}

public ClientImpl(ConnectionStringBuilder csb, HttpClient httpClient) throws URISyntaxException {
this(csb, httpClient, null);
}

public ClientImpl(ConnectionStringBuilder csb, HttpClient httpClient, TokenCredential credentials) throws URISyntaxException {
super(httpClient);
String clusterURL = UriUtils.createClusterURLFrom(csb.getClusterUrl());
csb.setClusterUrl(clusterURL);

clusterUrl = csb.getClusterUrl();
aadAuthenticationHelper = clusterUrl.toLowerCase().startsWith(CloudInfo.LOCALHOST) ? null : TokenProviderFactory.createTokenProvider(csb, httpClient);

// Allow the user to provide their own TokenCredentials from Azure Identity
if (credentials != null) {
aadAuthenticationHelper = new AzureIdentityTokenProvider(clusterURL, httpClient, credentials);
} else {
aadAuthenticationHelper = clusterUrl.toLowerCase().startsWith(CloudInfo.LOCALHOST) ? null :
TokenProviderFactory.createTokenProvider(csb, httpClient);
}

clientDetails = new ClientDetails(csb.getApplicationNameForTracing(), csb.getUserNameForTracing(), csb.getClientVersionForTracing());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.microsoft.azure.kusto.data.auth;

import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.http.HttpClient;

import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URISyntaxException;

public class AzureIdentityTokenProvider extends CloudDependentTokenProviderBase {
public static final String AZURE_ID_TOKEN_PROVIDER = "AzureIdentityTokenProvider";
private TokenRequestContext tokenRequestContext;
private final TokenCredential tokenCredential;

public AzureIdentityTokenProvider(@NotNull String clusterUrl, @Nullable HttpClient httpClient, @NotNull TokenCredential credentials) throws URISyntaxException {
super(clusterUrl, httpClient);
tokenCredential = credentials;
}

@Override
protected void initializeWithCloudInfo(CloudInfo cloudInfo) throws DataServiceException, DataClientException {
super.initializeWithCloudInfo(cloudInfo);
tokenRequestContext = new TokenRequestContext().addScopes(scopes.toArray(new String[0]));
}

@Override
protected String acquireAccessTokenImpl() throws DataServiceException {
// Fixme: Make this async
AccessToken accessToken = tokenCredential.getToken(tokenRequestContext).block();
if (accessToken == null) {
throw new DataServiceException(clusterUrl, "Couldn't get token from Azure Identity", true);
}
return accessToken.getToken();
}

@Override
protected String getAuthMethod() {
return AZURE_ID_TOKEN_PROVIDER;
}
}