Skip to content

Commit d682427

Browse files
committed
Replace direct use of winio with elastic-agent-libs/api/npipe
1 parent cb44899 commit d682427

12 files changed

Lines changed: 83 additions & 66 deletions

go.mod

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,28 @@ module github.com/elastic/elastic-agent-client/v7
33
go 1.20
44

55
require (
6-
github.com/Microsoft/go-winio v0.6.0
7-
github.com/gofrs/uuid v4.2.0+incompatible
6+
github.com/elastic/elastic-agent-libs v0.7.2
7+
github.com/gofrs/uuid v4.4.0+incompatible
88
github.com/golang/protobuf v1.5.3
99
github.com/google/go-cmp v0.5.9
1010
github.com/google/pprof v0.0.0-20230426061923-93006964c1fc
1111
github.com/hashicorp/go-multierror v1.1.1
1212
github.com/magefile/mage v1.13.0
13-
github.com/stretchr/testify v1.7.0
13+
github.com/stretchr/testify v1.8.2
1414
golang.org/x/exp v0.0.0-20231127185646-65229373498e
1515
google.golang.org/grpc v1.56.3
1616
google.golang.org/protobuf v1.30.0
1717
)
1818

1919
require (
20+
github.com/Microsoft/go-winio v0.5.2 // indirect
2021
github.com/davecgh/go-spew v1.1.1 // indirect
2122
github.com/hashicorp/errwrap v1.1.0 // indirect
2223
github.com/kr/text v0.2.0 // indirect
2324
github.com/pmezard/go-difflib v1.0.0 // indirect
24-
golang.org/x/mod v0.14.0 // indirect
2525
golang.org/x/net v0.19.0 // indirect
2626
golang.org/x/sys v0.15.0 // indirect
2727
golang.org/x/text v0.14.0 // indirect
28-
golang.org/x/tools v0.16.0 // indirect
2928
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
3029
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3130
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
2-
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
1+
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
2+
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
33
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
8-
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
7+
github.com/elastic/elastic-agent-libs v0.7.2 h1:yT0hF0UAxJCdQqhHh6SFpgYrcpB10oFzPj8IaytPS2o=
8+
github.com/elastic/elastic-agent-libs v0.7.2/go.mod h1:pVBEElQJUO9mr4WStWNXuQGsJn54lcjAoYAHmsvBLBc=
9+
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
10+
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
911
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1012
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
1113
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
@@ -29,22 +31,25 @@ github.com/magefile/mage v1.13.0 h1:XtLJl8bcCM7EFoO8FyH8XK3t7G5hQAeK+i4tq+veT9M=
2931
github.com/magefile/mage v1.13.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
3032
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3133
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34+
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
3235
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
34-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
36+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
37+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
38+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
39+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
40+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
41+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
42+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
3543
golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No=
3644
golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
37-
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
38-
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3945
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
4046
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
41-
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
47+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
48+
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4249
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
4350
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4451
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
4552
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
46-
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
47-
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
4853
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4954
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A=
5055
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=

pkg/client/client_v2.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/elastic/elastic-agent-client/v7/pkg/client/chunk"
21+
"github.com/elastic/elastic-agent-libs/api/npipe"
2122

2223
"google.golang.org/grpc"
2324
"google.golang.org/protobuf/types/known/timestamppb"
@@ -68,8 +69,6 @@ const (
6869
TriggeredAPMChange // apm_config_change_triggered
6970
)
7071

71-
const schemePipePrefix = "pipe://"
72-
7372
func (t Trigger) String() string {
7473
var triggers []string
7574
if t == 0 {
@@ -276,19 +275,18 @@ type clientV2 struct {
276275
// NewV2 creates a client connection to Elastic Agent over the V2 control protocol.
277276
// The "target" can be prefixed with scheme as explained here https://github.com/grpc/grpc/blob/master/doc/naming.md.
278277
// unix://absolute_path for unix domain socket
279-
// pipe://pipe_name for windows named pipe
278+
// npipe:///pipe_name for windows named pipe
280279
func NewV2(target string, token string, versionInfo VersionInfo, opts ...V2ClientOption) V2 {
281280
var options v2options
282281
options.maxMessageSize = DefaultMaxMessageSize
283282
for _, o := range opts {
284283
o(&options)
285284
}
286285

287-
// For compatibility with existing interface the target could contain pipe:// scheme prefix
286+
// For compatibility with existing interface the target could contain npipe:// scheme prefix
288287
// Set the named pipe dialer option if pipe:// prefix specified on windows
289-
if runtime.GOOS == "windows" && strings.HasPrefix(target, schemePipePrefix) {
290-
// Trim pipe:// prefix
291-
target = strings.TrimPrefix(target, schemePipePrefix)
288+
if runtime.GOOS == "windows" && npipe.IsNPipe(target) {
289+
target = transformNPipeUrl(target)
292290
// Set the winio named pipe dialer
293291
options.dialOptions = append(options.dialOptions, getOptions()...)
294292
}

pkg/client/client_v2_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131

3232
"github.com/elastic/elastic-agent-client/v7/pkg/client/mock"
3333
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
34-
"github.com/elastic/elastic-agent-client/v7/pkg/transport"
3534
)
3635

3736
func TestClientV2_DialError(t *testing.T) {
@@ -49,7 +48,7 @@ func TestClientV2_DialError(t *testing.T) {
4948
func TestRPCErrorRetryTimer(t *testing.T) {
5049
// Create a TCP listener that rejects all incoming connections, to
5150
// induce an RPC error when the client starts.
52-
listener, err := transport.Listen("tcp", "127.0.0.1:0")
51+
listener, err := net.Listen("tcp", "127.0.0.1:0")
5352
require.NoError(t, err)
5453
defer listener.Close()
5554
go rejectingListener(listener)

pkg/client/dial_windows.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import (
1111
"context"
1212
"net"
1313

14-
"github.com/Microsoft/go-winio"
14+
"github.com/elastic/elastic-agent-libs/api/npipe"
1515
"google.golang.org/grpc"
1616
)
1717

1818
func getOptions() []grpc.DialOption {
1919
return []grpc.DialOption{
2020
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
21-
return winio.DialPipeContext(ctx, s)
21+
// Awkward call of what's exposed from elastic-agent-libs/api/npipe
22+
// in order to replace the direct use of winio.DialPipeContext
23+
// elastic-agent-libs/api/npipe does the same under the hood anyways
24+
return npipe.DialContext(s)(ctx, "", "")
2225
}),
2326
}
2427
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
//go:build !windows
66
// +build !windows
77

8-
package transport
8+
package mock
99

10-
import "net"
10+
import (
11+
"net"
12+
)
1113

12-
// Listen returns net.Listener from net.Listen
13-
func Listen(network, address string) (net.Listener, error) {
14-
return net.Listen(network, address)
14+
func newNPipeListener(name, sd string) (net.Listener, error) {
15+
panic("unsupported")
1516
}

pkg/client/mock/npipe_windows.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
//go:build windows
6+
// +build windows
7+
8+
package mock
9+
10+
import "net"
11+
12+
func newNPipeListener(name, sd string) (net.Listener, error) {
13+
return npipe.NewListener(name, sd)
14+
}

pkg/client/mock/stub_serverV1.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sync"
1212

1313
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
14-
"github.com/elastic/elastic-agent-client/v7/pkg/transport"
1514
"github.com/gofrs/uuid"
1615
"google.golang.org/grpc"
1716
)
@@ -55,7 +54,7 @@ type StubServer struct {
5554

5655
// Start the sub V2 server
5756
func (s *StubServer) Start(opt ...grpc.ServerOption) error {
58-
lis, err := transport.Listen("tcp", ":0")
57+
lis, err := net.Listen("tcp", ":0")
5958
if err != nil {
6059
return err
6160
}

pkg/client/mock/stub_serverV2.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/elastic/elastic-agent-client/v7/pkg/client/chunk"
2323
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
24-
"github.com/elastic/elastic-agent-client/v7/pkg/transport"
2524
)
2625

2726
// StubServerCheckinV2 is the checkin function for the V2 controller
@@ -78,7 +77,7 @@ type StubServerV2 struct {
7877
func (s *StubServerV2) listen(opt ...grpc.ServerOption) (lis net.Listener, cleanup func() error, err error) {
7978
cleanup = func() error { return nil }
8079
if s.LocalRPC == "" {
81-
lis, err := transport.Listen("tcp", fmt.Sprintf(":%d", s.Port))
80+
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Port))
8281
if err != nil {
8382
return nil, nil, err
8483
}
@@ -88,9 +87,8 @@ func (s *StubServerV2) listen(opt ...grpc.ServerOption) (lis net.Listener, clean
8887
}
8988

9089
if runtime.GOOS == "windows" {
91-
rpcPath := fmt.Sprintf("\\\\.\\pipe\\%s", s.LocalRPC)
92-
s.target = fmt.Sprintf("pipe://%s", rpcPath)
93-
lis, err = transport.Listen("pipe", rpcPath)
90+
s.target = fmt.Sprintf("\\\\.\\pipe\\%s", s.LocalRPC)
91+
lis, err = newNPipeListener(s.target, "")
9492
} else {
9593
socketDir := filepath.Join(os.TempDir(), randomString(3))
9694
err = os.MkdirAll(socketDir, 0750)
@@ -110,7 +108,7 @@ func (s *StubServerV2) listen(opt ...grpc.ServerOption) (lis net.Listener, clean
110108
}()
111109
rpcPath := fmt.Sprintf("%s/%s.sock", socketDir, s.LocalRPC)
112110
s.target = fmt.Sprintf("unix://%s", rpcPath)
113-
lis, err = transport.Listen("unix", rpcPath)
111+
lis, err = net.Listen("unix", rpcPath)
114112
}
115113

116114
return lis, cleanup, err

pkg/client/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
//go:build !windows
6+
// +build !windows
7+
8+
package client
9+
10+
// transformNPipeUrl is noop on non-windows platforms.
11+
// The npipe.TransformString is windows only in github.com/elastic/elastic-agent-libs/api/npipe
12+
func transformNPipeUrl(s string) string {
13+
return s
14+
}

0 commit comments

Comments
 (0)