-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
executable file
·182 lines (160 loc) · 4.52 KB
/
main.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
// Copyright (c) 2019, salesforce.com, inc.
// All rights reserved.
// SPDX-License-Identifier: Apache License 2.0
// For full license text, see the LICENSE file in the repo root
package main
import (
"archive/tar"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
type Hash struct {
Hash string
}
type File struct {
Name string
Hashes []Hash
}
type Config struct {
Path string
Image string
Mode string
Verbose bool
VeryVerbose bool
Overwrite bool
Files []File
Hashes []Hash
}
var cfgPtr *string
func main() {
cfgPtr = flag.String("cfg", "cfg.yml", "Load config from provided yaml file")
flag.Parse()
_, err := os.Stat(*cfgPtr)
var cfg Config
if err != nil {
fmt.Println("[+] Could not load config from:", *cfgPtr)
os.Exit(1)
} else {
fmt.Println("[+] Loading config: ", *cfgPtr)
cfg, err = loadConfig(*cfgPtr)
if err != nil {
fmt.Println("[ERROR] ", err)
os.Exit(1)
}
switch cfg.Mode {
case "image":
fmt.Println("[+] Analysing Image")
if len(cfg.Files) > 0 || len(cfg.Hashes) > 0 {
doImageAnalysis(cfg)
} else {
fmt.Println("[!] Please provide list of files to analyse")
os.Exit(1)
}
case "container":
fmt.Println("[+] Analysing Container")
if len(cfg.Files) > 0 || len(cfg.Hashes) > 0 {
doContainerAnalysis(cfg)
} else {
fmt.Println("[!] Please provide list of files to analyse")
os.Exit(1)
}
default:
fmt.Println("[!] Please specify a mode")
}
}
}
func doContainerAnalysis(cfg Config) {
if len(cfg.Hashes) > 0 {
inspectContainerForHashes(cfg)
}
if len(cfg.Files) > 0 {
numInstancesVerified, identifiedFiles, verifiedFiles, err := inspectContainerForFiles(cfg)
if err != nil {
fmt.Println("[ERROR] ", err)
os.Exit(1)
}
if len(cfg.Files) != len(identifiedFiles) {
numNotIdentifiedFiles := len(cfg.Files) - len(identifiedFiles)
fmt.Printf("[!] Not all components were identifed: (%d/%d)\n", numNotIdentifiedFiles, len(cfg.Files))
for _, file := range cfg.Files {
if !checkIfFileInList(file.Name, identifiedFiles) {
fmt.Println("[!] Component not identified: ", file.Name)
}
}
os.Exit(1)
} else {
fmt.Printf("[!] All components were identified: (%d/%d)\n", len(identifiedFiles), len(cfg.Files))
}
if len(cfg.Files) != len(verifiedFiles) {
fmt.Printf("[!] Not all components were verified: (%d/%d)\n", len(cfg.Files)-len(verifiedFiles), len(cfg.Files))
for _, file := range identifiedFiles {
if !checkIfFileInList(file.Name, verifiedFiles) {
fmt.Println("[!] Component not verified: ", file.Name)
}
}
os.Exit(1)
} else {
fmt.Printf("[!] All components were identified and verified: (%d/%d)\n", numInstancesVerified, len(cfg.Files))
}
}
}
func startImageAnalysis(cfg Config) (int, []File, []File, error) {
fmt.Println("[+] Docker Image Source: ", cfg.Image)
file := getOnDiskFile(cfg.Image)
defer file.Close()
var fileReader io.ReadCloser = file
tarBallReader := tar.NewReader(fileReader)
return processTar(tarBallReader, cfg)
}
func doImageAnalysis(cfg Config) {
numInstancesFound := 0
var identifiedFiles []File
var verifiedFiles []File
var err error
numInstancesFound, identifiedFiles, verifiedFiles, err = startImageAnalysis(cfg)
if err != nil {
fmt.Println("[ERROR]", err)
os.Exit(1)
}
if len(cfg.Files) > 0 {
if len(cfg.Files) != len(identifiedFiles) {
numNotIdentifiedFiles := len(cfg.Files) - len(identifiedFiles)
fmt.Printf("[!] Not all components were identifed: (%d/%d)\n", numNotIdentifiedFiles, len(cfg.Files))
for _, file := range cfg.Files {
if !checkIfFileInList(file.Name, identifiedFiles) {
fmt.Println("[!] Component not identified: ", file.Name)
}
}
os.Exit(1)
} else {
fmt.Printf("[!] All components were identified: (%d/%d)\n", len(identifiedFiles), len(cfg.Files))
}
if len(cfg.Files) != len(verifiedFiles) {
fmt.Printf("[!] Not all components were verified: (%d/%d)\n", len(cfg.Files)-len(verifiedFiles), len(cfg.Files))
for _, file := range identifiedFiles {
if !checkIfFileInList(file.Name, verifiedFiles) {
fmt.Println("[!] Component not verified: ", file.Name)
}
}
os.Exit(1)
} else {
fmt.Printf("[!] All components were identified and verified: (%d/%d)\n", numInstancesFound, len(cfg.Files))
}
}
}
func loadConfig(filename string) (Config, error) {
var config Config
source, err := ioutil.ReadFile(filename)
if err != nil {
return config, err
}
err = yaml.Unmarshal(source, &config)
if err != nil {
return config, err
}
return config, nil
}