add secret plugin interface

This commit is contained in:
Brad Rydzewski 2018-02-21 14:23:37 -08:00
parent 29028c1fdb
commit a12fe49296
2 changed files with 116 additions and 0 deletions

48
plugins/secrets/plugin.go Normal file
View file

@ -0,0 +1,48 @@
// Copyright 2018 Drone.IO Inc
// Use of this software is governed by the Drone Enterpise License
// that can be found in the LICENSE file.
package secrets
import (
"github.com/drone/drone/model"
"github.com/drone/drone/store"
)
// NewDefault returns the Store wrapped as a Service.
func NewDefault(store store.Store) model.SecretService {
return New(store)
}
// Plugin defines the required interface for implementing a remote
// secret plugin and sourcing secrets from an external source.
type Plugin interface {
SecretListBuild(*model.Repo, *model.Build) ([]*model.Secret, error)
}
// Extend exetends the base secret service with the plugin.
func Extend(base model.SecretService, with Plugin) model.SecretService {
return &extender{base, with}
}
type extender struct {
model.SecretService
plugin Plugin
}
// extends the base secret service and combines the secret list with the
// secret list returned by the plugin.
func (e *extender) SecretListBuild(repo *model.Repo, build *model.Build) ([]*model.Secret, error) {
base, err := e.SecretService.SecretListBuild(repo, build)
if err != nil {
return nil, err
}
with, err := e.plugin.SecretListBuild(repo, build)
if err != nil {
return nil, err
}
for _, secret := range base {
with = append(with, secret)
}
return with, nil
}

View file

@ -0,0 +1,68 @@
// Copyright 2018 Drone.IO Inc
// Use of this software is governed by the Drone Enterpise License
// that can be found in the LICENSE file.
package secrets
import (
"testing"
"github.com/drone/drone/model"
)
func TestExtends(t *testing.T) {
base := &mocker{}
base.list = []*model.Secret{
{Name: "foo"},
{Name: "bar"},
}
with := &mocker{}
with.list = []*model.Secret{
{Name: "baz"},
{Name: "qux"},
}
extended := Extend(base, with)
list, err := extended.SecretListBuild(nil, nil)
if err != nil {
t.Errorf("Expected combined secret list, got error %q", err)
}
if got, want := list[0], with.list[0]; got != want {
t.Errorf("Expected correct precedence. Want %s, got %s", want.Name, got.Name)
}
if got, want := list[1], with.list[1]; got != want {
t.Errorf("Expected correct precedence. Want %s, got %s", want.Name, got.Name)
}
if got, want := list[2], base.list[0]; got != want {
t.Errorf("Expected correct precedence. Want %s, got %s", want.Name, got.Name)
}
if got, want := list[3], base.list[1]; got != want {
t.Errorf("Expected correct precedence. Want %s, got %s", want.Name, got.Name)
}
}
type mocker struct {
list []*model.Secret
error error
}
func (m *mocker) SecretFind(*model.Repo, string) (*model.Secret, error) {
return nil, nil
}
func (m *mocker) SecretList(*model.Repo) ([]*model.Secret, error) {
return nil, nil
}
func (m *mocker) SecretListBuild(*model.Repo, *model.Build) ([]*model.Secret, error) {
return m.list, m.error
}
func (m *mocker) SecretCreate(*model.Repo, *model.Secret) error {
return nil
}
func (m *mocker) SecretUpdate(*model.Repo, *model.Secret) error {
return nil
}
func (m *mocker) SecretDelete(*model.Repo, string) error {
return nil
}