forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_licenses.go
196 lines (182 loc) · 8.22 KB
/
check_licenses.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
// Copyright (c) 2017, 2019 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"os"
log "github.com/sirupsen/logrus"
"bufio"
"regexp"
"strings"
"github.com/projectcalico/felix/logutils"
"github.com/projectcalico/libcalico-go/lib/set"
)
var (
whitelistedLicenses = set.From(
"Apache License 2.0",
"MIT License",
"ISC License",
"BSD 3-clause \"New\" or \"Revised\" License",
)
whitelistedPkgs = set.FromArray([]pkgInfo{
// These packages are licensed under the LGPL, which is normally viral and hence
// incompatible with our licensing! However, they include the linking exception,
// allowing us to distribute a binary based on them as long as we don't modify them.
{pkgName: "github.com/projectcalico/felix/vendor/github.com/juju/ratelimit",
license: "GNU Lesser General Public License v3.0 (94%)"},
// Variants on MIT/BSD; files tend to include updated copyright statement.
{pkgName: "github.com/projectcalico/felix/vendor/github.com/PuerkitoBio/urlesc",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/beorn7/perks/quantile",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/gobuffalo/gogen/goimports",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/gogo/protobuf",
license: "BSD 3-clause \"New\" or \"Revised\" License (90%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/golang/protobuf",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/golang/protobuf/proto",
license: "BSD 3-clause \"New\" or \"Revised\" License (92%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/gregjones/httpcache/diskcache",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/howeyc/gopass",
license: "ISC License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/karrick/godirwalk",
license: "BSD 2-clause \"Simplified\" License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/davecgh/go-spew/spew",
license: "ISC License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/dgrijalva/jwt-go",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/imdario/mergo",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/kardianos/osext",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/kelseyhightower/envconfig",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/mailru/easyjson",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/pborman/uuid",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/peterbourgon/diskv",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/pkg/errors",
license: "BSD 2-clause \"Simplified\" License"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/rogpeppe/go-internal",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/satori/go.uuid",
license: "MIT License (98%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/spf13/pflag",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/google.golang.org/grpc",
license: "BSD 3-clause \"New\" or \"Revised\" License (97%)"},
{pkgName: "github.com/projectcalico/felix/vendor/gopkg.in/inf.v0",
license: "BSD 3-clause \"New\" or \"Revised\" License (97%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/dustin/go-humanize",
license: "MIT License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/google/go-cmp/cmp/internal",
license: "BSD 3-clause \"New\" or \"Revised\" License (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/google/go-cmp/cmp/internal",
license: "BSD 3-clause \"New\" or \"Revised\" License (83%)"},
// Mixed license, Apache and some files under BSD-like.
{pkgName: "github.com/projectcalico/felix/vendor/sigs.k8s.io/yaml",
license: "? (BSD 3-clause \"New\" or \"Revised\" License, 83%)"},
// Apache license with copyright statement in file.
{pkgName: "github.com/projectcalico/felix/vendor/github.com/Azure/go-autorest",
license: "Apache License 2.0 (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/gophercloud/gophercloud",
license: "Apache License 2.0 (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/vishvananda/netlink/nl",
license: "Apache License 2.0 (96%)"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/vishvananda/netns",
license: "Apache License 2.0 (96%)"},
// Mozilla Public License. Note, would prohibit us from ever releasing our code
// under a *GPL license (if we wanted to do that).
{pkgName: "github.com/projectcalico/felix/vendor/github.com/hashicorp/golang-lru/simplelru",
license: "Mozilla Public License 2.0"},
{pkgName: "github.com/projectcalico/felix/vendor/github.com/hashicorp/go-version",
license: "Mozilla Public License 2.0"},
// Not detected properly - But it's apache license - https://github.com/go-yaml/yaml/blob/v2.2.1/LICENSE
{pkgName: "github.com/projectcalico/felix/vendor/gopkg.in/yaml.v2",
license: "? (The Unlicense, 35%)"},
})
whitelistedPrefixes = []string{
// Standard golang BSD-like license.
"github.com/projectcalico/felix/vendor/golang.org/x/",
}
)
func main() {
logutils.ConfigureEarlyLogging()
wd, _ := os.Getwd()
log.WithField("PWD", wd).Info("Current directory")
file, err := os.Open("check-licenses/dependency-licenses.txt") // For read access.
if err != nil {
log.WithError(err).Panic("Failed to open licenses file")
}
scanner := bufio.NewScanner(file)
lineRE := regexp.MustCompile(`^(\S+)\s+(\S.*)$`)
badPackages := []pkgInfo{}
lineLoop:
for scanner.Scan() {
line := scanner.Text()
logCxt := log.WithField("line", line)
submatches := lineRE.FindStringSubmatch(line)
if len(submatches) != 3 {
logCxt.Panic("Expected line to match regex")
}
pkgName := submatches[1]
license := submatches[2]
logCxt = logCxt.WithFields(log.Fields{
"pkgName": pkgName,
"license": license,
})
logCxt.Debug("Found package")
pkgInfo := pkgInfo{
pkgName: pkgName,
license: license,
}
if strings.HasPrefix(pkgName, "github.com/projectcalico/felix/vendor/github.com/projectcalico/") ||
(strings.HasPrefix(pkgName, "github.com/projectcalico/") &&
!strings.Contains(pkgName, "vendor")) {
logCxt.Info("One of our packages")
continue
}
if whitelistedLicenses.Contains(license) {
logCxt.Info("License is whitelisted")
continue
}
if whitelistedPkgs.Contains(pkgInfo) {
logCxt.Info("Package is whitelisted")
continue
}
for _, prefix := range whitelistedPrefixes {
if strings.HasPrefix(pkgName, prefix) {
logCxt.WithField("prefix", prefix).Info("Package prefix is whitelisted")
continue lineLoop
}
}
logCxt.Error("License not whitelisted")
badPackages = append(badPackages, pkgInfo)
}
if len(badPackages) > 0 {
log.Error("Found bad licenses")
for _, pkg := range badPackages {
log.Errorf("\n\nNon-white-listed license:\n Package: %v\n License: %v\n", pkg.pkgName, pkg.license)
}
log.Info("")
os.Exit(1)
}
}
type pkgInfo struct {
pkgName string
license string
}