Skip to content

Commit 1bebf5a

Browse files
committed
conn str support
1 parent 8cf6cdb commit 1bebf5a

14 files changed

+211
-262
lines changed

azuremanaged/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616

1717
archivesBaseName = 'durabletask-azuremanaged'
1818
group 'com.microsoft'
19-
version = '1.5.0'
19+
version = '1.5.0-preview.1'
2020

2121
def grpcVersion = '1.59.0'
2222
def azureCoreVersion = '1.45.0'

azuremanaged/src/main/java/com/microsoft/durabletask/azuremanaged/DurableTaskSchedulerClientExtensions.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ private DurableTaskSchedulerClientExtensions() {}
2222
*
2323
* @param builder The builder to configure.
2424
* @param connectionString The connection string for Azure-managed Durable Task Scheduler.
25-
* @param tokenCredential The token credential for authentication, or null to use connection string credentials.
2625
* @throws NullPointerException if builder or connectionString is null
2726
*/
2827
public static void useDurableTaskScheduler(
2928
DurableTaskGrpcClientBuilder builder,
30-
String connectionString,
31-
@Nullable TokenCredential tokenCredential) {
29+
String connectionString) {
3230
Objects.requireNonNull(builder, "builder must not be null");
3331
Objects.requireNonNull(connectionString, "connectionString must not be null");
3432

3533
configureBuilder(builder,
36-
DurableTaskSchedulerClientOptions.fromConnectionString(connectionString, tokenCredential));
34+
DurableTaskSchedulerClientOptions.fromConnectionString(connectionString));
3735
}
3836

3937
/**
@@ -64,16 +62,14 @@ public static void useDurableTaskScheduler(
6462
* Creates a DurableTaskGrpcClientBuilder configured for Azure-managed Durable Task Scheduler using a connection string.
6563
*
6664
* @param connectionString The connection string for Azure-managed Durable Task Scheduler.
67-
* @param tokenCredential The token credential for authentication, or null to use connection string credentials.
6865
* @return A new configured DurableTaskGrpcClientBuilder instance.
6966
* @throws NullPointerException if connectionString is null
7067
*/
7168
public static DurableTaskGrpcClientBuilder createClientBuilder(
72-
String connectionString,
73-
@Nullable TokenCredential tokenCredential) {
69+
String connectionString) {
7470
Objects.requireNonNull(connectionString, "connectionString must not be null");
7571
return createBuilderFromOptions(
76-
DurableTaskSchedulerClientOptions.fromConnectionString(connectionString, tokenCredential));
72+
DurableTaskSchedulerClientOptions.fromConnectionString(connectionString));
7773
}
7874

