Skip to content

Commit

Permalink
servercore: introduced SharedMtlsGrpcListenerConfiguration that allow…
Browse files Browse the repository at this point in the history
…s creating separate listeners that share the same mtls configuration.
  • Loading branch information
hugosantos committed Sep 10, 2024
1 parent 34de34f commit eea4ffa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions std/go/grpc/servercore/configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var configConfigs = map[string]ListenerConfiguration{}

type DefaultConfiguration struct{}
type DefaultConfigurationWithSharedMtls struct{}

type ListenerConfiguration interface {
CreateListener(context.Context, string, ListenOpts) (net.Listener, error)
Expand All @@ -27,6 +28,13 @@ type GrpcListenerConfiguration interface {
ServerOpts(string) []grpc.ServerOption
}

type SharedMtlsGrpcListenerConfiguration interface {
ListenerConfiguration

UseFoundationMTLSConfiguration()
ServerOpts(string) []grpc.ServerOption
}

func SetListenerConfiguration(name string, conf ListenerConfiguration) {
core.AssertNotRunning("SetServiceConfiguration")

Expand All @@ -48,3 +56,5 @@ func listenerConfiguration(name string) ListenerConfiguration {
func (DefaultConfiguration) CreateListener(ctx context.Context, name string, opts ListenOpts) (net.Listener, error) {
return opts.CreateNamedListener(ctx, name)
}

func (DefaultConfigurationWithSharedMtls) UseFoundationMTLSConfiguration() {}
7 changes: 7 additions & 0 deletions std/go/grpc/servercore/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func Listen(ctx context.Context, opts ListenOpts, registerServices func(Server))
x = append(x, grpc.Creds(creds))
}

serversByConfiguration[cfg.Name] = append(serversByConfiguration[cfg.Name], grpc.NewServer(x...))
} else if cgrp, ok := c.(SharedMtlsGrpcListenerConfiguration); ok {
x := append(slices.Clone(grpcopts), cgrp.ServerOpts(cfg.Name)...)
if tlsConfig != nil {
x = append(x, grpc.Creds(credentials.NewTLS(tlsConfig)))
}

serversByConfiguration[cfg.Name] = append(serversByConfiguration[cfg.Name], grpc.NewServer(x...))
} else {
return fnerrors.New("listener configuration for %q does not support grpc", cfg.Name)
Expand Down

0 comments on commit eea4ffa

Please sign in to comment.