Skip to content

Commit c40093e

Browse files
committed
feat: replace allowed domains with prefixes
This introduces a new `prefixes` configuration option under `allowed` to replace the `domains` option, which is now deprecated. The term `prefixes` is more accurate since the matching logic checks for module prefixes rather than strict domain names. The `domains` configuration is still supported for backward compatibility but will emit a deprecation warning when used. Both configurations are combined and treated as prefixes under the hood. Changes include: - Renaming internal methods from `IsAllowedModuleDomain` to `IsAllowedModulePrefix` - Adding a deprecation warning in the CLI for `domains` - Updating the example `.gomodguard.yaml` files - Updating documentation in the README - Updating unit tests to reflect the new option
1 parent af3b40a commit c40093e

10 files changed

Lines changed: 49 additions & 33 deletions

File tree

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Allow and block list linter for direct Go module dependencies. This is useful fo
1414

1515
Allowed and blocked modules are defined in a `./.gomodguard.yaml` or `~/.gomodguard.yaml` file.
1616

17-
Modules can be allowed by module or domain name. When allowed modules are specified any modules not in the allowed configuration are blocked.
17+
Modules can be allowed by module or prefix name. When allowed modules are specified any modules not in the allowed configuration are blocked.
1818

19-
If no allowed modules or domains are specified then all modules are allowed except for blocked ones.
19+
If no allowed modules or module prefixes are specified then all modules are allowed except for blocked ones.
2020

2121
The linter looks for blocked modules in `go.mod` and searches for imported packages where the imported packages module is blocked. Indirect modules are not considered.
2222

@@ -41,8 +41,11 @@ allowed:
4141
- github.com/go-xmlfmt/xmlfmt
4242
- github.com/phayes/checkstyle
4343
- github.com/mitchellh/go-homedir
44-
domains: # List of allowed module domains
45-
- golang.org
44+
- github.com/confluentinc/confluent-kafka-go/v2 # Allow v2 only
45+
prefixes: # List of allowed module prefixes (Replaced domains which is now deprecated)
46+
- golang.org # Allow all golang.org modules
47+
- github.com/kubernetes # Allow all Kubernetes modules
48+
- github.com/apache/arrow-go # Allow all Apache Arrow module major versions
4649

4750
blocked:
4851
modules: # List of blocked modules
@@ -89,7 +92,7 @@ Flags:
8992
╰─ ./gomodguard -r checkstyle -f gomodguard-checkstyle.xml ./...
9093
9194
info: allowed modules, [gopkg.in/yaml.v3 github.com/go-xmlfmt/xmlfmt github.com/phayes/checkstyle github.com/mitchellh/go-homedir]
92-
info: allowed module domains, [golang.org]
95+
info: allowed module prefixes, [golang.org]
9396
info: blocked modules, [github.com/uudashr/go-module]
9497
info: found `2` blocked modules in the go.mod file, [github.com/gofrs/uuid github.com/uudashr/go-module]
9598
blocked_example.go:6: import of package `github.com/gofrs/uuid` is blocked because the module is not in the allowed modules list.

_example/allOptions/.gomodguard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ allowed:
44
- github.com/go-xmlfmt/xmlfmt
55
- github.com/Masterminds/semver
66
- github.com/ryancurrah/gomodguard
7-
domains: # List of allowed module domains
7+
prefixes: # List of allowed module prefixes
88
- golang.org
99

1010
blocked:

_example/indirectDep/.gomodguard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ allowed:
44
- github.com/go-xmlfmt/xmlfmt
55
- github.com/Masterminds/semver
66
- github.com/ryancurrah/gomodguard
7-
domains: # List of allowed module domains
7+
prefixes: # List of allowed module prefixes
88
- golang.org
99

1010
blocked:

_example/invalidConstraint/.gomodguard.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
allowed:
22
modules:
33
- github.com/pborman/uuid
4-
domains:
4+
prefixes:
55
- golang.org
66
blocked:
77
modules:
@@ -24,5 +24,5 @@ blocked:
2424
version: "!= 1.3.0"
2525
- github.com/gin-gonic/gin:
2626
version: "== 1.9.1"
27-
domains: []
27+
prefixes: []
2828

allowed.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package gomodguard
22

3-
import "strings"
3+
import (
4+
"slices"
5+
"strings"
6+
)
47

58
// Allowed is a list of modules and module
6-
// domains that are allowed to be used.
9+
// prefixes that are allowed to be used.
710
type Allowed struct {
8-
Modules []string `yaml:"modules"`
9-
Domains []string `yaml:"domains"`
11+
Modules []string `yaml:"modules"`
12+
Domains []string `yaml:"domains"`
13+
Prefixes []string `yaml:"prefixes"`
1014
}
1115

1216
// IsAllowedModule returns true if the given module
@@ -23,14 +27,17 @@ func (a *Allowed) IsAllowedModule(moduleName string) bool {
2327
return false
2428
}
2529

26-
// IsAllowedModuleDomain returns true if the given modules domain is
27-
// in the allowed module domains list.
28-
func (a *Allowed) IsAllowedModuleDomain(moduleName string) bool {
29-
allowedDomains := a.Domains
30+
// IsAllowedModulePrefix returns true if the given modules prefix is
31+
// in the allowed module prefixes list.
32+
func (a *Allowed) IsAllowedModulePrefix(moduleName string) bool {
33+
allowedPrefixes := make([]string, 0, len(a.Prefixes)+len(a.Domains))
34+
allowedPrefixes = append(allowedPrefixes, a.Prefixes...)
35+
allowedPrefixes = append(allowedPrefixes, a.Domains...)
36+
allowedPrefixes = slices.Compact(allowedPrefixes)
3037

31-
for i := range allowedDomains {
38+
for i := range allowedPrefixes {
3239
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)),
33-
strings.TrimSpace(strings.ToLower(allowedDomains[i]))) {
40+
strings.TrimSpace(strings.ToLower(allowedPrefixes[i]))) {
3441
return true
3542
}
3643
}