7975
/**

azuremanaged/src/main/java/com/microsoft/durabletask/azuremanaged/DurableTaskSchedulerClientOptions.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.azure.core.credential.TokenCredential;
77
import com.azure.core.credential.TokenRequestContext;
88
import io.grpc.*;
9-
import javax.annotation.Nullable;
109
import java.net.MalformedURLException;
1110
import java.time.Duration;
1211
import java.util.Objects;
@@ -34,12 +33,11 @@ public DurableTaskSchedulerClientOptions() {
3433
* Creates a new instance of DurableTaskSchedulerClientOptions from a connection string.
3534
*
3635
* @param connectionString The connection string to parse.
37-
* @param credential The credential to use for authentication. It is nullable for anonymous access.
3836
* @return A new DurableTaskSchedulerClientOptions object.
3937
*/
40-
public static DurableTaskSchedulerClientOptions fromConnectionString(String connectionString, @Nullable TokenCredential credential) {
38+
public static DurableTaskSchedulerClientOptions fromConnectionString(String connectionString) {
4139
DurableTaskSchedulerConnectionString parsedConnectionString = new DurableTaskSchedulerConnectionString(connectionString);
42-
return fromConnectionString(parsedConnectionString, credential);
40+
return fromConnectionString(parsedConnectionString);
4341
}
4442

4543
/**
@@ -48,13 +46,13 @@ public static DurableTaskSchedulerClientOptions fromConnectionString(String conn
4846
* @param connectionString The parsed connection string.
4947
* @return A new DurableTaskSchedulerClientOptions object.
5048
*/
51-
static DurableTaskSchedulerClientOptions fromConnectionString(DurableTaskSchedulerConnectionString connectionString, @Nullable TokenCredential credential) {
49+
static DurableTaskSchedulerClientOptions fromConnectionString(DurableTaskSchedulerConnectionString connectionString) {
5250
// TODO: Parse different credential types from connection string
5351
DurableTaskSchedulerClientOptions options = new DurableTaskSchedulerClientOptions();
5452
options.setEndpointAddress(connectionString.getEndpoint());
5553
options.setTaskHubName(connectionString.getTaskHubName());
56-
options.setCredential(credential);
57-
options.setAllowInsecureCredentials(credential == null);
54+
options.setCredential(connectionString.getCredential());
55+
options.setAllowInsecureCredentials(options.getCredential() == null);
5856
return options;
5957
}
6058

@@ -178,16 +176,6 @@ public DurableTaskSchedulerClientOptions setTokenRefreshMargin(Duration tokenRef
178176
return this;
179177
}
180178

181-
/**
182-
* Validates that the options are properly configured.
183-
*
184-
* @throws IllegalArgumentException If the options are not properly configured.
185-
*/
186-
public void validate() {
187-
Objects.requireNonNull(endpointAddress, "endpointAddress must not be null");
188-
Objects.requireNonNull(taskHubName, "taskHubName must not be null");
189-
}
190-
191179
/**
192180
* Creates a gRPC channel using the configured options.
193181
*

azuremanaged/src/main/java/com/microsoft/durabletask/azuremanaged/DurableTaskSchedulerConnectionString.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
import java.util.List;
99
import java.util.Map;
1010

11+
import javax.annotation.Nullable;
12+
13+
import com.azure.core.credential.TokenCredential;
14+
import com.azure.identity.AzureCliCredentialBuilder;
15+
import com.azure.identity.AzurePowerShellCredentialBuilder;
16+
import com.azure.identity.DefaultAzureCredentialBuilder;
17+
import com.azure.identity.EnvironmentCredentialBuilder;
18+
import com.azure.identity.ManagedIdentityCredentialBuilder;
19+
import com.azure.identity.WorkloadIdentityCredentialBuilder;
20+
1121
/**
1222
* Represents the constituent parts of a connection string for a Durable Task Scheduler service.
1323
*/
@@ -27,9 +37,9 @@ public DurableTaskSchedulerConnectionString(String connectionString) {
2737
this.properties = parseConnectionString(connectionString);
2838

2939
// Validate required properties
30-
getRequiredValue("Endpoint");
31-
getRequiredValue("Authentication");
32-
getRequiredValue("TaskHub");
40+
this.getAuthentication();
41+
this.getTaskHubName();
42+
this.getEndpoint();
3343
}
3444

3545
/**
@@ -127,4 +137,54 @@ private static Map<String, String> parseConnectionString(String connectionString
127137

128138
return properties;
129139
}
140+
141+
/**
142+
* Gets a TokenCredential based on the authentication type specified in the connection string.
143+
*
144+
* @return A TokenCredential instance based on the specified authentication type, or null if authentication type is "none".
145+
* @throws IllegalArgumentException If the connection string contains an unsupported authentication type.
146+
*/
147+
public @Nullable TokenCredential getCredential() {
148+
String authType = getAuthentication();
149+
150+
// Parse the supported auth types in a case-insensitive way
151+
switch (authType.toLowerCase().trim()) {
152+
case "defaultazure":
153+
return new DefaultAzureCredentialBuilder().build();
154+
case "managedidentity":
155+
return new ManagedIdentityCredentialBuilder().clientId(getClientId()).build();
156+
case "workloadidentity":
157+
WorkloadIdentityCredentialBuilder builder = new WorkloadIdentityCredentialBuilder();
158+
if (getClientId() != null && !getClientId().isEmpty()) {
159+
builder.clientId(getClientId());
160+
}
161+
162+
if (getTenantId() != null && !getTenantId().isEmpty()) {
163+
builder.tenantId(getTenantId());
164+
}
165+
166+
if (getTokenFilePath() != null && !getTokenFilePath().isEmpty()) {
167+
builder.tokenFilePath(getTokenFilePath());
168+
}
169+
170+
if (getAdditionallyAllowedTenants() != null) {
171+
for (String tenant : getAdditionallyAllowedTenants()) {
172+
builder.additionallyAllowedTenants(tenant);
173+
}
174+
}
175+
176+
return builder.build();
177+
case "environment":
178+
return new EnvironmentCredentialBuilder().build();
179+
case "azurecli":
180+
return new AzureCliCredentialBuilder().build();
181+
case "azurepowershell":
182+
return new AzurePowerShellCredentialBuilder().build();
183+
case "none":
184+
return null;
185+
default:
186+
throw new IllegalArgumentException(
187+
String.format("The connection string contains an unsupported authentication type '%s'.", authType));
188+
}
189+
}
130190
}

azuremanaged/src/main/java/com/microsoft/durabletask/azuremanaged/DurableTaskSchedulerWorkerExtensions.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ private DurableTaskSchedulerWorkerExtensions() {}
2222
*
2323
* @param builder The builder to configure.
2424
* @param connectionString The connection string for Azure-managed Durable Task Scheduler.
25-
* @param tokenCredential The token credential for authentication, or null to use connection string credentials.
2625
* @throws NullPointerException if builder or connectionString is null
2726
*/
2827
public static void useDurableTaskScheduler(
2928
DurableTaskGrpcWorkerBuilder builder,
30-
String connectionString,
31-
@Nullable TokenCredential tokenCredential) {
29+
String connectionString) {
3230
Objects.requireNonNull(builder, "builder must not be null");
3331
Objects.requireNonNull(connectionString, "connectionString must not be null");
3432

3533
configureBuilder(builder,
36-
DurableTaskSchedulerWorkerOptions.fromConnectionString(connectionString, tokenCredential));
34+
DurableTaskSchedulerWorkerOptions.fromConnectionString(connectionString));
3735
}
3836

