-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathapi_server.go
More file actions
316 lines (269 loc) · 11.3 KB
/
api_server.go
File metadata and controls
316 lines (269 loc) · 11.3 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright 2018- The Pixie Authors.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"strings"
"time"
"github.com/gorilla/handlers"
"github.com/olivere/elastic/v7"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"px.dev/pixie/src/api/proto/cloudpb"
"px.dev/pixie/src/api/proto/vizierpb"
"px.dev/pixie/src/cloud/api/apienv"
"px.dev/pixie/src/cloud/api/controllers"
"px.dev/pixie/src/cloud/api/ptproxy"
"px.dev/pixie/src/cloud/autocomplete"
"px.dev/pixie/src/cloud/shared/esutils"
"px.dev/pixie/src/cloud/shared/idprovider"
"px.dev/pixie/src/cloud/shared/vzshard"
"px.dev/pixie/src/shared/services"
svcEnv "px.dev/pixie/src/shared/services/env"
"px.dev/pixie/src/shared/services/handler"
"px.dev/pixie/src/shared/services/healthz"
"px.dev/pixie/src/shared/services/msgbus"
"px.dev/pixie/src/shared/services/server"
"px.dev/pixie/src/utils/script"
)
const (
defaultBundleFile = "https://storage.googleapis.com/pixie-prod-artifacts/script-bundles/bundle-core.json"
ossBundleFile = "https://artifacts.px.dev/pxl_scripts/bundle.json"
)
func init() {
pflag.String("domain_name", "dev.withpixie.dev", "The domain name of Pixie Cloud")
pflag.String("elastic_service", "https://pl-elastic-es-http.plc-dev.svc.cluster.local:9200", "The url of the elasticsearch cluster")
pflag.String("elastic_ca_cert", "/elastic-certs/ca.crt", "CA Cert for elastic cluster")
pflag.String("elastic_tls_cert", "/elastic-certs/tls.crt", "TLS Cert for elastic cluster")
pflag.String("elastic_tls_key", "/elastic-certs/tls.key", "TLS Key for elastic cluster")
pflag.String("elastic_username", "elastic", "Username for access to elastic cluster")
pflag.String("elastic_password", "", "Password for access to elastic")
pflag.String("md_index_name", "", "The elastic index name for metadata.")
pflag.String("allowed_origins", "", "The allowed origins for CORS")
pflag.String("auth_connector_name", "", "If any, the name of the auth connector to be used with Pixie")
pflag.String("auth_connector_callback_url", "", "If any, the callback URL for the auth connector")
pflag.Bool("script_modification_disabled", false, "If script modification should be disallowed to prevent arbitrary script execution")
pflag.Bool("disable_autocomplete", false, "Disable autocomplete functionality (no Elasticsearch required)")
}
func main() {
services.SetupService("api-service", 51200)
services.SetupSSLClientFlags()
vzshard.SetupFlags()
services.PostFlagSetupAndParse()
services.CheckServiceFlags()
services.CheckSSLClientFlags()
services.SetupServiceLogging()
flush := services.InitDefaultSentry()
defer flush()
ac, err := controllers.NewAuthClient()
if err != nil {
log.WithError(err).Fatal("Failed to init auth client")
}
pc, err := apienv.NewProfileServiceClient()
if err != nil {
log.WithError(err).Fatal("Failed to init profile client")
}
oc, err := apienv.NewOrgServiceClient()
if err != nil {
log.WithError(err).Error("Failed to init org client")
}
cm, err := apienv.NewConfigManagerServiceClient()
if err != nil {
log.WithError(err).Fatal("Failed to init config manager client")
}
vc, vk, err := apienv.NewVZMgrServiceClients()
if err != nil {
log.WithError(err).Fatal("Failed to init vzmgr clients")
}
at, err := apienv.NewArtifactTrackerClient()
if err != nil {
log.WithError(err).Fatal("Failed to init artifact tracker client")
}
ak, err := controllers.NewAPIKeyClient()
if err != nil {
log.WithError(err).Fatal("Failed to init API key client")
}
oa, err := idprovider.NewHydraKratosClient()
if err != nil {
log.WithError(err).Fatal("Failed to init Hydra + Kratos idprovider client")
}
ps, drps, err := apienv.NewPluginServiceClients()
if err != nil {
log.WithError(err).Fatal("Failed to connect to plugin service")
}
env, err := apienv.New(ac, pc, oc, vk, ak, vc, at, oa, cm, ps, drps)
if err != nil {
log.WithError(err).Fatal("Failed to create api environment")
}
// Connect to NATS.
nc := msgbus.MustConnectNATS()
disableAutocomplete := viper.GetBool("disable_autocomplete")
var es *elastic.Client
if !disableAutocomplete {
esConfig := &esutils.Config{
URL: []string{viper.GetString("elastic_service")},
User: viper.GetString("elastic_username"),
Passwd: viper.GetString("elastic_password"),
CaCertFile: viper.GetString("elastic_ca_cert"),
}
es, err = esutils.NewEsClient(esConfig)
if err != nil {
log.WithError(err).Fatal("Could not connect to elastic")
}
}
mux := http.NewServeMux()
mux.Handle("/api/auth/signup", handler.New(env, controllers.AuthSignupHandler))
mux.Handle("/api/auth/login", handler.New(env, controllers.AuthLoginHandler))
mux.Handle("/api/auth/loginEmbed", handler.New(env, controllers.AuthLoginHandlerEmbed))
mux.Handle("/api/auth/logout", handler.New(env, controllers.AuthLogoutHandler))
mux.Handle("/api/auth/refetch", handler.New(env, controllers.AuthRefetchHandler))
mux.Handle("/api/auth/oauth/login", handler.New(env, controllers.AuthOAuthLoginHandler))
// This is an unauthenticated path that will check and validate if a particular domain
// is available for registration. This need to be unauthenticated because we need to check this before
// the user registers.
mux.Handle("/api/authorized", controllers.WithAugmentedAuthMiddleware(env, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
})))
if viper.GetString("auth_connector_name") != "" {
mux.Handle(fmt.Sprintf("/api/auth/%s", viper.GetString("auth_connector_name")), handler.New(env, controllers.AuthConnectorHandler))
}
// This handles all the pprof endpoints.
mux.Handle("/debug/", http.DefaultServeMux)
healthz.RegisterDefaultChecks(mux)
// API service needs to convert any cookies into an augmented token in bearer auth.
serverOpts := &server.GRPCServerOptions{
AuthMiddleware: func(ctx context.Context, e svcEnv.Env) (string, error) {
apiEnv, ok := e.(apienv.APIEnv)
if !ok {
return "", errors.New("Could not convert env to apiEnv")
}
return controllers.GetAugmentedTokenGRPC(ctx, apiEnv)
},
DisableAuth: map[string]bool{
"/px.cloudapi.ArtifactTracker/GetArtifactList": true,
"/px.cloudapi.ArtifactTracker/GetDownloadLink": true,
"/pl.cloudapi.ArtifactTracker/GetArtifactList": true,
"/pl.cloudapi.ArtifactTracker/GetDownloadLink": true,
"/px.cloudapi.ConfigService/GetConfigForVizier": true,
"/px.cloudapi.ConfigService/GetConfigForOperator": true,
"/px.cloudapi.AuthService/Login": true,
},
}
domainName := viper.GetString("domain_name")
allowedOrigins := []string{"https://" + domainName, "https://work." + domainName}
if viper.GetString("allowed_origins") != "" {
allowedOrigins = append(allowedOrigins, strings.Split(viper.GetString("allowed_origins"), ",")...)
}
s := server.NewPLServerWithOptions(env, handlers.CORS(services.DefaultCORSConfig(allowedOrigins)...)(mux), serverOpts)
imageAuthServer := &controllers.VizierImageAuthServer{}
cloudpb.RegisterVizierImageAuthorizationServer(s.GRPCServer(), imageAuthServer)
artifactTrackerServer := controllers.ArtifactTrackerServer{
ArtifactTrackerClient: at,
}
cloudpb.RegisterArtifactTrackerServer(s.GRPCServer(), artifactTrackerServer)
cis := &controllers.VizierClusterInfo{VzMgr: vc, ArtifactTrackerClient: at}
cloudpb.RegisterVizierClusterInfoServer(s.GRPCServer(), cis)
vdks := &controllers.VizierDeploymentKeyServer{VzDeploymentKey: vk}
cloudpb.RegisterVizierDeploymentKeyManagerServer(s.GRPCServer(), vdks)
aks := &controllers.APIKeyServer{APIKeyClient: ak}
cloudpb.RegisterAPIKeyManagerServer(s.GRPCServer(), aks)
authServer := &controllers.AuthServer{AuthClient: ac}
cloudpb.RegisterAuthServiceServer(s.GRPCServer(), authServer)
sm, err := apienv.NewScriptMgrServiceClient()
if err != nil {
log.WithError(err).Fatal("Failed to init scriptmgr client.")
}
sms := &controllers.ScriptMgrServer{ScriptMgr: sm}
cloudpb.RegisterScriptMgrServer(s.GRPCServer(), sms)
scriptModificationDisabled := viper.GetBool("script_modification_disabled")
vpt := ptproxy.NewVizierPassThroughProxy(nc, vc, sm, scriptModificationDisabled)
vizierpb.RegisterVizierServiceServer(s.GRPCServer(), vpt)
vizierpb.RegisterVizierDebugServiceServer(s.GRPCServer(), vpt)
var suggester autocomplete.Suggester
if disableAutocomplete {
log.Info("Autocomplete disabled - using NoopSuggester")
suggester = autocomplete.NewNoopSuggester()
} else {
mdIndexName := viper.GetString("md_index_name")
if mdIndexName == "" {
log.Fatal("Must specify a name for the elastic index.")
}
esSuggester, err := autocomplete.NewElasticSuggester(es, mdIndexName, "scripts", pc)
if err != nil {
log.WithError(err).Fatal("Failed to start elastic suggester")
}
suggester = esSuggester
var br *script.BundleManager
var bundleErr error
updateBundle := func() {
// Requiring the bundle manager in the API service is temporary until we
// start indexing scripts.
br, bundleErr = script.NewBundleManagerWithOrg([]string{defaultBundleFile, ossBundleFile}, "", "")
if bundleErr != nil {
log.WithError(bundleErr).Error("Failed to init bundle manager")
br = nil
}
esSuggester.UpdateScriptBundle(br)
}
quitCh := make(chan bool)
go func() {
updateBundle()
scriptTimer := time.NewTicker(30 * time.Second)
defer scriptTimer.Stop()
for {
select {
case <-quitCh:
return
case <-scriptTimer.C:
updateBundle()
}
}
}()
defer close(quitCh)
}
as := &controllers.AutocompleteServer{Suggester: suggester}
cloudpb.RegisterAutocompleteServiceServer(s.GRPCServer(), as)
os := &controllers.OrganizationServiceServer{ProfileServiceClient: pc, AuthServiceClient: ac, OrgServiceClient: oc}
cloudpb.RegisterOrganizationServiceServer(s.GRPCServer(), os)
us := &controllers.UserServiceServer{ProfileServiceClient: pc, OrgServiceClient: oc}
cloudpb.RegisterUserServiceServer(s.GRPCServer(), us)
cs := &controllers.ConfigServiceServer{ConfigServiceClient: cm}
cloudpb.RegisterConfigServiceServer(s.GRPCServer(), cs)
pss := &controllers.PluginServiceServer{PluginServiceClient: ps, DataRetentionPluginServiceClient: drps}
cloudpb.RegisterPluginServiceServer(s.GRPCServer(), pss)
gqlEnv := controllers.GraphQLEnv{
ArtifactTrackerServer: artifactTrackerServer,
VizierClusterInfo: cis,
VizierDeployKeyMgr: vdks,
APIKeyMgr: aks,
ScriptMgrServer: sms,
AutocompleteServer: as,
OrgServer: os,
UserServer: us,
PluginServer: pss,
}
mux.Handle("/api/graphql", controllers.WithAugmentedAuthMiddleware(env, controllers.NewGraphQLHandler(gqlEnv)))
mux.Handle("/api/unauthenticated/graphql", controllers.NewUnauthenticatedGraphQLHandler(gqlEnv))
s.Start()
s.StopOnInterrupt()
}