-
Notifications
You must be signed in to change notification settings - Fork 61
/
php_packages.go
400 lines (339 loc) · 11.7 KB
/
php_packages.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
//
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
package integration
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
// PHP packages collection is a JSON string containing the detected PHP packages
// using a tool like composer
type PhpPackage struct {
Name string
Version string
}
type PhpPackagesCollection struct {
config PhpPackagesConfiguration
packages []PhpPackage
}
// PHP packages config describes how to collect the JSON for the packages installed
// for the current test case
type PhpPackagesConfiguration struct {
path string
command string
supportedListFile string
overrideVersionsFile string
expectedPackages []string
packageNameOnly []string
}
// composer package JSON
type ComposerPackage struct {
Name string
Version string
Description string
}
// composer entire JSON
type ComposerJSON struct {
Installed []ComposerPackage
}
//
// Given a JSON harvest payload, extract the PHP packages
//
// Params 1 : JSON byte string containing update_loaded_modules endpoint data
//
// Returns : []PhpPackage with extracted package info, sorted by package name
// nil upon error processing JSON
func GetPhpPackagesFromData(data []byte) ([]PhpPackage, error) {
var pkgs []PhpPackage
var x []interface{}
// extract string "Jars" and array of Php Packages JSON
err := json.Unmarshal(data, &x)
if nil != err {
return nil, err
}
if 2 != len(x) {
return nil, fmt.Errorf(("Expectd top level array of len 2"))
}
str, ok := x[0].(string)
if !ok || "Jars" != str {
return nil, fmt.Errorf(("Expected \"Jars\" string at top level"))
}
// walk through array of Php packages JSON
v2, _ := x[1].([]interface{})
for _, e1 := range v2 {
v3, _ := e1.([]interface{})
if 3 != len(v3) {
return nil, fmt.Errorf("Expected php packages json to have 3 values, has %d : %+v", len(v3), v3)
}
pkgs = append(pkgs, PhpPackage{v3[0].(string), v3[1].(string)})
}
// sort by package name to aid comparision later
sort.Slice(pkgs, func(i, j int) bool {
return pkgs[i].Name < pkgs[j].Name
})
return pkgs, nil
}
// convert PhpPackage to collector JSON representation
func (pkg *PhpPackage) CollectorJSON() ([]byte, error) {
buf := &bytes.Buffer{}
buf.WriteString(`["`)
buf.WriteString(pkg.Name)
buf.WriteString(`","`)
buf.WriteString(pkg.Version)
buf.WriteString(`",{}]`)
return buf.Bytes(), nil
}
// Create a package collection - requires config data from the EXPECT_PHP_PACKAGES stanza
func NewPhpPackagesCollection(path string, config []byte) (*PhpPackagesCollection, error) {
params := make(map[string]string)
for _, line := range bytes.Split(config, []byte("\n")) {
trimmed := bytes.TrimSpace(line)
if len(trimmed) == 0 {
continue
}
var key, value string
kv := bytes.SplitN(trimmed, []byte("="), 2)
key = string(bytes.TrimSpace(kv[0]))
if len(kv) == 2 {
value = string(bytes.TrimSpace(kv[1]))
}
if key == "" {
return nil, fmt.Errorf("invalid config format '%s'", string(config))
}
params[key] = value
}
// verify command and supported_list are defined
var supportedListFile string
var expectedPackages string
var packageNameOnly string
var overrideVersionsFile string
var expectedPackagesArr []string
var packageNameOnlyArr []string
var commandOK, supportedOK, expectedOK bool
var ok bool
var err error
command, commandOK := params["command"]
// either expect a "supported_packages" key which specifies a file listing all possible packages agent
// can detect and this is used to filter the auto-discovered packages (by integration_runner using "command")
supportedListFile, supportedOK = params["supported_packages"]
// or "expected_packages" which specifies a fixed list of packages we expect to show up in this test
expectedPackages, expectedOK = params["expected_packages"]
if expectedOK {
expectedPackagesArr, err = ParsePackagesList(expectedPackages)
if nil != err {
return nil, fmt.Errorf("Error parsing expected_packages list %s\n", err.Error())
}
}
if supportedOK && expectedOK {
return nil, fmt.Errorf("Improper EXPECT_PHP_PACKAGES config - cannot specify 'supported_packages' and 'expected packages' - got %+v", params)
}
if !supportedOK && !expectedOK {
return nil, fmt.Errorf("Improper EXPECT_PHP_PACKAGES config - must specify 'supported_packages' or 'expected packages' - got %+v", params)
}
if supportedOK && !commandOK {
return nil, fmt.Errorf("Improper EXPECT_PHP_PACKAGES config - must specify 'command' option with `supported_packages` - got %+v", params)
}
// optional option to specify which packages will only have a name because agent cannot determine the version
packageNameOnly, ok = params["package_name_only"]
if ok {
packageNameOnlyArr, err = ParsePackagesList(packageNameOnly)
if nil != err {
return nil, fmt.Errorf("Error parsing package_name_only list %s\n", err.Error())
}
}
// option file containing overrides for expected package versions
// this is useful when a package is detected as the wrong version
// because the internal mechanism the agent uses to get pacakge
// versions was not updated by the upstream maintainers properly
// on a release.
//
// this is a JSON file of the format
// {
// "<expected>": "<override",
// ...
// }
//
// such as:
// {
// "4.13.0": "4.12.0",
// "3.4.5": "3.4.4"
// }
//
// which creates overrides to version "4.13.0" to change its expecation to "4.12.0"
// and "3.4.5" to be changed to an expectation of "3.4.4"
overrideVersionsFile, ok = params["override_versions_file"]
p := &PhpPackagesCollection{
config: PhpPackagesConfiguration{
command: command,
path: path,
supportedListFile: supportedListFile,
overrideVersionsFile: overrideVersionsFile,
expectedPackages: expectedPackagesArr,
packageNameOnly: packageNameOnlyArr},
}
return p, nil
}
func LoadSupportedPackagesList(path, supportedListFile string) ([]string, error) {
jsonFile, err := os.Open(filepath.Dir(path) + "/" + supportedListFile)
if err != nil {
return nil, fmt.Errorf("error opening supported list %s!", err.Error())
} else {
defer jsonFile.Close()
}
supported_json, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("Error reading supported list %s!", err.Error())
}
var supported []string
err = json.Unmarshal([]byte(supported_json), &supported)
if nil != err {
return nil, fmt.Errorf("Error unmarshalling supported list %s\n", err.Error())
}
return supported, nil
}
// expect string containing comma separated list of package names
// returns an array of strings with all leading/trailing whitespace removed
func ParsePackagesList(expectedPackages string) ([]string, error) {
tmp := strings.ReplaceAll(expectedPackages, " ", "")
return strings.Split(tmp, ","), nil
}
func ParseOverrideVersionsFile(path, overrideVersionFile string) (map[string]interface{}, error) {
jsonFile, err := os.Open(filepath.Dir(path) + "/" + overrideVersionFile)
if err != nil {
return nil, fmt.Errorf("error opening versions override list %s!", err.Error())
} else {
defer jsonFile.Close()
}
versions_json, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("Error reading versions override list %s!", err.Error())
}
var versions_override map[string]interface{}
err = json.Unmarshal([]byte(versions_json), &versions_override)
if nil != err {
return nil, fmt.Errorf("Error unmarshalling versions override list %s\n", err.Error())
}
return versions_override, nil
}
// Returns name of versions override file (if exists)
func (pkgs *PhpPackagesCollection) OverrideVersionsFile() string {
if nil == pkgs {
return ""
}
return pkgs.config.overrideVersionsFile
}
// Detects installed PHP packages
//
// Returns : []PhpPackage with extracted package info, sorted by package name
// nil upon error processing JSON
//
// Notes : Currently only supports an application created with composer
//
func (pkgs *PhpPackagesCollection) GatherInstalledPackages() ([]PhpPackage, error) {
var err error
if nil == pkgs {
return nil, fmt.Errorf("GatherInstallPackages(): pkgs is nil")
}
var supported []string
// get list of packages we expected the agent to detect
// this can be one of 2 scenarios:
// 1) test case used the "supported_packages" option which gives a JSON file which
// lists all the packages the agent can detect
// 2) test case used the "expected_packages" options which provides a comma separated
// list of packages we expect the agent to detect
//
// Option #1 is preferable as it provides the most comprehensive view of what the agent can do.
//
// Option #2 is needed because some test cases do not exercise all the packages which are
// installed and so the agent will not detect everything for that test case run which it could
// theorectically detect if the test case used all the available packages installed.
//
// Once the list of packages the agent is expected to detect is created it is used to filter
// down the package list returned by running the "command" (usually composer) option for the
// test case provided.
if 0 < len(pkgs.config.supportedListFile) {
supported, err = LoadSupportedPackagesList(pkgs.config.path, pkgs.config.supportedListFile)
if nil != err {
return nil, err
}
} else if 0 < len(pkgs.config.expectedPackages) {
supported = pkgs.config.expectedPackages
} else {
return nil, fmt.Errorf("Error determining expected packages - supported_packages and expected_packages are both empty")
}
splitCmd := strings.Split(pkgs.config.command, " ")
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.Dir = filepath.Dir(pkgs.config.path)
cmd.Stderr = os.Stderr
var out []byte
out, err = cmd.Output()
if nil != err {
fmt.Printf("Error from output command %s\n", err.Error())
}
if "composer" == splitCmd[0] {
detected := ComposerJSON{}
json.Unmarshal([]byte(out), &detected)
for _, v := range detected.Installed {
//fmt.Printf("composer detected %s %s\n", v.Name, v.Version)
if StringSliceContains(supported, v.Name) {
var version string
// remove any 'v' from front of version string
if 0 < len(v.Version) && string(v.Version[0]) == "v" {
version = v.Version[1:]
} else {
version = v.Version
}
pkgs.packages = append(pkgs.packages, PhpPackage{v.Name, version})
//fmt.Printf(" -> %s in supported!\n", v.Name)
} else {
//fmt.Printf(" -> %s NOT in supported!\n", v.Name)
}
}
} else if 1 < len(splitCmd) && "wp-cli.phar" == splitCmd[1] {
lines := strings.Split(string(out), "\n")
version := ""
for _, line := range lines {
//fmt.Printf("line is |%s|\n", line)
splitLine := strings.Split(line, ":")
if 2 == len(splitLine) {
if "wordpress version" == strings.TrimSpace(strings.ToLower(splitLine[0])) {
version = strings.TrimSpace(splitLine[1])
//fmt.Printf("wordpress version is %s\n", version)
break
}
}
}
if 0 < len(version) {
pkgs.packages = append(pkgs.packages, PhpPackage{"wordpress", version})
}
} else {
return nil, fmt.Errorf("ERROR - unknown method '%s'\n", splitCmd[0])
}
// sort by package name to aid comparision later
sort.Slice(pkgs.packages, func(i, j int) bool {
return pkgs.packages[i].Name < pkgs.packages[j].Name
})
return pkgs.packages, nil
}
// convert PhpPackage to collector JSON representation
func (pkgs *PhpPackagesCollection) CollectorJSON() ([]byte, error) {
buf := &bytes.Buffer{}
buf.WriteString(`["Jars",[`)
for i, pkg := range pkgs.packages {
json, _ := pkg.CollectorJSON()
buf.Write(json)
if i != (len(pkgs.packages) - 1) {
buf.WriteByte(',')
}
}
buf.WriteString(`]]`)
return buf.Bytes(), nil
}