3937
/**
@@ -64,16 +62,14 @@ public static void useDurableTaskScheduler(
6462
* Creates a DurableTaskGrpcWorkerBuilder configured for Azure-managed Durable Task Scheduler using a connection string.
6563
*
6664
* @param connectionString The connection string for Azure-managed Durable Task Scheduler.
67-
* @param tokenCredential The token credential for authentication, or null to use connection string credentials.
6865
* @return A new configured DurableTaskGrpcWorkerBuilder instance.
6966
* @throws NullPointerException if connectionString is null
7067
*/
7168
public static DurableTaskGrpcWorkerBuilder createWorkerBuilder(
72-
String connectionString,
73-
@Nullable TokenCredential tokenCredential) {
69+
String connectionString) {
7470
Objects.requireNonNull(connectionString, "connectionString must not be null");
7571
return createBuilderFromOptions(
76-
DurableTaskSchedulerWorkerOptions.fromConnectionString(connectionString, tokenCredential));
72+
DurableTaskSchedulerWorkerOptions.fromConnectionString(connectionString));
7773
}
7874

7975
/**

azuremanaged/src/main/java/com/microsoft/durabletask/azuremanaged/DurableTaskSchedulerWorkerOptions.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Objects;
2222
import java.net.URL;
2323
import java.net.MalformedURLException;
24-
import javax.annotation.Nullable;
2524

2625
/**
2726
* Options for configuring the Durable Task Scheduler worker.
@@ -45,27 +44,25 @@ public DurableTaskSchedulerWorkerOptions() {
4544
* Creates a new instance of DurableTaskSchedulerWorkerOptions from a connection string.
4645
*
4746
* @param connectionString The connection string to parse.
48-
* @param credential The token credential for authentication, or null to use connection string credentials.
4947
* @return A new DurableTaskSchedulerWorkerOptions object.
5048
*/
51-
public static DurableTaskSchedulerWorkerOptions fromConnectionString(String connectionString, @Nullable TokenCredential credential) {
49+
public static DurableTaskSchedulerWorkerOptions fromConnectionString(String connectionString) {
5250
DurableTaskSchedulerConnectionString parsedConnectionString = new DurableTaskSchedulerConnectionString(connectionString);
53-
return fromConnectionString(parsedConnectionString, credential);
51+
return fromConnectionString(parsedConnectionString);
5452
}
5553

5654
/**
5755
* Creates a new instance of DurableTaskSchedulerWorkerOptions from a parsed connection string.
5856
*
5957
* @param connectionString The parsed connection string.
60-
* @param credential The token credential for authentication, or null to use connection string credentials.
6158
* @return A new DurableTaskSchedulerWorkerOptions object.
6259
*/
63-
static DurableTaskSchedulerWorkerOptions fromConnectionString(DurableTaskSchedulerConnectionString connectionString, @Nullable TokenCredential credential) {
60+
static DurableTaskSchedulerWorkerOptions fromConnectionString(DurableTaskSchedulerConnectionString connectionString) {
6461
DurableTaskSchedulerWorkerOptions options = new DurableTaskSchedulerWorkerOptions();
6562
options.setEndpointAddress(connectionString.getEndpoint());
6663
options.setTaskHubName(connectionString.getTaskHubName());
67-
options.setCredential(credential);
68-
options.setAllowInsecureCredentials(credential == null);
64+
options.setCredential(connectionString.getCredential());
65+
options.setAllowInsecureCredentials(options.getCredential() == null);
6966
return options;
7067
}
7168

@@ -189,16 +186,6 @@ public DurableTaskSchedulerWorkerOptions setTokenRefreshMargin(Duration tokenRef
189186
return this;
190187
}
191188

192-
/**
193-
* Validates that the options are properly configured.
194-
*
195-
* @throws IllegalArgumentException If the options are not properly configured.
196-
*/
197-
public void validate() {
198-
Objects.requireNonNull(endpointAddress, "endpointAddress must not be null");
199-
Objects.requireNonNull(taskHubName, "taskHubName must not be null");
200-
}
201-
202189
/**
203190
* Creates a gRPC channel using the configured options.
204191
*

azuremanaged/src/test/java/com/microsoft/durabletask/azuremanaged/DurableTaskSchedulerClientExtensionsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void useDurableTaskScheduler_WithConnectionString_ConfiguresBuilder() {
3737

3838
// Act
3939
DurableTaskSchedulerClientExtensions.useDurableTaskScheduler(
40-
mockBuilder, VALID_CONNECTION_STRING, mockCredential);
40+
mockBuilder, VALID_CONNECTION_STRING);
4141

4242
// Assert
4343
verify(mockBuilder).grpcChannel(any(Channel.class));
@@ -49,7 +49,7 @@ public void useDurableTaskScheduler_WithConnectionString_ThrowsForNullBuilder()
4949
// Act & Assert
5050
assertThrows(NullPointerException.class,
5151
() -> DurableTaskSchedulerClientExtensions.useDurableTaskScheduler(
52-
null, VALID_CONNECTION_STRING, mockCredential));
52+
null, VALID_CONNECTION_STRING));
5353
}
5454

5555
@Test
@@ -58,7 +58,7 @@ public void useDurableTaskScheduler_WithConnectionString_ThrowsForNullConnection
5858
// Act & Assert
5959
assertThrows(NullPointerException.class,
6060
() -> DurableTaskSchedulerClientExtensions.useDurableTaskScheduler(
61-
mockBuilder, null, mockCredential));
61+
mockBuilder, null));
6262
}
6363

6464
@Test
@@ -107,7 +107,7 @@ public void useDurableTaskScheduler_WithExplicitParameters_ThrowsForNullTaskHubN
107107
public void createClientBuilder_WithConnectionString_CreatesValidBuilder() {
108108
// Act
109109
DurableTaskGrpcClientBuilder result =
110-
DurableTaskSchedulerClientExtensions.createClientBuilder(VALID_CONNECTION_STRING, mockCredential);
110+
DurableTaskSchedulerClientExtensions.createClientBuilder(VALID_CONNECTION_STRING);
111111

112112
// Assert
113113
assertNotNull(result);
@@ -118,7 +118,7 @@ public void createClientBuilder_WithConnectionString_CreatesValidBuilder() {
118118
public void createClientBuilder_WithConnectionString_ThrowsForNullConnectionString() {
119119
// Act & Assert
120120
assertThrows(NullPointerException.class,
121-
() -> DurableTaskSchedulerClientExtensions.createClientBuilder(null, mockCredential));
121+
() -> DurableTaskSchedulerClientExtensions.createClientBuilder(null));
122122
}
123123

124124
@Test

0 commit comments

Comments
 (0)