Skip to content

Commit 11227dd

Browse files
author
Quang-Minh Nguyen
committed
Add DNS discovery support for Gitaly/Praefect
All the implementations of DNS discovery were done in this epic: https://gitlab.com/groups/gitlab-org/-/epics/8971. Gitaly allows clients to configure DNS discovery via dial option. This MR adds the exposed dial options to client connection creation in Gitlab-shell. Issue: https://gitlab.com/gitlab-org/gitaly/-/issues/4722 Changelog: added
1 parent a2ad411 commit 11227dd

File tree

5 files changed

+212
-163
lines changed

5 files changed

+212
-163
lines changed

client/testserver/gitalyserver.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,51 @@ func (s *TestGitalyServer) SSHUploadArchive(stream pb.SSHService_SSHUploadArchiv
8585
return nil
8686
}
8787

88-
func StartGitalyServer(t *testing.T) (string, *TestGitalyServer) {
88+
func StartGitalyServer(t *testing.T, network string) (string, *TestGitalyServer) {
8989
t.Helper()
9090

91-
tempDir, _ := os.MkdirTemp("", "gitlab-shell-test-api")
92-
gitalySocketPath := path.Join(tempDir, "gitaly.sock")
93-
t.Cleanup(func() { os.RemoveAll(tempDir) })
91+
switch network {
92+
case "unix":
93+
tempDir, _ := os.MkdirTemp("", "gitlab-shell-test-api")
94+
gitalySocketPath := path.Join(tempDir, "gitaly.sock")
95+
t.Cleanup(func() { require.NoError(t, os.RemoveAll(tempDir)) })
9496

95-
err := os.MkdirAll(filepath.Dir(gitalySocketPath), 0700)
96-
require.NoError(t, err)
97+
err := os.MkdirAll(filepath.Dir(gitalySocketPath), 0700)
98+
require.NoError(t, err)
99+
100+
addr, testServer := doStartTestServer(t, "unix", gitalySocketPath)
101+
return fmt.Sprintf("unix:%s", addr), testServer
102+
103+
case "tcp":
104+
addr, testServer := doStartTestServer(t, "tcp", "127.0.0.1:0")
105+
return fmt.Sprintf("tcp://%s", addr), testServer
106+
107+
case "dns":
108+
addr, testServer := doStartTestServer(t, "tcp", "127.0.0.1:0")
109+
// gRPC URL with DNS scheme follows this format: https://grpc.github.io/grpc/core/md_doc_naming.html
110+
// When the authority is dropped, the URL have 3 splashes.
111+
return fmt.Sprintf("dns:///%s", addr), testServer
97112

113+
default:
114+
panic(fmt.Sprintf("Unsupported network %s", network))
115+
}
116+
}
117+
118+
func doStartTestServer(t *testing.T, network string, path string) (string, *TestGitalyServer) {
98119
server := grpc.NewServer(
99120
client.SidechannelServer(log.ContextLogger(context.Background()), insecure.NewCredentials()),
100121
)
101122

102-
listener, err := net.Listen("unix", gitalySocketPath)
123+
listener, err := net.Listen(network, path)
103124
require.NoError(t, err)
104125

105126
testServer := TestGitalyServer{}
106127
pb.RegisterSSHServiceServer(server, &testServer)
107128

108-
go server.Serve(listener)
129+
go func() {
130+
require.NoError(t, server.Serve(listener))
131+
}()
109132
t.Cleanup(func() { server.Stop() })
110133

111-
gitalySocketUrl := "unix:" + gitalySocketPath
112-
113-
return gitalySocketUrl, &testServer
134+
return listener.Addr().String(), &testServer
114135
}

internal/command/receivepack/gitalycall_test.go

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package receivepack
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"testing"
78

89
"github.com/stretchr/testify/require"
@@ -17,80 +18,85 @@ import (
1718
)
1819

1920
func TestReceivePack(t *testing.T) {
20-
gitalyAddress, testServer := testserver.StartGitalyServer(t)
21+
for _, network := range []string{"unix", "tcp", "dns"} {
22+
t.Run(fmt.Sprintf("via %s network", network), func(t *testing.T) {
23+
gitalyAddress, testServer := testserver.StartGitalyServer(t, network)
24+
t.Log(fmt.Sprintf("Server address: %s", gitalyAddress))
2125

22-
requests := requesthandlers.BuildAllowedWithGitalyHandlers(t, gitalyAddress)
23-
url := testserver.StartHttpServer(t, requests)
26+
requests := requesthandlers.BuildAllowedWithGitalyHandlers(t, gitalyAddress)
27+
url := testserver.StartHttpServer(t, requests)
2428

25-
testCases := []struct {
26-
username string
27-
keyId string
28-
}{
29-
{
30-
username: "john.doe",
31-
},
32-
{
33-
keyId: "123",
34-
},
35-
}
29+
testCases := []struct {
30+
username string
31+
keyId string
32+
}{
33+
{
34+
username: "john.doe",
35+
},
36+
{
37+
keyId: "123",
38+
},
39+
}
3640

37-
for _, tc := range testCases {
38-
output := &bytes.Buffer{}
39-
input := &bytes.Buffer{}
40-
repo := "group/repo"
41+
for _, tc := range testCases {
42+
output := &bytes.Buffer{}
43+
input := &bytes.Buffer{}
44+
repo := "group/repo"
4145

42-
env := sshenv.Env{
43-
IsSSHConnection: true,
44-
OriginalCommand: "git-receive-pack " + repo,
45-
RemoteAddr: "127.0.0.1",
46-
}
46+
env := sshenv.Env{
47+
IsSSHConnection: true,
48+
OriginalCommand: "git-receive-pack " + repo,
49+
RemoteAddr: "127.0.0.1",
50+
}
4751

48-
args := &commandargs.Shell{
49-
CommandType: commandargs.ReceivePack,
50-
SshArgs: []string{"git-receive-pack", repo},
51-
Env: env,
52-
}
52+
args := &commandargs.Shell{
53+
CommandType: commandargs.ReceivePack,
54+
SshArgs: []string{"git-receive-pack", repo},
55+
Env: env,
56+
}
5357

54-
if tc.username != "" {
55-
args.GitlabUsername = tc.username
56-
} else {
57-
args.GitlabKeyId = tc.keyId
58-
}
58+
if tc.username != "" {
59+
args.GitlabUsername = tc.username
60+
} else {
61+
args.GitlabKeyId = tc.keyId
62+
}
5963

60-
cfg := &config.Config{GitlabUrl: url}
61-
cfg.GitalyClient.InitSidechannelRegistry(context.Background())
62-
cmd := &Command{
63-
Config: cfg,
64-
Args: args,
65-
ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output, In: input},
66-
}
64+
cfg := &config.Config{GitlabUrl: url}
65+
cfg.GitalyClient.InitSidechannelRegistry(context.Background())
66+
cmd := &Command{
67+
Config: cfg,
68+
Args: args,
69+
ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output, In: input},
70+
}
6771

68-
ctx := correlation.ContextWithCorrelation(context.Background(), "a-correlation-id")
69-
ctx = correlation.ContextWithClientName(ctx, "gitlab-shell-tests")
72+
ctx := correlation.ContextWithCorrelation(context.Background(), "a-correlation-id")
73+
ctx = correlation.ContextWithClientName(ctx, "gitlab-shell-tests")
7074

71-
err := cmd.Execute(ctx)
72-
require.NoError(t, err)
75+
err := cmd.Execute(ctx)
76+
require.NoError(t, err)
7377

74-
if tc.username != "" {
75-
require.Equal(t, "ReceivePack: 1 "+repo, output.String())
76-
} else {
77-
require.Equal(t, "ReceivePack: key-123 "+repo, output.String())
78-
}
78+
if tc.username != "" {
79+
require.Equal(t, "ReceivePack: 1 "+repo, output.String())
80+
} else {
81+
require.Equal(t, "ReceivePack: key-123 "+repo, output.String())
82+
}
7983

80-
for k, v := range map[string]string{
81-
"gitaly-feature-cache_invalidator": "true",
82-
"gitaly-feature-inforef_uploadpack_cache": "false",
83-
"x-gitlab-client-name": "gitlab-shell-tests-git-receive-pack",
84-
"key_id": "123",
85-
"user_id": "1",
86-
"remote_ip": "127.0.0.1",
87-
"key_type": "key",
88-
} {
89-
actual := testServer.ReceivedMD[k]
90-
require.Len(t, actual, 1)
91-
require.Equal(t, v, actual[0])
92-
}
93-
require.Empty(t, testServer.ReceivedMD["some-other-ff"])
94-
require.Equal(t, testServer.ReceivedMD["x-gitlab-correlation-id"][0], "a-correlation-id")
84+
for k, v := range map[string]string{
85+
"gitaly-feature-cache_invalidator": "true",
86+
"gitaly-feature-inforef_uploadpack_cache": "false",
87+
"x-gitlab-client-name": "gitlab-shell-tests-git-receive-pack",
88+
"key_id": "123",
89+
"user_id": "1",
90+
"remote_ip": "127.0.0.1",
91+
"key_type": "key",
92+
} {
93+
actual := testServer.ReceivedMD[k]
94+
require.Len(t, actual, 1)
95+
require.Equal(t, v, actual[0])
96+
}
97+
require.Empty(t, testServer.ReceivedMD["some-other-ff"])
98+
require.Equal(t, testServer.ReceivedMD["x-gitlab-correlation-id"][0], "a-correlation-id")
99+
}
100+
})
95101
}
96102
}

