Skip to content

Commit

Permalink
chore: start using plugin-framework package (#372)
Browse files Browse the repository at this point in the history
**Note:** we introduced a small breaking change; the type for
`GenerateSignatureResponse#SigningAlgorithm` has been changed from
`string` to `plugin.SignatureAlgorithm`

---------

Signed-off-by: Pritesh Bandi <[email protected]>
  • Loading branch information
priteshbandi authored Feb 1, 2024
1 parent 4606472 commit 9ff1891
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 296 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/go-ldap/ldap/v3 v3.4.6
github.com/notaryproject/notation-core-go v1.0.2
github.com/notaryproject/notation-plugin-framework-go v1.0.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc6
github.com/veraison/go-cose v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/notaryproject/notation-core-go v1.0.2 h1:VEt+mbsgdANd9b4jqgmx2C7U0DmwynOuD2Nhxh3bANw=
github.com/notaryproject/notation-core-go v1.0.2/go.mod h1:2HkQzUwg08B3x9oVIztHsEh7Vil2Rj+tYgxH+JObLX4=
github.com/notaryproject/notation-plugin-framework-go v1.0.0 h1:6Qzr7DGXoCgXEQN+1gTZWuJAZvxh3p8Lryjn5FaLzi4=
github.com/notaryproject/notation-plugin-framework-go v1.0.0/go.mod h1:RqWSrTOtEASCrGOEffq0n8pSg2KOgKYiWqFWczRSics=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc6 h1:XDqvyKsJEbRtATzkgItUqBA7QHk58yxX1Ov9HERHNqU=
Expand Down
21 changes: 10 additions & 11 deletions internal/mock/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
_ "embed"

"github.com/notaryproject/notation-core-go/signature"
"github.com/notaryproject/notation-go/plugin"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-plugin-framework-go/plugin"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand Down Expand Up @@ -168,36 +167,36 @@ func (t Repository) PushSignature(ctx context.Context, mediaType string, blob []
}

type PluginMock struct {
Metadata proto.GetMetadataResponse
Metadata plugin.GetMetadataResponse
ExecuteResponse interface{}
ExecuteError error
}

