-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathupstream_recording.go
More file actions
159 lines (134 loc) · 4.12 KB
/
upstream_recording.go
File metadata and controls
159 lines (134 loc) · 4.12 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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package recording
import (
"context"
"errors"
"sync"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v12/explorer/resources"
"go.mondoo.com/cnquery/v12/llx"
"go.mondoo.com/cnquery/v12/providers-sdk/v1/inventory"
)
type Upstream struct {
ctx context.Context
service resources.ResourcesExplorer
asset *inventory.Asset
lock sync.Mutex
resourcesCache map[string]resourceCache
}
func NewUpstreamRecording(ctx context.Context, service resources.ResourcesExplorer, assetMrn string) (*Upstream, error) {
recording := &Upstream{
ctx: ctx,
service: service,
asset: &inventory.Asset{
Mrn: assetMrn,
},
resourcesCache: map[string]resourceCache{},
}
raw, ok := recording.GetResource(0, "asset", "")
if !ok {
return nil, errors.New("failed to get asset info for " + assetMrn)
}
asset := rawdata2assetinfo(raw)
asset.Mrn = assetMrn
recording.asset = asset
return recording, nil
}
func rawdata2assetinfo(fields map[string]*llx.RawData) *inventory.Asset {
ids := rawValue[[]any](fields["ids"])
sids := make([]string, len(ids))
for i := range ids {
sids[i] = ids[i].(string)
}
return &inventory.Asset{
Name: rawValue[string](fields["name"]),
PlatformIds: sids,
Platform: &inventory.Platform{
Version: rawValue[string](fields["version"]),
Arch: rawValue[string](fields["arch"]),
Title: rawValue[string](fields["title"]),
Runtime: rawValue[string](fields["runtime"]),
Kind: rawValue[string](fields["kind"]),
Name: rawValue[string](fields["platform"]),
Build: rawValue[string](fields["build"]),
},
}
}
func rawValue[T any](field *llx.RawData) T {
var empty T
if field == nil || field.Value == nil {
return empty
}
res, ok := field.Value.(T)
if !ok {
return empty
}
return res
}
type resourceCache struct {
fields map[string]*llx.RawData
err error
}
func (n *Upstream) Asset() *inventory.Asset {
return n.asset
}
func (n *Upstream) EnsureAsset(asset *inventory.Asset, provider string, connectionID uint32, conf *inventory.Config) {
// We don't sync assets with upstream, we only read at this time
}
func (n *Upstream) AddData(connectionID uint32, resource string, id string, field string, data *llx.RawData) {
// We don't store asset data this way, we use StoreResults for now.
// We will expand this method to handle more ad-hoc data methods in the
// future. (e.g. for shell)
}
func (n *Upstream) GetData(connectionID uint32, resource string, id string, field string) (*llx.RawData, bool) {
fields, ok := n.GetResource(connectionID, resource, id)
if !ok || len(fields) == 0 {
return nil, false
}
res, ok := fields[field]
return res, ok
}
func (n *Upstream) GetResource(_ uint32, resource string, id string) (map[string]*llx.RawData, bool) {
n.lock.Lock()
defer n.lock.Unlock()
cacheID := resource + "\x00" + id
if exist, ok := n.resourcesCache[cacheID]; ok {
return exist.fields, true
}
res, err := n.service.GetResourcesData(n.ctx, &resources.EntityResourcesReq{
EntityMrn: n.asset.Mrn,
Resources: []*resources.ResourceDataReq{{
Resource: resource,
Id: id,
}},
})
if err != nil {
n.resourcesCache[cacheID] = resourceCache{err: err}
return nil, false
}
if len(res.Resources) == 0 {
n.resourcesCache[cacheID] = resourceCache{}
return nil, false
} else if len(res.Resources) != 1 {
log.Warn().Str("asset", n.asset.Mrn).Str("resource", resource).Msg("too many resources returned for asset request")
}
fields := make(map[string]*llx.RawData, len(res.Resources[0].Fields))
for k, v := range res.Resources[0].Fields {
fields[k] = v.RawData()
}
n.resourcesCache[cacheID] = resourceCache{fields: fields}
return fields, true
}
func (n *Upstream) GetAssetData(assetMrn string) (map[string]*llx.ResourceRecording, bool) {
// We currently don't dump entire upstream asset data.
return nil, false
}
func (n *Upstream) GetAssetRecordings() []llx.Recording {
return nil
}
func (n *Upstream) Save() error {
// No need to save anything with upstream recordings for now. We will make
// use of this once we use AddData with upstream.
return nil
}