internal/command/uploadarchive/gitalycall_test.go

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package uploadarchive
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"testing"
78

89
"github.com/stretchr/testify/require"
@@ -17,59 +18,64 @@ import (
1718
)
1819

1920
func TestUploadArchive(t *testing.T) {
20-
gitalyAddress, testServer := testserver.StartGitalyServer(t)
21+
for _, network := range []string{"unix", "tcp", "dns"} {
22+
t.Run(fmt.Sprintf("via %s network", network), func(t *testing.T) {
23+
gitalyAddress, testServer := testserver.StartGitalyServer(t, network)
24+
t.Log(fmt.Sprintf("Server address: %s", gitalyAddress))
2125

22-
requests := requesthandlers.BuildAllowedWithGitalyHandlers(t, gitalyAddress)
23-
url := testserver.StartHttpServer(t, requests)
26+
requests := requesthandlers.BuildAllowedWithGitalyHandlers(t, gitalyAddress)
27+
url := testserver.StartHttpServer(t, requests)
2428

25-
output := &bytes.Buffer{}
26-
input := &bytes.Buffer{}
29+
output := &bytes.Buffer{}
30+
input := &bytes.Buffer{}
2731

28-
userId := "1"
29-
repo := "group/repo"
32+
userId := "1"
33+
repo := "group/repo"
3034

31-
env := sshenv.Env{
32-
IsSSHConnection: true,
33-
OriginalCommand: "git-upload-archive " + repo,
34-
RemoteAddr: "127.0.0.1",
35-
}
35+
env := sshenv.Env{
36+
IsSSHConnection: true,
37+
OriginalCommand: "git-upload-archive " + repo,
38+
RemoteAddr: "127.0.0.1",
39+
}
3640

37-
args := &commandargs.Shell{
38-
GitlabKeyId: userId,
39-
CommandType: commandargs.UploadArchive,
40-
SshArgs: []string{"git-upload-archive", repo},
41-
Env: env,
42-
}
41+
args := &commandargs.Shell{
42+
GitlabKeyId: userId,
43+
CommandType: commandargs.UploadArchive,
44+
SshArgs: []string{"git-upload-archive", repo},
45+
Env: env,
46+
}
4347

44-
cfg := &config.Config{GitlabUrl: url}
45-
cfg.GitalyClient.InitSidechannelRegistry(context.Background())
46-
cmd := &Command{
47-
Config: cfg,
48-
Args: args,
49-
ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output, In: input},
50-
}
48+
cfg := &config.Config{GitlabUrl: url}
49+
cfg.GitalyClient.InitSidechannelRegistry(context.Background())
50+
cmd := &Command{
51+
Config: cfg,
52+
Args: args,
53+
ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output, In: input},
54+
}
5155

52-
ctx := correlation.ContextWithCorrelation(context.Background(), "a-correlation-id")
53-
ctx = correlation.ContextWithClientName(ctx, "gitlab-shell-tests")
56+
ctx := correlation.ContextWithCorrelation(context.Background(), "a-correlation-id")
57+
ctx = correlation.ContextWithClientName(ctx, "gitlab-shell-tests")
5458

55-
err := cmd.Execute(ctx)
56-
require.NoError(t, err)
59+
err := cmd.Execute(ctx)
60+
require.NoError(t, err)
5761

58-
require.Equal(t, "UploadArchive: "+repo, output.String())
62+
require.Equal(t, "UploadArchive: "+repo, output.String())
5963

60-
for k, v := range map[string]string{
61-
"gitaly-feature-cache_invalidator": "true",
62-
"gitaly-feature-inforef_uploadpack_cache": "false",
63-
"x-gitlab-client-name": "gitlab-shell-tests-git-upload-archive",
64-
"key_id": "123",
65-
"user_id": "1",
66-
"remote_ip": "127.0.0.1",
67-
"key_type": "key",
68-
} {
69-
actual := testServer.ReceivedMD[k]
70-
require.Len(t, actual, 1)
71-
require.Equal(t, v, actual[0])
64+
for k, v := range map[string]string{
65+
"gitaly-feature-cache_invalidator": "true",
66+
"gitaly-feature-inforef_uploadpack_cache": "false",
67+
"x-gitlab-client-name": "gitlab-shell-tests-git-upload-archive",
68+
"key_id": "123",
69+
"user_id": "1",
70+
"remote_ip": "127.0.0.1",
71+
"key_type": "key",
72+
} {
73+
actual := testServer.ReceivedMD[k]
74+
require.Len(t, actual, 1)
75+
require.Equal(t, v, actual[0])
76+
}
77+
require.Empty(t, testServer.ReceivedMD["some-other-ff"])
78+
require.Equal(t, testServer.ReceivedMD["x-gitlab-correlation-id"][0], "a-correlation-id")
79+
})
7280
}
73-
require.Empty(t, testServer.ReceivedMD["some-other-ff"])
74-
require.Equal(t, testServer.ReceivedMD["x-gitlab-correlation-id"][0], "a-correlation-id")
7581
}

0 commit comments

Comments
 (0)