-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathservices.go
91 lines (74 loc) · 2.37 KB
/
services.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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package policy
import (
"context"
"net/http"
"go.mondoo.com/cnquery/v11"
"go.mondoo.com/cnquery/v11/explorer/transport"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/mqlc"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/resources"
"go.mondoo.com/ranger-rpc"
)
type ResolvedPolicyVersion string
const (
V2Code ResolvedPolicyVersion = "v2"
)
var globalEmpty = &Empty{}
// Library is a subset of the DataLake focused on methods around policy and query existence
type Library interface {
// QueryExists checks if the given MRN exists
QueryExists(ctx context.Context, mrn string) (bool, error)
// PolicyExists checks if the given MRN exists
PolicyExists(ctx context.Context, mrn string) (bool, error)
}
type Services struct {
PolicyHub
PolicyResolver
}
// LocalServices is a bundle of all the services for handling policies.
// It has an optional upstream-handler embedded. If a local service does not
// yield results for a request, and the upstream handler is defined, it will
// be used instead.
type LocalServices struct {
DataLake DataLake
Upstream *Services
Incognito bool
Runtime llx.Runtime
}
// NewLocalServices initializes a reasonably configured local services struct
func NewLocalServices(datalake DataLake, uuid string, runtime llx.Runtime) *LocalServices {
return &LocalServices{
DataLake: datalake,
Upstream: nil,
Incognito: false,
Runtime: runtime,
}
}
// NewRemoteServices initializes a services struct with a remote endpoint
func NewRemoteServices(addr string, auth []ranger.ClientPlugin, httpClient *http.Client) (*Services, error) {
if httpClient == nil {
httpClient = ranger.DefaultHttpClient()
}
// restrict parallel upstream connections to two connections
httpClient.Transport = transport.NewMaxParallelConnTransport(httpClient.Transport, 2)
policyHub, err := NewPolicyHubClient(addr, httpClient, auth...)
if err != nil {
return nil, err
}
policyResolver, err := NewPolicyResolverClient(addr, httpClient, auth...)
if err != nil {
return nil, err
}
return &Services{
PolicyHub: policyHub,
PolicyResolver: policyResolver,
}, nil
}
func (l *LocalServices) Schema() resources.ResourcesSchema {
return l.Runtime.Schema()
}
func (l *LocalServices) NewCompilerConfig() mqlc.CompilerConfig {
return mqlc.NewConfig(l.Schema(), cnquery.DefaultFeatures)
}