-
Notifications
You must be signed in to change notification settings - Fork 293
/
server.go
267 lines (228 loc) · 7.86 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
// Copyright 2019 The Grafeas Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package server includes all generic instantiation of the Grafeas server, designed to be called from a very small main() implementation.
package server
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/cockroachdb/cmux"
"github.com/grafeas/grafeas/go/config"
"github.com/grafeas/grafeas/go/v1beta1/api"
"github.com/grafeas/grafeas/go/v1beta1/project"
"github.com/grafeas/grafeas/go/v1beta1/storage"
pb "github.com/grafeas/grafeas/proto/v1beta1/grafeas_go_proto"
prpb "github.com/grafeas/grafeas/proto/v1beta1/project_go_proto"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"
)
var (
configFile = flag.String("config", "", "Path to a config file")
)
// StartGrafeas starts the Grafeas server, instantiating the storage of the type specified in the config.
func StartGrafeas() error {
flag.Parse()
cfg, err := config.LoadConfig(*configFile)
if err != nil {
return status.Errorf(codes.Internal, "failed to load cfg file: %s", err)
}
var db grafeas.Storage
var proj project.Storage
s, err := storage.CreateStorageOfType(cfg.StorageType, cfg.StorageConfig)
if err != nil {
return status.Errorf(codes.Internal, "failed to create storage: %s", err)
}
db = s
proj = s
if err := run(cfg.API, &db, &proj); err != nil {
return status.Errorf(codes.Internal, "internal error: %s", err)
} else {
return nil
}
}
// run initializes grpc and grpc gateway api services on the same address
func run(config *config.ServerConfig, db *grafeas.Storage, proj *project.Storage) error {
network, address := "tcp", config.Address
if strings.HasPrefix(config.Address, "unix://") {
network = "unix"
address = strings.TrimPrefix(config.Address, "unix://")
// Remove existing socket if found
os.Remove(address)
}
l, err := net.Listen(network, address)
if err != nil {
return errors.New(fmt.Sprintf("could not listen to address %s", config.Address))
}
log.Printf("starting grpc server on %s", address)
var (
apiHandler http.Handler
apiListener net.Listener
srv *http.Server
ctx = context.Background()
httpMux = http.NewServeMux()
tcpMux = cmux.New(l)
)
tlsConfig, err := tlsClientConfig(config.CAFile)
if err != nil {
return errors.New(fmt.Sprintf("failed to create tls config %s", err))
}
if tlsConfig != nil {
cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
return errors.New(fmt.Sprintf("failed to load certificate files %s", err))
}
tlsConfig.Certificates = []tls.Certificate{cert}
tlsConfig.NextProtos = []string{"h2"}
apiListener = tls.NewListener(tcpMux.Match(cmux.Any()), tlsConfig)
go func() { handleShutdown(tcpMux.Serve()) }()
grpcServer := newGrpcServer(tlsConfig, db, proj)
gwmux, err := newGrpcGatewayServer(ctx, apiListener.Addr().String(), tlsConfig)
if err != nil {
return err
}
httpMux.Handle("/", gwmux)
apiHandler = grpcHandlerFunc(grpcServer, httpMux)
log.Println("grpc server is configured with client certificate authentication")
} else {
grpcL := tcpMux.Match(cmux.HTTP2())
apiListener = tcpMux.Match(cmux.HTTP1())
go func() { handleShutdown(tcpMux.Serve()) }()
grpcServer := newGrpcServer(nil, db, proj)
go func() { handleShutdown(grpcServer.Serve(grpcL)) }()
gwmux, err := newGrpcGatewayServer(ctx, apiListener.Addr().String(), nil)
if err != nil {
return err
}
httpMux.Handle("/", gwmux)
apiHandler = httpMux
log.Println("grpc server is configured without client certificate authentication")
}
// Setup the CORS middleware. If `config.CORSAllowedOrigins` is empty, no CORS
// Origins will be allowed through.
cors := cors.New(cors.Options{
AllowedOrigins: config.CORSAllowedOrigins,
})
srv = &http.Server{
Handler: cors.Handler(apiHandler),
TLSConfig: tlsConfig,
}
// blocking call
if err := handleShutdown(srv.Serve(apiListener)); err != nil {
return errors.New(fmt.Sprintf("fatal error on shutdown %s", err))
}
log.Println("Grpc API stopped")
return nil
}
// handleShutdown handles the server shut down error.
func handleShutdown(err error) error {
if err != nil {
if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") {
return err
}
}
return nil
}
func newGrpcServer(tlsConfig *tls.Config, db *grafeas.Storage, proj *project.Storage) *grpc.Server {
grpcOpts := []grpc.ServerOption{}
if tlsConfig != nil {
grpcOpts = append(grpcOpts, grpc.Creds(credentials.NewTLS(tlsConfig)))
}
grpcServer := grpc.NewServer(grpcOpts...)
g := grafeas.API{
Storage: *db,
Auth: &grafeas.NoOpAuth{},
Filter: &grafeas.NoOpFilter{},
Logger: &grafeas.NoOpLogger{},
EnforceValidation: true,
}
pb.RegisterGrafeasV1Beta1Server(grpcServer, &g)
gp := project.API{Storage: *proj}
prpb.RegisterProjectsServer(grpcServer, &gp)
return grpcServer
}
func newGrpcGatewayServer(ctx context.Context, listenerAddr string, tlsConfig *tls.Config) (http.Handler, error) {
var (
gwTLSConfig *tls.Config
gwOpts []grpc.DialOption
)
if tlsConfig != nil {
gwTLSConfig = tlsConfig.Clone()
gwTLSConfig.InsecureSkipVerify = true
gwOpts = append(gwOpts, grpc.WithTransportCredentials(credentials.NewTLS(gwTLSConfig)))
} else {
gwOpts = append(gwOpts, grpc.WithInsecure())
}
// changes json serializer to include empty fields with default values
jsonOpt := runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{})
gwmux := runtime.NewServeMux(jsonOpt)
conn, err := grpc.DialContext(ctx, listenerAddr, gwOpts...)
if err != nil {
return nil, errors.New("could not initialize grpc gateway connection")
}
err = pb.RegisterGrafeasV1Beta1Handler(ctx, gwmux, conn)
if err != nil {
return nil, errors.New("could not initialize ancestry grpc gateway")
}
err = prpb.RegisterProjectsHandler(ctx, gwmux, conn)
if err != nil {
return nil, errors.New("could not initialize notification grpc gateway")
}
return http.Handler(gwmux), nil
}
// grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
// connections or otherHandler otherwise. Copied from cockroachdb.
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
})
}
// tlsClientConfig initializes a *tls.Config using the given CA. The resulting
// *tls.Config is meant to be used to configure an HTTP server to do client
// certificate authentication.
//
// If no CA is given, a nil *tls.Config is returned; no client certificate will
// be required and verified. In other words, authentication will be disabled.
func tlsClientConfig(caPath string) (*tls.Config, error) {
if caPath == "" {
return nil, nil
}
caCert, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
return tlsConfig, nil
}