This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
/
main.go
221 lines (209 loc) · 5.87 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
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
// Copyright 2021 VMware Tanzu Community Edition contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
_ "embed"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/rs/xid"
imgwrapper "github.com/vmware-tanzu/community-edition/hack/imagelinter/pkg/imagewrapper"
imglint "github.com/vmware-tanzu/community-edition/hack/imagelinter/pkg/lint"
)
var (
//go:embed config/imagelintconfig.yaml
data string
pathFlag, configPathFlag, detailedSummary *string
showSumary *bool
counter int
isFatal bool
imc *imglint.ImageLintConfig
)
func init() {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
pathFlag = flag.String("path", wd, "path to be provided") // default is current working directory
configPathFlag = flag.String("config", "", "path for the configuration file to be provided") // default config is the config.json file that is there in the imagelint path
showSumary = flag.Bool("summary", false, "to get summary pass summary=true;to off either dont pass or summary=false")
detailedSummary = flag.String("details", "Fail", "detailed summary can be Fail,Pass,Not-Identified or Pull-Failed")
flag.Parse()
if *configPathFlag == "" {
imc, err = imglint.NewFromContent([]byte(data))
if err != nil {
log.Fatal(err)
}
} else {
imc, err = imglint.New(*configPathFlag)
if err != nil {
log.Fatal(err)
}
}
err = imc.Init(*pathFlag)
if err != nil {
log.Fatal(err)
}
fmt.Println("User given Path", *pathFlag)
fmt.Println("Total number of images to process:", len(imc.ImageMap))
counter = 0
isFatal = false
}
func main() {
for key := range imc.ImageMap {
counter++
fmt.Println("Currently processing", counter, " out of ", len(imc.ImageMap), "image(s)")
containerName := xid.New().String()
wrapper, err := imgwrapper.New(strings.Trim(key, " "), containerName, nil)
if err != nil {
log.Fatalln(err)
}
cont, err := LintAll(imc, wrapper, key)
if err != nil {
fmt.Println(err)
}
if cont {
continue
}
imc.OnEvent("Not Identified", "Cound not find Container OS", key)
_, err = wrapper.DeleteContainer()
if err != nil {
fmt.Println("Error deleting container:", err)
}
}
imc.ShowDetailedSummary(*detailedSummary)
if *showSumary {
imc.ShowOverallSummary()
fmt.Println()
}
if isFatal {
os.Exit(1)
}
}
func IsAlpine(imc *imglint.ImageLintConfig, wrapper *imgwrapper.Wrapper, key string) (fatal, cont bool, err error) {
osdata, err := os.ReadFile("os-release")
if err == nil {
err = os.Remove("os-release")
if err != nil {
return false, false, err
}
if strings.Contains(string(osdata), "Alpine") {
imc.OnEvent("Fail", "Alpine Image", key)
_, err = wrapper.DeleteContainer()
if err != nil {
return true, false, err
}
return true, true, nil
}
imc.OnEvent("Pass", "Not an Alpine image", key)
_, err = wrapper.DeleteContainer()
if err != nil {
fmt.Println("Error deleting container:", err)
}
return false, true, nil
}
return false, false, nil
}
func HasLicense(imc *imglint.ImageLintConfig, wrapper *imgwrapper.Wrapper, key string) (cont bool, err error) {
data, err := os.ReadFile("LICENSE")
if err == nil && len(data) > 0 {
err = os.Remove("LICENSE")
if err != nil {
_, err = wrapper.DeleteContainer()
if err != nil {
return false, err
}
return false, err
}
if strings.Contains(string(data), "Apache License") {
imc.OnEvent("Pass", "Valid license file found", key)
_, err = wrapper.DeleteContainer()
if err != nil {
return true, err
}
return true, nil
}
}
_, err = wrapper.DeleteContainer()
if err != nil {
return false, err
}
return false, nil
}
func IsBusyBox(imc *imglint.ImageLintConfig, wrapper *imgwrapper.Wrapper, key string) (cont bool, err error) {
result, err := wrapper.RunCommand("logs", wrapper.Container+"2")
if err != nil {
return false, err
}
_, err = wrapper.RunCommand("rm", "-f", wrapper.Container+"2")
if err != nil {
return false, err
}
if strings.Contains(strings.ToUpper(result), "BUSYBOX") && err == nil {
imc.OnEvent("Pass", "According to the containr bins, this is not an Alpine Image;Its busybox", key)
return true, nil
}
return false, err
}
func InitialChecks(imc *imglint.ImageLintConfig, wrapper *imgwrapper.Wrapper, key string) (cont bool, err error) {
_, err = wrapper.PullImage()
if err != nil {
imc.OnEvent("Pull Failed", "Pulling Image Failed:"+err.Error(), key)
return true, err
}
skip, err := wrapper.Validate(imc.SuccessValidators)
if skip && err == nil {
imc.OnEvent("Pass", "According to the image history, this is not an Alpine Image", key)
return true, nil
}
return false, nil
}
func LintAll(imc *imglint.ImageLintConfig, wrapper *imgwrapper.Wrapper, key string) (bool, error) {
cont, err := InitialChecks(imc, wrapper, key)
if cont {
return true, err
}
_, err = wrapper.CreateContainer()
if err != nil {
return false, err
}
if wrapper.IsContainerExists() {
_, err := wrapper.ContainerCP("/etc/os-release", "./")
if err == nil {
fatal, cont, err := IsAlpine(imc, wrapper, key)
if fatal {
isFatal = fatal
}
if cont {
return true, err
}
}
_, err = wrapper.ContainerCP("/usr/lib/os-release", "./")
if err == nil {
fatal, cont, err := IsAlpine(imc, wrapper, key)
if fatal {
isFatal = fatal
}
if cont {
return true, err
}
}
_, err = wrapper.ContainerCP("/licenses/LICENSE", "./")
if err == nil {
cont, err := HasLicense(imc, wrapper, key)
if cont {
return true, err
}
}
_, err = wrapper.RunCommand("run", `--entrypoint=/bin/busybox`, "--name", wrapper.Container+"a", key)
if err == nil {
cont, err := IsBusyBox(imc, wrapper, key)
if cont {
return true, err
}
}
}
return false, nil
}