func (p *PluginMock) GetMetadata(ctx context.Context, req *proto.GetMetadataRequest) (*proto.GetMetadataResponse, error) {
func (p *PluginMock) GetMetadata(ctx context.Context, req *plugin.GetMetadataRequest) (*plugin.GetMetadataResponse, error) {
return &p.Metadata, nil
}

func (p *PluginMock) VerifySignature(ctx context.Context, req *proto.VerifySignatureRequest) (*proto.VerifySignatureResponse, error) {
if resp, ok := p.ExecuteResponse.(*proto.VerifySignatureResponse); ok {
func (p *PluginMock) VerifySignature(ctx context.Context, req *plugin.VerifySignatureRequest) (*plugin.VerifySignatureResponse, error) {
if resp, ok := p.ExecuteResponse.(*plugin.VerifySignatureResponse); ok {
return resp, nil
}
return nil, p.ExecuteError
}

func (p *PluginMock) DescribeKey(ctx context.Context, req *proto.DescribeKeyRequest) (*proto.DescribeKeyResponse, error) {
func (p *PluginMock) DescribeKey(ctx context.Context, req *plugin.DescribeKeyRequest) (*plugin.DescribeKeyResponse, error) {
panic("not implemented") // TODO: Implement
}

func (p *PluginMock) GenerateSignature(ctx context.Context, req *proto.GenerateSignatureRequest) (*proto.GenerateSignatureResponse, error) {
func (p *PluginMock) GenerateSignature(ctx context.Context, req *plugin.GenerateSignatureRequest) (*plugin.GenerateSignatureResponse, error) {
panic("not implemented") // TODO: Implement
}

func (p *PluginMock) GenerateEnvelope(ctx context.Context, req *proto.GenerateEnvelopeRequest) (*proto.GenerateEnvelopeResponse, error) {
func (p *PluginMock) GenerateEnvelope(ctx context.Context, req *plugin.GenerateEnvelopeRequest) (*plugin.GenerateEnvelopeResponse, error) {
panic("not implemented") // TODO: Implement
}

type PluginManager struct {
PluginCapabilities []proto.Capability
PluginCapabilities []plugin.Capability
GetPluginError error
PluginRunnerLoadError error
PluginRunnerExecuteResponse interface{}
Expand All @@ -206,7 +205,7 @@ type PluginManager struct {

func (pm PluginManager) Get(ctx context.Context, name string) (plugin.Plugin, error) {
return &PluginMock{
Metadata: proto.GetMetadataResponse{
Metadata: plugin.GetMetadataResponse{
Name: "plugin-name",
Description: "for mocking in unit tests",
Version: "1.0.0",
Expand Down
10 changes: 5 additions & 5 deletions plugin/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (
"testing"

"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-plugin-framework-go/plugin"
)

var exampleMetadata = proto.GetMetadataResponse{
var exampleMetadata = plugin.GetMetadataResponse{
Name: "foo",
Description: "friendly",
Version: "1",
URL: "example.com",
SupportedContractVersions: []string{"1.0"},
Capabilities: []proto.Capability{"cap"}}
Capabilities: []plugin.Capability{"cap"}}

func preparePlugin(t *testing.T) string {
root := t.TempDir()
Expand Down Expand Up @@ -87,11 +87,11 @@ func TestIntegration(t *testing.T) {
}

// validate and create
plugin, err := mgr.Get(context.Background(), "foo")
pl, err := mgr.Get(context.Background(), "foo")
if err != nil {
t.Fatal(err)
}
metadata, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
metadata, err := pl.GetMetadata(context.Background(), &plugin.GetMetadataRequest{})
if err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
"github.com/notaryproject/notation-go/internal/file"
"github.com/notaryproject/notation-go/internal/semver"
"github.com/notaryproject/notation-go/log"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-plugin-framework-go/plugin"
)

// Manager manages plugins installed on the system.
type Manager interface {
Get(ctx context.Context, name string) (Plugin, error)
Get(ctx context.Context, name string) (plugin.Plugin, error)
List(ctx context.Context) ([]string, error)
}

Expand All @@ -48,7 +48,7 @@ func NewCLIManager(pluginFS dir.SysFS) *CLIManager {
// Get returns a plugin on the system by its name.
//
// If the plugin is not found, the error is of type os.ErrNotExist.
func (m *CLIManager) Get(ctx context.Context, name string) (Plugin, error) {
func (m *CLIManager) Get(ctx context.Context, name string) (plugin.Plugin, error) {
pluginPath := path.Join(name, binName(name))
path, err := m.pluginFS.SysPath(pluginPath)
if err != nil {
Expand Down Expand Up @@ -118,7 +118,7 @@ type CLIInstallOptions struct {
//
// If overwrite is set, version check is skipped. If existing
// plugin is malfunctioning, it will be overwritten.
func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions) (*proto.GetMetadataResponse, *proto.GetMetadataResponse, error) {
func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions) (*plugin.GetMetadataResponse, *plugin.GetMetadataResponse, error) {
// initialization
logger := log.GetLogger(ctx)
overwrite := installOpts.Overwrite
Expand Down Expand Up @@ -153,20 +153,20 @@ func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions)
if err != nil {
return nil, nil, err
}
newPluginMetadata, err := newPlugin.GetMetadata(ctx, &proto.GetMetadataRequest{})
newPluginMetadata, err := newPlugin.GetMetadata(ctx, &plugin.GetMetadataRequest{})
if err != nil {
return nil, nil, fmt.Errorf("failed to get metadata of new plugin: %w", err)
}
// check plugin existence and get existing plugin metadata
var existingPluginMetadata *proto.GetMetadataResponse
var existingPluginMetadata *plugin.GetMetadataResponse
existingPlugin, err := m.Get(ctx, pluginName)
if err != nil {
// fail only if overwrite is not set
if !errors.Is(err, os.ErrNotExist) && !overwrite {
return nil, nil, fmt.Errorf("failed to check plugin existence: %w", err)
}
} else { // plugin already exists
existingPluginMetadata, err = existingPlugin.GetMetadata(ctx, &proto.GetMetadataRequest{})
existingPluginMetadata, err = existingPlugin.GetMetadata(ctx, &plugin.GetMetadataRequest{})
if err != nil && !overwrite { // fail only if overwrite is not set
return nil, nil, fmt.Errorf("failed to get metadata of existing plugin: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions plugin/manager_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"os"
"strings"

"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-plugin-framework-go/plugin"
)

func binName(name string) string {
return proto.Prefix + name
return plugin.BinaryPrefix + name
}

// isExecutableFile checks if a file at filePath is user executable
Expand All @@ -44,7 +44,7 @@ func isExecutableFile(filePath string) (bool, error) {
// parsePluginName checks if fileName is a valid plugin file name
// and gets plugin name from it based on spec: https://github.com/notaryproject/specifications/blob/main/specs/plugin-extensibility.md#installation
func parsePluginName(fileName string) (string, error) {
pluginName, found := strings.CutPrefix(fileName, proto.Prefix)
pluginName, found := strings.CutPrefix(fileName, plugin.BinaryPrefix)
if !found || pluginName == "" {
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got %s", fileName)
}
Expand Down
6 changes: 3 additions & 3 deletions plugin/manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"strings"

"github.com/notaryproject/notation-go/internal/file"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-plugin-framework-go/plugin"
)

func binName(name string) string {
return proto.Prefix + name + ".exe"
return plugin.BinaryPrefix + name + ".exe"
}

// isExecutableFile checks if a file at filePath is executable
Expand All @@ -46,7 +46,7 @@ func parsePluginName(fileName string) (string, error) {
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got %s", fileName)
}
fname := file.TrimFileExtension(fileName)
pluginName, found := strings.CutPrefix(fname, proto.Prefix)
pluginName, found := strings.CutPrefix(fname, plugin.BinaryPrefix)
if !found || pluginName == "" {
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got %s", fileName)
}
Expand Down
Loading

0 comments on commit 9ff1891

Please sign in to comment.