From a12fe492969831e0118b72d9bb16b2e284423274 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 21 Feb 2018 14:23:37 -0800 Subject: [PATCH] add secret plugin interface --- plugins/secrets/plugin.go | 48 ++++++++++++++++++++++++ plugins/secrets/plugin_test.go | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 plugins/secrets/plugin.go create mode 100644 plugins/secrets/plugin_test.go diff --git a/plugins/secrets/plugin.go b/plugins/secrets/plugin.go new file mode 100644 index 000000000..0ec22c5e9 --- /dev/null +++ b/plugins/secrets/plugin.go @@ -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 +} diff --git a/plugins/secrets/plugin_test.go b/plugins/secrets/plugin_test.go new file mode 100644 index 000000000..3f411aea6 --- /dev/null +++ b/plugins/secrets/plugin_test.go @@ -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 +}