Files
ocis/ocis-pkg/service/grpc/service.go
Roman Perekhod 9143307b70 fix: [ocisdev-1433] fix public share, add The grpc clients keepalive and more (#13020) (#13068)
* fix: the public share configuration and fixed graceful shutdown

* fix: [OCISDEV-1433] remove GRPC_MAX_CONNECTION_AGE

The setting closed healthy grpc connections on a timer so that clients would redo
their DNS lookup. It never helped against the failure it was reached for - a
connection whose peer stopped answering - because the age limit is enforced by
the server, the grace period was left at infinity so a request already in flight
was never ended, and any value without a unit suffix silently disabled it again.

Detecting a peer that stops answering is now the job of the grpc clients, which
ping the peer while a request is in flight. The servers here got the matching
enforcement policy so they accept those pings; the client side arrives with the
next reva bump.

* fix: Added the new variable OCDAV_DISABLE_PROPFIND_PUBLIC_LINK_RESOLUTION that disable the resolution of public link shares when answering PROPFIND requests.

* chore: bump reva

* fix: The keepalive added to grpc clinet

* chore: bump reva

* build(deps): bump go 1.26.8, grpc-go dependency

---------

(cherry picked from commit e86eb4d210)

For the 9.0 branch, the reva dependency was pointed at reva's main-branch
merge of PR #748 (commit 373f9509a, pseudo-version
v2.0.0-20261005091647-373f9509a363) instead of the 8.2-targeted
e0beaa380005, since the 8.2 merge predates the VaultMode/UploadSessionFilter.Orphaned
APIs already present on the 9.0 reva lineage.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

# Conflicts:
#	changelog/unreleased/security-bump-grpc.md
#	go.mod
#	go.sum
#	vendor/google.golang.org/grpc/server.go
#	vendor/google.golang.org/grpc/stream.go
#	vendor/google.golang.org/grpc/version.go
#	vendor/modules.txt
2026-10-06 17:21:46 +02:00

135 lines
4.7 KiB
Go

package grpc
import (
"context"
"crypto/tls"
"fmt"
"strings"
"time"
mgrpcs "github.com/go-micro/plugins/v4/server/grpc"
"github.com/go-micro/plugins/v4/wrapper/monitoring/prometheus"
mtracer "github.com/go-micro/plugins/v4/wrapper/trace/opentelemetry"
"github.com/rs/zerolog"
"go-micro.dev/v4"
"go-micro.dev/v4/client"
"go-micro.dev/v4/server"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/reva/v2/pkg/autoprop"
)
// _minKeepalivePingInterval is the shortest keepalive ping interval we accept
// from a client. Clients cannot ping more often than every ten seconds, so this
// leaves headroom below that; grpc's own default would GOAWAY anything more
// frequent than every five minutes.
const _minKeepalivePingInterval = 5 * time.Second
// Service simply wraps the go-micro grpc service.
type Service struct {
micro.Service
}
// NewServiceWithClient initializes a new grpc service with explicit client.
func NewServiceWithClient(client client.Client, opts ...Option) (Service, error) {
var mServer server.Server
sopts := newOptions(opts...)
// accept the keepalive pings clients send while an rpc is in flight, so that
// they can tell a peer that stopped answering from one that is merely slow
keepaliveParams := grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
MinTime: _minKeepalivePingInterval,
PermitWithoutStream: true,
})
tlsConfig := &tls.Config{}
if sopts.TLSEnabled {
var cert tls.Certificate
var err error
if sopts.TLSCert != "" {
cert, err = tls.LoadX509KeyPair(sopts.TLSCert, sopts.TLSKey)
if err != nil {
sopts.Logger.Error().Err(err).Str("cert", sopts.TLSCert).Str("key", sopts.TLSKey).Msg("error loading server certifcate and key")
return Service{}, fmt.Errorf("grpc service error loading server certificate and key: %w", err)
}
} else {
// Generate a self-signed server certificate on the fly. This requires the clients
// to connect with InsecureSkipVerify.
cert, err = ociscrypto.GenTempCertForAddr(sopts.Address)
if err != nil {
return Service{}, fmt.Errorf("grpc service error creating temporary self-signed certificate: %w", err)
}
}
tlsConfig.Certificates = []tls.Certificate{cert}
mServer = mgrpcs.NewServer(mgrpcs.Options(keepaliveParams), mgrpcs.AuthTLS(tlsConfig))
} else {
mServer = mgrpcs.NewServer(mgrpcs.Options(keepaliveParams))
}
handlerWrappers := []server.HandlerWrapper{
mtracer.NewHandlerWrapper(
mtracer.WithTraceProvider(sopts.TraceProvider),
),
autoprop.NewGoMicroServerHandlerWrapper(),
}
if sopts.Logger.GetLevel() == zerolog.DebugLevel {
handlerWrappers = append(handlerWrappers, LogHandler(&sopts.Logger))
}
handlerWrappers = append(handlerWrappers, sopts.HandlerWrappers...)
mopts := []micro.Option{
// first add a server because it will reset any options
micro.Server(mServer),
// also add a client that can be used after initializing the service
micro.Client(client),
micro.Address(sopts.Address),
micro.Name(strings.Join([]string{sopts.Namespace, sopts.Name}, ".")),
micro.Version(sopts.Version),
micro.Context(sopts.Context),
micro.Registry(registry.GetRegistry()),
micro.RegisterTTL(registry.GetRegisterTTL()),
micro.RegisterInterval(registry.GetRegisterInterval()),
micro.WrapHandler(prometheus.NewHandlerWrapper()),
micro.WrapClient(
mtracer.NewClientWrapper(
mtracer.WithTraceProvider(sopts.TraceProvider),
),
autoprop.NewGoMicroClientWrapper(),
),
micro.WrapHandler(handlerWrappers...),
micro.WrapSubscriber(
mtracer.NewSubscriberWrapper(mtracer.WithTraceProvider(sopts.TraceProvider)),
autoprop.NewGoMicroServerSubscriberWrapper(),
),
}
return Service{micro.NewService(mopts...)}, nil
}
// If used with tracing, please ensure this is registered (by micro.WrapHandler()) after
// micro-plugin's opentracing wrapper: `opentracing.NewHandlerWrapper()`
func LogHandler(l *log.Logger) func(fn server.HandlerFunc) server.HandlerFunc {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
now := time.Now()
spanContext := trace.SpanContextFromContext(ctx)
defer func() {
l.Debug().
Str("traceid", spanContext.TraceID().String()).
Str("method", req.Method()).
Str("endpoint", req.Endpoint()).
Str("content-type", req.ContentType()).
Str("service", req.Service()).
Interface("headers", req.Header()).
Dur("duration", time.Since(now)).
Msg("grpc call")
}()
return fn(ctx, req, rsp)
}
}
}