allowed_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ func TestAllowedIsAllowedModule(t *testing.T) {
3838
}
3939
}
4040

41-
func TestAllowedIsAllowedModuleDomain(t *testing.T) {
41+
func TestAllowedIsAllowedModulePrefix(t *testing.T) {
4242
var tests = []struct {
4343
testName string
4444
allowedModules gomodguard.Allowed
4545
lintedModuleName string
46-
wantIsAllowedModuleDomain bool
46+
wantIsAllowedModulePrefix bool
4747
}{
4848
{
4949
"module is allowed",
50-
gomodguard.Allowed{Domains: []string{"github.com"}},
50+
gomodguard.Allowed{Prefixes: []string{"github.com"}},
5151
"github.com/someallowed/module",
5252
true,
5353
},
@@ -61,9 +61,9 @@ func TestAllowedIsAllowedModuleDomain(t *testing.T) {
6161

6262
for _, tt := range tests {
6363
t.Run(tt.testName, func(t *testing.T) {
64-
isAllowedModuleDomain := tt.allowedModules.IsAllowedModuleDomain(tt.lintedModuleName)
65-
if !reflect.DeepEqual(isAllowedModuleDomain, tt.wantIsAllowedModuleDomain) {
66-
t.Errorf("got '%v' want '%v'", isAllowedModuleDomain, tt.wantIsAllowedModuleDomain)
64+
isAllowedModulePrefix := tt.allowedModules.IsAllowedModulePrefix(tt.lintedModuleName)
65+
if !reflect.DeepEqual(isAllowedModulePrefix, tt.wantIsAllowedModulePrefix) {
66+
t.Errorf("got '%v' want '%v'", isAllowedModulePrefix, tt.wantIsAllowedModulePrefix)
6767
}
6868
})
6969
}

internal/cli/cli.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ func Run() int {
9494
}
9595

9696
logger.Printf("info: allowed modules, %+v", config.Allowed.Modules)
97-
logger.Printf("info: allowed module domains, %+v", config.Allowed.Domains)
97+
logger.Printf("info: allowed module prefixes, %+v", config.Allowed.Prefixes)
98+
99+
if len(config.Allowed.Domains) > 0 {
100+
logger.Printf("info: allowed module domains, %+v", config.Allowed.Domains)
101+
logger.Printf("warning: 'domains' is deprecated, please use 'prefixes' instead")
102+
}
103+
98104
logger.Printf("info: blocked modules, %+v", config.Blocked.Modules.Get())
99105
logger.Printf("info: blocked modules with version constraints, %+v", config.Blocked.Versions.Get())
100106

processor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (p *Processor) ProcessFiles(filenames []string) (issues []Issue) {
9494
// the go.mod file of the module that is being linted.
9595
//
9696
// It works by iterating over the dependant modules specified in the require
97-
// directive, checking if the module domain or full name is in the allowed list.
97+
// directive, checking if the module prefix or full name is in the allowed list.
9898
func (p *Processor) SetBlockedModules() { //nolint:funlen
9999
blockedModules := make(map[string][]string, len(p.Modfile.Require))
100100
currentModuleName := p.Modfile.Module.Mod.Path
@@ -108,9 +108,9 @@ func (p *Processor) SetBlockedModules() { //nolint:funlen
108108
var isAllowed bool
109109

110110
switch {
111-
case len(p.Config.Allowed.Modules) == 0 && len(p.Config.Allowed.Domains) == 0:
111+
case len(p.Config.Allowed.Modules) == 0 && len(p.Config.Allowed.Prefixes) == 0 && len(p.Config.Allowed.Domains) == 0:
112112
isAllowed = true
113-
case p.Config.Allowed.IsAllowedModuleDomain(lintedModuleName):
113+
case p.Config.Allowed.IsAllowedModulePrefix(lintedModuleName):
114114
isAllowed = true
115115
case p.Config.Allowed.IsAllowedModule(lintedModuleName):
116116
isAllowed = true

processor_internal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestProcessorSetBlockedModulesWithAllowList(t *testing.T) {
182182
"github.com/Masterminds/semver/v3",
183183
"github.com/ryancurrah/gomodguard",
184184
},
185-
Domains: []string{
185+
Prefixes: []string{
186186
"golang.org",
187187
},
188188
},
@@ -403,7 +403,7 @@ func TestProcessorSetBlockedModulesWithIndirectDependency(t *testing.T) {
403403
"github.com/Masterminds/semver/v3",
404404
"github.com/ryancurrah/gomodguard",
405405
},
406-
Domains: []string{
406+
Prefixes: []string{
407407
"golang.org",
408408
},
409409
},
@@ -452,7 +452,7 @@ func TestProcessorSetBlockedModulesWithIndirectDependency(t *testing.T) {
452452
"github.com/Masterminds/semver/v3",
453453
"github.com/ryancurrah/gomodguard",
454454
},
455-
Domains: []string{
455+
Prefixes: []string{
456456
"golang.org",
457457
},
458458
},

processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestProcessorProcessFiles(t *testing.T) { //nolint:funlen
3939
"github.com/Masterminds/semver/v3",
4040
"github.com/ryancurrah/gomodguard",
4141
},
42-
Domains: []string{
42+
Prefixes: []string{
4343
"golang.org",
4444
},
4545
},

0 commit comments

Comments
 (0)