forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-verifier.go
359 lines (321 loc) · 11.1 KB
/
import-verifier.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
const (
rootPackage = "github.com/openshift/origin"
)
// Package is a subset of cmd/go.Package
type Package struct {
ImportPath string `json:",omitempty"` // import path of package in dir
Imports []string `json:",omitempty"` // import paths used by this package
TestImports []string `json:",omitempty"` // imports from TestGoFiles
XTestImports []string `json:",omitempty"` // imports from XTestGoFiles
}
type ImportRestriction struct {
// CheckedPackageRoots are the roots of the package tree
// that are restricted by this configuration
CheckedPackageRoots []string `json:"checkedPackageRoots"`
// CheckedPackages are the specific packages
// that are restricted by this configuration
CheckedPackages []string `json:"checkedPackages"`
// IgnoredSubTrees are roots of sub-trees of the
// BaseImportPath for which we do not want to enforce
// any import restrictions whatsoever
IgnoredSubTrees []string `json:"ignoredSubTrees,omitempty"`
// AllowedImportPackages are roots of package trees that
// are allowed to be imported for this restriction
AllowedImportPackages []string `json:"allowedImportPackages"`
// AllowedImportPackageRoots are roots of package trees that
// are allowed to be imported for this restriction
AllowedImportPackageRoots []string `json:"allowedImportPackageRoots"`
// ForbiddenImportPackageRoots are roots of package trees that
// are NOT allowed to be imported for this restriction
ForbiddenImportPackageRoots []string `json:"forbiddenImportPackageRoots"`
}
// ForbiddenImportsFor determines all of the forbidden
// imports for a package given the import restrictions
func (i *ImportRestriction) ForbiddenImportsFor(pkg Package) []string {
if !i.isRestrictedPath(pkg.ImportPath) {
return []string{}
}
return i.forbiddenImportsFor(pkg)
}
// isRestrictedPath determines if the import path has
// any restrictions placed on it by this configuration.
// A path will be restricted if:
// - it falls under the base import path
// - it does not fall under any of the ignored sub-trees
func (i *ImportRestriction) isRestrictedPath(packageToCheck string) bool {
// if its not under our root, then its a built-in. Everything else is under
// github.com/openshift/origin or github.com/openshift/origin/vendor
if !strings.HasPrefix(packageToCheck, rootPackage) {
return false
}
// some subtrees are specifically excluded. Not sure if we still need this given
// explicit inclusion
for _, ignored := range i.IgnoredSubTrees {
if strings.HasPrefix(packageToCheck, ignored) {
return false
}
}
return true
}
// forbiddenImportsFor determines all of the forbidden
// imports for a package given the import restrictions
// and returns a deduplicated list of them
func (i *ImportRestriction) forbiddenImportsFor(pkg Package) []string {
forbiddenImportSet := map[string]struct{}{}
for _, packageToCheck := range append(pkg.Imports, append(pkg.TestImports, pkg.XTestImports...)...) {
if !i.isAllowed(packageToCheck) {
forbiddenImportSet[relativePackage(packageToCheck)] = struct{}{}
}
}
var forbiddenImports []string
for imp := range forbiddenImportSet {
forbiddenImports = append(forbiddenImports, imp)
}
return forbiddenImports
}
// isForbidden determines if an import is forbidden,
// which is true when the import is:
// - of a package under the rootPackage
// - is not of the base import path or a sub-package of it
// - is not of an allowed path or a sub-package of one
func (i *ImportRestriction) isAllowed(packageToCheck string) bool {
// if its not under our root, then its a built-in. Everything else is under
// github.com/openshift/origin or github.com/openshift/origin/vendor
if !strings.HasPrefix(packageToCheck, rootPackage) {
return true
}
if i.isIncludedInRestrictedPackages(packageToCheck) {
return true
}
for _, forbiddenPackageRoot := range i.ForbiddenImportPackageRoots {
if strings.HasPrefix(forbiddenPackageRoot, "vendor") {
forbiddenPackageRoot = rootPackage + "/" + forbiddenPackageRoot
}
if strings.HasPrefix(packageToCheck, forbiddenPackageRoot) {
return false
}
}
for _, allowedPackage := range i.AllowedImportPackages {
if strings.HasPrefix(allowedPackage, "vendor") {
allowedPackage = rootPackage + "/" + allowedPackage
}
if packageToCheck == allowedPackage {
return true
}
}
for _, allowedPackageRoot := range i.AllowedImportPackageRoots {
if strings.HasPrefix(allowedPackageRoot, "vendor") {
allowedPackageRoot = rootPackage + "/" + allowedPackageRoot
}
if strings.HasPrefix(packageToCheck, allowedPackageRoot) {
return true
}
}
return false
}
// isIncludedInRestrictedPackages checks to see if a package is included in the list of packages we're
// restricting. Any package being restricted is assumed to be allowed to import another package being
// restricted since they are grouped
func (i *ImportRestriction) isIncludedInRestrictedPackages(packageToCheck string) bool {
// some subtrees are specifically excluded. Not sure if we still need this given
// explicit inclusion
for _, ignored := range i.IgnoredSubTrees {
if strings.HasPrefix(packageToCheck, ignored) {
return false
}
}
for _, currBase := range i.CheckedPackageRoots {
if strings.HasPrefix(packageToCheck, currBase) {
return true
}
}
for _, currPackageName := range i.CheckedPackages {
if currPackageName == packageToCheck {
return true
}
}
return false
}
func relativePackage(absolutePackage string) string {
if strings.HasPrefix(absolutePackage, rootPackage+"/vendor") {
return absolutePackage[len(rootPackage)+1:]
}
return absolutePackage
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("%s requires the configuration file as it's only argument", os.Args[0])
}
configFile := os.Args[1]
importRestrictions, err := loadImportRestrictions(configFile)
if err != nil {
log.Fatalf("Failed to load import restrictions: %v", err)
}
failedRestrictionCheck := false
for _, restriction := range importRestrictions {
packages := []Package{}
for _, currBase := range restriction.CheckedPackageRoots {
log.Printf("Inspecting imports under %s...\n", currBase)
currPackages, err := resolvePackage(currBase + "/...")
if err != nil {
log.Fatalf("Failed to resolve package tree %v: %v", currBase, err)
}
packages = mergePackages(packages, currPackages)
}
for _, currPackageName := range restriction.CheckedPackages {
log.Printf("Inspecting imports at %s...\n", currPackageName)
currPackages, err := resolvePackage(currPackageName)
if err != nil {
log.Fatalf("Failed to resolve package %v: %v", currPackageName, err)
}
packages = mergePackages(packages, currPackages)
}
if len(packages) == 0 {
log.Fatalf("No packages found")
}
log.Printf("-- validating imports for %d packages in the tree", len(packages))
for _, pkg := range packages {
if forbidden := restriction.ForbiddenImportsFor(pkg); len(forbidden) != 0 {
logForbiddenPackages(relativePackage(pkg.ImportPath), forbidden)
failedRestrictionCheck = true
}
}
// make sure that all the allowed imports are used
if unused := unusedPackageImports(restriction.AllowedImportPackages, packages); len(unused) > 0 {
log.Printf("-- found unused package imports\n")
for _, unusedPackage := range unused {
log.Printf("\t%s\n", unusedPackage)
}
failedRestrictionCheck = true
}
if unused := unusedPackageImportRoots(restriction.AllowedImportPackageRoots, packages); len(unused) > 0 {
log.Printf("-- found unused package import roots\n")
for _, unusedPackage := range unused {
log.Printf("\t%s\n", unusedPackage)
}
failedRestrictionCheck = true
}
log.Printf("\n")
}
if failedRestrictionCheck {
os.Exit(1)
}
}
func unusedPackageImports(allowedPackageImports []string, packages []Package) []string {
ret := []string{}
for _, allowedImport := range allowedPackageImports {
if strings.HasPrefix(allowedImport, "vendor") {
allowedImport = rootPackage + "/" + allowedImport
}
found := false
for _, pkg := range packages {
for _, packageToCheck := range append(pkg.Imports, append(pkg.TestImports, pkg.XTestImports...)...) {
if packageToCheck == allowedImport {
found = true
break
}
}
}
if !found {
ret = append(ret, relativePackage(allowedImport))
}
}
return ret
}
func unusedPackageImportRoots(allowedPackageImportRoots []string, packages []Package) []string {
ret := []string{}
for _, allowedImportRoot := range allowedPackageImportRoots {
if strings.HasPrefix(allowedImportRoot, "vendor") {
allowedImportRoot = rootPackage + "/" + allowedImportRoot
}
found := false
for _, pkg := range packages {
for _, packageToCheck := range append(pkg.Imports, append(pkg.TestImports, pkg.XTestImports...)...) {
if strings.HasPrefix(packageToCheck, allowedImportRoot) {
found = true
break
}
}
}
if !found {
ret = append(ret, relativePackage(allowedImportRoot))
}
}
return ret
}
func mergePackages(existingPackages, currPackages []Package) []Package {
for _, currPackage := range currPackages {
found := false
for _, existingPackage := range existingPackages {
if existingPackage.ImportPath == currPackage.ImportPath {
log.Printf("-- Skipping: %v", currPackage.ImportPath)
found = true
}
}
if !found {
// this was super noisy.
//log.Printf("-- Adding: %v", currPackage.ImportPath)
existingPackages = append(existingPackages, currPackage)
}
}
return existingPackages
}
func loadImportRestrictions(configFile string) ([]ImportRestriction, error) {
config, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to load configuration from %s: %v", configFile, err)
}
var importRestrictions []ImportRestriction
if err := json.Unmarshal(config, &importRestrictions); err != nil {
return nil, fmt.Errorf("failed to unmarshal from %s: %v", configFile, err)
}
return importRestrictions, nil
}
func resolvePackage(targetPackage string) ([]Package, error) {
cmd := "go"
args := []string{"list", "-json", targetPackage}
stdout, err := exec.Command(cmd, args...).Output()
if err != nil {
return nil, fmt.Errorf("Failed to run `%s %s`: %v\n", cmd, strings.Join(args, " "), err)
}
packages, err := decodePackages(bytes.NewReader(stdout))
if err != nil {
return nil, fmt.Errorf("Failed to decode packages: %v", err)
}
return packages, nil
}
func decodePackages(r io.Reader) ([]Package, error) {
// `go list -json` concatenates package definitions
// instead of emitting a single valid JSON, so we
// need to stream the output to decode it into the
// data we are looking for instead of just using a
// simple JSON decoder on stdout
var packages []Package
decoder := json.NewDecoder(r)
for decoder.More() {
var pkg Package
if err := decoder.Decode(&pkg); err != nil {
return nil, fmt.Errorf("invalid package: %v", err)
}
packages = append(packages, pkg)
}
return packages, nil
}
func logForbiddenPackages(base string, forbidden []string) {
log.Printf("-- found forbidden imports for %s:\n", base)
for _, forbiddenPackage := range forbidden {
log.Printf("\t%s\n", forbiddenPackage)
}
}