Skip to content

Commit f3ca020

Browse files
authored
Cherry-picking from master : Fixing the infinite loop that runs to renew SAS token after 20 minutes when a connection string contains the SAS token instead of SAS key. (#342) (#346)
# Conflicts: # azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CommonRequestResponseOperations.java # azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CoreMessageSender.java # azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/RequestResponseLink.java
1 parent bf53563 commit f3ca020

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CommonRequestResponseOperations.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ static CompletableFuture<Void> sendCBSTokenAsync(RequestResponseLink requestResp
8181
Message requestMessage = RequestResponseUtils.createRequestMessageFromValueBody(ClientConstants.REQUEST_RESPONSE_PUT_TOKEN_OPERATION, securityToken.getTokenValue(), Util.adjustServerTimeout(operationTimeout));
8282
requestMessage.getApplicationProperties().getValue().put(ClientConstants.REQUEST_RESPONSE_PUT_TOKEN_TYPE, securityToken.getTokenType().toString());
8383
requestMessage.getApplicationProperties().getValue().put(ClientConstants.REQUEST_RESPONSE_PUT_TOKEN_AUDIENCE, securityToken.getTokenAudience());
84-
requestMessage.getApplicationProperties().getValue().put(ClientConstants.REQUEST_RESPONSE_PUT_TOKEN_EXPIRATION, securityToken.getValidUntil().toEpochMilli());
8584
CompletableFuture<Message> responseFuture = requestResponseLink.requestAysnc(requestMessage, TransactionContext.NULL_TXN, operationTimeout);
8685
return responseFuture.thenComposeAsync((responseMessage) -> {
8786
CompletableFuture<Void> returningFuture = new CompletableFuture<Void>();

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CoreMessageReceiver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ private synchronized CompletableFuture<Void> ensureLinkIsOpen()
12291229
Throwable cause = ExceptionUtil.extractAsyncCompletionCause(sendTokenEx);
12301230
TRACE_LOGGER.error("Sending SAS Token to '{}' failed.", this.receivePath, cause);
12311231
this.receiveLinkReopenFuture.completeExceptionally(sendTokenEx);
1232+
this.clearAllPendingWorkItems(sendTokenEx);
12321233
}
12331234
else
12341235
{
@@ -1282,7 +1283,7 @@ private void completePendingUpdateStateWorkItem(Delivery delivery, String delive
12821283
}
12831284
}
12841285

1285-
private void clearAllPendingWorkItems(Exception exception)
1286+
private void clearAllPendingWorkItems(Throwable exception)
12861287
{
12871288
TRACE_LOGGER.info("Completeing all pending receive and updateState operation on the receiver to '{}'", this.receivePath);
12881289
final boolean isTransientException = exception == null ||

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CoreMessageSender.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ else if (outcome instanceof Released)
609609
}
610610
}
611611

612-
private void clearAllPendingSendsWithException(Exception failureException)
612+
private void clearAllPendingSendsWithException(Throwable failureException)
613613
{
614614
synchronized (this.pendingSendLock)
615615
{
@@ -623,7 +623,7 @@ private void clearAllPendingSendsWithException(Exception failureException)
623623
}
624624
}
625625

626-
private void cleanupFailedSend(final SendWorkItem<DeliveryState> failedSend, final Exception exception)
626+
private void cleanupFailedSend(final SendWorkItem<DeliveryState> failedSend, final Throwable exception)
627627
{
628628
failedSend.cancelTimeoutTask(false);
629629
ExceptionUtil.completeExceptionally(failedSend.getWork(), exception, this, true);
@@ -812,6 +812,7 @@ private synchronized CompletableFuture<Void> ensureLinkIsOpen()
812812
Throwable cause = ExceptionUtil.extractAsyncCompletionCause(sendTokenEx);
813813
TRACE_LOGGER.error("Sending SAS Token to '{}' failed.", this.sendPath, cause);
814814
this.sendLinkReopenFuture.completeExceptionally(sendTokenEx);
815+
this.clearAllPendingSendsWithException(sendTokenEx);
815816
}
816817
else
817818
{

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/ExceptionUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ else if (errorCondition.getCondition() == AmqpErrorCode.DecodeError)
125125
return new ServiceBusException(ClientConstants.DEFAULT_IS_TRANSIENT, errorCondition.toString());
126126
}
127127

128-
static <T> void completeExceptionally(CompletableFuture<T> future, Exception exception, IErrorContextProvider contextProvider, boolean completeAsynchronously)
128+
static <T> void completeExceptionally(CompletableFuture<T> future, Throwable exception, IErrorContextProvider contextProvider, boolean completeAsynchronously)
129129
{
130130
if (exception != null && exception instanceof ServiceBusException)
131131
{

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/MessagingFactory.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,17 @@ private CompletableFuture<Instant> generateAndSendSecurityToken(String sasTokenA
725725

726726
private static ScheduledFuture<?> scheduleRenewTimer(Instant currentTokenValidUntil, Runnable validityRenewer)
727727
{
728-
// It will eventually expire. Renew it
729-
int renewInterval = Util.getTokenRenewIntervalInSeconds((int)Duration.between(Instant.now(), currentTokenValidUntil).getSeconds());
730-
return Timer.schedule(validityRenewer, Duration.ofSeconds(renewInterval), TimerType.OneTimeRun);
728+
if(currentTokenValidUntil == Instant.MAX)
729+
{
730+
// User provided token or will never expire
731+
return null;
732+
}
733+
else
734+
{
735+
// It will eventually expire. Renew it
736+
int renewInterval = Util.getTokenRenewIntervalInSeconds((int)Duration.between(Instant.now(), currentTokenValidUntil).getSeconds());
737+
return Timer.schedule(validityRenewer, Duration.ofSeconds(renewInterval), TimerType.OneTimeRun);
738+
}
731739
}
732740

733741
CompletableFuture<RequestResponseLink> obtainRequestResponseLinkAsync(String entityPath, MessagingEntityType entityType)

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/RequestResponseLink.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ private CompletableFuture<Void> recreateInternalLinks()
377377
Throwable cause = ExceptionUtil.extractAsyncCompletionCause(sasTokenEx);
378378
TRACE_LOGGER.error("Sending SAS Token failed. RequestResponseLink path:{}", this.linkPath, cause);
379379
recreateInternalLinksFuture.completeExceptionally(cause);
380+
this.completeAllPendingRequestsWithException(cause);
380381
}
381382
else
382383
{
@@ -439,7 +440,7 @@ public void run()
439440
return recreateInternalLinksFuture;
440441
}
441442

442-
private void completeAllPendingRequestsWithException(Exception exception)
443+
private void completeAllPendingRequestsWithException(Throwable exception)
443444
{
444445
TRACE_LOGGER.warn("Completing all pending requests with exception in request response link to {}", this.linkPath);
445446
for(RequestResponseWorkItem workItem : this.pendingRequests.values())

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public static ClientSettings getClientSettingsFromConnectionStringBuilder(Connec
444444
}
445445
else
446446
{
447-
tokenProvider = new SharedAccessSignatureTokenProvider(builder.getSharedAccessSignatureToken(), Instant.now().plus(Duration.ofSeconds(SecurityConstants.DEFAULT_SAS_TOKEN_VALIDITY_IN_SECONDS)));
447+
tokenProvider = new SharedAccessSignatureTokenProvider(builder.getSharedAccessSignatureToken(), Instant.MAX); // Max validity as we will not generate another token
448448
}
449449

450450
return new ClientSettings(tokenProvider, builder.getRetryPolicy(), builder.getOperationTimeout(), builder.getTransportType());

0 commit comments

Comments
 (0)