-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 1 commit
9d849c8
ea937c9
a48ecb7
24d7400
9303c45
3e48912
1f11515
7a14ea8
3e7cc7f
80567ea
98613ff
47452a6
27ed656
13c2431
43763e5
6a5b1ac
f81a310
762202c
a5d24a4
89810ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ | |
// 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.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; | ||
|
@@ -26,6 +28,7 @@ | |
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
|
@@ -83,6 +86,7 @@ public final class QueueClientBuilder { | |
private HttpLogDetailLevel logLevel; | ||
private RetryPolicy retryPolicy; | ||
private Configuration configuration; | ||
private TokenCredential tokenCredential; | ||
|
||
/** | ||
* Creates a builder instance that is able to configure and construct {@link QueueClient QueueClients} | ||
|
@@ -134,7 +138,7 @@ public QueueAsyncClient buildAsyncClient() { | |
Objects.requireNonNull(endpoint); | ||
Objects.requireNonNull(queueName); | ||
|
||
if (sasTokenCredential == null && sharedKeyCredential == null) { | ||
if (sasTokenCredential == null && sharedKeyCredential == null && tokenCredential == null) { | ||
LOGGER.asError().log("Credentials are required for authorization"); | ||
throw new IllegalArgumentException("Credentials are required for authorization"); | ||
} | ||
|
@@ -151,7 +155,9 @@ public QueueAsyncClient buildAsyncClient() { | |
|
||
if (sharedKeyCredential != null) { | ||
policies.add(new SharedKeyCredentialPolicy(sharedKeyCredential)); | ||
} else { | ||
} else if (tokenCredential != null) { | ||
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, String.format("%s/.default", endpoint))); | ||
} else if (sasTokenCredential != null) { | ||
policies.add(new SASTokenCredentialPolicy(sasTokenCredential)); | ||
} | ||
|
||
|
@@ -177,7 +183,7 @@ public QueueAsyncClient buildAsyncClient() { | |
* <p>The first path segment, if the endpoint contains path segments, will be assumed to be the name of the queue | ||
* that the client will interact with.</p> | ||
* | ||
* <p>Query parameters of the endpoint will be parsed using {@link SASTokenCredential#fromQuery(String) fromQuery} in an | ||
* <p>Query parameters of the endpoint will be parsed using {@link SASTokenCredential#fromQueryParameters(Map)} fromQuery} in an | ||
* attempt to generate a {@link SASTokenCredential} to authenticate requests sent to the service.</p> | ||
* | ||
* @param endpoint The URL of the Azure Storage Queue instance to send service requests to and receive responses from. | ||
|
@@ -197,9 +203,10 @@ public QueueClientBuilder endpoint(String endpoint) { | |
} | ||
|
||
// Attempt to get the SAS token from the URL passed | ||
SASTokenCredential credential = SASTokenCredential.fromQuery(fullURL.getQuery()); | ||
if (credential != null) { | ||
this.sasTokenCredential = credential; | ||
this.sasTokenCredential = SASTokenCredential.fromQueryParameters(Utility.parseQueryString(fullURL.getQuery())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we parsing a query string into a map just to pass it into a method that turns it back into a query string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agreed with take the string directly. Moved it logic to SasTokenCredential for current use. Alan will have another common module PR which will take care the token as a whole. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One possible reason this was happening is for validation. We do something similar with permissions (parse the permissions string passed to validate it and then immediately toString it). If there is any sort of validation logic in either parseQueryString or fromQueryString, I'd recommend keeping this as is and adding a comment. If there's nothing of value and it's just a complete waste of cpu time, then I'm fine changing this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line has been changed from Alan's common module PR. |
||
if (this.sasTokenCredential != null) { | ||
this.sharedKeyCredential = null; | ||
this.tokenCredential = null; | ||
} | ||
} catch (MalformedURLException ex) { | ||
LOGGER.asError().log("The Azure Storage Queue endpoint url is malformed. Endpoint: " + endpoint); | ||
|
@@ -230,6 +237,8 @@ public QueueClientBuilder queueName(String queueName) { | |
*/ | ||
public QueueClientBuilder credential(SASTokenCredential credential) { | ||
this.sasTokenCredential = Objects.requireNonNull(credential); | ||
this.sharedKeyCredential = null; | ||
this.tokenCredential = null; | ||
return this; | ||
} | ||
|
||
|
@@ -242,6 +251,21 @@ public QueueClientBuilder credential(SASTokenCredential credential) { | |
*/ | ||
public QueueClientBuilder credential(SharedKeyCredential credential) { | ||
this.sharedKeyCredential = Objects.requireNonNull(credential); | ||
this.sasTokenCredential = null; | ||
this.tokenCredential = 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.tokenCredential = Objects.requireNonNull(credential); | ||
this.sharedKeyCredential = null; | ||
this.sasTokenCredential = null; | ||
return this; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put this up with the other credential declarations so they're all together? Also, can we name the field
bearerTokenCredential
? That way, compared with the namesasTokenCredential
, they both have qualifiers and it doesn't sound like one's a generic version of the other.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed the tokenCred as suggested.