Skip to content

Commit 61bb5d5

Browse files
Construct a deduped slice and directly modify ClientNames
1 parent 831440c commit 61bb5d5

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

cmd/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ type GRPCServerConfig struct {
465465
// These service names must match the service names advertised by gRPC itself,
466466
// which are identical to the names set in our gRPC .proto files prefixed by
467467
// the package names set in those files (e.g. "ca.CertificateAuthority").
468-
Services map[string]GRPCServiceConfig `json:"services" validate:"required,dive,required"`
468+
Services map[string]*GRPCServiceConfig `json:"services" validate:"required,dive,required"`
469469
// MaxConnectionAge specifies how long a connection may live before the server sends a GoAway to the
470470
// client. Because gRPC connections re-resolve DNS after a connection close,
471471
// this controls how long it takes before a client learns about changes to its
@@ -476,10 +476,10 @@ type GRPCServerConfig struct {
476476

477477
// GRPCServiceConfig contains the information needed to configure a gRPC service.
478478
type GRPCServiceConfig struct {
479-
// PerServiceClientNames is a map of gRPC service names to client certificate
480-
// SANs. The upstream listening server will reject connections from clients
481-
// which do not appear in this list, and the server interceptor will reject
482-
// RPC calls for this service from clients which are not listed here.
479+
// ClientNames is the list of accepted gRPC client certificate SANs.
480+
// Connections from clients not in this list will be rejected by the
481+
// upstream listener, and RPCs from unlisted clients will be denied by the
482+
// server interceptor.
483483
ClientNames []string `json:"clientNames" validate:"min=1,dive,hostname,required"`
484484
}
485485

grpc/server.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"net"
9+
"slices"
910
"strings"
1011
"time"
1112

@@ -123,20 +124,20 @@ func (sb *serverBuilder) Build(tlsConfig *tls.Config, statsRegistry prometheus.R
123124
// This is the names which are allowlisted at the server level, plus the union
124125
// of all names which are allowlisted for any individual service.
125126
acceptedSANs := make(map[string]struct{})
127+
var acceptedSANsSlice []string
126128
for _, service := range sb.cfg.Services {
127129
for _, name := range service.ClientNames {
128130
acceptedSANs[name] = struct{}{}
131+
if !slices.Contains(acceptedSANsSlice, name) {
132+
acceptedSANsSlice = append(acceptedSANsSlice, name)
133+
}
129134
}
130135
}
131136

132137
// Ensure that the health service has the same ClientNames as the other
133138
// services, so that health checks can be performed by clients which are
134139
// allowed to connect to the server.
135-
healthService := sb.cfg.Services[healthpb.Health_ServiceDesc.ServiceName]
136-
for as := range acceptedSANs {
137-
healthService.ClientNames = append(healthService.ClientNames, as)
138-
}
139-
sb.cfg.Services[healthpb.Health_ServiceDesc.ServiceName] = healthService
140+
sb.cfg.Services[healthpb.Health_ServiceDesc.ServiceName].ClientNames = acceptedSANsSlice
140141

141142
creds, err := bcreds.NewServerCredentials(tlsConfig, acceptedSANs)
142143
if err != nil {

0 commit comments

Comments
 (0)