Skip to content

Commit 13375cc

Browse files
committed
fix: Migrate from gprc.DialContext to grpc.NewClient
This moves to the non-deprecated API. Fixes: #738 Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
1 parent a9ee905 commit 13375cc

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

cmd/synthetic-monitoring-agent/grpc.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"crypto/tls"
6-
"log"
76
"strings"
87

98
"github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
@@ -13,11 +12,13 @@ import (
1312
"google.golang.org/grpc/keepalive"
1413
)
1514

16-
func dialAPIServer(ctx context.Context, addr string, allowInsecure bool, apiToken string) (*grpc.ClientConn, error) {
17-
apiCreds := creds{Token: apiToken}
15+
func dialAPIServer(addr string, allowInsecure bool, apiToken string) (*grpc.ClientConn, error) {
16+
apiCreds := creds{
17+
Token: apiToken,
18+
AllowInsecure: allowInsecure,
19+
}
1820

1921
opts := []grpc.DialOption{
20-
grpc.WithBlock(), //nolint:staticcheck,nolintlint // Will be removed in v2. TODO: Migrate to NewClient.
2122
grpc.WithPerRPCCredentials(apiCreds),
2223
// Keep-alive is necessary to detect network failures in absence of writes from the client.
2324
// Without it, the agent would hang if the server disappears while waiting for a response.
@@ -40,7 +41,7 @@ func dialAPIServer(ctx context.Context, addr string, allowInsecure bool, apiToke
4041
}
4142
opts = append(opts, grpc.WithTransportCredentials(transportCreds))
4243

43-
return grpc.DialContext(ctx, addr, opts...) //nolint:staticcheck,nolintlint // Will be removed in v2. TODO: Migrate to NewClient.
44+
return grpc.NewClient(addr, opts...)
4445
}
4546

4647
func grpcApiHost(addr string) string {
@@ -53,7 +54,8 @@ func grpcApiHost(addr string) string {
5354
}
5455

5556
type creds struct {
56-
Token string
57+
Token string
58+
AllowInsecure bool
5759
}
5860

5961
func (c creds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
@@ -63,7 +65,8 @@ func (c creds) GetRequestMetadata(ctx context.Context, uri ...string) (map[strin
6365
}
6466

6567
func (c creds) RequireTransportSecurity() bool {
66-
log.Printf("RequireTransportSecurity")
67-
// XXX(mem): this is true
68-
return false
68+
// Only require transport security when insecure mode is NOT enabled.
69+
// This allows the agent to use unencrypted connections for development/testing
70+
// when the -api-insecure flag is set, while enforcing TLS by default.
71+
return !c.AllowInsecure
6972
}

cmd/synthetic-monitoring-agent/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func run(args []string, stdout io.Writer) error {
304304

305305
tenantCh := make(chan synthetic_monitoring.Tenant)
306306

307-
conn, err := dialAPIServer(ctx, config.GrpcApiServerAddr, config.GrpcInsecure, string(config.ApiToken))
307+
conn, err := dialAPIServer(config.GrpcApiServerAddr, config.GrpcInsecure, string(config.ApiToken))
308308
if err != nil {
309309
return fmt.Errorf("dialing GRPC server %s: %w", config.GrpcApiServerAddr, err)
310310
}

0 commit comments

Comments
 (0)