@@ -56,36 +56,59 @@ type ServerVersionInfo struct {
5656}
5757
5858type infraServerImpl struct {
59+ server * ServerMux
5960 version ServerVersionInfo
6061 protobuf.UnimplementedInfraServer
6162}
6263
6364func (s * infraServerImpl ) GetServerVersion (ctx context.Context , req * emptypb.Empty ) (* protobuf.ServerVersionInfo , error ) {
64- peer , ok := peer .FromContext (ctx )
65- if ! ok {
66- return nil , fmt .Errorf ("no peer in context" )
67- }
68- authInfo := peer .AuthInfo .(* infraTlsAuthInfo )
69- log .Printf ("GetServerVersion: server name %s, module name %s" , authInfo .TLSInfo .State .ServerName , authInfo .moduleName )
7065 return & protobuf.ServerVersionInfo {
7166 Version : s .version .Version ,
7267 Commit : s .version .Commit ,
7368 BuildDate : s .version .BuildDate ,
7469 }, nil
7570}
7671
77- type pluginConnection struct {
72+ func (s * infraServerImpl ) WhoAmI (ctx context.Context , req * emptypb.Empty ) (* protobuf.Info , error ) {
73+ peer , ok := peer .FromContext (ctx )
74+ if ! ok {
75+ return nil , fmt .Errorf ("no peer in context" )
76+ }
77+ authInfo := peer .AuthInfo .(* infraTlsAuthInfo )
78+ return s .server .GetPluginInfo (authInfo .moduleName )
79+ }
80+
81+ type PluginConnection struct {
7882 info * protobuf.Info
7983 conn * grpc.ClientConn
8084}
8185
8286type ServerMux struct {
87+ version ServerVersionInfo
8388 tlsClient * EphemeralTLSClient
8489 infraAddr net.Addr
8590 infraListener net.Listener
8691 infraServer * grpc.Server
8792 pluginDNSToModulePath map [string ]string
88- pluginConnections map [string ]pluginConnection
93+ pluginConnections map [string ]PluginConnection
94+ protobuf.UnimplementedInfraServer
95+ }
96+
97+ func (s * ServerMux ) GetServerVersion (ctx context.Context , req * emptypb.Empty ) (* protobuf.ServerVersionInfo , error ) {
98+ return & protobuf.ServerVersionInfo {
99+ Version : s .version .Version ,
100+ Commit : s .version .Commit ,
101+ BuildDate : s .version .BuildDate ,
102+ }, nil
103+ }
104+
105+ func (s * ServerMux ) WhoAmI (ctx context.Context , req * emptypb.Empty ) (* protobuf.Info , error ) {
106+ peer , ok := peer .FromContext (ctx )
107+ if ! ok {
108+ return nil , fmt .Errorf ("no peer in context" )
109+ }
110+ authInfo := peer .AuthInfo .(* infraTlsAuthInfo )
111+ return s .GetPluginInfo (authInfo .moduleName )
89112}
90113
91114type infraTlsCreds struct {
@@ -121,6 +144,7 @@ func (c *infraTlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials
121144 }, nil
122145}
123146
147+ // NewServerMux creates a server-side mux with an infra server that handles plugin-to-server calls.
124148func NewServerMux (info ServerVersionInfo ) * ServerMux {
125149 tlsClient , err := NewEphemeralTLSClient ()
126150 if err != nil {
@@ -169,33 +193,41 @@ func NewServerMux(info ServerVersionInfo) *ServerMux {
169193 pluginDNSToModulePath : pluginDNSToModulePath ,
170194 TransportCredentials : credentials .NewTLS (infraTlsConfig ),
171195 }))
172- protobuf .RegisterInfraServer (infraServer , & infraServerImpl {
173- version : info ,
174- })
196+
175197 listener , err := newListener ()
176198 if err != nil {
177199 panic (err )
178200 }
179- go infraServer .Serve (listener )
180-
181- return & ServerMux {
201+ mux := & ServerMux {
202+ version : info ,
182203 tlsClient : tlsClient ,
183204 infraAddr : listener .Addr (),
184205 infraListener : listener ,
185206 infraServer : infraServer ,
186207 pluginDNSToModulePath : pluginDNSToModulePath ,
187- pluginConnections : make (map [string ]pluginConnection ),
208+ pluginConnections : make (map [string ]PluginConnection ),
188209 }
210+ protobuf .RegisterInfraServer (infraServer , & infraServerImpl {
211+ server : mux ,
212+ version : info ,
213+ })
214+
215+ go infraServer .Serve (listener )
216+
217+ return mux
189218}
190219
220+ // InfraAddr returns the address of the infra server for plugin-to-server callbacks.
191221func (s * ServerMux ) InfraAddr () net.Addr {
192222 return s .infraAddr
193223}
194224
225+ // CACert returns the CA certificate for mutual TLS authentication.
195226func (s * ServerMux ) CACert () * x509.Certificate {
196227 return s .tlsClient .caCert
197228}
198229
230+ // SignPluginCSR signs a certificate request for a plugin.
199231func (s * ServerMux ) SignPluginCSR (moduleName string , csr * x509.CertificateRequest ) ([]byte , error ) {
200232 return s .tlsClient .SignPluginCSR (moduleName , csr )
201233}
@@ -205,19 +237,40 @@ func (s *ServerMux) RegisterPlugin(target string, moduleName string) (*grpc.Clie
205237 if err != nil {
206238 return nil , err
207239 }
240+ if _ , exists := s .pluginDNSToModulePath [buildPluginTLSName (moduleName )]; exists {
241+ return nil , fmt .Errorf ("plugin %s already registered" , moduleName )
242+ }
208243 s .pluginDNSToModulePath [buildPluginTLSName (moduleName )] = moduleName
209244 pluginClient := protobuf .NewPluginClient (grpcConn )
210245 pluginInfo , err := pluginClient .GetPluginInfo (context .Background (), & emptypb.Empty {})
211246 if err != nil {
212247 return nil , err
213248 }
214- s .pluginConnections [moduleName ] = pluginConnection {
249+ s .pluginConnections [moduleName ] = PluginConnection {
215250 info : pluginInfo ,
216251 conn : grpcConn ,
217252 }
218253 return grpcConn , nil
219254}
220255
256+ // GetPluginInfo returns the info of a plugin.
257+ func (s * ServerMux ) GetPluginInfo (moduleName string ) (* protobuf.Info , error ) {
258+ conn , ok := s .pluginConnections [moduleName ]
259+ if ! ok {
260+ return nil , fmt .Errorf ("plugin %s not registered" , moduleName )
261+ }
262+ return conn .info , nil
263+ }
264+
265+ // GetPluginConnection returns the connection to the plugin for Server-to-Plugin calls.
266+ func (s * ServerMux ) GetPluginConnection (moduleName string ) (* grpc.ClientConn , error ) {
267+ conn , ok := s .pluginConnections [moduleName ]
268+ if ! ok {
269+ return nil , fmt .Errorf ("plugin %s not registered" , moduleName )
270+ }
271+ return conn .conn , nil
272+ }
273+
221274func (s * ServerMux ) Close () error {
222275 for _ , conn := range s .pluginConnections {
223276 conn .conn .Close ()
0 commit comments