Skip to content

Commit 2510cf1

Browse files
committed
Add user friendly error
1 parent 1dac488 commit 2510cf1

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/stretchr/testify v1.11.1
1212
golang.stackrox.io/grpc-http1 v0.5.1
1313
google.golang.org/grpc v1.81.0
14+
google.golang.org/protobuf v1.36.11
1415
)
1516

1617
require (
@@ -40,7 +41,6 @@ require (
4041
golang.org/x/text v0.35.0 // indirect
4142
google.golang.org/genproto/googleapis/api v0.0.0-20260316180232-0b37fe3546d5 // indirect
4243
google.golang.org/genproto/googleapis/rpc v0.0.0-20260316180232-0b37fe3546d5 // indirect
43-
google.golang.org/protobuf v1.36.11 // indirect
4444
gopkg.in/yaml.v3 v3.0.1 // indirect
4545
)
4646

internal/client/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func (c *Client) Connect(ctx context.Context) error {
7070

7171
tlsConfig, err := c.tlsConfig()
7272
if err != nil {
73-
return err
73+
slog.Error("TLS configuration failed", "error", err)
74+
75+
return errors.New("invalid TLS configuration: verify CA certificate configuration, check server logs for details")
7476
}
7577

7678
var conn *grpc.ClientConn

internal/client/client_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/stretchr/testify/assert"
2525
"github.com/stretchr/testify/require"
2626
"google.golang.org/grpc"
27+
"google.golang.org/protobuf/types/known/emptypb"
2728
)
2829

2930
func TestClientReconnectsAfterServerRestart(t *testing.T) {
@@ -313,6 +314,23 @@ func TestClient_tlsConfig_NonexistentCACertPath(t *testing.T) {
313314
assert.Contains(t, err.Error(), "failed to access CA certificate")
314315
}
315316

317+
func TestClient_Connect_SanitizesTLSConfigError(t *testing.T) {
318+
client := &Client{
319+
config: &config.CentralConfig{
320+
URL: "central.stackrox.io:8443",
321+
AuthType: config.AuthTypeStatic,
322+
APIToken: "dummy",
323+
CACertPath: "/nonexistent/secret/path/ca.crt",
324+
},
325+
}
326+
327+
err := client.Connect(context.Background())
328+
require.Error(t, err)
329+
assert.NotContains(t, err.Error(), "/nonexistent/secret/path/ca.crt")
330+
assert.Contains(t, err.Error(), "invalid TLS configuration")
331+
assert.Contains(t, err.Error(), "check server logs for details")
332+
}
333+
316334
// generateTestCert creates a certificate PEM with the given options, signed by the given CA.
317335
// If ca/caKey are nil, the cert is self-signed.
318336
func generateTestCert(
@@ -472,7 +490,7 @@ func TestClient_ConnectWithCACert_Positive(t *testing.T) {
472490

473491
// Invoke a dummy RPC to trigger the TLS handshake. The method doesn't exist,
474492
// so the server returns Unimplemented — any non-TLS error proves the handshake succeeded.
475-
err = conn.Invoke(ctx, "/test.Service/Method", nil, nil)
493+
err = conn.Invoke(ctx, "/test.Service/Method", &emptypb.Empty{}, &emptypb.Empty{})
476494
require.Error(t, err)
477495

478496
// Verify the error is NOT a TLS certificate verification failure.
@@ -514,7 +532,7 @@ func TestClient_ConnectWithoutCACert_Negative(t *testing.T) {
514532

515533
// Invoke triggers the actual TLS handshake. Because no CA cert is provided and
516534
// InsecureSkipTLSVerify is false, the self-signed server cert cannot be verified.
517-
err = conn.Invoke(ctx, "/test.Service/Method", nil, nil)
535+
err = conn.Invoke(ctx, "/test.Service/Method", &emptypb.Empty{}, &emptypb.Empty{})
518536
require.Error(t, err)
519537

520538
// Verify the error IS a TLS certificate verification failure.

0 commit comments

Comments
 (0)