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

Added token credential for queue #4673

Merged
merged 20 commits into from
Aug 13, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the MIT License.
package com.azure.storage.queue;

import com.azure.core.credentials.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.AddDatePolicy;
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
Expand Down Expand Up @@ -83,6 +85,7 @@ public final class QueueClientBuilder {
private String queueName;
private SASTokenCredential sasTokenCredential;
private SharedKeyCredential sharedKeyCredential;
private TokenCredential bearerTokenCredential;
private HttpClient httpClient;
private HttpPipeline pipeline;
private HttpLogDetailLevel logLevel;
Expand Down Expand Up @@ -139,9 +142,9 @@ public QueueAsyncClient buildAsyncClient() {
Objects.requireNonNull(endpoint);
Objects.requireNonNull(queueName);

if (sasTokenCredential == null && sharedKeyCredential == null) {
LOGGER.error("Credentials are required for authorization");
throw new IllegalArgumentException("Credentials are required for authorization");
if (sasTokenCredential == null && sharedKeyCredential == null && bearerTokenCredential == null) {
LOGGER.logExceptionAsError(new IllegalArgumentException("Credentials are required for authorization"));
return null;
}

if (pipeline != null) {
Expand All @@ -156,7 +159,9 @@ public QueueAsyncClient buildAsyncClient() {

if (sharedKeyCredential != null) {
policies.add(new SharedKeyCredentialPolicy(sharedKeyCredential));
} else {
} else if (bearerTokenCredential != null) {
policies.add(new BearerTokenAuthenticationPolicy(bearerTokenCredential, String.format("%s/.default", endpoint)));
} else if (sasTokenCredential != null) {
policies.add(new SASTokenCredentialPolicy(sasTokenCredential));
}

Expand Down Expand Up @@ -202,13 +207,14 @@ public QueueClientBuilder endpoint(String endpoint) {
}

// Attempt to get the SAS token from the URL passed
SASTokenCredential credential = SASTokenCredential.fromQueryParameters(Utility.parseQueryString(fullURL.getQuery()));
if (credential != null) {
this.sasTokenCredential = credential;
this.sasTokenCredential = SASTokenCredential.fromQueryParameters(Utility.parseQueryString(fullURL.getQuery()));
if (this.sasTokenCredential != null) {
this.sharedKeyCredential = null;
this.bearerTokenCredential = null;
}
} catch (MalformedURLException ex) {
LOGGER.error("The Azure Storage Queue endpoint url is malformed. Endpoint: " + endpoint);
throw new IllegalArgumentException("The Azure Storage Queue endpoint url is malformed. Endpoint: " + endpoint);
LOGGER.logExceptionAsError(new IllegalArgumentException("The Azure Storage Queue endpoint url is malformed. Endpoint: " + endpoint));
return null;
}

return this;
Expand All @@ -235,6 +241,8 @@ public QueueClientBuilder queueName(String queueName) {
*/
public QueueClientBuilder credential(SASTokenCredential credential) {
this.sasTokenCredential = Objects.requireNonNull(credential);
this.sharedKeyCredential = null;
this.bearerTokenCredential = null;
return this;
}

Expand All @@ -247,6 +255,21 @@ public QueueClientBuilder credential(SASTokenCredential credential) {
*/
public QueueClientBuilder credential(SharedKeyCredential credential) {
this.sharedKeyCredential = Objects.requireNonNull(credential);
this.sasTokenCredential = null;
this.bearerTokenCredential = null;
return this;
}

/**
* Sets the {@link TokenCredential} used to authenticate requests sent to the Queue service.
* @param credential authorization credential
* @return the updated QueueServiceClientBuilder object
* @throws NullPointerException If {@code credential} is {@code null}
*/
public QueueClientBuilder credential(TokenCredential credential) {
this.bearerTokenCredential = Objects.requireNonNull(credential);
this.sharedKeyCredential = null;
this.sasTokenCredential = null;
return this;
}

Expand Down Expand Up @@ -275,10 +298,8 @@ private void getEndPointFromConnectionString(String connectionString) {
try {
this.endpoint = new URL(String.format("https://%s.queue.core.windows.net", accountName));
} catch (MalformedURLException e) {
LOGGER.error("There is no valid account for the connection string. "
+ "Connection String: %s", connectionString);
throw new IllegalArgumentException(String.format("There is no valid account for the connection string. "
+ "Connection String: %s", connectionString));
LOGGER.logExceptionAsError(new IllegalArgumentException(String.format("There is no valid account for the connection string. "
+ "Connection String: %s", connectionString)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the MIT License.
package com.azure.storage.queue;

import com.azure.core.credentials.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.AddDatePolicy;
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
Expand Down Expand Up @@ -79,6 +81,7 @@ public final class QueueServiceClientBuilder {
private URL endpoint;
private SASTokenCredential sasTokenCredential;
private SharedKeyCredential sharedKeyCredential;
private TokenCredential bearerTokenCredential;
private HttpClient httpClient;
private HttpPipeline pipeline;
private HttpLogDetailLevel logLevel;
Expand Down Expand Up @@ -113,9 +116,9 @@ public QueueServiceClientBuilder() {
public QueueServiceAsyncClient buildAsyncClient() {
Objects.requireNonNull(endpoint);

if (sasTokenCredential == null && sharedKeyCredential == null) {
LOGGER.error("Credentials are required for authorization");
throw new IllegalArgumentException("Credentials are required for authorization");
if (sasTokenCredential == null && sharedKeyCredential == null && bearerTokenCredential == null) {
LOGGER.logExceptionAsError(new IllegalArgumentException("Credentials are required for authorization"));
return null;
}

if (pipeline != null) {
Expand All @@ -130,7 +133,9 @@ public QueueServiceAsyncClient buildAsyncClient() {

if (sharedKeyCredential != null) {
policies.add(new SharedKeyCredentialPolicy(sharedKeyCredential));
} else {
} else if (bearerTokenCredential != null) {
policies.add(new BearerTokenAuthenticationPolicy(bearerTokenCredential, String.format("%s/.default", endpoint)));
} else if (sasTokenCredential != null) {
policies.add(new SASTokenCredentialPolicy(sasTokenCredential));
}

Expand Down Expand Up @@ -186,13 +191,14 @@ public QueueServiceClientBuilder endpoint(String endpoint) {
this.endpoint = new URL(fullURL.getProtocol() + "://" + fullURL.getHost());

// Attempt to get the SAS token from the URL passed
SASTokenCredential credential = SASTokenCredential.fromQueryParameters(Utility.parseQueryString(fullURL.getQuery()));
if (credential != null) {
this.sasTokenCredential = credential;
this.sasTokenCredential = SASTokenCredential.fromQueryParameters(Utility.parseQueryString(fullURL.getQuery()));
if (this.sasTokenCredential != null) {
this.sharedKeyCredential = null;
this.bearerTokenCredential = null;
}
} catch (MalformedURLException ex) {
LOGGER.error("The Azure Storage Queue endpoint url is malformed.");
throw new IllegalArgumentException("The Azure Storage Queue endpoint url is malformed.");
LOGGER.logExceptionAsError(new IllegalArgumentException("The Azure Storage Queue endpoint url is malformed."));
return null;
}

return this;
Expand All @@ -207,6 +213,8 @@ public QueueServiceClientBuilder endpoint(String endpoint) {
*/
public QueueServiceClientBuilder credential(SASTokenCredential credential) {
this.sasTokenCredential = Objects.requireNonNull(credential);
this.sharedKeyCredential = null;
this.bearerTokenCredential = null;
return this;
}

Expand All @@ -219,9 +227,23 @@ public QueueServiceClientBuilder credential(SASTokenCredential credential) {
*/
public QueueServiceClientBuilder credential(SharedKeyCredential credential) {
this.sharedKeyCredential = Objects.requireNonNull(credential);
this.sasTokenCredential = null;
this.bearerTokenCredential = null;
return this;
}

/**
* Sets the {@link TokenCredential} used to authenticate requests sent to the Queue service.
* @param credential authorization credential
* @return the updated QueueServiceClientBuilder object
* @throws NullPointerException If {@code credential} is {@code null}
*/
public QueueServiceClientBuilder credential(TokenCredential credential) {
this.bearerTokenCredential = Objects.requireNonNull(credential);
this.sharedKeyCredential = null;
this.sasTokenCredential = null;
return this;
}

/**
* Creates a {@link SharedKeyCredential} from the {@code connectionString} used to authenticate requests sent to the
Expand All @@ -247,10 +269,8 @@ private void getEndPointFromConnectionString(String connectionString) {
try {
this.endpoint = new URL(String.format("https://%s.queue.core.windows.net", accountName));
} catch (MalformedURLException e) {
LOGGER.error("There is no valid account for the connection string. "
+ "Connection String: %s", connectionString);
throw new IllegalArgumentException(String.format("There is no valid account for the connection string. "
+ "Connection String: %s", connectionString));
LOGGER.logExceptionAsError(new IllegalArgumentException(String.format("There is no valid account for the connection string. "
+ "Connection String: %s", connectionString)));
}
}

Expand Down
Loading