|
| 1 | +// Package licensing provides helpers for validating, normalising, and working |
| 2 | +// with SPDX licence identifiers and expressions. |
| 3 | +// |
| 4 | +// The package builds on and is inspired by |
| 5 | +// https://github.com/git-pkgs/spdx, which provides parsing, |
| 6 | +// normalisation, and validation of SPDX licence expressions. |
| 7 | +// |
| 8 | +// It adds higher-level utilities commonly needed when handling licensing |
| 9 | +// metadata in applications |
| 10 | +package licensing |
| 11 | + |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "iter" |
| 15 | + "net/url" |
| 16 | + "slices" |
| 17 | + |
| 18 | + "github.com/git-pkgs/spdx" |
| 19 | + validation "github.com/go-ozzo/ozzo-validation/v4" |
| 20 | + |
| 21 | + "github.com/ARM-software/golang-utils/utils/collection" |
| 22 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 23 | + "github.com/ARM-software/golang-utils/utils/field" |
| 24 | + "github.com/ARM-software/golang-utils/utils/reflection" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + // errSPDXInvalid is returned when a string is not a valid SPDX licence |
| 29 | + errSPDXInvalid = validation.NewError("validation_is_spdx_licence", "must be a valid SPDX licence") |
| 30 | + |
| 31 | + // IsSPDXLicence defines an ozzo-validation rule that ensures a string |
| 32 | + // contains a valid SPDX licence expression. |
| 33 | + // |
| 34 | + // This rule can be used with github.com/go-ozzo/ozzo-validation to validate |
| 35 | + // fields containing licence identifiers or expressions such as: |
| 36 | + // |
| 37 | + // MIT |
| 38 | + // Apache-2.0 |
| 39 | + // MIT OR Apache-2.0 |
| 40 | + // GPL-3.0-only WITH Classpath-exception-2.0 |
| 41 | + // |
| 42 | + // Example: |
| 43 | + // |
| 44 | + // validation.Field(&pkg.Licence, licensing.IsSPDXLicence) |
| 45 | + // |
| 46 | + // Validation internally relies on ValidateSPDXLicence. |
| 47 | + IsSPDXLicence = validation.NewStringRuleWithError( |
| 48 | + func(l string) bool { return ValidateSPDXLicence(l) == nil }, |
| 49 | + errSPDXInvalid, |
| 50 | + ) |
| 51 | +) |
| 52 | + |
| 53 | +// ValidateSPDXLicence validates that the provided string is a valid SPDX |
| 54 | +// licence expression. |
| 55 | +// |
| 56 | +// The expression is parsed using an lenient SPDX parser which will try to identify licences even if they are not in their canonical form. |
| 57 | +// |
| 58 | +// Returns an error if: |
| 59 | +// - the expression is empty |
| 60 | +// - the expression cannot be parsed as a valid SPDX licence expression |
| 61 | +// |
| 62 | +// Example valid expressions: |
| 63 | +// |
| 64 | +// MIT |
| 65 | +// Apache-2.0 |
| 66 | +// MIT OR Apache-2.0 |
| 67 | +// GPL-2.0-or-later |
| 68 | +func ValidateSPDXLicence(licence string) error { |
| 69 | + if reflection.IsEmpty(licence) { |
| 70 | + return commonerrors.UndefinedVariable("licence expression") |
| 71 | + } |
| 72 | + _, err := spdx.Parse(licence) |
| 73 | + if err != nil { |
| 74 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, err, "failed normalising SPDX expression") |
| 75 | + } |
| 76 | + return err |
| 77 | +} |
| 78 | + |
| 79 | +// NormaliseSPDXLicence converts an SPDX licence expression into its canonical |
| 80 | +// SPDX representation. |
| 81 | +// |
| 82 | +// This function performs a lax normalisation using the SPDX library, allowing |
| 83 | +// minor variations in input formatting while still producing a valid canonical |
| 84 | +// SPDX expression. |
| 85 | +// |
| 86 | +// For example: |
| 87 | +// |
| 88 | +// "apache 2" → "Apache-2.0" |
| 89 | +// "mit or apache2" → "MIT OR Apache-2.0" |
| 90 | +// |
| 91 | +// Returns the canonical SPDX expression or an error if the expression cannot |
| 92 | +// be parsed or normalised. |
| 93 | +func NormaliseSPDXLicence(expression string) (canonical string, err error) { |
| 94 | + if reflection.IsEmpty(expression) { |
| 95 | + err = commonerrors.UndefinedVariable("licence expression") |
| 96 | + return |
| 97 | + } |
| 98 | + canonical, err = spdx.NormalizeExpressionLax(expression) //nolint:misspell |
| 99 | + if err != nil { |
| 100 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, err, "failed normalising SPDX expression") |
| 101 | + } |
| 102 | + return |
| 103 | +} |
| 104 | + |
| 105 | +// SatisfiesLicensingConstraints determines whether a licence expression is |
| 106 | +// compatible with a list of allowed licences. |
| 107 | +// |
| 108 | +// Note: The input licence expression and all entries in the allowed list are first |
| 109 | +// normalised to their canonical SPDX form before evaluation. |
| 110 | +// |
| 111 | +// Behaviour: |
| 112 | +// - The expression may contain SPDX operators such as AND / OR. |
| 113 | +// - The function returns true if the licence expression satisfies at least |
| 114 | +// one licence in the allowed list according to SPDX semantics. |
| 115 | +// |
| 116 | +// Example: |
| 117 | +// |
| 118 | +// licence = "MIT OR Apache-2.0" |
| 119 | +// allowedList = ["MIT"] |
| 120 | +// |
| 121 | +// Result: |
| 122 | +// |
| 123 | +// true |
| 124 | +// |
| 125 | +// This behaviour is similar to: |
| 126 | +// https://pkg.go.dev/github.com/github/go-spdx/v2/spdxexp#Satisfies |
| 127 | +func SatisfiesLicensingConstraints(licence string, allowedList []string) (pass bool, err error) { |
| 128 | + norm, err := NormaliseSPDXLicence(licence) |
| 129 | + if err != nil { |
| 130 | + return |
| 131 | + } |
| 132 | + allowed, err := collection.MapWithError[string, string](allowedList, NormaliseSPDXLicence) |
| 133 | + if err != nil { |
| 134 | + return |
| 135 | + } |
| 136 | + pass, err = spdx.Satisfies(norm, allowed) |
| 137 | + if err != nil { |
| 138 | + err = commonerrors.WrapError(commonerrors.ErrUnexpected, err, "failed checking licence constraints") |
| 139 | + } |
| 140 | + return |
| 141 | +} |
| 142 | + |
| 143 | +// FetchLicenceURL returns the SPDX website URL corresponding to the provided |
| 144 | +// SPDX licence identifier. |
| 145 | +// |
| 146 | +// The input licence is normalised before constructing the URL. |
| 147 | +// |
| 148 | +// Example: |
| 149 | +// |
| 150 | +// MIT → https://spdx.org/licenses/MIT.html |
| 151 | +// Apache-2.0 → https://spdx.org/licenses/Apache-2.0.html |
| 152 | +// |
| 153 | +// The input must represent a single licence identifier rather than a compound |
| 154 | +// SPDX expression. |
| 155 | +func FetchLicenceURL(spdxLicence *string) (licenceURL *url.URL, err error) { |
| 156 | + if reflection.IsEmpty(spdxLicence) { |
| 157 | + err = commonerrors.UndefinedVariable("licence") |
| 158 | + return |
| 159 | + } |
| 160 | + lStr := field.OptionalString(spdxLicence, "") |
| 161 | + l, err := NormaliseSPDXLicence(lStr) |
| 162 | + if err != nil { |
| 163 | + err = commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "failed identifying SPDX licence [%v]", lStr) |
| 164 | + return |
| 165 | + } |
| 166 | + if !spdx.ValidLicense(l) { |
| 167 | + err = commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "not a valid SPDX licence [%v]", lStr) |
| 168 | + return |
| 169 | + } |
| 170 | + licenceURL, err = url.Parse(fmt.Sprintf("https://spdx.org/licenses/%v.html", l)) |
| 171 | + if err != nil { |
| 172 | + err = commonerrors.WrapErrorf(commonerrors.ErrInvalid, err, "failed determining the licence's URL [%v]", lStr) |
| 173 | + return |
| 174 | + } |
| 175 | + return |
| 176 | +} |
| 177 | + |
| 178 | +// FetchLicenceURLs extracts all licences referenced in an SPDX licence |
| 179 | +// expression and returns the SPDX reference URLs for each licence. |
| 180 | +// |
| 181 | +// Note: The expression is first normalised before extracting the licences. |
| 182 | +// Moreover, operators such as AND, OR, and WITH are ignored when extracting licences |
| 183 | +// |
| 184 | +// Example: |
| 185 | +// |
| 186 | +// expression: "MIT OR Apache-2.0" |
| 187 | +// |
| 188 | +// returns: |
| 189 | +// |
| 190 | +// https://spdx.org/licenses/MIT.html |
| 191 | +// https://spdx.org/licenses/Apache-2.0.html |
| 192 | +func FetchLicenceURLs(expression string) (urls iter.Seq[url.URL], err error) { |
| 193 | + l, err := NormaliseSPDXLicence(expression) |
| 194 | + if err != nil { |
| 195 | + return |
| 196 | + } |
| 197 | + licences, err := spdx.ExtractLicenses(l) |
| 198 | + if err != nil { |
| 199 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, err, "failed extracting the licences from the expression") |
| 200 | + return |
| 201 | + } |
| 202 | + urls = collection.MapSequenceRefWithError[string, url.URL](slices.Values(licences), FetchLicenceURL) |
| 203 | + return |
| 204 | +} |
0 commit comments