-
Notifications
You must be signed in to change notification settings - Fork 53
/
server.go
276 lines (246 loc) · 7.39 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package management
import (
"context"
"crypto/tls"
"crypto/x509"
_ "embed"
"errors"
"net"
"net/http"
"time"
"github.com/jhump/protoreflect/desc"
"github.com/kralicky/grpc-gateway/v2/runtime"
"github.com/rancher/opni/pkg/capabilities"
"github.com/rancher/opni/pkg/config"
"github.com/rancher/opni/pkg/config/meta"
"github.com/rancher/opni/pkg/config/v1beta1"
"github.com/rancher/opni/pkg/core"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/pkp"
"github.com/rancher/opni/pkg/plugins"
"github.com/rancher/opni/pkg/plugins/apis/apiextensions"
"github.com/rancher/opni/pkg/rbac"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/pkg/util"
"github.com/rancher/opni/pkg/util/waitctx"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
//go:embed management.swagger.json
var managementSwaggerJson []byte
func OpenAPISpec() []byte {
buf := make([]byte, len(managementSwaggerJson))
copy(buf, managementSwaggerJson)
return buf
}
// CoreDataSource provides a way to obtain data which the management
// server needs to serve its core API
type CoreDataSource interface {
StorageBackend() storage.Backend
TLSConfig() *tls.Config
}
// CapabilitiesDataSource provides a way to obtain data which the management
// server needs to serve capabilities-related endpoints
type CapabilitiesDataSource interface {
CapabilitiesStore() capabilities.BackendStore
}
type apiExtension struct {
client apiextensions.ManagementAPIExtensionClient
clientConn *grpc.ClientConn
serviceDesc *desc.ServiceDescriptor
httpRules []*HTTPRuleDescriptor
}
type Server struct {
UnsafeManagementServer
ManagementServerOptions
config *v1beta1.ManagementSpec
logger *zap.SugaredLogger
rbacProvider rbac.Provider
ctx context.Context
coreDataSource CoreDataSource
apiExtensions []apiExtension
}
var _ ManagementServer = (*Server)(nil)
type APIExtensionPlugin = plugins.TypedActivePlugin[apiextensions.ManagementAPIExtensionClient]
type ManagementServerOptions struct {
lifecycler config.Lifecycler
apiExtPlugins []APIExtensionPlugin
systemPlugins []plugins.ActivePlugin
capabilitiesDataSource CapabilitiesDataSource
}
type ManagementServerOption func(*ManagementServerOptions)
func (o *ManagementServerOptions) Apply(opts ...ManagementServerOption) {
for _, op := range opts {
op(o)
}
}
func WithAPIExtensions(exts []APIExtensionPlugin) ManagementServerOption {
return func(o *ManagementServerOptions) {
o.apiExtPlugins = append(o.apiExtPlugins, exts...)
}
}
func WithSystemPlugins(plugins []plugins.ActivePlugin) ManagementServerOption {
return func(o *ManagementServerOptions) {
o.systemPlugins = plugins
}
}
func WithLifecycler(lc config.Lifecycler) ManagementServerOption {
return func(o *ManagementServerOptions) {
o.lifecycler = lc
}
}
func WithCapabilitiesDataSource(src CapabilitiesDataSource) ManagementServerOption {
return func(o *ManagementServerOptions) {
o.capabilitiesDataSource = src
}
}
func NewServer(
ctx context.Context,
conf *v1beta1.ManagementSpec,
cds CoreDataSource,
opts ...ManagementServerOption,
) *Server {
lg := logger.New().Named("mgmt")
options := ManagementServerOptions{
lifecycler: config.NewUnavailableLifecycler(meta.ObjectList{}),
}
options.Apply(opts...)
return &Server{
ManagementServerOptions: options,
ctx: ctx,
config: conf,
logger: lg,
coreDataSource: cds,
rbacProvider: storage.NewRBACProvider(cds.StorageBackend()),
}
}
type managementApiServer interface {
ServeManagementAPI(ManagementServer)
}
func (m *Server) ListenAndServe() error {
if m.config.GRPCListenAddress == "" {
return errors.New("GRPCListenAddress not configured")
}
lg := m.logger
listener, err := util.NewProtocolListener(m.config.GRPCListenAddress)
if err != nil {
return err
}
lg.With(
"address", listener.Addr().String(),
).Info("management gRPC server starting")
director := m.configureApiExtensionDirector(m.ctx)
srv := grpc.NewServer(
grpc.Creds(insecure.NewCredentials()),
grpc.UnknownServiceHandler(unknownServiceHandler(director)),
)
RegisterManagementServer(srv, m)
for _, plugin := range m.systemPlugins {
go plugin.Raw.(managementApiServer).ServeManagementAPI(m)
}
waitctx.Go(m.ctx, func() {
<-m.ctx.Done()
srv.GracefulStop()
})
if m.config.HTTPListenAddress != "" {
go m.listenAndServeHttp(listener)
}
waitctx.AddOne(m.ctx)
defer waitctx.Done(m.ctx)
return srv.Serve(listener)
}
func (m *Server) listenAndServeHttp(listener net.Listener) {
lg := m.logger
lg.With(
"address", m.config.HTTPListenAddress,
).Info("management HTTP server starting")
mux := http.NewServeMux()
mux.HandleFunc("/swagger.json", func(w http.ResponseWriter, _ *http.Request) {
if _, err := w.Write(OpenAPISpec()); err != nil {
lg.Error(err)
}
})
gwmux := runtime.NewServeMux()
if err := RegisterManagementHandlerFromEndpoint(m.ctx, gwmux, listener.Addr().String(),
[]grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}); err != nil {
lg.With(
zap.Error(err),
).Fatal("failed to register management handler")
}
m.configureHttpApiExtensions(gwmux)
mux.Handle("/", gwmux)
server := &http.Server{
Addr: m.config.HTTPListenAddress,
Handler: mux,
BaseContext: func(net.Listener) context.Context {
return m.ctx
},
}
waitctx.Go(m.ctx, func() {
<-m.ctx.Done()
if err := server.Close(); err != nil {
lg.With(
zap.Error(err),
).Error("failed to close http gateway")
}
})
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
lg.With(
zap.Error(err),
).Error("http gateway exited with error")
}
}
func (m *Server) CertsInfo(ctx context.Context, _ *emptypb.Empty) (*CertsInfoResponse, error) {
resp := &CertsInfoResponse{
Chain: []*core.CertInfo{},
}
for _, tlsCert := range m.coreDataSource.TLSConfig().Certificates[:1] {
for _, der := range tlsCert.Certificate {
cert, err := x509.ParseCertificate(der)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
resp.Chain = append(resp.Chain, &core.CertInfo{
Issuer: cert.Issuer.String(),
Subject: cert.Subject.String(),
IsCA: cert.IsCA,
NotBefore: cert.NotBefore.Format(time.RFC3339),
NotAfter: cert.NotAfter.Format(time.RFC3339),
Fingerprint: pkp.NewSha256(cert).Encode(),
})
}
}
return resp, nil
}
func (m *Server) ListCapabilities(ctx context.Context, _ *emptypb.Empty) (*CapabilityList, error) {
if m.capabilitiesDataSource == nil {
return nil, status.Error(codes.Unavailable, "capability backend store not configured")
}
return &CapabilityList{
Items: m.capabilitiesDataSource.CapabilitiesStore().List(),
}, nil
}
func (m *Server) CapabilityInstaller(
ctx context.Context,
req *CapabilityInstallerRequest,
) (*CapabilityInstallerResponse, error) {
if m.capabilitiesDataSource == nil {
return nil, status.Error(codes.Unavailable, "capability backend store not configured")
}
cmd, err := m.capabilitiesDataSource.
CapabilitiesStore().
RenderInstaller(req.Name, capabilities.UserInstallerTemplateSpec{
Token: req.Token,
Pin: req.Pin,
})
if err != nil {
return nil, err
}
return &CapabilityInstallerResponse{
Command: cmd,
}, nil
}