Skip to content

Commit a10818a

Browse files
authored
fix(trillianclient): strip dns:/// scheme from TLS ServerName in gRPC dial (#2812)
* fix(trillianclient): strip dns:/// scheme from TLS ServerName in gRPC dial Signed-off-by: kdacosta0 <[email protected]> * test(trillianclient): add regression tests for cleanDialHostname Signed-off-by: kdacosta0 <[email protected]> * test(e2e): add dns:/// scheme test case to TestDialE2E Signed-off-by: kdacosta0 <[email protected]> --------- Signed-off-by: kdacosta0 <[email protected]>
1 parent c31f3fc commit a10818a

3 files changed

Lines changed: 94 additions & 5 deletions

File tree

‎pkg/trillianclient/manager.go‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"os"
2525
"path/filepath"
26+
"strings"
2627
"sync"
2728
"time"
2829

@@ -157,13 +158,25 @@ func CreateAndInitTree(ctx context.Context, config GRPCConfig) (*trillian.Tree,
157158
return t, nil
158159
}
159160

161+
// cleanDialHostname strips gRPC resolver scheme prefixes (e.g. "dns:///")
162+
// from a hostname, returning the bare hostname suitable for TLS SNI and
163+
// certificate verification. The original address with its scheme must be
164+
// preserved for the grpc.NewClient target so the chosen resolver stays active.
165+
func cleanDialHostname(hostname string) string {
166+
return strings.TrimPrefix(hostname, "dns:///")
167+
}
168+
160169
func dial(hostname string, port uint16, tlsCACertFile string, useSystemTrustStore bool, serviceConfig string) (*grpc.ClientConn, error) {
161-
// Set up and test connection to rpc server
170+
// Strip gRPC resolver scheme before TLS: if hostname is e.g.
171+
// "dns:///host.svc", passing it raw into tls.Config.ServerName causes
172+
// x509 verification to fail with `certificate valid for host.svc, not dns`.
173+
cleanHostname := cleanDialHostname(hostname)
174+
162175
var creds credentials.TransportCredentials
163176
switch {
164177
case useSystemTrustStore:
165178
creds = credentials.NewTLS(&tls.Config{
166-
ServerName: hostname,
179+
ServerName: cleanHostname,
167180
MinVersion: tls.VersionTLS12,
168181
})
169182
case tlsCACertFile != "":
@@ -176,18 +189,25 @@ func dial(hostname string, port uint16, tlsCACertFile string, useSystemTrustStor
176189
return nil, fmt.Errorf("failed to append CA certificate to pool")
177190
}
178191
creds = credentials.NewTLS(&tls.Config{
179-
ServerName: hostname,
192+
ServerName: cleanHostname,
180193
RootCAs: certPool,
181194
MinVersion: tls.VersionTLS12,
182195
})
183196
default:
184197
creds = insecure.NewCredentials()
185198
}
186199

187-
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
200+
opts := []grpc.DialOption{
201+
grpc.WithTransportCredentials(creds),
202+
grpc.WithAuthority(cleanHostname),
203+
}
204+
188205
if serviceConfig != "" {
189206
opts = append(opts, grpc.WithDefaultServiceConfig(serviceConfig))
190207
}
208+
// hostname (not cleanHostname) is intentional: the dns:/// scheme must
209+
// reach grpc.NewClient so gRPC uses the DNS resolver for client-side
210+
// load balancing. TLS and authority are handled separately above.
191211
conn, err := grpc.NewClient(fmt.Sprintf("%s:%d", hostname, port), opts...)
192212
if err != nil {
193213
return nil, fmt.Errorf("failed to connect to RPC server: %w", err)

‎pkg/trillianclient/manager_test.go‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Copyright 2026 The Sigstore Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package trillianclient
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestCleanDialHostname(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
input string
28+
want string
29+
}{
30+
{
31+
name: "dns:/// scheme is stripped",
32+
input: "dns:///trillian-logserver.ns.svc",
33+
want: "trillian-logserver.ns.svc",
34+
},
35+
{
36+
name: "plain hostname is unchanged",
37+
input: "trillian-logserver.ns.svc",
38+
want: "trillian-logserver.ns.svc",
39+
},
40+
{
41+
name: "localhost is unchanged",
42+
input: "localhost",
43+
want: "localhost",
44+
},
45+
{
46+
name: "dns:/// with port is stripped correctly",
47+
input: "dns:///trillian-logserver.ns.svc:8091",
48+
want: "trillian-logserver.ns.svc:8091",
49+
},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
got := cleanDialHostname(tt.input)
55+
assert.Equal(t, tt.want, got)
56+
})
57+
}
58+
}

‎tests/e2e_test.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,28 +368,39 @@ func TestDialE2E(t *testing.T) {
368368

369369
tests := []struct {
370370
name string
371+
hostname string
371372
tlsCACert string
372373
useSystemTLS bool
373374
expectedError bool
374375
}{
375376
{
376377
name: "Connection with custom CA cert",
378+
hostname: "localhost",
377379
tlsCACert: serverCertPath,
378380
useSystemTLS: false,
379381
expectedError: false,
380382
},
381383
{
382384
name: "Connection with system TLS",
385+
hostname: "localhost",
383386
tlsCACert: "",
384387
useSystemTLS: true,
385388
expectedError: true,
386389
},
387390
{
388391
name: "Insecure connection attempt",
392+
hostname: "localhost",
389393
tlsCACert: "",
390394
useSystemTLS: false,
391395
expectedError: true,
392396
},
397+
{
398+
name: "Connection with dns:/// scheme and custom CA cert",
399+
hostname: "dns:///localhost",
400+
tlsCACert: serverCertPath,
401+
useSystemTLS: false,
402+
expectedError: false,
403+
},
393404
}
394405

395406
for _, tt := range tests {
@@ -398,7 +409,7 @@ func TestDialE2E(t *testing.T) {
398409
viper.Set("trillian_log_server.tls_ca_cert", tt.tlsCACert)
399410
viper.Set("trillian_log_server.tls", tt.useSystemTLS)
400411

401-
conn, _ := trillianclient.TestDial("localhost", uint16(listener.Addr().(*net.TCPAddr).Port), tt.tlsCACert, tt.useSystemTLS)
412+
conn, _ := trillianclient.TestDial(tt.hostname, uint16(listener.Addr().(*net.TCPAddr).Port), tt.tlsCACert, tt.useSystemTLS)
402413
require.NotNil(t, conn)
403414

404415
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)

0 commit comments

Comments
 (0)