fix(search): honor the gRPC client TLS mode in the index command

The index command read OC_GRPC_CLIENT_TLS_MODE with a meaning of its
own: "insecure" dialed without TLS, and every other value, including
the default and "off", dialed with verified TLS and ignored
OC_GRPC_CLIENT_TLS_CACERT. Against the default setup, where the gRPC
services run without TLS, it failed unless --insecure was passed, and
against services with TLS enabled it only connected when their
certificate was trusted by the system roots, never with the generated
certificate or a private CA.

Map the mode with pool.StringToTLSMode and dial with pool.NewConn, the
mapping and dialer the other services' reva clients use: "off" dials
without TLS, "insecure" uses TLS without verifying the certificate and
"on" verifies it against the configured CA. --insecure still forces a
connection without TLS, and then the mode is not read at all.
This commit is contained in:
José M. Requena Plens committed 2026-10-04 23:34:06 +02:00
1 parent 4dec19089d
commit 3dfae3b984
2 files changed
+136 -12

No files matched your search

+12 -12
View File
@@ -2,7 +2,6 @@ package command
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
@@ -14,11 +13,9 @@ import (
searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/opencloud-eu/opencloud/services/search/pkg/config/parser"
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
// Index is the entrypoint for the server command.
@@ -45,16 +42,19 @@ func Index(cfg *config.Config) *cobra.Command {
return fmt.Errorf("concurrency %d exceeds max allowed %d", concurrencyFlag, cfg.ReindexMaxConcurrency)
}
var dialOpts []grpc.DialOption
if cfg.GRPCClientTLS.Mode == "insecure" || insecureFlag {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS12,
})))
tlsMode := pool.TLSOff
if !insecureFlag {
mode, err := pool.StringToTLSMode(cfg.GRPCClientTLS.Mode)
if err != nil {
return err
}
tlsMode = mode
}
conn, err := grpc.NewClient(endpointFlag, dialOpts...)
conn, err := pool.NewConn(endpointFlag,
pool.WithTLSMode(tlsMode),
pool.WithTLSCACert(cfg.GRPCClientTLS.CACert),
)
if err != nil {
return fmt.Errorf("failed to dial %s: %w", endpointFlag, err)
}
+124
View File
@@ -0,0 +1,124 @@
package command
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"os"
"path/filepath"
"testing"
"time"
"github.com/opencloud-eu/opencloud/pkg/shared"
searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type fakeSearchProvider struct {
searchsvc.UnimplementedSearchProviderServer
}
func (fakeSearchProvider) IndexSpace(_ *searchsvc.IndexSpaceRequest, stream grpc.ServerStreamingServer[searchsvc.IndexSpaceResponse]) error {
return stream.Send(&searchsvc.IndexSpaceResponse{SpaceId: "space-1", IndexedSpaces: 1, TotalSpaces: 1})
}
// startSearchServer serves a fake search service on a random local port and
// returns its address.
func startSearchServer(t *testing.T, opts ...grpc.ServerOption) string {
t.Helper()
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
srv := grpc.NewServer(opts...)
searchsvc.RegisterSearchProviderServer(srv, fakeSearchProvider{})
go func() { _ = srv.Serve(lis) }()
t.Cleanup(srv.Stop)
return lis.Addr().String()
}
// selfSignedCert returns a certificate for 127.0.0.1 and the path of a PEM
// file holding it, to be used as the CA certificate by the client.
func selfSignedCert(t *testing.T) (tls.Certificate, string) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "127.0.0.1"},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
IsCA: true,
BasicConstraintsValid: true,
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
require.NoError(t, err)
keyDER, err := x509.MarshalECPrivateKey(key)
require.NoError(t, err)
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
cert, err := tls.X509KeyPair(certPEM, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}))
require.NoError(t, err)
caFile := filepath.Join(t.TempDir(), "ca.pem")
require.NoError(t, os.WriteFile(caFile, certPEM, 0o600))
return cert, caFile
}
func TestIndexHonorsGRPCClientTLSMode(t *testing.T) {
plain := startSearchServer(t)
cert, caFile := selfSignedCert(t)
withTLS := startSearchServer(t, grpc.Creds(credentials.NewServerTLSFromCert(&cert)))
tests := []struct {
name string
endpoint string
mode string
caCert string
insecure bool
wantErr bool
errContains string
}{
{name: "no TLS, mode unset", endpoint: plain},
{name: "no TLS, mode off", endpoint: plain, mode: "off"},
{name: "no TLS, --insecure", endpoint: plain, insecure: true},
{name: "TLS, mode insecure", endpoint: withTLS, mode: "insecure"},
{name: "TLS, mode on with CA certificate", endpoint: withTLS, mode: "on", caCert: caFile},
{name: "no TLS, mode insecure", endpoint: plain, mode: "insecure", wantErr: true},
{name: "TLS, mode off", endpoint: withTLS, mode: "off", wantErr: true},
{name: "unknown mode", endpoint: plain, mode: "bogus", wantErr: true, errContains: "unknown TLS mode"},
{name: "unknown mode, --insecure", endpoint: plain, mode: "bogus", insecure: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config.Config{
GRPCClientTLS: &shared.GRPCClientTLS{Mode: tt.mode, CACert: tt.caCert},
ReindexMaxConcurrency: 3,
}
cmd := Index(cfg)
require.NoError(t, cmd.Flags().Set("all-spaces", "true"))
require.NoError(t, cmd.Flags().Set("endpoint", tt.endpoint))
if tt.insecure {
require.NoError(t, cmd.Flags().Set("insecure", "true"))
}
err := cmd.RunE(cmd, nil)
switch {
case tt.errContains != "":
require.ErrorContains(t, err, tt.errContains)
case tt.wantErr:
require.Error(t, err)
default:
require.NoError(t, err)
}
})
}
}