Repository navigation
Expand file tree
/
Copy pathresource_id_test.go
More file actions
442 lines (423 loc) · 18.3 KB
/
Copy pathresource_id_test.go
File metadata and controls
442 lines (423 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package durabletaskscheduler
import (
"context"
"crypto/tls"
"crypto/x509"
"net/http/httptest"
"os"
"os/exec"
"testing"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/microsoft/durabletask-go/api"
durabletaskclient "github.com/microsoft/durabletask-go/client"
"github.com/microsoft/durabletask-go/internal/protos"
"github.com/microsoft/durabletask-go/task"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/types/known/emptypb"
)
var resourceIDCases = []struct {
name string
region string
resourceID string
want string
}{
{name: "unset region", want: DefaultResourceID},
{name: "empty region", want: DefaultResourceID},
{name: "public", region: "westus2", want: DefaultResourceID},
{name: "china is not inferred", region: "chinaeast2", want: DefaultResourceID},
{name: "government substring", region: "notusgov", want: DefaultResourceID},
{name: "DoD substring", region: "notusdod", want: DefaultResourceID},
{name: "government", region: "usgovvirginia", want: GovernmentResourceID},
{name: "government uppercase", region: "USGOVARIZONA", want: GovernmentResourceID},
{name: "government mixed case", region: "UsGovTexas", want: GovernmentResourceID},
{name: "DoD", region: "usdodcentral", want: GovernmentResourceID},
{name: "DoD uppercase", region: "USDODEAST", want: GovernmentResourceID},
{name: "DoD mixed case", region: "UsDodCentral", want: GovernmentResourceID},
{name: "exact government prefix", region: "usgov", want: GovernmentResourceID},
{name: "exact DoD prefix", region: "usdod", want: GovernmentResourceID},
{name: "leading space is not a prefix", region: " usgovvirginia", want: DefaultResourceID},
{name: "public override government", region: "usgovvirginia", resourceID: DefaultResourceID, want: DefaultResourceID},
{name: "public override DoD", region: "usdodcentral", resourceID: DefaultResourceID, want: DefaultResourceID},
{name: "government override public", region: "westus2", resourceID: GovernmentResourceID, want: GovernmentResourceID},
{name: "custom", region: "chinaeast2", resourceID: "https://Custom.Example/Resource", want: "https://Custom.Example/Resource"},
{name: "trailing slash", resourceID: GovernmentResourceID + "/", want: GovernmentResourceID},
{name: "existing suffix", resourceID: GovernmentResourceID + "/.default", want: GovernmentResourceID},
{name: "uppercase suffix and slashes", resourceID: GovernmentResourceID + "//.DEFAULT//", want: GovernmentResourceID},
{name: "surrounding whitespace", resourceID: " \t\n" + GovernmentResourceID + "/.default/ \r\n", want: GovernmentResourceID},
{name: "custom URI casing", region: "usgovvirginia", resourceID: "api://CustomAudience/resource/.DEFAULT/", want: "api://CustomAudience/resource"},
{name: "strip only one suffix", resourceID: "api://custom/.default/.default", want: "api://custom/.default"},
{name: "preserve meaningful suffix casing", resourceID: "api://custom/.DEFAULT//.default/", want: "api://custom/.DEFAULT"},
{name: "preserve equals", resourceID: "api://Custom/resource=value", want: "api://Custom/resource=value"},
}
func setResourceRegion(t *testing.T, name, region string) {
t.Helper()
t.Setenv("REGION_NAME", region)
if name == "unset region" {
require.NoError(t, os.Unsetenv("REGION_NAME"))
}
}
func recordedTokenOptions(c *recordingCredential) []policy.TokenRequestOptions {
c.mu.Lock()
defer c.mu.Unlock()
return append([]policy.TokenRequestOptions(nil), c.options...)
}
func assertResourceScopeAndRefresh(t *testing.T, options *Options, want string) {
t.Helper()
for _, role := range []connectionRole{clientRole, workerRole} {
credential := &recordingCredential{}
prepared, err := prepareOptionsWith(options, func(credentialSpec) (azcore.TokenCredential, error) {
return credential, nil
})
require.NoError(t, err)
if options.Authentication == AuthenticationTokenCredential {
var ok bool
credential, ok = prepared.Credential.(*recordingCredential)
require.True(t, ok)
}
before := len(recordedTokenOptions(credential))
perRPC := newPerRPCCredentials(&prepared, role, "worker")
require.Len(t, recordedTokenOptions(credential), before, "construction must not acquire a token")
for range 2 {
values, err := perRPC.GetRequestMetadata(context.Background())
require.NoError(t, err)
if options.Authentication == AuthenticationNone {
require.NotContains(t, values, "authorization")
} else {
require.Equal(t, "Bearer token", values["authorization"])
}
}
if options.Authentication == AuthenticationNone {
require.Empty(t, recordedTokenOptions(credential))
continue
}
require.Len(t, recordedTokenOptions(credential), before+1, "fresh tokens must be cached")
perRPC.token.Store(&schedulerCredentialState{
refreshAfter: time.Now().Add(-time.Hour),
validUntil: time.Now().Add(-time.Minute),
})
errs := callSchedulerCredentialsConcurrently(perRPC, 16)
for range 16 {
require.NoError(t, <-errs)
}
calls := recordedTokenOptions(credential)[before:]
require.Len(t, calls, 2)
for _, call := range calls {
require.Equal(t, []string{want + "/.default"}, call.Scopes)
require.Empty(t, call.TenantID)
}
}
}
func TestResourceIDScopesForEveryAuthenticationMode(t *testing.T) {
for _, tt := range resourceIDCases {
t.Run(tt.name, func(t *testing.T) {
setResourceRegion(t, tt.name, tt.region)
for _, authentication := range allAuthenticationTypes {
t.Run(string(authentication), func(t *testing.T) {
for _, source := range []string{"options", "connection string"} {
if source == "connection string" && authentication == AuthenticationTokenCredential {
continue
}
t.Run(source, func(t *testing.T) {
options := NewOptions("https://scheduler.example.com", "hub")
options.Authentication = authentication
options.ResourceID = tt.resourceID
if source == "connection string" {
var err error
options, err = NewOptionsFromConnectionString(
"Endpoint=https://scheduler.example.com;TaskHub=hub;Authentication=" +
string(authentication) + ";ResourceId=" + tt.resourceID,
)
require.NoError(t, err)
}
if authentication == AuthenticationTokenCredential {
options.Credential = &recordingCredential{}
}
original := options.ResourceID
assertResourceScopeAndRefresh(t, options, tt.want)
require.Equal(t, original, options.ResourceID, "preparation must not mutate caller options")
require.Equal(t, "https://scheduler.example.com", options.EndpointAddress)
require.Empty(t, options.AuthorityHost)
})
}
})
}
})
}
}
func TestResourceIDDefaultsArePerInstance(t *testing.T) {
for _, source := range []string{"NewOptions", "NewOptionsWithCredential", "connection string", "connection string empty", "literal", "reset empty"} {
t.Run(source, func(t *testing.T) {
newOptions := func() *Options {
credential := &recordingCredential{}
switch source {
case "NewOptions":
return NewOptions("scheduler.example.com", "hub")
case "NewOptionsWithCredential":
return NewOptionsWithCredential("scheduler.example.com", "hub", credential)
case "connection string", "connection string empty":
connectionString := "Endpoint=scheduler.example.com;TaskHub=hub;Authentication=DefaultAzure"
if source == "connection string empty" {
connectionString += ";ResourceId="
}
options, err := NewOptionsFromConnectionString(connectionString)
require.NoError(t, err)
return options
case "literal":
return &Options{EndpointAddress: "scheduler.example.com", TaskHubName: "hub"}
default:
options := NewOptions("scheduler.example.com", "hub")
options.ResourceID = ""
return options
}
}
var instances []Options
for _, region := range []string{"UsGovVirginia", "westus2", "USDODEAST"} {
t.Setenv("REGION_NAME", region)
options := newOptions()
if source != "literal" && source != "reset empty" {
t.Setenv("REGION_NAME", "changed-after-options")
}
prepared, err := prepareOptionsWith(options, func(credentialSpec) (azcore.TokenCredential, error) {
return &recordingCredential{}, nil
})
require.NoError(t, err)
instances = append(instances, prepared)
}
t.Setenv("REGION_NAME", "chinaeast2")
for i := range instances {
want := GovernmentResourceID
if i == 1 {
want = DefaultResourceID
}
assertResourceScopeAndRefresh(t, &instances[i], want)
}
})
}
}
func TestInvalidResourceIDsFailBeforeAuthentication(t *testing.T) {
for _, resourceID := range []string{" \t ", "\r\n", "///", "/.default", "/.DEFAULT///", " /.DEFAULT/// "} {
t.Run(resourceID, func(t *testing.T) {
for _, authentication := range allAuthenticationTypes {
options := NewOptions("scheduler.example.com", "hub")
options.Authentication = authentication
options.ResourceID = resourceID
require.ErrorContains(t, options.Validate(), "resource ID cannot be empty after normalization")
_, err := prepareOptionsWith(options, func(credentialSpec) (azcore.TokenCredential, error) {
t.Fatal("invalid ResourceID must be rejected before credential construction")
return nil, nil
})
require.ErrorContains(t, err, "resource ID cannot be empty after normalization")
client, err := NewClient(context.Background(), options, nil)
require.Nil(t, client)
require.ErrorContains(t, err, "resource ID cannot be empty after normalization")
worker, err := NewWorker(options, task.NewTaskRegistry(), nil)
require.Nil(t, worker)
require.ErrorContains(t, err, "resource ID cannot be empty after normalization")
if authentication != AuthenticationTokenCredential {
_, err = NewOptionsFromConnectionString("Endpoint=scheduler.example.com;TaskHub=hub;Authentication=" +
string(authentication) + ";ResourceId=" + resourceID)
require.ErrorContains(t, err, "resource ID cannot be empty after normalization")
}
}
})
}
}
func TestResourceIDConnectionStringKeysAndDuplicates(t *testing.T) {
t.Setenv("REGION_NAME", "usgovvirginia")
for _, key := range []string{"ResourceId", "resourceid", "RESOURCEID", "ReSoUrCeId"} {
for _, value := range []string{"", "api://Custom/.default/.DEFAULT/"} {
options, err := NewOptionsFromConnectionString(
"Endpoint=scheduler.example.com;TaskHub=hub;Authentication=DefaultAzure;ResourceId=///;" + key + "=" + value,
)
require.NoError(t, err)
want := "api://Custom/.default"
if value == "" {
want = GovernmentResourceID
}
assertResourceScopeAndRefresh(t, options, want)
}
_, err := NewOptionsFromConnectionString(
"Endpoint=scheduler.example.com;TaskHub=hub;Authentication=None;ResourceId=api://valid;" + key + "= ",
)
require.ErrorContains(t, err, "resource ID cannot be empty after normalization")
}
}
// Called only in an isolated test subprocess because fallback roots are
// process-global and cannot be restored. No system certificate is installed.
func audienceTestTLS() *tls.Config {
server := httptest.NewTLSServer(nil)
defer server.Close()
roots := x509.NewCertPool()
roots.AddCert(server.Certificate())
x509.SetFallbackRoots(roots)
return server.TLS.Clone()
}
type audienceServer struct {
metadataServer
disconnect chan struct{}
}
func (s *audienceServer) RaiseEvent(context.Context, *protos.RaiseEventRequest) (*protos.RaiseEventResponse, error) {
return nil, status.Error(codes.Unavailable, "replace the test channel")
}
func (s *audienceServer) GetWorkItems(_ *protos.GetWorkItemsRequest, stream protos.TaskHubSidecarService_GetWorkItemsServer) error {
incoming, _ := metadata.FromIncomingContext(stream.Context())
s.metadata <- incoming
select {
case <-s.disconnect:
return status.Error(codes.Unavailable, "reconnect the test worker")
case <-stream.Context().Done():
return nil
}
}
func awaitAudienceMetadata(t *testing.T, server *audienceServer) metadata.MD {
t.Helper()
select {
case incoming := <-server.metadata:
require.Equal(t, []string{"Bearer token"}, incoming.Get("authorization"))
return incoming
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for authenticated RPC")
return nil
}
}
func TestPublicAuthenticationPathsPreserveResourceIDAcrossReconnects(t *testing.T) {
if os.Getenv("DTS_AUDIENCE_TLS_TEST") != "1" {
command := exec.Command(os.Args[0], "-test.run=^TestPublicAuthenticationPathsPreserveResourceIDAcrossReconnects$", "-test.timeout=2m")
command.Env = append(os.Environ(), "DTS_AUDIENCE_TLS_TEST=1")
output, err := command.CombinedOutput()
require.NoError(t, err, "%s", output)
return
}
t.Setenv("GODEBUG", os.Getenv("GODEBUG")+",x509usefallbackroots=1")
tlsConfig := audienceTestTLS()
for _, tt := range resourceIDCases {
t.Run(tt.name, func(t *testing.T) {
for _, path := range []string{"client", "worker", "compatibility listener"} {
t.Run(path, func(t *testing.T) {
setResourceRegion(t, tt.name, tt.region)
server := &audienceServer{
metadataServer: metadataServer{metadata: make(chan metadata.MD, 16)},
disconnect: make(chan struct{}, 1),
}
listener := bufconn.Listen(1024 * 1024)
grpcServer := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
protos.RegisterTaskHubSidecarServiceServer(grpcServer, server)
go func() { _ = grpcServer.Serve(listener) }()
defer grpcServer.Stop()
defer func() { require.NoError(t, listener.Close()) }()
credential := &recordingCredential{}
if tt.name == "government" {
credential.refreshOn = time.Now().Add(-time.Hour)
}
options := NewOptionsWithCredential("https://127.0.0.1", "hub", credential)
options.ResourceID = tt.resourceID
options.dialer = bufconnDialer(listener)
options.ChannelRecreateFailureThreshold = 1
options.ChannelRecreateMinInterval = 0
connections := make(chan *grpc.ClientConn, 1)
options.UnaryInterceptors = []grpc.UnaryClientInterceptor{
func(ctx context.Context, method string, request, reply any, connection *grpc.ClientConn, invoker grpc.UnaryInvoker, callOptions ...grpc.CallOption) error {
select {
case connections <- connection:
default:
}
return invoker(ctx, method, request, reply, connection, callOptions...)
},
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
wantTokenCalls := 2
refreshTransport := func() {
if tt.name != "government" {
return
}
connection := <-connections
// Exercise refresh on the same public transport after its
// minimum one-second cache interval, before recreating it.
require.Eventually(t, func() bool {
_, err := protos.NewTaskHubSidecarServiceClient(connection).Hello(ctx, &emptypb.Empty{})
require.NoError(t, err)
awaitAudienceMetadata(t, server)
return len(recordedTokenOptions(credential)) == 2
}, 3*time.Second, 20*time.Millisecond)
wantTokenCalls++
}
if path == "worker" {
worker, err := NewWorker(options, task.NewTaskRegistry(), nil,
durabletaskclient.WithWorkerReconnectBackoff(time.Millisecond, time.Millisecond))
require.NoError(t, err)
require.Empty(t, recordedTokenOptions(credential), "worker token acquisition stays lazy")
t.Setenv("REGION_NAME", "changed-after-worker-construction")
options.ResourceID = "api://changed-after-worker-construction"
require.NoError(t, worker.Start(ctx))
defer func() { require.NoError(t, worker.Shutdown(ctx)) }()
hello := awaitAudienceMetadata(t, server)
stream := awaitAudienceMetadata(t, server)
require.Equal(t, hello.Get("workerid"), stream.Get("workerid"))
require.Len(t, recordedTokenOptions(credential), 1)
refreshTransport()
server.disconnect <- struct{}{}
reconnectedHello := awaitAudienceMetadata(t, server)
reconnectedStream := awaitAudienceMetadata(t, server)
require.Equal(t, hello.Get("workerid"), reconnectedHello.Get("workerid"))
require.Equal(t, hello.Get("workerid"), reconnectedStream.Get("workerid"))
} else {
client, err := NewClient(ctx, options, nil)
require.NoError(t, err)
defer func() { require.NoError(t, client.Close()) }()
awaitAudienceMetadata(t, server)
require.Len(t, recordedTokenOptions(credential), 1, "client acquires a token for its eager Hello")
if path == "compatibility listener" {
require.NoError(t, client.StartWorkItemListener(ctx, task.NewTaskRegistry(),
durabletaskclient.WithWorkerReconnectBackoff(time.Millisecond, time.Millisecond)))
awaitAudienceMetadata(t, server)
awaitAudienceMetadata(t, server)
require.Len(t, recordedTokenOptions(credential), 1, "listener shares the client's cached token")
}
t.Setenv("REGION_NAME", "changed-after-client-construction")
options.ResourceID = "api://changed-after-client-construction"
refreshTransport()
client.connection.mu.Lock()
old := client.connection.current
client.connection.mu.Unlock()
require.Error(t, client.RaiseEvent(ctx, api.InstanceID("test"), "recreate"))
awaitAudienceMetadata(t, server)
// The replacement Hello can arrive before the swap completes.
require.Eventually(t, func() bool {
client.connection.mu.Lock()
defer client.connection.mu.Unlock()
return !client.connection.recreateInFlight && client.connection.current != old
}, 5*time.Second, time.Millisecond)
if path == "compatibility listener" {
server.disconnect <- struct{}{}
awaitAudienceMetadata(t, server)
awaitAudienceMetadata(t, server)
}
}
calls := recordedTokenOptions(credential)
require.Len(t, calls, wantTokenCalls)
for _, call := range calls {
require.Equal(t, []string{tt.want + "/.default"}, call.Scopes)
}
require.Empty(t, options.AuthorityHost)
require.Equal(t, "https://127.0.0.1", options.EndpointAddress)
})
}
})
}
}
func TestResourceIDDoesNotInferAudienceFromEndpoint(t *testing.T) {
t.Setenv("REGION_NAME", "")
for _, endpoint := range []string{"https://scheduler.durabletask.azure.us", "https://scheduler.durabletask.io"} {
options := NewOptions(endpoint, "hub")
assertResourceScopeAndRefresh(t, options, DefaultResourceID)
}
}