license_gate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE +21 -0
- data/README.md +198 -0
- data/exe/license_gate +5 -0
- data/lib/license_gate/check.rb +72 -0
- data/lib/license_gate/cli.rb +110 -0
- data/lib/license_gate/configuration.rb +152 -0
- data/lib/license_gate/ecosystem.rb +8 -0
- data/lib/license_gate/license_string.rb +64 -0
- data/lib/license_gate/package.rb +17 -0
- data/lib/license_gate/sources/bundler.rb +28 -0
- data/lib/license_gate/sources/pnpm.rb +71 -0
- data/lib/license_gate/version.rb +5 -0
- data/lib/license_gate.rb +26 -0
- metadata +60 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0de94efe71cb1b283e4e299fe0e93946e91b36cbeb3b1ba79fbbf135bd01b094
|
|
4
|
+
data.tar.gz: dda7059623164092f2d80d57cd248721f1ede6812c29682ca137cc6ad3e734ba
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0c6781af42aa380a30ece58e579f63ccc34003ef43f7dade068b7715090b27ce44531599b05a8b1d51976c431d9e3454b8372741dd9bc0e92908199fe6b5ab20
|
|
7
|
+
data.tar.gz: 4a220138b187873decf0d9f6e2757ee03158826da5222609163cf3b0c2afee76c72a793f925bc0f28d4140459da0aa78fab541718d941e05dcbd0b7599f6186a
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
## [0.1.0] - 2026-11-18
|
|
2
|
+
|
|
3
|
+
- Checks the licenses of Bundler gems and pnpm packages.
|
|
4
|
+
- Reads configuration from `config/license_gate.yml` by default.
|
|
5
|
+
- Allows specifying configuration file location through `--config` CLI option.
|
|
6
|
+
- Lists all packages with their licenses through `--report` CLI option.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arne Hartherz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# License Gate
|
|
2
|
+
|
|
3
|
+
`license_gate` checks licenses of your project's dependencies against a configuration in your repository.
|
|
4
|
+
It covers gems of your [Bundler](https://bundler.io/) Gemfile and npm packages installed through [pnpm](https://pnpm.io/).
|
|
5
|
+
|
|
6
|
+
License Gate works with an allowlist: any license your configuration does not permit fails the check.
|
|
7
|
+
If you accept some licenses only for specific packages, those can be specified as well.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Add the gem to your Gemfile, e.g. in development and test groups:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
group :development, :test do
|
|
16
|
+
gem 'license_gate'
|
|
17
|
+
end
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Run the CLI command from your project:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
bundle exec license_gate
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
It always checks your gems, and npm packages when there is a `pnpm-lock.yaml`.
|
|
30
|
+
When packages use licenses that are not permitted, it lists them and fails:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
$ bundle exec license_gate
|
|
34
|
+
✗ gem some-gem 1.2.0: GPL-3.0-only
|
|
35
|
+
✗ npm some-package 0.4.0: Unknown (no license found)
|
|
36
|
+
|
|
37
|
+
2 of 123 packages use licenses that are not permitted.
|
|
38
|
+
Permit a license in config/license_gate.yml, or add a license exception with a reason.
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
When all licenses are permitted, it prints `All 123 packages use permitted licenses (23 gems, 100 npm packages).`
|
|
42
|
+
|
|
43
|
+
Configuration is read from `config/license_gate.yml`, or from a file passed as `--config PATH`.
|
|
44
|
+
A configuration file must exist in your project. See below for an example.
|
|
45
|
+
|
|
46
|
+
Run License Gate in your CI pipeline or as part of your test suite to avoid adding dependencies with problematic licenses.
|
|
47
|
+
|
|
48
|
+
To list all packages with their licenses, run `license_gate --report`:
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
✓ gem actioncable 8.1.4: MIT
|
|
52
|
+
✗ gem some-gem 1.2.0: GPL-3.0-only
|
|
53
|
+
✓ npm left-pad 1.3.0: MIT
|
|
54
|
+
…
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## Example configuration
|
|
59
|
+
|
|
60
|
+
To get you started, here is an example `config/license_gate.yml`:
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
# Licenses that any dependency may use.
|
|
64
|
+
# Case is ignored, and spaces match dashes, e.g. `bsd 2-Clause` works for `BSD-2-Clause`.
|
|
65
|
+
permitted_licenses:
|
|
66
|
+
MIT: Permissive; only requires keeping the copyright notice
|
|
67
|
+
ISC: Permissive; equivalent to MIT
|
|
68
|
+
Apache-2.0: Permissive; requires keeping notices and includes a patent grant
|
|
69
|
+
BSD-2-Clause: Permissive; only requires keeping the copyright notice
|
|
70
|
+
BSD-3-Clause: Permissive; like BSD-2-Clause, but forbids promoting with the authors' names
|
|
71
|
+
0BSD: Permissive without any conditions
|
|
72
|
+
Ruby: License of Ruby and its default gems (like json); permissive, or BSD-2-Clause terms
|
|
73
|
+
|
|
74
|
+
# Licenses you do not permit globally, but accept for specific packages.
|
|
75
|
+
# Specify each exception explicitly.
|
|
76
|
+
# license_exceptions:
|
|
77
|
+
# - gems: [prawn, pdf-core, ttfunk]
|
|
78
|
+
# licenses: [Nonstandard]
|
|
79
|
+
# why: Prawn offers its own variant of the permissive Ruby License (besides GPL)
|
|
80
|
+
# - npm_packages: [some-old-library]
|
|
81
|
+
# licenses: [Unknown] # declares no license at all
|
|
82
|
+
# why: Its package.json lacks a license, but LICENSE file says MIT
|
|
83
|
+
|
|
84
|
+
# Dependency groups to skip. By default, everything is checked.
|
|
85
|
+
# ignored_groups:
|
|
86
|
+
# bundler: [development, test]
|
|
87
|
+
# npm: [devDependencies]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
Licenses are named as your package managers report them, which is usually an [SPDX identifier](https://spdx.org/licenses/).
|
|
94
|
+
Unlisted licenses are considered not permitted.
|
|
95
|
+
|
|
96
|
+
### `permitted_licenses`
|
|
97
|
+
|
|
98
|
+
Maps licenses that any dependency may use to a reason why they are allowed.
|
|
99
|
+
A reason is recommended, but optional: use `true` or leave the value empty to permit a license without one.
|
|
100
|
+
|
|
101
|
+
### `license_exceptions`
|
|
102
|
+
|
|
103
|
+
Use this to specify a list of packages which may use licenses that are not generally permitted.
|
|
104
|
+
An entry has the following keys.
|
|
105
|
+
- `gems` and/or `npm_packages`: names of libraries
|
|
106
|
+
- `licenses`: licenses permitted for these packages
|
|
107
|
+
- `why`: reason these licenses are acceptable (optional, but recommended)
|
|
108
|
+
|
|
109
|
+
An exception specifies licenses, not only packages.
|
|
110
|
+
If a package changes to a license that isn't otherwise permitted, License Gate will report it.
|
|
111
|
+
|
|
112
|
+
### `ignored_groups`
|
|
113
|
+
|
|
114
|
+
Skips Bundler groups (`bundler`) and npm dependency types (`npm`).
|
|
115
|
+
For npm, `devDependencies` and `optionalDependencies` can be skipped.
|
|
116
|
+
|
|
117
|
+
Both keys default to `[]`, i.e. nothing is skipped.
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
## How licenses are matched
|
|
121
|
+
|
|
122
|
+
- License names are compared ignoring case, with spaces and dashes being interchangeable; e.g. `BSD 2-Clause` matches `bsd-2-clause`.
|
|
123
|
+
- A gem may declare several licenses to choose from. When any of them is permitted, it passes.
|
|
124
|
+
- An npm package declares one license entry which may be an expression combining several licenses.
|
|
125
|
+
- `(MIT OR Apache-2.0)` passes when one of them is permitted.
|
|
126
|
+
- `(Apache-2.0 AND BSD-3-Clause)` requires all to be permitted.
|
|
127
|
+
- Note that only uppercase `OR` and `AND` operators work.
|
|
128
|
+
- A package without any license counts as `Unknown`.
|
|
129
|
+
If you encounter such a package, and its license is actually acceptable, we recommend specifying a `license_exceptions` entry for only that package.
|
|
130
|
+
|
|
131
|
+
### Known limitations
|
|
132
|
+
|
|
133
|
+
- Expressions mixing `OR` and `AND`, e.g. `(MIT AND Zlib) OR Apache-2.0`, or with nested parentheses, are not interpreted and will fail.
|
|
134
|
+
You may permit any such string in full.
|
|
135
|
+
- `WITH` clauses are not interpreted, e.g. `Apache-2.0 WITH LLVM-exception` counts as a license of its own.
|
|
136
|
+
- A gem's licenses are treated as alternatives.
|
|
137
|
+
Some gems list licenses that apply to different parts instead, e.g. MIT code with Apache-licensed data; License Gate cannot tell these apart.
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
## Supported package managers
|
|
141
|
+
|
|
142
|
+
### Bundler
|
|
143
|
+
|
|
144
|
+
Reads the Gemfile Bundler uses (`Gemfile`, `gems.rb` or `BUNDLE_GEMFILE`) and its lockfile.
|
|
145
|
+
Gems must be installed.
|
|
146
|
+
If some groups are not installed (through `BUNDLE_WITHOUT`, or `optional: true` groups), install them for the check, or list them in `ignored_groups.bundler`.
|
|
147
|
+
|
|
148
|
+
### pnpm
|
|
149
|
+
|
|
150
|
+
When your project root has a `pnpm-lock.yaml`, licenses of all workspace projects are checked through `pnpm licenses list`.
|
|
151
|
+
Packages must be installed (`node_modules`).
|
|
152
|
+
|
|
153
|
+
Requires pnpm 9 or later; tested with pnpm 9, 10, 11 and 12.
|
|
154
|
+
|
|
155
|
+
> [!WARNING]
|
|
156
|
+
> Older pnpm 11 and 12 releases report every license as `Unknown` with `nodeLinker: hoisted`.
|
|
157
|
+
> This is fixed in pnpm 11.28 and 12.7.
|
|
158
|
+
|
|
159
|
+
### Currently unsupported
|
|
160
|
+
|
|
161
|
+
License Gate does not yet support plain npm, Yarn, or other package managers.
|
|
162
|
+
Contributions adding them are welcome.
|
|
163
|
+
|
|
164
|
+
When your `package.json` declares dependencies, but there is no `pnpm-lock.yaml`, it fails with an error.
|
|
165
|
+
|
|
166
|
+
Projects without npm dependencies are checked for gems only.
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
## Exit codes
|
|
170
|
+
|
|
171
|
+
| Exit code | Meaning |
|
|
172
|
+
| --- | --- |
|
|
173
|
+
| `0` | All dependencies use permitted licenses. |
|
|
174
|
+
| `1` | Some dependencies use licenses that are not permitted. They are listed in the output. |
|
|
175
|
+
| `2` | The check could not run, e.g. because the configuration is invalid or pnpm failed. |
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
## Development
|
|
179
|
+
|
|
180
|
+
Run specs and RuboCop through `bundle exec rake`.
|
|
181
|
+
Specs require 100% line and branch coverage.
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
## Contributing
|
|
185
|
+
|
|
186
|
+
If you would like to contribute:
|
|
187
|
+
|
|
188
|
+
- Fork the repository.
|
|
189
|
+
- Push your changes with specs.
|
|
190
|
+
- Send a pull request.
|
|
191
|
+
|
|
192
|
+
It is important to us that we keep the gem lightweight and on topic.
|
|
193
|
+
If you're unsure whether a change would make it into the gem, open an issue and let's discuss.
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
License Gate is available as open source under the [MIT License](LICENSE).
|
data/exe/license_gate
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
class Check
|
|
3
|
+
|
|
4
|
+
Result = Data.define(:packages, :violations, :npm_checked)
|
|
5
|
+
|
|
6
|
+
DEPENDENCY_KEYS = %w[dependencies devDependencies optionalDependencies].freeze
|
|
7
|
+
|
|
8
|
+
def initialize(configuration, gemfile:, lockfile:)
|
|
9
|
+
@configuration = configuration
|
|
10
|
+
@gemfile = gemfile
|
|
11
|
+
@lockfile = lockfile
|
|
12
|
+
@root = gemfile.dirname
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def run
|
|
16
|
+
packages = sources
|
|
17
|
+
.flat_map(&:packages)
|
|
18
|
+
.sort_by { |package| [package.ecosystem, package.name, package.version] }
|
|
19
|
+
violations = packages.reject { |package| permitted?(package) }
|
|
20
|
+
|
|
21
|
+
Result.new(
|
|
22
|
+
packages:,
|
|
23
|
+
violations:,
|
|
24
|
+
npm_checked: !!npm_source,
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def sources
|
|
31
|
+
@sources ||= [
|
|
32
|
+
gem_source,
|
|
33
|
+
npm_source,
|
|
34
|
+
].compact
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def gem_source
|
|
38
|
+
@gem_source ||= Sources::Bundler.new(@gemfile, @lockfile, ignored_groups: @configuration.ignored_bundler_groups)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def npm_source
|
|
42
|
+
return @npm_source if defined?(@npm_source)
|
|
43
|
+
|
|
44
|
+
@npm_source = if pnpm_project?
|
|
45
|
+
Sources::Pnpm.new(@root, ignored_groups: @configuration.ignored_npm_groups)
|
|
46
|
+
elsif npm_dependencies?
|
|
47
|
+
raise UnsupportedPackageManagerError, "package.json declares dependencies, but there is no #{Sources::Pnpm::LOCKFILE}; only pnpm is supported"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def pnpm_project?
|
|
52
|
+
@root.join(Sources::Pnpm::LOCKFILE).exist?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def npm_dependencies?
|
|
56
|
+
package_json = @root.join('package.json')
|
|
57
|
+
if package_json.exist?
|
|
58
|
+
dependency_lists = JSON.parse(package_json.read).values_at(*DEPENDENCY_KEYS).compact
|
|
59
|
+
dependency_lists.any?(&:any?)
|
|
60
|
+
else
|
|
61
|
+
false
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# A gem lists licenses to choose from, so one permitted license is enough.
|
|
66
|
+
def permitted?(package)
|
|
67
|
+
permitted_licenses = @configuration.permitted_licenses_for(package)
|
|
68
|
+
package.license_strings.any? { |license| license.permitted?(permitted_licenses) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
class CLI
|
|
3
|
+
|
|
4
|
+
module ExitCode
|
|
5
|
+
# All licenses are permitted, or help or version was shown.
|
|
6
|
+
SUCCESS = 0
|
|
7
|
+
|
|
8
|
+
# Some packages use licenses that are not permitted.
|
|
9
|
+
VIOLATIONS = 1
|
|
10
|
+
|
|
11
|
+
# The check could not run, e.g. because of invalid configuration.
|
|
12
|
+
ERROR = 2
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(arguments, out: $stdout, err: $stderr, gemfile: nil, lockfile: nil)
|
|
16
|
+
@arguments = arguments
|
|
17
|
+
@out = out
|
|
18
|
+
@err = err
|
|
19
|
+
@gemfile = gemfile
|
|
20
|
+
@lockfile = lockfile
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def run
|
|
24
|
+
options = {}
|
|
25
|
+
parser = option_parser
|
|
26
|
+
unexpected_arguments = parser.parse(@arguments, into: options)
|
|
27
|
+
|
|
28
|
+
if unexpected_arguments.any?
|
|
29
|
+
raise UsageError, "unexpected argument #{unexpected_arguments.first.inspect}"
|
|
30
|
+
elsif options[:help]
|
|
31
|
+
@out.puts parser
|
|
32
|
+
ExitCode::SUCCESS
|
|
33
|
+
elsif options[:version]
|
|
34
|
+
@out.puts VERSION
|
|
35
|
+
ExitCode::SUCCESS
|
|
36
|
+
else
|
|
37
|
+
check(options[:config], all_packages: options[:report])
|
|
38
|
+
end
|
|
39
|
+
rescue StandardError => e
|
|
40
|
+
@err.puts "license_gate: #{e.message}"
|
|
41
|
+
ExitCode::ERROR
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def option_parser
|
|
47
|
+
OptionParser.new do |parser|
|
|
48
|
+
parser.banner = 'Usage: license_gate [options]'
|
|
49
|
+
parser.on('--config PATH', "Configuration file, defaults to #{Configuration::DEFAULT_PATH}")
|
|
50
|
+
parser.on('--report', 'List all packages with their licenses')
|
|
51
|
+
parser.on('--version', 'Show version')
|
|
52
|
+
parser.on('-h', '--help', 'Show this help')
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def check(config, all_packages:)
|
|
57
|
+
# Bundler's defaults follow `BUNDLE_GEMFILE`, and find `gems.rb` with `gems.locked`.
|
|
58
|
+
gemfile = @gemfile || ::Bundler.default_gemfile
|
|
59
|
+
lockfile = @lockfile || ::Bundler.default_lockfile
|
|
60
|
+
configuration_path = config || gemfile.dirname.join(Configuration::DEFAULT_PATH)
|
|
61
|
+
display_path = config || Configuration::DEFAULT_PATH
|
|
62
|
+
configuration = Configuration.load(configuration_path, display_path)
|
|
63
|
+
result = Check.new(configuration, gemfile:, lockfile:).run
|
|
64
|
+
report(result, display_path, all_packages:)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def report(result, display_path, all_packages:)
|
|
68
|
+
@out.puts "npm packages: not checked (no #{Sources::Pnpm::LOCKFILE})" unless result.npm_checked
|
|
69
|
+
listed_packages = all_packages ? result.packages : result.violations
|
|
70
|
+
if listed_packages.any?
|
|
71
|
+
listed_packages.each { |package| @out.puts package_line(package, result) }
|
|
72
|
+
@out.puts
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
if result.violations.empty?
|
|
76
|
+
@out.puts "All #{result.packages.size} packages use permitted licenses (#{counts(result)})."
|
|
77
|
+
ExitCode::SUCCESS
|
|
78
|
+
else
|
|
79
|
+
@out.puts "#{result.violations.size} of #{result.packages.size} packages use licenses that are not permitted."
|
|
80
|
+
@out.puts "Permit a license in #{display_path}, or add a license exception with a reason."
|
|
81
|
+
ExitCode::VIOLATIONS
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def package_line(package, result)
|
|
86
|
+
if result.violations.include?(package)
|
|
87
|
+
violation(package)
|
|
88
|
+
else
|
|
89
|
+
"✓ #{package}: #{package.license_strings.join(' OR ')}"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def violation(package)
|
|
94
|
+
licenses = package.license_strings
|
|
95
|
+
line = "✗ #{package}: #{licenses.join(' OR ')}"
|
|
96
|
+
line << ' (unsupported expression)' if licenses.any?(&:unsupported?)
|
|
97
|
+
line << ' (no license found)' if licenses.any?(&:unknown?)
|
|
98
|
+
line
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def counts(result)
|
|
102
|
+
counts = result.packages.map(&:ecosystem).tally
|
|
103
|
+
gem_count = counts.fetch(Ecosystem::GEM, 0)
|
|
104
|
+
npm_count = counts.fetch(Ecosystem::NPM, 0)
|
|
105
|
+
|
|
106
|
+
"#{gem_count} gems, #{npm_count} npm packages"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
class Configuration
|
|
3
|
+
|
|
4
|
+
DEFAULT_PATH = 'config/license_gate.yml'.freeze
|
|
5
|
+
|
|
6
|
+
LicenseException = Data.define(:packages, :licenses) do
|
|
7
|
+
|
|
8
|
+
def applies_to?(package)
|
|
9
|
+
packages
|
|
10
|
+
.fetch(package.ecosystem)
|
|
11
|
+
.include?(package.name)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attr_reader :ignored_bundler_groups, :ignored_npm_groups
|
|
17
|
+
|
|
18
|
+
def self.load(path, display_path)
|
|
19
|
+
data = read(path, display_path)
|
|
20
|
+
begin
|
|
21
|
+
new(data)
|
|
22
|
+
rescue ConfigurationError => e
|
|
23
|
+
raise ConfigurationError, "#{display_path}: #{e.message}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.read(path, display_path)
|
|
28
|
+
data = YAML.safe_load_file(path, aliases: true)
|
|
29
|
+
raise ConfigurationError, "#{display_path} is empty" unless data
|
|
30
|
+
|
|
31
|
+
data
|
|
32
|
+
rescue Errno::ENOENT
|
|
33
|
+
raise ConfigurationError, "#{display_path} not found"
|
|
34
|
+
rescue Psych::SyntaxError => e
|
|
35
|
+
raise ConfigurationError, "#{display_path} is not valid YAML: #{e.problem} (line #{e.line}, column #{e.column})"
|
|
36
|
+
rescue Psych::Exception => e
|
|
37
|
+
raise ConfigurationError, "#{display_path} is not valid YAML: #{e.message}"
|
|
38
|
+
end
|
|
39
|
+
private_class_method :read
|
|
40
|
+
|
|
41
|
+
def initialize(data)
|
|
42
|
+
assert_hash(data)
|
|
43
|
+
assert_keys(data, %w[permitted_licenses license_exceptions ignored_groups])
|
|
44
|
+
@permitted_licenses = load_permitted_licenses(data.fetch('permitted_licenses', {}))
|
|
45
|
+
@license_exceptions = load_license_exceptions(data.fetch('license_exceptions', []))
|
|
46
|
+
load_ignored_groups(data.fetch('ignored_groups', {}))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def permitted_licenses_for(package)
|
|
50
|
+
exceptions = @license_exceptions.select { |exception| exception.applies_to?(package) }
|
|
51
|
+
@permitted_licenses + exceptions.flat_map(&:licenses)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def load_permitted_licenses(permitted_licenses)
|
|
57
|
+
assert_hash(permitted_licenses, at: 'permitted_licenses')
|
|
58
|
+
permitted_licenses.each do |license, reason|
|
|
59
|
+
unless license.is_a?(String)
|
|
60
|
+
raise ConfigurationError, "key #{license.inspect} in permitted_licenses must be a String, but is #{license.class}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
assert_reason(reason, at: "permitted_licenses.#{license}")
|
|
64
|
+
end
|
|
65
|
+
permitted_licenses.keys
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def load_license_exceptions(license_exceptions)
|
|
69
|
+
unless license_exceptions.is_a?(Array)
|
|
70
|
+
raise ConfigurationError, "license_exceptions must be an Array, but is #{license_exceptions.inspect}"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
license_exceptions.map.with_index do |exception, index|
|
|
74
|
+
load_license_exception(exception, at: "license_exceptions[#{index}]")
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def load_license_exception(exception, at:)
|
|
79
|
+
assert_hash(exception, at:)
|
|
80
|
+
assert_keys(exception, %w[gems npm_packages licenses why], at:)
|
|
81
|
+
unless names?(exception['licenses'])
|
|
82
|
+
raise ConfigurationError, "#{at}.licenses must be a non-empty Array of Strings, but is #{exception['licenses'].inspect}"
|
|
83
|
+
end
|
|
84
|
+
unless package_lists?(exception)
|
|
85
|
+
raise ConfigurationError, "#{at} must list at least one package in gems or npm_packages (Arrays of Strings)"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
assert_reason(exception['why'], at: "#{at}.why")
|
|
89
|
+
LicenseException.new(
|
|
90
|
+
packages: {
|
|
91
|
+
Ecosystem::GEM => Array(exception['gems']),
|
|
92
|
+
Ecosystem::NPM => Array(exception['npm_packages']),
|
|
93
|
+
},
|
|
94
|
+
licenses: exception['licenses'],
|
|
95
|
+
)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def load_ignored_groups(ignored_groups)
|
|
99
|
+
assert_hash(ignored_groups, at: 'ignored_groups')
|
|
100
|
+
assert_keys(ignored_groups, %w[bundler npm], at: 'ignored_groups')
|
|
101
|
+
@ignored_bundler_groups = ignored_groups.fetch('bundler', [])
|
|
102
|
+
@ignored_npm_groups = ignored_groups.fetch('npm', [])
|
|
103
|
+
unless strings?(@ignored_bundler_groups)
|
|
104
|
+
raise ConfigurationError, "ignored_groups.bundler must be an Array of Strings, but is #{@ignored_bundler_groups.inspect}"
|
|
105
|
+
end
|
|
106
|
+
unless npm_groups?(@ignored_npm_groups)
|
|
107
|
+
raise ConfigurationError, "ignored_groups.npm may only list #{npm_groups.join(' and ')}, but is #{@ignored_npm_groups.inspect}"
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def assert_hash(value, at: 'the configuration')
|
|
112
|
+
raise ConfigurationError, "#{at} must be a Hash, but is #{value.inspect}" unless value.is_a?(Hash)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def assert_keys(hash, keys, at: 'the configuration')
|
|
116
|
+
unknown_keys = hash.keys - keys
|
|
117
|
+
raise ConfigurationError, "unknown key #{unknown_keys.first.inspect} in #{at} (expected one of #{keys.join(', ')})" unless unknown_keys.empty?
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def assert_reason(reason, at:)
|
|
121
|
+
raise ConfigurationError, "reason at #{at} must be a String, true or empty, but is #{reason.inspect}" unless reason?(reason)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def reason?(value)
|
|
125
|
+
value.nil? || value == true || value.is_a?(String)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def strings?(value)
|
|
129
|
+
value.is_a?(Array) && value.all?(String)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def names?(value)
|
|
133
|
+
strings?(value) && value.any?
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Either list may be empty, as long as the exception names a package.
|
|
137
|
+
def package_lists?(exception)
|
|
138
|
+
lists = exception.values_at('gems', 'npm_packages').compact
|
|
139
|
+
lists.all? { |list| strings?(list) } && lists.flatten.any?
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Groups `pnpm licenses list` can leave out.
|
|
143
|
+
def npm_groups
|
|
144
|
+
Sources::Pnpm::OPTIONS.keys
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def npm_groups?(value)
|
|
148
|
+
strings?(value) && (value - npm_groups).empty?
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
|
|
3
|
+
# One license as a package manager reports it, e.g. `MIT` or `(MIT OR Apache-2.0)`.
|
|
4
|
+
# An expression may combine licenses with either `OR` or `AND`; anything more complex is only permitted when listed verbatim.
|
|
5
|
+
class LicenseString
|
|
6
|
+
|
|
7
|
+
UNKNOWN = 'Unknown'.freeze
|
|
8
|
+
|
|
9
|
+
def initialize(string)
|
|
10
|
+
@string = string
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def permitted?(permitted_licenses)
|
|
14
|
+
permitted_names = permitted_licenses.to_set { |license| normalize(license) }
|
|
15
|
+
permitted_names.include?(normalize(@string)) || expression_permitted?(permitted_names)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def unsupported?
|
|
19
|
+
expression.match?(/[()]/) || (expression.include?(' OR ') && expression.include?(' AND '))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def unknown?
|
|
23
|
+
normalize(@string) == normalize(UNKNOWN)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_s
|
|
27
|
+
@string
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def expression_permitted?(permitted_names)
|
|
33
|
+
if unsupported?
|
|
34
|
+
false
|
|
35
|
+
elsif expression.include?(' AND ')
|
|
36
|
+
terms(' AND ').all? { |term| permitted_names.include?(term) }
|
|
37
|
+
else
|
|
38
|
+
terms(' OR ').any? { |term| permitted_names.include?(term) }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Splits before normalizing, which would turn ` OR ` into `-or-`.
|
|
43
|
+
# Keeps empty terms, so an operator without licenses (like `( AND )`) is never permitted.
|
|
44
|
+
def terms(operator)
|
|
45
|
+
expression.split(operator, -1).map { |term| normalize(term) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def normalize(license)
|
|
49
|
+
license.strip.tr(' ', '-').downcase
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# String without one pair of enclosing parentheses.
|
|
53
|
+
def expression
|
|
54
|
+
stripped = @string.strip
|
|
55
|
+
if stripped.start_with?('(') && stripped.end_with?(')')
|
|
56
|
+
stripped[1...-1]
|
|
57
|
+
else
|
|
58
|
+
stripped
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
|
|
3
|
+
# A gem or npm package (`ecosystem` is an Ecosystem) with licenses as its package manager reports them.
|
|
4
|
+
Package = Data.define(:ecosystem, :name, :version, :licenses) do
|
|
5
|
+
|
|
6
|
+
def license_strings
|
|
7
|
+
reported_licenses = licenses.empty? ? [LicenseString::UNKNOWN] : licenses
|
|
8
|
+
reported_licenses.map { |license| LicenseString.new(license) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def to_s
|
|
12
|
+
"#{ecosystem} #{name} #{version}"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
module Sources
|
|
3
|
+
|
|
4
|
+
# Gems of a project's lockfile, except those only needed by ignored groups.
|
|
5
|
+
class Bundler
|
|
6
|
+
|
|
7
|
+
def initialize(gemfile, lockfile, ignored_groups:)
|
|
8
|
+
@gemfile = gemfile
|
|
9
|
+
@lockfile = lockfile
|
|
10
|
+
@ignored_groups = ignored_groups.map(&:to_sym)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def packages
|
|
14
|
+
definition = ::Bundler::Definition.build(@gemfile, @lockfile, nil)
|
|
15
|
+
groups = definition.groups - @ignored_groups
|
|
16
|
+
# `specs_for` returns gems of all groups when given no groups.
|
|
17
|
+
specs = groups.any? ? definition.specs_for(groups) : []
|
|
18
|
+
specs.map do |spec|
|
|
19
|
+
# A gemspec setting `license = nil` reports `[nil]`.
|
|
20
|
+
licenses = spec.licenses.compact
|
|
21
|
+
Package.new(ecosystem: Ecosystem::GEM, name: spec.name, version: spec.version.to_s, licenses:)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
module LicenseGate
|
|
2
|
+
module Sources
|
|
3
|
+
|
|
4
|
+
# npm packages as reported by `pnpm licenses list`.
|
|
5
|
+
# Note that it only knows installed packages.
|
|
6
|
+
class Pnpm
|
|
7
|
+
|
|
8
|
+
LOCKFILE = 'pnpm-lock.yaml'.freeze
|
|
9
|
+
|
|
10
|
+
# The pnpm option leaving out each group.
|
|
11
|
+
OPTIONS = {
|
|
12
|
+
'devDependencies' => '--prod',
|
|
13
|
+
'optionalDependencies' => '--no-optional',
|
|
14
|
+
}.freeze
|
|
15
|
+
|
|
16
|
+
def initialize(root, ignored_groups:)
|
|
17
|
+
@root = root
|
|
18
|
+
@ignored_groups = ignored_groups
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def packages
|
|
22
|
+
# pnpm records every install in this file. Without installed packages, it would report every license as `Unknown`.
|
|
23
|
+
raise PnpmError, "#{LOCKFILE} found, but packages are not installed; run `pnpm install` first" unless @root.join('node_modules/.modules.yaml').exist?
|
|
24
|
+
|
|
25
|
+
output = list_licenses
|
|
26
|
+
# Older pnpm versions answer `--json` with this text when they find no packages.
|
|
27
|
+
return [] if output.start_with?('No licenses in packages found')
|
|
28
|
+
|
|
29
|
+
licenses = JSON.parse(output)
|
|
30
|
+
entries = licenses.values.flatten
|
|
31
|
+
entries.flat_map do |entry|
|
|
32
|
+
versions = entry.fetch('versions') do
|
|
33
|
+
# pnpm before 9 reports a single version instead.
|
|
34
|
+
raise PnpmError, 'unexpected output from `pnpm licenses list`; pnpm 9 or later is required'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
versions.map do |version|
|
|
38
|
+
Package.new(
|
|
39
|
+
ecosystem: Ecosystem::NPM,
|
|
40
|
+
name: entry['name'],
|
|
41
|
+
version:,
|
|
42
|
+
licenses: [entry['license']],
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def list_licenses
|
|
51
|
+
# Without `--recursive`, pnpm 12 lists only the root project of a workspace.
|
|
52
|
+
command = ['pnpm', 'licenses', 'list', '--json', '--recursive', *@ignored_groups.map { |group| OPTIONS.fetch(group) }]
|
|
53
|
+
|
|
54
|
+
# Binary mode keeps non-ASCII package metadata readable under any locale; JSON is parsed as UTF-8.
|
|
55
|
+
output, error, status = Open3.capture3(*command, chdir: @root.to_s, binmode: true)
|
|
56
|
+
|
|
57
|
+
if status.success?
|
|
58
|
+
output
|
|
59
|
+
else
|
|
60
|
+
# pnpm before 12 reports errors as JSON on stdout.
|
|
61
|
+
details = error.empty? ? output : error
|
|
62
|
+
raise PnpmError, "`#{command.join(' ')}` failed:\n#{details}"
|
|
63
|
+
end
|
|
64
|
+
rescue Errno::ENOENT
|
|
65
|
+
raise PnpmError, "#{LOCKFILE} found, but pnpm is not installed"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/license_gate.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'bundler'
|
|
2
|
+
require 'json'
|
|
3
|
+
require 'open3'
|
|
4
|
+
require 'optparse'
|
|
5
|
+
require 'yaml'
|
|
6
|
+
|
|
7
|
+
module LicenseGate
|
|
8
|
+
|
|
9
|
+
# Prevents a check from running.
|
|
10
|
+
Error = Class.new(StandardError)
|
|
11
|
+
ConfigurationError = Class.new(Error)
|
|
12
|
+
PnpmError = Class.new(Error)
|
|
13
|
+
UnsupportedPackageManagerError = Class.new(Error)
|
|
14
|
+
UsageError = Class.new(Error)
|
|
15
|
+
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
require_relative 'license_gate/version'
|
|
19
|
+
require_relative 'license_gate/license_string'
|
|
20
|
+
require_relative 'license_gate/ecosystem'
|
|
21
|
+
require_relative 'license_gate/package'
|
|
22
|
+
require_relative 'license_gate/configuration'
|
|
23
|
+
require_relative 'license_gate/sources/bundler'
|
|
24
|
+
require_relative 'license_gate/sources/pnpm'
|
|
25
|
+
require_relative 'license_gate/check'
|
|
26
|
+
require_relative 'license_gate/cli'
|
metadata
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: license_gate
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Arne Hartherz
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Fails when a gem or npm package uses a license your project has not permitted.
|
|
13
|
+
Further licenses can be allowed for specific packages only.
|
|
14
|
+
email:
|
|
15
|
+
- arne.hartherz@makandra.de
|
|
16
|
+
executables:
|
|
17
|
+
- license_gate
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- exe/license_gate
|
|
25
|
+
- lib/license_gate.rb
|
|
26
|
+
- lib/license_gate/check.rb
|
|
27
|
+
- lib/license_gate/cli.rb
|
|
28
|
+
- lib/license_gate/configuration.rb
|
|
29
|
+
- lib/license_gate/ecosystem.rb
|
|
30
|
+
- lib/license_gate/license_string.rb
|
|
31
|
+
- lib/license_gate/package.rb
|
|
32
|
+
- lib/license_gate/sources/bundler.rb
|
|
33
|
+
- lib/license_gate/sources/pnpm.rb
|
|
34
|
+
- lib/license_gate/version.rb
|
|
35
|
+
homepage: https://github.com/makandra/license_gate
|
|
36
|
+
licenses:
|
|
37
|
+
- MIT
|
|
38
|
+
metadata:
|
|
39
|
+
source_code_uri: https://github.com/makandra/license_gate
|
|
40
|
+
changelog_uri: https://github.com/makandra/license_gate/blob/main/CHANGELOG.md
|
|
41
|
+
bug_tracker_uri: https://github.com/makandra/license_gate/issues
|
|
42
|
+
rubygems_mfa_required: 'true'
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '3.3'
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubygems_version: 4.0.20
|
|
58
|
+
specification_version: 4
|
|
59
|
+
summary: Checks licenses of your Bundler and pnpm dependencies against an allowlist
|
|
60
|
+
test_files: []
|