forked from chef/automate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliance.go
751 lines (641 loc) · 28.6 KB
/
compliance.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package compliance
import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"github.com/chef/automate/api/external/secrets"
auth "github.com/chef/automate/api/interservice/authn"
iam_v2 "github.com/chef/automate/api/interservice/authz/v2"
dls "github.com/chef/automate/api/interservice/data_lifecycle"
"github.com/chef/automate/api/interservice/es_sidecar"
"github.com/chef/automate/api/interservice/event"
aEvent "github.com/chef/automate/api/interservice/event"
feed "github.com/chef/automate/components/compliance-service/api/automate-feed"
feedserver "github.com/chef/automate/components/compliance-service/api/automate-feed/server"
dlsserver "github.com/chef/automate/components/compliance-service/api/datalifecycle/server"
"github.com/chef/automate/components/compliance-service/api/jobs"
jobsserver "github.com/chef/automate/components/compliance-service/api/jobs/server"
"github.com/chef/automate/components/compliance-service/api/profiles"
profilesserver "github.com/chef/automate/components/compliance-service/api/profiles/server"
"github.com/chef/automate/components/compliance-service/api/reporting"
reportingserver "github.com/chef/automate/components/compliance-service/api/reporting/server"
"github.com/chef/automate/components/compliance-service/api/stats"
statsserver "github.com/chef/automate/components/compliance-service/api/stats/server"
"github.com/chef/automate/components/compliance-service/api/status"
statusserver "github.com/chef/automate/components/compliance-service/api/status/server"
"github.com/chef/automate/components/compliance-service/api/version"
versionserver "github.com/chef/automate/components/compliance-service/api/version/server"
"github.com/chef/automate/components/compliance-service/config"
"github.com/chef/automate/components/compliance-service/dao/pgdb"
"github.com/chef/automate/components/compliance-service/ingest/ingest"
"github.com/chef/automate/components/compliance-service/ingest/ingestic"
"github.com/chef/automate/components/compliance-service/ingest/ingestic/mappings"
ingestserver "github.com/chef/automate/components/compliance-service/ingest/server"
"github.com/chef/automate/components/compliance-service/inspec"
"github.com/chef/automate/components/compliance-service/inspec-agent/remote"
"github.com/chef/automate/components/compliance-service/inspec-agent/runner"
"github.com/chef/automate/components/compliance-service/inspec-agent/scheduler"
"github.com/chef/automate/components/compliance-service/reporting/relaxting"
"github.com/chef/automate/components/compliance-service/scanner"
"github.com/chef/automate/components/compliance-service/utils/logging"
"github.com/chef/automate/components/nodemanager-service/api/manager"
"github.com/chef/automate/components/nodemanager-service/api/nodes"
notifications "github.com/chef/automate/components/notifications-client/api"
"github.com/chef/automate/components/notifications-client/notifier"
"github.com/chef/automate/lib/grpc/secureconn"
"github.com/chef/automate/lib/tracing"
)
type serviceState int
const (
serviceStateUnknown = iota
serviceStateStarting
serviceStateStarted
)
var SERVICE_STATE serviceState
func createESBackend(servConf *config.Compliance) relaxting.ES2Backend {
// define the ElasticSearch backend config with legacy automate auth
esr := relaxting.ES2Backend{
ESUrl: servConf.ElasticSearch.Url,
Enterprise: servConf.Delivery.Enterprise,
ChefDeliveryUser: servConf.Delivery.User,
ChefDeliveryToken: servConf.Delivery.Token,
}
return esr
}
func createPGBackend(conf *config.Postgres) (*pgdb.DB, error) {
// define the Postgres Scanner backend
return pgdb.New(conf)
}
// hand over the array of job ids to the scheduler to be executed by the inspec-agent
func runHungJobs(ctx context.Context, scheduledJobsIds []string, schedulerServer *scheduler.Scheduler) {
for SERVICE_STATE != serviceStateStarted {
time.Sleep(time.Second)
}
schedulerServer.RunHungJobs(ctx, scheduledJobsIds)
}
// here we execute migrations, create the es and pg backends, read certs, set up the needed env vars,
// and modify config info
func initBits(ctx context.Context, conf *config.Compliance) (db *pgdb.DB, connFactory *secureconn.Factory, esr relaxting.ES2Backend, statusSrv *statusserver.Server, err error) {
statusSrv = statusserver.New()
statusserver.AddMigrationUpdate(statusSrv, statusserver.MigrationLabelPG, "Initializing DB connection and schema migration...")
// start pg backend
db, err = createPGBackend(&conf.Postgres)
if err != nil {
statusserver.AddMigrationUpdate(statusSrv, statusserver.MigrationLabelPG, err.Error())
statusserver.AddMigrationUpdate(statusSrv, statusserver.MigrationLabelPG, statusserver.MigrationFailedMsg)
return db, connFactory, esr, statusSrv, errors.Wrap(err, "createPGBackend failed")
}
statusserver.AddMigrationUpdate(statusSrv, statusserver.MigrationLabelPG, statusserver.MigrationCompletedMsg)
// create esconfig info backend
esr = createESBackend(conf)
backendCacheBool, err := strconv.ParseBool(conf.InspecAgent.BackendCache)
if err != nil {
logrus.Errorf("Unable to parse value for inspec agent backend cache configuration, %s - Valid configuration options are 'true' and 'false' ", conf.InspecAgent.BackendCache)
inspec.BackendCache = true
} else {
inspec.BackendCache = backendCacheBool
}
inspec.TmpDir = conf.InspecAgent.TmpDir
// Let's have something sensible if the temp dir is not specified
if inspec.TmpDir == "" {
inspec.TmpDir = "/tmp"
}
err = os.Setenv("TMPDIR", inspec.TmpDir)
if err != nil {
return db, connFactory, esr, statusSrv, errors.Wrap(err, "Unable to set TMPDIR env variable")
}
serviceCerts, err := conf.Service.ReadCerts()
if err != nil {
return db, connFactory, esr, statusSrv, errors.Wrap(err, "Unable to load service certificates")
}
connFactory = secureconn.NewFactory(*serviceCerts)
conf.Secrets.Endpoint = fmt.Sprintf("%s:%d", conf.Secrets.HostBind, conf.Secrets.Port)
conf.Authz.Endpoint = fmt.Sprintf("%s:%d", conf.Authz.HostBind, conf.Authz.Port)
conf.Manager.Endpoint = fmt.Sprintf("%s:%d", conf.Manager.Host, conf.Manager.Port)
conf.Service.Endpoint = fmt.Sprintf("%s:%d", conf.Service.HostBind, conf.Service.Port)
return db, connFactory, esr, statusSrv, nil
}
// register all the services, start the grpc server, and call setup
func serveGrpc(ctx context.Context, db *pgdb.DB, connFactory *secureconn.Factory,
esr relaxting.ES2Backend, conf config.Compliance, binding string,
statusSrv *statusserver.Server) {
lis, err := net.Listen("tcp", binding)
if err != nil {
logrus.Fatalf("failed to listen: %v", err)
}
esClient, err := esr.ES2Client()
if err != nil {
logrus.Fatalf("could not connect to elasticsearch: %v", err)
}
eventClient := getEventConnection(connFactory, conf.EventConfig.Endpoint)
notifier := getNotificationsConnection(connFactory, conf.Notifications.Target)
authzProjectsClient := createAuthzProjectsClient(connFactory, conf.Authz.Endpoint)
nodeManagerServiceClient := getManagerConnection(connFactory, conf.Manager.Endpoint)
ingesticESClient := ingestic.NewESClient(esClient)
ingesticESClient.InitializeStore(context.Background())
s := connFactory.NewServer(tracing.GlobalServerInterceptor())
// needs to be the first one, since it creates the es indices
ingest.RegisterComplianceIngesterServer(s,
ingestserver.NewComplianceIngestServer(ingesticESClient, nodeManagerServiceClient,
conf.InspecAgent.AutomateFQDN, notifier, authzProjectsClient, eventClient))
jobs.RegisterJobsServiceServer(s, jobsserver.New(db, connFactory, eventClient,
conf.Service.Endpoint, conf.Secrets.Endpoint, conf.Manager.Endpoint))
reporting.RegisterReportingServiceServer(s, reportingserver.New(&esr))
ps := profilesserver.New(db, &esr, &conf.Profiles, eventClient, statusSrv)
profiles.RegisterProfilesServiceServer(s, ps)
profiles.RegisterProfilesAdminServiceServer(s, ps)
stats.RegisterStatsServiceServer(s, statsserver.New(&esr))
version.RegisterVersionServiceServer(s, versionserver.New())
feed.RegisterFeedServiceServer(s, feedserver.New(&conf.FeedConfig, &esr))
status.RegisterComplianceStatusServer(s, statusSrv)
dlsManageable, err := setupDataLifecycleManageableInterface(ctx, connFactory, conf)
if err != nil {
logrus.Fatalf("serveGrpc aborting, can't setup dlsManageable: %s", err)
}
dls.RegisterDataLifecycleManageableServer(s, dlsManageable)
// Register reflection service on gRPC server.
reflection.Register(s)
logrus.Info("Starting GRPC server on " + binding)
// running ElasticSearch migration
err = relaxting.RunMigrations(esr, statusSrv)
if err != nil {
logrus.Fatalf("serveGrpc aborting, unable to run migrations: %v", err)
}
errc := make(chan error)
defer close(errc)
go func() {
err := s.Serve(lis)
errc <- errors.Wrap(err, "Serve")
}()
// `setup` depends on `Serve` because it dials back to the compliance-service itself.
// For this to work we launch `Serve` in a goroutine and connect WithBlock to itself and other dependent services from `setup`
// A connect timeout is used to ensure error reporting in the event of failures to connect
err = setup(ctx, connFactory, conf, esr, db)
if err != nil {
logrus.Fatalf("serveGrpc aborting, we have a problem, setup failed: %s", err.Error())
}
// block here: there's at most one error we care about
err = <-errc
// if we reach this, we've had an issue in Serve()
logrus.Fatalf("serveGrpc aborting, we have a problem: %s", err.Error())
}
func getEventConnection(connectionFactory *secureconn.Factory,
eventEndpoint string) aEvent.EventServiceClient {
if eventEndpoint == "" || eventEndpoint == ":0" {
if os.Getenv("RUN_MODE") == "test" {
logrus.Infof("using mock Event service Client")
return &EventServiceClientMock{}
}
logrus.Fatalf("eventEndpoint cannot be empty or Dial will get stuck")
}
logrus.Debugf("Connecting to event-service %q", eventEndpoint)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
conn, err := connectionFactory.DialContext(timeoutCtx, "event-service",
eventEndpoint, grpc.WithBlock())
if err != nil {
logrus.Fatalf("compliance setup, error grpc dialing to event-service aborting...")
}
// get event client
eventClient := aEvent.NewEventServiceClient(conn)
if eventClient == nil {
logrus.Fatalf("compliance setup, could not obtain automate events service client: %s", err)
}
return eventClient
}
func getNotificationsConnection(connectionFactory *secureconn.Factory,
notificationsEndpoint string) notifier.Notifier {
if notificationsEndpoint == "" || notificationsEndpoint == ":0" {
if os.Getenv("RUN_MODE") == "test" {
logrus.Infof("using mock Notifications")
return &NotifierMock{}
}
logrus.Fatalf("notificationsEndpoint cannot be empty or Dial will get stuck")
}
logrus.Debugf("Connecting to notifications-service %q", notificationsEndpoint)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
conn, err := connectionFactory.DialContext(timeoutCtx, "notifications-service",
notificationsEndpoint, grpc.WithBlock())
if err != nil {
logrus.Fatalf("getNotificationsConnection, error grpc dialing to manager %s", err.Error())
}
notifier := notifier.New(conn)
if notifier == nil {
logrus.Fatalf("compliance setup, could not obtain notification client: %s", err)
}
return notifier
}
func createAuthzProjectsClient(connectionFactory *secureconn.Factory,
authzEndpoint string) iam_v2.ProjectsClient {
if authzEndpoint == "" || authzEndpoint == ":0" {
if os.Getenv("RUN_MODE") == "test" {
logrus.Infof("using mock ProjectsClient")
return &ProjectsClientMock{}
}
logrus.Fatal("authzEndpoint cannot be empty or Dial will get stuck")
}
logrus.Debugf("Connecting to authz-service %q", authzEndpoint)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
conn, err := connectionFactory.DialContext(timeoutCtx, "authz-service",
authzEndpoint, grpc.WithBlock())
if err != nil {
logrus.Fatalf("getAuthzConnection, error grpc dialing to Authz %s", err.Error())
}
authzProjectsClient := iam_v2.NewProjectsClient(conn)
if authzProjectsClient == nil {
logrus.Fatalf("getAuthzConnection got nil for NewProjectsClient")
}
return authzProjectsClient
}
func getManagerConnection(connectionFactory *secureconn.Factory,
managerEndpoint string) manager.NodeManagerServiceClient {
if managerEndpoint == "" || managerEndpoint == ":0" {
if os.Getenv("RUN_MODE") == "test" {
logrus.Infof("using mock NodeManagerMock")
return &NodeManagerMock{}
}
logrus.Fatal("managerEndpoint cannot be empty or Dial will get stuck")
}
logrus.Debugf("Connecting to nodemanager-service %q", managerEndpoint)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
conn, err := connectionFactory.DialContext(timeoutCtx, "nodemanager-service",
managerEndpoint, grpc.WithBlock())
if err != nil {
logrus.Fatalf("getManagerConnection, error grpc dialing to manager %s", err.Error())
}
mgrClient := manager.NewNodeManagerServiceClient(conn)
if mgrClient == nil {
logrus.Fatalf("getManagerConnection got nil for NewNodeManagerServiceClient")
}
return mgrClient
}
func setupDataLifecycleManageableInterface(ctx context.Context, connFactory *secureconn.Factory,
conf config.Compliance) (*dlsserver.DataLifecycleManageableServer, error) {
purgePolicies := []dlsserver.PurgePolicy{}
if conf.ComplianceReportDays > 0 {
purgePolicies = append(purgePolicies, dlsserver.PurgePolicy{
IndexName: fmt.Sprintf("comp-%s-s", mappings.ComplianceCurrentTimeSeriesIndicesVersion),
PurgeOlderThanDays: conf.ComplianceReportDays,
})
purgePolicies = append(purgePolicies, dlsserver.PurgePolicy{
IndexName: fmt.Sprintf("comp-%s-r", mappings.ComplianceCurrentTimeSeriesIndicesVersion),
PurgeOlderThanDays: conf.ComplianceReportDays,
})
}
if conf.ComplianceEventDays > 0 {
purgePolicies = append(purgePolicies, dlsserver.PurgePolicy{
IndexName: fmt.Sprintf("comp-%s-feeds", mappings.ComplianceCurrentFeedsIndicesVersion),
PurgeOlderThanDays: conf.ComplianceEventDays,
IsNonTimeSeries: true,
CustomPurgeField: "pub_timestamp",
})
logrus.Debugf("feed purge config: older than %d days", conf.ComplianceEventDays)
}
var err error
var esSidecarConn *grpc.ClientConn
if os.Getenv("RUN_MODE") == "test" {
logrus.Infof(`Skipping ES sidecar-service dial due to RUN_MODE env var being set to "test"`)
} else {
timeoutCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
// Data Lifecycle Interface
logrus.Infof("Connecting to %s", conf.ESSidecarAddress)
esSidecarConn, err = connFactory.DialContext(timeoutCtx, "es-sidecar-service",
conf.ESSidecarAddress, grpc.WithBlock())
if err != nil || esSidecarConn == nil {
logrus.WithFields(logrus.Fields{"error": err}).Fatal("Failed to create ES Sidecar connection")
return nil, err
}
}
return dlsserver.NewDataLifecycleManageableServer(es_sidecar.NewEsSidecarClient(esSidecarConn), purgePolicies), nil
}
func setup(ctx context.Context, connFactory *secureconn.Factory, conf config.Compliance,
esr relaxting.ES2Backend, db *pgdb.DB) error {
var err error
var conn, mgrConn, secretsConn, authConn *grpc.ClientConn
timeoutCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
// get compliance connection for ingest
logrus.Debugf("compliance setup, dialing compliance-service manager(%s)", conf.Service.Endpoint)
conn, err = connFactory.DialContext(timeoutCtx, "compliance-service", conf.Service.Endpoint, grpc.WithBlock())
if err != nil || conn == nil {
err = errors.New("compliance setup, error grpc dialing to compliance-service...")
return err
}
// get ingest client
logrus.Debugf("compliance setup, getting an ingest client")
ingestClient := ingest.NewComplianceIngesterClient(conn)
if ingestClient == nil {
return fmt.Errorf("compliance setup, got nil for NewComplianceIngesterClient")
}
// get nodemanager connection
logrus.Debugf("compliance setup, dialing nodemanager(%s)", conf.Manager.Endpoint)
mgrConn, err = connFactory.DialContext(timeoutCtx, "nodemanager-service", conf.Manager.Endpoint, grpc.WithBlock())
if err != nil || mgrConn == nil {
err = errors.New("compliance setup, error grpc dialing to manager aborting...")
return err
}
// get nodemanager client
logrus.Debugf("compliance setup, getting a node manager client")
mgrClient := manager.NewNodeManagerServiceClient(mgrConn)
if mgrClient == nil {
return fmt.Errorf("compliance setup, got nil for NewNodeManagerServiceClient")
}
// get nodes client
logrus.Debugf("compliance setup, getting a nodes client")
nodesClient := nodes.NewNodesServiceClient(mgrConn)
if nodesClient == nil {
return fmt.Errorf("compliance setup, got nil for NewNodesServiceClient")
}
// get secrets connection
logrus.Debugf("compliance setup, dialing secrets-service(%s)", conf.Secrets.Endpoint)
secretsConn, err = connFactory.DialContext(timeoutCtx, "secrets-service", conf.Secrets.Endpoint, grpc.WithBlock())
if err != nil {
return fmt.Errorf("compliance setup, error grpc dialing to secrets")
}
// get secrets client
logrus.Debugf("compliance setup, getting a secrets service client")
secretsClient := secrets.NewSecretsServiceClient(secretsConn)
if secretsClient == nil {
return fmt.Errorf("compliance setup, could not obtain secrets service client")
}
// set up the scanner, scheduler, and runner servers with needed clients
// these are all inspec-agent packages
scannerServer := scanner.New(mgrClient, nodesClient, db)
schedulerServer := scheduler.New(mgrClient, nodesClient, db, ingestClient, secretsClient)
runnerServer := runner.New(mgrClient, nodesClient, db, ingestClient)
// start polling for jobs with a recurrence schedule that are due to run.
// this function will sleep for one minute, then query the db for all jobs
// with recurrence and check if it's time to run the job
go schedulerServer.PollForJobs(ctx)
// start the InspecAgent workers. the workers represent the amount of goroutines
// available to execute a scan. the buffer size represents the maximum amount of jobs
// that may get backed up in the agent queue
logrus.Infof("compliance service initializing %d job workers with %d job buffer size",
conf.InspecAgent.JobWorkers, conf.InspecAgent.JobBufferSize)
runnerServer.SetWorkers(conf.InspecAgent.JobWorkers, conf.InspecAgent.JobBufferSize)
if os.Getenv("RUN_MODE") == "test" {
logrus.Infof(`Skipping AUTHN client setup due to RUN_MODE env var being set to "test"`)
} else {
// get the authn-service connection
logrus.Debugf("compliance setup, dialing authn-service(%s)", conf.InspecAgent.AuthnTarget)
authConn, err = connFactory.DialContext(timeoutCtx, "authn-service", conf.InspecAgent.AuthnTarget,
grpc.WithBlock())
if err != nil || authConn == nil {
err = errors.New("compliance setup, error grpc dialing to authn aborting...")
return err
}
// get the authn client
authClient := auth.NewTokensMgmtClient(authConn)
if authClient == nil {
logrus.Errorf("serveGrpc got nil for NewTokensMgmtClient: %s", err)
return err
}
// in order to execute scan jobs remotely (i.e. on a different server, reporting back out
// to automate), we need access to the auth client for a token and the automate fqdn for reporting
remote.RemoteJobInfo = remote.RemoteJob{
TokensMgmtClient: authClient,
AutomateFQDN: conf.InspecAgent.AutomateFQDN,
}
}
SERVICE_STATE = serviceStateStarted
go checkAndRunHungJobs(ctx, scannerServer, schedulerServer)
return nil
}
func checkAndRunHungJobs(ctx context.Context, scannerServer *scanner.Scanner,
schedulerServer *scheduler.Scheduler) {
// check for 'abandoned' jobs by querying for jobs with a status of running or scheduled
// of type exec that have not been marked for deletion. jobs with a status of running will
// be marked as aborted. only jobs with a status of scheduled will be returned.
// this is done b/c the inspec-agent is ephemeral -- if a service restart occurs when the agent
// has already been given jobs, it will lose all references to those jobs
scheduledJobsIds, err := scannerServer.CheckForHungJobs(ctx)
if err != nil {
logrus.Errorf("unable to check for abandoned jobs %+v", err)
} else {
// hand over all 'abandoned' jobs with status scheduled to the scheduler
runHungJobs(ctx, scheduledJobsIds, schedulerServer)
}
}
// Serve grpc
func Serve(conf config.Compliance, grpcBinding string) error {
SERVICE_STATE = serviceStateUnknown
logging.SetLogLevel(conf.Service.LogLevel)
ctx := context.Background()
db, connFactory, esr, statusSrv, err := initBits(ctx, &conf)
if err != nil {
return err
}
SERVICE_STATE = serviceStateStarting
go serveGrpc(ctx, db, connFactory, esr, conf, grpcBinding, statusSrv) // nolint: errcheck
cfg := NewServiceConfig(&conf, connFactory)
return cfg.serveCustomRoutes()
}
// ServiceInfo holds service listen info
type ServiceInfo struct {
HostBind string
Port int
ServerBind string
connFactory *secureconn.Factory
}
//TODO(jaym) If these don't get exposed in the gateway, we need to provide the http server certs
// this custom route is used by the inspec-agent scanner to retrieve profile tars for scan execution
func (conf *ServiceInfo) serveCustomRoutes() error {
conf.ServerBind = fmt.Sprintf("%s:%d", conf.HostBind, conf.Port)
serveAddress := fmt.Sprintf("127.0.0.1:%d", 2133) // Similarly hard-coded in inspec-agent
_, cancel := context.WithCancel(context.Background())
defer cancel()
r := http.NewServeMux()
r.HandleFunc("/profiles/tar", conf.ProfileTarHandler)
return http.ListenAndServe(serveAddress, r)
}
// NewServiceConfig returns a ServiceInfo instance
func NewServiceConfig(cfg *config.Compliance, connFactory *secureconn.Factory) *ServiceInfo {
return &ServiceInfo{
HostBind: cfg.Service.HostBind,
Port: cfg.Service.Port,
connFactory: connFactory,
}
}
// ProfileTarHandler is the http handler for profile tarballs, used by the inspec-agent
// for executing scans
func (conf *ServiceInfo) ProfileTarHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
profileName := r.Form.Get("name")
profileVersion := r.Form.Get("version")
profileOwner := r.Form.Get("owner")
conn, err := conf.connFactory.Dial("compliance-service", conf.ServerBind)
if err != nil {
msg := fmt.Sprintf("grpc service unavailable %s", conf.ServerBind)
http.Error(w, msg, http.StatusServiceUnavailable)
return
}
defer conn.Close() // nolint: errcheck
profilesClient := profiles.NewProfilesServiceClient(conn)
stream, err := profilesClient.ReadTar(context.Background(),
&profiles.ProfileDetails{Name: profileName, Version: profileVersion, Owner: profileOwner})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for {
data, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
contentLength := strconv.Itoa(len(data.GetData()))
w.Header().Set("Content-Length", contentLength)
w.Header().Set("Content-Type", "application/x-gzip")
w.Header().Set("Accept-Ranges", "bytes")
w.Write(data.GetData()) // nolint: errcheck
}
}
type ProjectsClientMock struct {
}
func (pm *ProjectsClientMock) UpdateProject(ctx context.Context, in *iam_v2.UpdateProjectReq,
opts ...grpc.CallOption) (*iam_v2.UpdateProjectResp, error) {
return &iam_v2.UpdateProjectResp{}, nil
}
func (pm *ProjectsClientMock) CreateProject(ctx context.Context, in *iam_v2.CreateProjectReq,
opts ...grpc.CallOption) (*iam_v2.CreateProjectResp, error) {
return &iam_v2.CreateProjectResp{}, nil
}
func (pm *ProjectsClientMock) GetProject(ctx context.Context, in *iam_v2.GetProjectReq,
opts ...grpc.CallOption) (*iam_v2.GetProjectResp, error) {
return &iam_v2.GetProjectResp{}, nil
}
func (pm *ProjectsClientMock) DeleteProject(ctx context.Context, in *iam_v2.DeleteProjectReq,
opts ...grpc.CallOption) (*iam_v2.DeleteProjectResp, error) {
return &iam_v2.DeleteProjectResp{}, nil
}
func (pm *ProjectsClientMock) ListProjects(ctx context.Context, in *iam_v2.ListProjectsReq,
opts ...grpc.CallOption) (*iam_v2.ListProjectsResp, error) {
return &iam_v2.ListProjectsResp{}, nil
}
func (pm *ProjectsClientMock) ListProjectRules(ctx context.Context, in *iam_v2.ListProjectRulesReq,
opts ...grpc.CallOption) (*iam_v2.ProjectCollectionRulesResp, error) {
return &iam_v2.ProjectCollectionRulesResp{}, nil
}
func (pm *ProjectsClientMock) GetProjectRules(ctx context.Context, in *iam_v2.GetProjectRulesReq,
opts ...grpc.CallOption) (*iam_v2.GetProjectRulesResp, error) {
return &iam_v2.GetProjectRulesResp{}, nil
}
func (pm *ProjectsClientMock) HandleEvent(ctx context.Context, in *event.EventMsg,
opts ...grpc.CallOption) (*event.EventResponse, error) {
return &event.EventResponse{}, nil
}
func (pm *ProjectsClientMock) ProjectUpdateStatus(ctx context.Context,
req *iam_v2.ProjectUpdateStatusReq, opts ...grpc.CallOption) (*iam_v2.ProjectUpdateStatusResp, error) {
return &iam_v2.ProjectUpdateStatusResp{}, nil
}
type NotifierMock struct {
}
func (n *NotifierMock) Send(context.Context, *notifications.Event) {
}
func (n *NotifierMock) QueueSize() int {
return 0
}
type EventServiceClientMock struct {
}
func (n *EventServiceClientMock) Publish(ctx context.Context, in *event.PublishRequest,
opts ...grpc.CallOption) (*event.PublishResponse, error) {
return &event.PublishResponse{}, nil
}
func (n *EventServiceClientMock) Subscribe(ctx context.Context, in *event.SubscribeRequest,
opts ...grpc.CallOption) (*event.SubscribeResponse, error) {
return &event.SubscribeResponse{}, nil
}
func (n *EventServiceClientMock) Start(ctx context.Context, in *event.StartRequest,
opts ...grpc.CallOption) (*event.StartResponse, error) {
return &event.StartResponse{}, nil
}
func (n *EventServiceClientMock) Stop(ctx context.Context, in *event.StopRequest,
opts ...grpc.CallOption) (*event.StopResponse, error) {
return &event.StopResponse{}, nil
}
type NodeManagerMock struct {
}
func (nm *NodeManagerMock) Create(ctx context.Context, in *manager.NodeManager,
opts ...grpc.CallOption) (*manager.Ids, error) {
return &manager.Ids{}, nil
}
func (nm *NodeManagerMock) Read(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*manager.NodeManager, error) {
return &manager.NodeManager{}, nil
}
func (nm *NodeManagerMock) Update(ctx context.Context, in *manager.NodeManager,
opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func (nm *NodeManagerMock) Delete(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func (nm *NodeManagerMock) DeleteWithNodes(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*manager.Ids, error) {
return &manager.Ids{}, nil
}
func (nm *NodeManagerMock) DeleteWithNodeStateStopped(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func (nm *NodeManagerMock) DeleteWithNodeStateTerminated(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func (nm *NodeManagerMock) List(ctx context.Context, in *manager.Query,
opts ...grpc.CallOption) (*manager.NodeManagers, error) {
return &manager.NodeManagers{}, nil
}
func (nm *NodeManagerMock) Connect(ctx context.Context, in *manager.NodeManager,
opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func (nm *NodeManagerMock) ConnectManager(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
func (nm *NodeManagerMock) SearchNodeFields(ctx context.Context, in *manager.FieldQuery,
opts ...grpc.CallOption) (*manager.Fields, error) {
return &manager.Fields{}, nil
}
func (nm *NodeManagerMock) SearchNodes(ctx context.Context, in *manager.NodeQuery,
opts ...grpc.CallOption) (*manager.Nodes, error) {
return &manager.Nodes{}, nil
}
func (nm *NodeManagerMock) ProcessNode(ctx context.Context, in *manager.NodeMetadata,
opts ...grpc.CallOption) (*manager.ProcessNodeResponse, error) {
return &manager.ProcessNodeResponse{}, nil
}
func (nm *NodeManagerMock) ChangeNodeState(ctx context.Context, in *manager.NodeState,
opts ...grpc.CallOption) (*manager.ChangeNodeStateResponse, error) {
return &manager.ChangeNodeStateResponse{}, nil
}
func (nm *NodeManagerMock) GetNodeWithSecrets(ctx context.Context, in *manager.Id,
opts ...grpc.CallOption) (*nodes.Node, error) {
return &nodes.Node{}, nil
}
func (nm *NodeManagerMock) SearchManagerNodes(ctx context.Context, in *manager.NodeQuery,
opts ...grpc.CallOption) (*manager.ManagerNodes, error) {
return &manager.ManagerNodes{}, nil
}