Skip to content

Commit 64a7317

Browse files
gaultierory-bot
authored andcommitted
fix: data race in Kratos webhook tests
GitOrigin-RevId: 382628a420f566c2ae4c0b15a1b0adf86f695206
1 parent 3df05c5 commit 64a7317

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

selfservice/hook/web_hook_integration_test.go

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
sdktrace "go.opentelemetry.io/otel/sdk/trace"
3030
"go.opentelemetry.io/otel/sdk/trace/tracetest"
3131

32+
"github.com/ory/kratos/driver"
3233
"github.com/ory/kratos/driver/config"
3334
"github.com/ory/kratos/identity"
3435
"github.com/ory/kratos/pkg"
@@ -60,12 +61,9 @@ var transientPayload = json.RawMessage(`{
6061
}
6162
}`)
6263

63-
func TestWebHooks(t *testing.T) {
64-
ctx := context.Background()
64+
func makeConfigurationAndRegistry(t *testing.T) *driver.RegistryDefault {
6565
conf, reg := pkg.NewFastRegistryWithMocks(t)
66-
logger := logrusx.New("kratos", "test")
67-
68-
conf.MustSet(ctx, config.ViperKeyWebhookHeaderAllowlist, []string{
66+
conf.MustSet(t.Context(), config.ViperKeyWebhookHeaderAllowlist, []string{
6967
"Accept",
7068
"Accept-Encoding",
7169
"Accept-Language",
@@ -85,16 +83,27 @@ func TestWebHooks(t *testing.T) {
8583
"User-Agent",
8684
"Valid-Header",
8785
})
86+
return reg
87+
}
8888

89-
whDeps := struct {
90-
x.BasicRegistry
91-
*jsonnetsecure.TestProvider
92-
config.Provider
93-
}{
94-
x.BasicRegistry{L: logger, C: reg.HTTPClient(ctx), T: otelx.NewNoop()},
95-
jsonnetsecure.NewTestProvider(t),
96-
reg,
89+
type webHookDeps struct {
90+
x.BasicRegistry
91+
*jsonnetsecure.TestProvider
92+
config.Provider
93+
}
94+
95+
func newWebHookDeps(t *testing.T, logger *logrusx.Logger, reg *driver.RegistryDefault) *webHookDeps {
96+
t.Helper()
97+
return &webHookDeps{
98+
BasicRegistry: x.BasicRegistry{L: logger, C: reg.HTTPClient(t.Context()), T: otelx.NewNoop()},
99+
TestProvider: jsonnetsecure.NewTestProvider(t),
100+
Provider: reg,
97101
}
102+
}
103+
104+
func TestWebHooks(t *testing.T) {
105+
logger := logrusx.New("kratos", "test")
106+
98107
type WebHookRequest struct {
99108
Body string
100109
Headers http.Header
@@ -355,7 +364,7 @@ func TestWebHooks(t *testing.T) {
355364
RequestURI: "/some_end_point",
356365
Method: http.MethodPost,
357366
URL: &url.URL{Path: "/some_end_point"},
358-
}).WithContext(ctx)
367+
}).WithContext(t.Context())
359368
cookie, err := req.Cookie("Some-Cookie-1")
360369
require.NoError(t, err)
361370
require.Equal(t, cookie.Name, "Some-Cookie-1")
@@ -373,7 +382,8 @@ func TestWebHooks(t *testing.T) {
373382
whr := &WebHookRequest{}
374383
ts := newServer(webHookEndPoint(whr))
375384

376-
wh := hook.NewWebHook(&whDeps, &request.Config{
385+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
386+
wh := hook.NewWebHook(whDeps, &request.Config{
377387
Method: method,
378388
URL: ts.URL + path,
379389
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -653,7 +663,8 @@ func TestWebHooks(t *testing.T) {
653663
code, res := tc.webHookResponse()
654664
ts := newServer(webHookHttpCodeWithBodyEndPoint(t, code, res))
655665

656-
wh := hook.NewWebHook(&whDeps, &request.Config{
666+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
667+
wh := hook.NewWebHook(whDeps, &request.Config{
657668
Method: method,
658669
URL: ts.URL + path,
659670
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -688,7 +699,8 @@ func TestWebHooks(t *testing.T) {
688699
URL: &url.URL{Path: "some_end_point"},
689700
}
690701
ts := newServer(webHookHttpCodeWithBodyEndPoint(t, responseCode, response))
691-
wh := hook.NewWebHook(&whDeps, &request.Config{
702+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
703+
wh := hook.NewWebHook(whDeps, &request.Config{
692704
Method: "POST",
693705
URL: ts.URL + path,
694706
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -795,7 +807,8 @@ func TestWebHooks(t *testing.T) {
795807
Method: http.MethodPost,
796808
}
797809
f := &login.Flow{ID: x.NewUUID()}
798-
wh := hook.NewWebHook(&whDeps, &request.Config{
810+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
811+
wh := hook.NewWebHook(whDeps, &request.Config{
799812
Method: "GET",
800813
URL: ts.URL + path,
801814
TemplateURI: "./stub/test_body.jsonnet",
@@ -828,7 +841,8 @@ func TestWebHooks(t *testing.T) {
828841
Method: http.MethodPost,
829842
}
830843
f := &settings.Flow{ID: x.NewUUID()}
831-
wh := hook.NewWebHook(&whDeps, &request.Config{
844+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
845+
wh := hook.NewWebHook(whDeps, &request.Config{
832846
Method: "POST",
833847
URL: ts.URL + path,
834848
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -865,7 +879,8 @@ func TestWebHooks(t *testing.T) {
865879
Method: http.MethodPost,
866880
}
867881
f := &login.Flow{ID: x.NewUUID()}
868-
wh := hook.NewWebHook(&whDeps, &request.Config{
882+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
883+
wh := hook.NewWebHook(whDeps, &request.Config{
869884
Method: "POST",
870885
URL: ts.URL + path,
871886
TemplateURI: "file://./stub/bad_template.jsonnet",
@@ -885,7 +900,8 @@ func TestWebHooks(t *testing.T) {
885900
Method: http.MethodPost,
886901
}
887902
f := &login.Flow{ID: x.NewUUID()}
888-
wh := hook.NewWebHook(&whDeps, &request.Config{
903+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
904+
wh := hook.NewWebHook(whDeps, &request.Config{
889905
Method: "GET",
890906
URL: ts.URL + path,
891907
TemplateURI: "file://./stub/bad_template.jsonnet",
@@ -909,7 +925,8 @@ func TestWebHooks(t *testing.T) {
909925
Method: http.MethodPost,
910926
}
911927
f := &login.Flow{ID: x.NewUUID()}
912-
wh := hook.NewWebHook(&whDeps, &request.Config{
928+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
929+
wh := hook.NewWebHook(whDeps, &request.Config{
913930
Method: "POST",
914931
URL: "https://i-do-not-exist/",
915932
TemplateURI: "file://./stub/cancel_template.jsonnet",
@@ -946,7 +963,8 @@ func TestWebHooks(t *testing.T) {
946963
Method: http.MethodPost,
947964
}
948965
f := &login.Flow{ID: x.NewUUID()}
949-
wh := hook.NewWebHook(&whDeps, &request.Config{
966+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
967+
wh := hook.NewWebHook(whDeps, &request.Config{
950968
Method: "GET",
951969
URL: ts.URL + path,
952970
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -985,7 +1003,8 @@ func TestWebHooks(t *testing.T) {
9851003
Method: http.MethodPost,
9861004
}
9871005
f := &login.Flow{ID: x.NewUUID()}
988-
wh := hook.NewWebHook(&whDeps, &request.Config{
1006+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
1007+
wh := hook.NewWebHook(whDeps, &request.Config{
9891008
Method: "GET",
9901009
URL: ts.URL + path,
9911010
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -1023,7 +1042,8 @@ func TestWebHooks(t *testing.T) {
10231042
Method: http.MethodPost,
10241043
}
10251044
f := &login.Flow{ID: x.NewUUID()}
1026-
wh := hook.NewWebHook(&whDeps, &request.Config{
1045+
whDeps := newWebHookDeps(t, logger, makeConfigurationAndRegistry(t))
1046+
wh := hook.NewWebHook(whDeps, &request.Config{
10271047
Method: "POST",
10281048
URL: ts.URL + path,
10291049
TemplateURI: "file://./stub/test_body.jsonnet",
@@ -1041,20 +1061,7 @@ func TestWebHooks(t *testing.T) {
10411061

10421062
func TestDisallowPrivateIPRanges(t *testing.T) {
10431063
t.Parallel()
1044-
ctx := context.Background()
1045-
conf, reg := pkg.NewFastRegistryWithMocks(t)
1046-
conf.MustSet(ctx, config.ViperKeyClientHTTPNoPrivateIPRanges, true)
1047-
conf.MustSet(ctx, config.ViperKeyClientHTTPPrivateIPExceptionURLs, []string{"http://localhost/exception"})
10481064
logger := logrusx.New("kratos", "test")
1049-
whDeps := struct {
1050-
x.BasicRegistry
1051-
*jsonnetsecure.TestProvider
1052-
config.Provider
1053-
}{
1054-
x.BasicRegistry{L: logger, C: reg.HTTPClient(context.Background()), T: otelx.NewNoop()},
1055-
jsonnetsecure.NewTestProvider(t),
1056-
reg,
1057-
}
10581065

10591066
req := &http.Request{
10601067
Header: map[string][]string{"Some-Header": {"Some-Value"}},
@@ -1068,7 +1075,11 @@ func TestDisallowPrivateIPRanges(t *testing.T) {
10681075

10691076
t.Run("not allowed to call url", func(t *testing.T) {
10701077
t.Parallel()
1071-
wh := hook.NewWebHook(&whDeps, &request.Config{
1078+
conf, reg := pkg.NewFastRegistryWithMocks(t)
1079+
conf.MustSet(t.Context(), config.ViperKeyClientHTTPNoPrivateIPRanges, true)
1080+
conf.MustSet(t.Context(), config.ViperKeyClientHTTPPrivateIPExceptionURLs, []string{"http://localhost/exception"})
1081+
whDeps := newWebHookDeps(t, logger, reg)
1082+
wh := hook.NewWebHook(whDeps, &request.Config{
10721083
URL: "https://localhost:1234/",
10731084
Method: "GET",
10741085
TemplateURI: "file://stub/test_body.jsonnet",
@@ -1079,7 +1090,11 @@ func TestDisallowPrivateIPRanges(t *testing.T) {
10791090

10801091
t.Run("allowed to call exempt url", func(t *testing.T) {
10811092
t.Parallel()
1082-
wh := hook.NewWebHook(&whDeps, &request.Config{
1093+
conf, reg := pkg.NewFastRegistryWithMocks(t)
1094+
conf.MustSet(t.Context(), config.ViperKeyClientHTTPNoPrivateIPRanges, true)
1095+
conf.MustSet(t.Context(), config.ViperKeyClientHTTPPrivateIPExceptionURLs, []string{"http://localhost/exception"})
1096+
whDeps := newWebHookDeps(t, logger, reg)
1097+
wh := hook.NewWebHook(whDeps, &request.Config{
10831098
URL: "http://localhost/exception",
10841099
Method: "GET",
10851100
TemplateURI: "file://stub/test_body.jsonnet",
@@ -1100,7 +1115,11 @@ func TestDisallowPrivateIPRanges(t *testing.T) {
11001115
}
11011116
s := &session.Session{ID: x.NewUUID(), Identity: &identity.Identity{ID: x.NewUUID()}}
11021117
f := &login.Flow{ID: x.NewUUID()}
1103-
wh := hook.NewWebHook(&whDeps, &request.Config{
1118+
conf, reg := pkg.NewFastRegistryWithMocks(t)
1119+
conf.MustSet(t.Context(), config.ViperKeyClientHTTPNoPrivateIPRanges, true)
1120+
conf.MustSet(t.Context(), config.ViperKeyClientHTTPPrivateIPExceptionURLs, []string{"http://localhost/exception"})
1121+
whDeps := newWebHookDeps(t, logger, reg)
1122+
wh := hook.NewWebHook(whDeps, &request.Config{
11041123
URL: "https://www.google.com/",
11051124
Method: "GET",
11061125
TemplateURI: "http://192.168.178.0/test_body.jsonnet",
@@ -1116,15 +1135,7 @@ func TestAsyncWebhook(t *testing.T) {
11161135
logger := logrusx.New("kratos", "test")
11171136
logHook := new(test.Hook)
11181137
logger.Logger.Hooks.Add(logHook)
1119-
whDeps := struct {
1120-
x.BasicRegistry
1121-
*jsonnetsecure.TestProvider
1122-
config.Provider
1123-
}{
1124-
x.BasicRegistry{L: logger, C: reg.HTTPClient(context.Background()), T: otelx.NewNoop()},
1125-
jsonnetsecure.NewTestProvider(t),
1126-
reg,
1127-
}
1138+
whDeps := newWebHookDeps(t, logger, reg)
11281139

11291140
req := &http.Request{
11301141
Header: map[string][]string{"Some-Header": {"Some-Value"}},
@@ -1154,7 +1165,7 @@ func TestAsyncWebhook(t *testing.T) {
11541165
}))
11551166
t.Cleanup(webhookReceiver.Close)
11561167

1157-
wh := hook.NewWebHook(&whDeps, &request.Config{
1168+
wh := hook.NewWebHook(whDeps, &request.Config{
11581169
URL: webhookReceiver.URL,
11591170
Method: "GET",
11601171
TemplateURI: "file://stub/test_body.jsonnet",
@@ -1197,15 +1208,7 @@ func TestWebhookEvents(t *testing.T) {
11971208
t.Parallel()
11981209
_, reg := pkg.NewFastRegistryWithMocks(t)
11991210
logger := logrusx.New("kratos", "test")
1200-
whDeps := struct {
1201-
x.BasicRegistry
1202-
*jsonnetsecure.TestProvider
1203-
config.Provider
1204-
}{
1205-
x.BasicRegistry{L: logger, C: reg.HTTPClient(context.Background()), T: otelx.NewNoop()},
1206-
jsonnetsecure.NewTestProvider(t),
1207-
reg,
1208-
}
1211+
whDeps := newWebHookDeps(t, logger, reg)
12091212

12101213
req := &http.Request{
12111214
Header: map[string][]string{"Some-Header": {"Some-Value"}},
@@ -1243,7 +1246,7 @@ func TestWebhookEvents(t *testing.T) {
12431246

12441247
t.Run("success", func(t *testing.T) {
12451248
whID := x.NewUUID()
1246-
wh := hook.NewWebHook(&whDeps, &request.Config{
1249+
wh := hook.NewWebHook(whDeps, &request.Config{
12471250
ID: whID.String(),
12481251
URL: webhookReceiver.URL + "/ok",
12491252
Method: "GET",
@@ -1252,7 +1255,7 @@ func TestWebhookEvents(t *testing.T) {
12521255

12531256
recorder := tracetest.NewSpanRecorder()
12541257
tracer := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)).Tracer("test")
1255-
ctx, span := tracer.Start(context.Background(), "parent")
1258+
ctx, span := tracer.Start(t.Context(), "parent")
12561259
defer span.End()
12571260

12581261
r1 := req.Clone(ctx)
@@ -1290,7 +1293,7 @@ func TestWebhookEvents(t *testing.T) {
12901293

12911294
t.Run("failed", func(t *testing.T) {
12921295
whID := x.NewUUID()
1293-
wh := hook.NewWebHook(&whDeps, &request.Config{
1296+
wh := hook.NewWebHook(whDeps, &request.Config{
12941297
ID: whID.String(),
12951298
URL: webhookReceiver.URL + "/fail",
12961299
Method: "GET",
@@ -1299,7 +1302,7 @@ func TestWebhookEvents(t *testing.T) {
12991302

13001303
recorder := tracetest.NewSpanRecorder()
13011304
tracer := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)).Tracer("test")
1302-
ctx, span := tracer.Start(context.Background(), "parent")
1305+
ctx, span := tracer.Start(t.Context(), "parent")
13031306
defer span.End()
13041307

13051308
r1 := req.Clone(ctx)
@@ -1346,7 +1349,7 @@ func TestWebhookEvents(t *testing.T) {
13461349
})
13471350

13481351
t.Run("event disabled", func(t *testing.T) {
1349-
wh := hook.NewWebHook(&whDeps, &request.Config{
1352+
wh := hook.NewWebHook(whDeps, &request.Config{
13501353
URL: webhookReceiver.URL + "/fail",
13511354
Method: "GET",
13521355
TemplateURI: "file://stub/test_body.jsonnet",
@@ -1355,7 +1358,7 @@ func TestWebhookEvents(t *testing.T) {
13551358

13561359
recorder := tracetest.NewSpanRecorder()
13571360
tracer := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)).Tracer("test")
1358-
ctx, span := tracer.Start(context.Background(), "parent")
1361+
ctx, span := tracer.Start(t.Context(), "parent")
13591362
defer span.End()
13601363

13611364
r1 := req.Clone(ctx)

0 commit comments

Comments
 (0)