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

add origin application version to header #65

Merged
merged 8 commits into from
Jan 31, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ public class ClientImpl implements Client {

private AadAuthenticationHelper aadAuthenticationHelper;
private String clusterUrl;
private String clientVersionForTracing;

public ClientImpl(ConnectionStringBuilder csb) throws URISyntaxException {
clusterUrl = csb.getClusterUrl();
aadAuthenticationHelper = new AadAuthenticationHelper(csb);
clientVersionForTracing = "Kusto.Java.Client";
String version = Utils.GetPackageVersion();
if (StringUtils.isNotBlank(version)) {
clientVersionForTracing += ":" + version;
}

if (StringUtils.isNotBlank(csb.getClientVersionForTracing())) {
ohadbitt marked this conversation as resolved.
Show resolved Hide resolved
clientVersionForTracing += "[" + csb.getClientVersionForTracing() + "]";
ohadbitt marked this conversation as resolved.
Show resolved Hide resolved
}
}

public Results execute(String command) throws DataServiceException, DataClientException {
Expand Down Expand Up @@ -74,6 +84,6 @@ public Results execute(String database, String command, ClientRequestProperties
throw new DataClientException(clusterEndpoint, String.format(clusterEndpoint, "Error in executing command: %s, in database: %s", command, database), e);
}

return Utils.post(clusterEndpoint, aadAccessToken, jsonString, timeoutMs.intValue());
return Utils.post(clusterEndpoint, aadAccessToken, jsonString, timeoutMs.intValue(), clientVersionForTracing);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,45 @@ public class ConnectionStringBuilder {
private X509Certificate x509Certificate;
private PrivateKey privateKey;
private String aadAuthorityId; // AAD tenant Id (GUID)
private String clientVersionForTracing;

String getClusterUrl() { return clusterUri; }
String getUserUsername() { return username; }
String getUserPassword() { return password; }
String getApplicationClientId() { return applicationClientId; }
String getApplicationKey() { return applicationKey; }
String getAuthorityId() { return aadAuthorityId; }
X509Certificate getX509Certificate() { return x509Certificate; }
PrivateKey getPrivateKey(){ return privateKey; }
private ConnectionStringBuilder(String resourceUri)
{
String getClusterUrl() {
return clusterUri;
}

String getUserUsername() {
return username;
}

String getUserPassword() {
return password;
}

String getApplicationClientId() {
return applicationClientId;
}

String getApplicationKey() {
return applicationKey;
}

String getAuthorityId() {
return aadAuthorityId;
}

String getClientVersionForTracing() {
return clientVersionForTracing;
}

X509Certificate getX509Certificate() {
return x509Certificate;
}

PrivateKey getPrivateKey() {
return privateKey;
}

private ConnectionStringBuilder(String resourceUri) {
clusterUri = resourceUri;
username = null;
password = null;
Expand All @@ -37,17 +65,16 @@ private ConnectionStringBuilder(String resourceUri)
}

public static ConnectionStringBuilder createWithAadUserCredentials(String resourceUri,
String username,
String password,
String authorityId)
{
if (StringUtils.isEmpty(resourceUri)){
String username,
String password,
String authorityId) {
if (StringUtils.isEmpty(resourceUri)) {
throw new IllegalArgumentException("resourceUri cannot be null or empty");
}
if (StringUtils.isEmpty(username)){
if (StringUtils.isEmpty(username)) {
throw new IllegalArgumentException("username cannot be null or empty");
}
if (StringUtils.isEmpty(password)){
if (StringUtils.isEmpty(password)) {
throw new IllegalArgumentException("password cannot be null or empty");
}
ConnectionStringBuilder csb = new ConnectionStringBuilder(resourceUri);
Expand All @@ -59,24 +86,22 @@ public static ConnectionStringBuilder createWithAadUserCredentials(String resour

public static ConnectionStringBuilder createWithAadUserCredentials(String resourceUri,
String username,
String password)
{
String password) {
return createWithAadUserCredentials(resourceUri, username, password, null);
}

public static ConnectionStringBuilder createWithAadApplicationCredentials(String resourceUri,
String applicationClientId,
String applicationKey,
String authorityId)
{
String applicationClientId,
String applicationKey,
String authorityId) {

if (StringUtils.isEmpty(resourceUri)){
if (StringUtils.isEmpty(resourceUri)) {
throw new IllegalArgumentException("resourceUri cannot be null or empty");
}
if (StringUtils.isEmpty(applicationClientId)){
if (StringUtils.isEmpty(applicationClientId)) {
throw new IllegalArgumentException("applicationClientId cannot be null or empty");
}
if (StringUtils.isEmpty(applicationKey)){
if (StringUtils.isEmpty(applicationKey)) {
throw new IllegalArgumentException("applicationKey cannot be null or empty");
}

Expand All @@ -89,13 +114,12 @@ public static ConnectionStringBuilder createWithAadApplicationCredentials(String

public static ConnectionStringBuilder createWithAadApplicationCredentials(String resourceUri,
String applicationClientId,
String applicationKey)
{
String applicationKey) {
return createWithAadApplicationCredentials(resourceUri, applicationClientId, applicationKey, null);
}

public static ConnectionStringBuilder createWithDeviceCodeCredentials(String resourceUri){
if (StringUtils.isEmpty(resourceUri)){
public static ConnectionStringBuilder createWithDeviceCodeCredentials(String resourceUri) {
if (StringUtils.isEmpty(resourceUri)) {
throw new IllegalArgumentException("resourceUri cannot be null or empty");
}
return new ConnectionStringBuilder(resourceUri);
Expand All @@ -104,17 +128,17 @@ public static ConnectionStringBuilder createWithDeviceCodeCredentials(String res
public static ConnectionStringBuilder createWithAadApplicationCertificate(String resourceUri,
String applicationClientId,
X509Certificate x509Certificate,
PrivateKey privateKey){
if (StringUtils.isEmpty(resourceUri)){
PrivateKey privateKey) {
if (StringUtils.isEmpty(resourceUri)) {
throw new IllegalArgumentException("resourceUri cannot be null or empty");
}
if (StringUtils.isEmpty(applicationClientId)){
if (StringUtils.isEmpty(applicationClientId)) {
throw new IllegalArgumentException("applicationClientId cannot be null or empty");
}
if (x509Certificate == null){
if (x509Certificate == null) {
throw new IllegalArgumentException("certificate cannot be null");
}
if (privateKey == null){
if (privateKey == null) {
throw new IllegalArgumentException("privateKey cannot be null");
}

Expand All @@ -126,4 +150,7 @@ public static ConnectionStringBuilder createWithAadApplicationCertificate(String
return csb;
}

public void setClientVersionForTracing(String clientVersionForTracing) {
ohadbitt marked this conversation as resolved.
Show resolved Hide resolved
this.clientVersionForTracing = clientVersionForTracing;
}
}
14 changes: 6 additions & 8 deletions data/src/main/java/com/microsoft/azure/kusto/data/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class Utils {

static Results post(String url, String aadAccessToken, String payload, Integer timeoutMs) throws DataServiceException, DataClientException {
static Results post(String url, String aadAccessToken, String payload, Integer timeoutMs, String clientVersionForTracing) throws DataServiceException, DataClientException {

HttpClient httpClient;
if (timeoutMs != null) {
Expand All @@ -50,13 +50,7 @@ static Results post(String url, String aadAccessToken, String payload, Integer t
httpPost.addHeader("Accept-Encoding", "gzip,deflate");
httpPost.addHeader("Fed", "True");

String version = Utils.class.getPackage().getImplementationVersion();
String clientVersion = "Kusto.Java.Client";
if (StringUtils.isNotBlank(version)) {
clientVersion += ":" + version;
}

httpPost.addHeader("x-ms-client-version", clientVersion);
httpPost.addHeader("x-ms-client-version", clientVersionForTracing);
httpPost.addHeader("x-ms-client-request-id", String.format("KJC.execute;%s", java.util.UUID.randomUUID()));

try {
Expand Down Expand Up @@ -111,4 +105,8 @@ static Results post(String url, String aadAccessToken, String payload, Integer t
}
return null;
}

static String GetPackageVersion(){
return Utils.class.getPackage().getImplementationVersion();
}
}