@@ -56,36 +56,59 @@ type ServerVersionInfo struct {
56
56
}
57
57
58
58
type infraServerImpl struct {
59
+ server * ServerMux
59
60
version ServerVersionInfo
60
61
protobuf.UnimplementedInfraServer
61
62
}
62
63
63
64
func (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 )
70
65
return & protobuf.ServerVersionInfo {
71
66
Version : s .version .Version ,
72
67
Commit : s .version .Commit ,
73
68
BuildDate : s .version .BuildDate ,
74
69
}, nil
75
70
}
76
71
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 {
78
82
info * protobuf.Info
79
83
conn * grpc.ClientConn
80
84
}
81
85
82
86
type ServerMux struct {
87
+ version ServerVersionInfo
83
88
tlsClient * EphemeralTLSClient
84
89
infraAddr net.Addr
85
90
infraListener net.Listener
86
91
infraServer * grpc.Server
87
92
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 )
89
112
}
90
113
91
114
type infraTlsCreds struct {
@@ -121,6 +144,7 @@ func (c *infraTlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials
121
144
}, nil
122
145
}
123
146
147
+ // NewServerMux creates a server-side mux with an infra server that handles plugin-to-server calls.
124
148
func NewServerMux (info ServerVersionInfo ) * ServerMux {
125
149
tlsClient , err := NewEphemeralTLSClient ()
126
150
if err != nil {
@@ -169,33 +193,41 @@ func NewServerMux(info ServerVersionInfo) *ServerMux {
169
193
pluginDNSToModulePath : pluginDNSToModulePath ,
170
194
TransportCredentials : credentials .NewTLS (infraTlsConfig ),
171
195
}))
172
- protobuf .RegisterInfraServer (infraServer , & infraServerImpl {
173
- version : info ,
174
- })
196
+
175
197
listener , err := newListener ()
176
198
if err != nil {
177
199
panic (err )
178
200
}
179
- go infraServer .Serve (listener )
180
-
181
- return & ServerMux {
201
+ mux := & ServerMux {
202
+ version : info ,
182
203
tlsClient : tlsClient ,
183
204
infraAddr : listener .Addr (),
184
205
infraListener : listener ,
185
206
infraServer : infraServer ,
186
207
pluginDNSToModulePath : pluginDNSToModulePath ,
187
- pluginConnections : make (map [string ]pluginConnection ),
208
+ pluginConnections : make (map [string ]PluginConnection ),
188
209
}
210
+ protobuf .RegisterInfraServer (infraServer , & infraServerImpl {
211
+ server : mux ,
212
+ version : info ,
213
+ })
214
+
215
+ go infraServer .Serve (listener )
216
+
217
+ return mux
189
218
}
190
219
220
+ // InfraAddr returns the address of the infra server for plugin-to-server callbacks.
191
221
func (s * ServerMux ) InfraAddr () net.Addr {
192
222
return s .infraAddr
193
223
}
194
224
225
+ // CACert returns the CA certificate for mutual TLS authentication.
195
226
func (s * ServerMux ) CACert () * x509.Certificate {
196
227
return s .tlsClient .caCert
197
228
}
198
229
230
+ // SignPluginCSR signs a certificate request for a plugin.
199
231
func (s * ServerMux ) SignPluginCSR (moduleName string , csr * x509.CertificateRequest ) ([]byte , error ) {
200
232
return s .tlsClient .SignPluginCSR (moduleName , csr )
201
233
}
@@ -205,19 +237,40 @@ func (s *ServerMux) RegisterPlugin(target string, moduleName string) (*grpc.Clie
205
237
if err != nil {
206
238
return nil , err
207
239
}
240
+ if _ , exists := s .pluginDNSToModulePath [buildPluginTLSName (moduleName )]; exists {
241
+ return nil , fmt .Errorf ("plugin %s already registered" , moduleName )
242
+ }
208
243
s .pluginDNSToModulePath [buildPluginTLSName (moduleName )] = moduleName
209
244
pluginClient := protobuf .NewPluginClient (grpcConn )
210
245
pluginInfo , err := pluginClient .GetPluginInfo (context .Background (), & emptypb.Empty {})
211
246
if err != nil {
212
247
return nil , err
213
248
}
214
- s .pluginConnections [moduleName ] = pluginConnection {
249
+ s .pluginConnections [moduleName ] = PluginConnection {
215
250
info : pluginInfo ,
216
251
conn : grpcConn ,
217
252
}
218
253
return grpcConn , nil
219
254
}
220
255
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
+
221
274
func (s * ServerMux ) Close () error {
222
275
for _ , conn := range s .pluginConnections {
223
276
conn .conn .Close ()
0 commit comments