Skip to content
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

contrib/google.golang.org/grpc: isolate tests using an independent rig instance instead of a shared one #2278

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 55 additions & 36 deletions contrib/google.golang.org/grpc/appsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ func TestAppSec(t *testing.T) {
t.Skip("appsec disabled")
}

rig, err := newRig(false)
require.NoError(t, err)
defer rig.Close()
setup := func() (FixtureClient, mocktracer.Tracer, func()) {
rig, err := newRig(false)
require.NoError(t, err)

mt := mocktracer.Start()

client := rig.client
return rig.client, mt, func() {
rig.Close()
mt.Stop()
}
}

t.Run("unary", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log"))
Expand All @@ -58,8 +64,8 @@ func TestAppSec(t *testing.T) {
})

t.Run("stream", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log"))
Expand Down Expand Up @@ -110,15 +116,21 @@ func TestBlocking(t *testing.T) {
t.Skip("appsec disabled")
}

rig, err := newRig(false)
require.NoError(t, err)
defer rig.Close()
setup := func() (FixtureClient, mocktracer.Tracer, func()) {
rig, err := newRig(false)
require.NoError(t, err)

client := rig.client
mt := mocktracer.Start()

return rig.client, mt, func() {
rig.Close()
mt.Stop()
}
}

t.Run("unary-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.4"))
Expand All @@ -136,8 +148,8 @@ func TestBlocking(t *testing.T) {
})

t.Run("unary-no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, _, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.5"))
Expand All @@ -148,8 +160,8 @@ func TestBlocking(t *testing.T) {
})

t.Run("stream-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.4"))
stream, err := client.StreamPing(ctx)
Expand All @@ -168,8 +180,8 @@ func TestBlocking(t *testing.T) {
})

t.Run("stream-no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, _, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("dd-canary", "dd-test-scanner-log", "x-client-ip", "1.2.3.5"))
stream, err := client.StreamPing(ctx)
Expand Down Expand Up @@ -197,14 +209,21 @@ func TestUserBlocking(t *testing.T) {
t.Skip("appsec disabled")
}

rig, err := newAppsecRig(false)
require.NoError(t, err)
defer rig.Close()
client := rig.client
setup := func() (FixtureClient, mocktracer.Tracer, func()) {
rig, err := newAppsecRig(false)
require.NoError(t, err)

t.Run("unary-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

return rig.client, mt, func() {
rig.Close()
mt.Stop()
}
}

t.Run("unary-block", func(t *testing.T) {
client, mt, cleanup := setup()
defer cleanup()

// Send a XSS attack in the payload along with the canary value in the RPC metadata
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "blocked-user-1"))
Expand All @@ -223,8 +242,8 @@ func TestUserBlocking(t *testing.T) {
})

t.Run("unary-no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, _, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "legit user"))
reply, err := client.Ping(ctx, &FixtureRequest{Name: "<script>alert('xss');</script>"})
Expand All @@ -236,8 +255,8 @@ func TestUserBlocking(t *testing.T) {
// This test checks that IP blocking happens BEFORE user blocking, since user blocking needs the request handler
// to be invoked while IP blocking doesn't
t.Run("unary-mixed-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "blocked-user-1", "x-forwarded-for", "1.2.3.4"))
reply, err := client.Ping(ctx, &FixtureRequest{})
Expand All @@ -253,8 +272,8 @@ func TestUserBlocking(t *testing.T) {
})

t.Run("stream-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "blocked-user-1"))
stream, err := client.StreamPing(ctx)
Expand All @@ -273,8 +292,8 @@ func TestUserBlocking(t *testing.T) {
})

t.Run("stream-no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, _, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "legit user"))
stream, err := client.StreamPing(ctx)
Expand All @@ -293,8 +312,8 @@ func TestUserBlocking(t *testing.T) {
// This test checks that IP blocking happens BEFORE user blocking, since user blocking needs the request handler
// to be invoked while IP blocking doesn't
t.Run("stream-mixed-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
client, mt, cleanup := setup()
defer cleanup()

ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("user-id", "blocked-user-1", "x-forwarded-for", "1.2.3.4"))
stream, err := client.StreamPing(ctx)
Expand Down
10 changes: 5 additions & 5 deletions contrib/google.golang.org/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ import (
func TestUnary(t *testing.T) {
assert := assert.New(t)

rig, err := newRig(true, WithServiceName("grpc"), WithRequestTags())
require.NoError(t, err, "error setting up rig")
defer rig.Close()
client := rig.client

for name, tt := range map[string]struct {
message string
error bool
Expand All @@ -67,6 +62,11 @@ func TestUnary(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
rig, err := newRig(true, WithServiceName("grpc"), WithRequestTags())
require.NoError(t, err, "error setting up rig")
defer rig.Close()
client := rig.client

mt := mocktracer.Start()
defer mt.Stop()

Expand Down
Loading