diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2020-09-30 14:17:23 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2020-09-30 14:36:19 +0300 |
commit | 2ec2156a9d8e345d42d1bf093144a68735cc08ac (patch) | |
tree | 7d38a2125a2a7011ebe9a923f3507f4c5f1b534d /internal/helper | |
parent | be4a37406e949983d483a4aabb7af0c0768e60af (diff) |
Fix injection of gitaly servers info
InjectGitalyServers used `NewOutgoingContext` that completely
overrides all outgoing metadata values, so no values except
'gitaly-servers' were passed with outgoing requests.
Fixed by replacing `NewOutgoingContext` with `AppendToOutgoingContext`
that appends passed values to existing metadata or crete it if
not present.
Closes: https://gitlab.com/gitlab-org/gitaly/-/issues/3158
Diffstat (limited to 'internal/helper')
-rw-r--r-- | internal/helper/storage.go | 2 | ||||
-rw-r--r-- | internal/helper/storage_test.go | 41 |
2 files changed, 42 insertions, 1 deletions
diff --git a/internal/helper/storage.go b/internal/helper/storage.go index 523ca7ed6..0a465756f 100644 --- a/internal/helper/storage.go +++ b/internal/helper/storage.go @@ -74,5 +74,5 @@ func InjectGitalyServers(ctx context.Context, name, address, token string) (cont return nil, err } - return metadata.NewOutgoingContext(ctx, metadata.Pairs("gitaly-servers", base64.StdEncoding.EncodeToString(gitalyServersJSON))), nil + return metadata.AppendToOutgoingContext(ctx, "gitaly-servers", base64.StdEncoding.EncodeToString(gitalyServersJSON)), nil } diff --git a/internal/helper/storage_test.go b/internal/helper/storage_test.go index d0f229e0e..1e50e04cd 100644 --- a/internal/helper/storage_test.go +++ b/internal/helper/storage_test.go @@ -1,7 +1,10 @@ package helper import ( + "context" "encoding/base64" + "encoding/json" + "strings" "testing" "github.com/stretchr/testify/require" @@ -56,3 +59,41 @@ func TestExtractGitalyServers(t *testing.T) { }) } } + +func TestInjectGitalyServers(t *testing.T) { + check := func(t *testing.T, ctx context.Context) { + t.Helper() + + newCtx, err := InjectGitalyServers(ctx, "gitaly-1", "1.1.1.1", "secret") + require.NoError(t, err) + + md, found := metadata.FromOutgoingContext(newCtx) + require.True(t, found) + + gs, found := md["gitaly-servers"] + require.True(t, found) + + require.Len(t, gs, 1) + + var servers map[string]interface{} + require.NoError(t, json.NewDecoder(base64.NewDecoder(base64.StdEncoding, strings.NewReader(gs[0]))).Decode(&servers), "received %s", gs[0]) + require.EqualValues(t, map[string]interface{}{"gitaly-1": map[string]interface{}{"address": "1.1.1.1", "token": "secret"}}, servers) + } + + t.Run("brand new context", func(t *testing.T) { + ctx := context.Background() + + check(t, ctx) + }) + + t.Run("context with existing outgoing metadata should not be re-written", func(t *testing.T) { + existing := metadata.New(map[string]string{"foo": "bar"}) + + ctx := metadata.NewOutgoingContext(context.Background(), existing) + check(t, ctx) + + md, found := metadata.FromOutgoingContext(ctx) + require.True(t, found) + require.Equal(t, []string{"bar"}, md["foo"]) + }) +} |