-
Notifications
You must be signed in to change notification settings - Fork 255
/
version_checker.go
209 lines (173 loc) · 6.53 KB
/
version_checker.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
package plugin
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"strings"
"time"
"github.com/turbot/steampipe/pkg/ociinstaller"
"github.com/turbot/steampipe/pkg/ociinstaller/versionfile"
"github.com/turbot/steampipe/pkg/steampipeconfig"
"github.com/turbot/steampipe/pkg/utils"
)
// VersionCheckReport ::
type VersionCheckReport struct {
Plugin *versionfile.InstalledVersion
CheckResponse versionCheckResponsePayload
CheckRequest versionCheckRequestPayload
}
func (vr *VersionCheckReport) ShortName() string {
return fmt.Sprintf("%s/%s", vr.CheckResponse.Org, vr.CheckResponse.Name)
}
func (vr *VersionCheckReport) ShortNameWithStream() string {
// dont show stream names for latest streams
if vr.CheckResponse.Stream != "latest" {
return fmt.Sprintf("%s/%s@%s", vr.CheckResponse.Org, vr.CheckResponse.Name, vr.CheckResponse.Stream)
}
return fmt.Sprintf("%s/%s", vr.CheckResponse.Org, vr.CheckResponse.Name)
}
// VersionChecker :: wrapper struct over the plugin version check utilities
type VersionChecker struct {
pluginsToCheck []*versionfile.InstalledVersion
signature string
}
// GetUpdateReport looks up and reports the updated version of selective turbot plugins which are listed in versions.json
func GetUpdateReport(ctx context.Context, installationID string, check []*versionfile.InstalledVersion) map[string]VersionCheckReport {
versionChecker := new(VersionChecker)
versionChecker.signature = installationID
for _, c := range check {
if strings.HasPrefix(c.Name, ociinstaller.DefaultImageRepoDisplayURL) {
versionChecker.pluginsToCheck = append(versionChecker.pluginsToCheck, c)
}
}
return versionChecker.reportPluginUpdates(ctx)
}
// GetAllUpdateReport looks up and reports the updated version of all turbot plugins which are listed in versions.json
func GetAllUpdateReport(ctx context.Context, installationID string) map[string]VersionCheckReport {
versionChecker := new(VersionChecker)
versionChecker.signature = installationID
versionChecker.pluginsToCheck = []*versionfile.InstalledVersion{}
// retrieve the plugin version data from steampipe config
pluginVersions := steampipeconfig.GlobalConfig.PluginVersions
for _, p := range pluginVersions {
if strings.HasPrefix(p.Name, ociinstaller.DefaultImageRepoDisplayURL) {
versionChecker.pluginsToCheck = append(versionChecker.pluginsToCheck, p)
}
}
return versionChecker.reportPluginUpdates(ctx)
}
func (v *VersionChecker) reportPluginUpdates(ctx context.Context) map[string]VersionCheckReport {
// retrieve the plugin version data from steampipe config
versionFileData, err := versionfile.LoadPluginVersionFile(ctx)
if err != nil {
log.Println("[TRACE]", "CheckAndReportPluginUpdates", "could not load versionfile")
return nil
}
if len(v.pluginsToCheck) == 0 {
// there's no plugin installed. no point continuing
return nil
}
reports := v.getLatestVersionsForPlugins(ctx, v.pluginsToCheck)
// remove elements from `reports` which have empty strings in CheckResponse
// this happens if we have sent a plugin to the API which doesn't exist
// in the registry
for key, value := range reports {
if value.CheckResponse.Name == "" {
// delete this key
delete(reports, key)
}
}
// update the version file
for _, plugin := range v.pluginsToCheck {
versionFileData.Plugins[plugin.Name].LastCheckedDate = versionfile.FormatTime(time.Now())
}
if err = versionFileData.Save(); err != nil {
log.Println("[TRACE]", "CheckAndReportPluginUpdates", "could not save versionfile")
return nil
}
return reports
}
func (v *VersionChecker) getLatestVersionsForPlugins(ctx context.Context, plugins []*versionfile.InstalledVersion) map[string]VersionCheckReport {
getMapKey := func(thisPayload versionCheckRequestPayload) string {
return fmt.Sprintf("%s/%s/%s", thisPayload.Org, thisPayload.Name, thisPayload.Stream)
}
var requestPayload []versionCheckRequestPayload
reports := map[string]VersionCheckReport{}
for _, ref := range plugins {
thisPayload := v.getPayloadFromInstalledData(ref)
requestPayload = append(requestPayload, thisPayload)
reports[getMapKey(thisPayload)] = VersionCheckReport{
Plugin: ref,
CheckRequest: thisPayload,
CheckResponse: versionCheckResponsePayload{},
}
}
serverResponse, err := v.requestServerForLatest(ctx, requestPayload)
if err != nil {
log.Printf("[TRACE] PluginVersionChecker getLatestVersionsForPlugins returned error: %s", err.Error())
// return a blank map
return map[string]VersionCheckReport{}
}
for _, pluginResponseData := range serverResponse {
r := reports[pluginResponseData.getMapKey()]
r.CheckResponse = pluginResponseData
reports[pluginResponseData.getMapKey()] = r
}
return reports
}
func (v *VersionChecker) getPayloadFromInstalledData(plugin *versionfile.InstalledVersion) versionCheckRequestPayload {
ref := ociinstaller.NewSteampipeImageRef(plugin.Name)
org, name, stream := ref.GetOrgNameAndStream()
payload := versionCheckRequestPayload{
Org: org,
Name: name,
Stream: stream,
Version: plugin.Version,
Digest: plugin.ImageDigest,
}
// if Digest field is missing, populate with dummy field
// - this will force and update an in doing so fix the versions.json
if payload.Digest == "" {
payload.Digest = "no digest"
}
return payload
}
func (v *VersionChecker) getVersionCheckURL() url.URL {
var u url.URL
u.Scheme = "https"
u.Host = "hub.steampipe.io"
u.Path = "api/plugin/version"
return u
}
func (v *VersionChecker) requestServerForLatest(ctx context.Context, payload []versionCheckRequestPayload) ([]versionCheckResponsePayload, error) {
// Set a default timeout of 3 sec for the check request (in milliseconds)
sendRequestTo := v.getVersionCheckURL()
requestBody := utils.BuildRequestPayload(v.signature, map[string]interface{}{
"plugins": payload,
})
resp, err := utils.SendRequest(ctx, v.signature, "POST", sendRequestTo, requestBody)
if err != nil {
log.Printf("[TRACE] Could not send request")
return nil, err
}
if resp.StatusCode != 200 {
log.Printf("[TRACE] Unknown response during version check: %d\n", resp.StatusCode)
return nil, fmt.Errorf("requestServerForLatest failed - SendRequest returned %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("[TRACE] Error reading body stream: %v", err)
return nil, err
}
defer resp.Body.Close()
var responseData []versionCheckResponsePayload
err = json.Unmarshal(bodyBytes, &responseData)
if err != nil {
log.Println("[TRACE] Error in unmarshalling plugin update response", err)
return nil, err
}
return responseData, nil
}