Skip to content

Commit

Permalink
Remove bias from plugin config and implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mach6 authored and foosinn committed Sep 4, 2020
1 parent 55c8aef commit e649725
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ There is an official Docker image: https://hub.docker.com/r/bitsbeats/drone-tree

## Limitations

Currently supports
Currently supports

* Github
* Gitlab
Expand All @@ -24,7 +24,7 @@ Currently supports
- `PLUGIN_DEBUG`: Set this to `true` to enable debug messages.
- `PLUGIN_ADDRESS`: Listen address for the plugins webserver. Defaults to `:3000`.
- `PLUGIN_SECRET`: Shared secret with drone. You can generate the token using `openssl rand -hex 16`.
- `PLUGIN_WHITELIST_FILE`: (Optional) Path to regex pattern file. Matches the repo slug(s) against a list of regex patterns. Defaults to `""`, match everything
- `PLUGIN_ALLOW_LIST_FILE`: (Optional) Path to regex pattern file. Matches the repo slug(s) against a list of regex patterns. Defaults to `""`, match everything.

Backend specific options

Expand Down Expand Up @@ -83,10 +83,10 @@ services:
Edit the Secrets (`***`), `<SECRET>` and `<GITHUB_TOKEN>` to your needs. `<SECRET>` is used between Drone and drone-tree-config.

#### Whitelisting repos with regex matching:
#### Enable repos via regex matching:

By default this plugin matches against ALL repo slugs. If you want to enable the plugin for specific repos only, turn on
regex matching by specifying a `PLUGIN_WHITELIST_FILE`.
By default, this plugin matches against ALL repo slugs. If you want to enable the plugin for specific repos only, turn on
regex matching by specifying a `PLUGIN_ALLOW_LIST_FILE`.

* Regex match rules must comply with [re2][3] syntax.
* Each line is a single rule.
Expand All @@ -104,7 +104,7 @@ Updated docker-compose:
- PLUGIN_FALLBACK=true
- PLUGIN_SECRET=<SECRET>
- GITHUB_TOKEN=<GITHUB_TOKEN>
- PLUGIN_WHITELIST_FILE=/drone-tree-config-matchfile
- PLUGIN_ALLOW_LIST_FILE=/drone-tree-config-matchfile
restart: always
volumes:
- /var/lib/drone/drone-tree-config-matchfile:/drone-tree-config-matchfile
Expand Down
10 changes: 8 additions & 2 deletions cmd/drone-tree-config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

type (
spec struct {
AllowListFile string `envconfig:"PLUGIN_ALLOW_LIST_FILE"`
Concat bool `envconfig:"PLUGIN_CONCAT"`
MaxDepth int `envconfig:"PLUGIN_MAXDEPTH" default:"2"`
Fallback bool `envconfig:"PLUGIN_FALLBACK"`
Debug bool `envconfig:"PLUGIN_DEBUG"`
WhitelistFile string `envconfig:"PLUGIN_WHITELIST_FILE"`
Address string `envconfig:"PLUGIN_ADDRESS" default:":3000"`
Secret string `envconfig:"PLUGIN_SECRET"`
Server string `envconfig:"SERVER" default:"https://api.github.com"`
Expand All @@ -26,6 +26,8 @@ type (
BitBucketAuthServer string `envconfig:"BITBUCKET_AUTH_SERVER"`
BitBucketClient string `envconfig:"BITBUCKET_CLIENT"`
BitBucketSecret string `envconfig:"BITBUCKET_SECRET"`
// Deprecated: Use AllowListFile instead.
WhitelistFile string `envconfig:"PLUGIN_WHITELIST_FILE"`
}
)

Expand All @@ -50,14 +52,18 @@ func main() {
if spec.BitBucketAuthServer == "" {
spec.BitBucketAuthServer = spec.Server
}
// TODO :: Remove this check, once the deprecation is deleted
if spec.AllowListFile == "" && spec.WhitelistFile != "" {
spec.AllowListFile = spec.WhitelistFile
}

handler := config.Handler(
plugin.New(
plugin.WithConcat(spec.Concat),
plugin.WithFallback(spec.Fallback),
plugin.WithMaxDepth(spec.MaxDepth),
plugin.WithServer(spec.Server),
plugin.WithWhitelistFile(spec.WhitelistFile),
plugin.WithAllowListFile(spec.AllowListFile),
plugin.WithBitBucketAuthServer(spec.BitBucketAuthServer),
plugin.WithBitBucketClient(spec.BitBucketClient),
plugin.WithBitBucketSecret(spec.BitBucketSecret),
Expand Down
8 changes: 4 additions & 4 deletions plugin/whitelist.go → plugin/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
"github.com/sirupsen/logrus"
)

// whitelisted determines if the plugin is enabled for the repo slug. decisions are made
// allowlisted determines if the plugin is enabled for the repo slug. decisions are made
// by considering the regex patterns in the regexFile.
//
// returns true (match) or false (no match). false means the repo slug should be bypassed
func (p *Plugin) whitelisted(req *request) bool {
func (p *Plugin) allowlisted(req *request) bool {
slug := req.Repo.Slug
noMatchMsg := fmt.Sprintf("%s no match: %s", req.UUID, slug)
matchMsg := fmt.Sprintf("%s match: %s", req.UUID, slug)

// requires a regex file
if p.whitelistFile == "" {
if p.allowListFile == "" {
// match
logrus.Info(matchMsg)
return true
}

buf, err := ioutil.ReadFile(p.whitelistFile)
buf, err := ioutil.ReadFile(p.allowListFile)
if err != nil {
// match
logrus.Warnf("%s regex file read error: %s", req.UUID, err)
Expand Down
10 changes: 8 additions & 2 deletions plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ func WithMaxDepth(maxDepth int) func(*Plugin) {
}

// WithWhitelistFile configures with repo slug regex match list file
func WithWhitelistFile(whitelistFile string) func(*Plugin) {
// Deprecated: Use WithAllowlistFile instead.
func WithWhitelistFile(file string) func(*Plugin) {
return WithAllowListFile(file)
}

// WithAllowListFile configures with repo slug regex match list file
func WithAllowListFile(file string) func(*Plugin) {
return func(p *Plugin) {
p.whitelistFile = whitelistFile
p.allowListFile = file
}
}
4 changes: 2 additions & 2 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type (
concat bool
fallback bool
maxDepth int
whitelistFile string
allowListFile string
}

droneConfig struct {
Expand Down Expand Up @@ -67,7 +67,7 @@ func (p *Plugin) Find(ctx context.Context, droneRequest *config.Request) (*drone
req := request{droneRequest, someUuid, client}

// make sure this plugin is enabled for the requested repo slug
if ok := p.whitelisted(&req); !ok {
if ok := p.allowlisted(&req); !ok {
// do the default behavior by returning nil, nil
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestMatchEnable(t *testing.T) {
WithGithubToken(mockToken),
WithFallback(true),
WithMaxDepth(2),
WithWhitelistFile(s.file),
WithAllowListFile(s.file),
)
droneConfig, err := plugin.Find(noContext, req)
if err != nil {
Expand Down

0 comments on commit e649725

Please sign in to comment.