forked from ericrpowers/trireme
/
external.go
55 lines (44 loc) · 1.55 KB
/
external.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
package cliextractor
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"github.com/docker/docker/api/types"
"github.com/headingy/trireme/monitor/dockermonitor"
"github.com/headingy/trireme/policy"
)
// NewExternalExtractor returns a new bash metadata extractor for Docker that will call
// the executable given in parameter and will generate a Policy Runtime as standard output
// The format of Input/Output of the executable are in standard JSON.
func NewExternalExtractor(filePath string) (dockermonitor.DockerMetadataExtractor, error) {
if filePath == "" {
return nil, fmt.Errorf("file argument is empty in NewBashExtractor")
}
path, err := exec.LookPath(filePath)
if err != nil {
return nil, fmt.Errorf("Exec file was not found at filePath %s: %s", filePath, err)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, fmt.Errorf("Exec file was not found at filePath %s: %s", filePath, err)
}
// Generate a new function
externalExtractor := func(dockerInfo *types.ContainerJSON) (*policy.PURuntime, error) {
dockerInfoJSON, err := json.Marshal(dockerInfo)
if err != nil {
return nil, fmt.Errorf("Error marshaling dockerInfo: %s", err)
}
cmd := exec.Command(path, string(dockerInfoJSON))
jsonResult, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("Error running bash extractor: %s", err)
}
var m policy.PURuntime
err = json.Unmarshal(jsonResult, &m)
if err != nil {
return nil, fmt.Errorf("Error Unmarshaling return from bash extractor: %s", err)
}
return &m, nil
}
return externalExtractor, nil
}