Skip to content

Commit

Permalink
chore: start using plugin-framework package
Browse files Browse the repository at this point in the history
Signed-off-by: Pritesh Bandi <[email protected]>
  • Loading branch information
Pritesh Bandi authored and priteshbandi committed Dec 23, 2023
1 parent 85a5bb9 commit 5f289b6
Show file tree
Hide file tree
Showing 24 changed files with 316 additions and 643 deletions.
3 changes: 3 additions & 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.1
github.com/notaryproject/notation-plugin-framework-go v0.0.0-20231222132521-c8c68d2fe13b
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc5
github.com/veraison/go-cose v1.1.0
Expand All @@ -22,3 +23,5 @@ require (
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/sync v0.4.0 // indirect
)

replace github.com/notaryproject/notation-plugin-framework-go => /Volumes/workplace/notaryproject/notation-plugin-framework-go
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.1 h1:01doxjDERbd0vocLQrlJdusKrRLNNn50OJzp0c5I4Cw=
github.com/notaryproject/notation-core-go v1.0.1/go.mod h1:rayl8WlKgS4YxOZgDO0iGGB4Ef515ZFZUFaZDmsPXgE=
github.com/notaryproject/notation-plugin-framework-go v0.0.0-20231222132521-c8c68d2fe13b h1:HA7UI2hTamsehj2820fyVU4hECtyv9yx9LvFeErErDg=
github.com/notaryproject/notation-plugin-framework-go v0.0.0-20231222132521-c8c68d2fe13b/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-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
Expand Down
23 changes: 11 additions & 12 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"
pluginframework "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,45 +167,45 @@ func (t Repository) PushSignature(ctx context.Context, mediaType string, blob []
}

type PluginMock struct {
Metadata proto.GetMetadataResponse
Metadata pluginframework.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 *pluginframework.GetMetadataRequest) (*pluginframework.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 *pluginframework.VerifySignatureRequest) (*pluginframework.VerifySignatureResponse, error) {
if resp, ok := p.ExecuteResponse.(*pluginframework.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 *pluginframework.DescribeKeyRequest) (*pluginframework.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 *pluginframework.GenerateSignatureRequest) (*pluginframework.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 *pluginframework.GenerateEnvelopeRequest) (*pluginframework.GenerateEnvelopeResponse, error) {
panic("not implemented") // TODO: Implement
}

type PluginManager struct {
PluginCapabilities []proto.Capability
PluginCapabilities []pluginframework.Capability
GetPluginError error
PluginRunnerLoadError error
PluginRunnerExecuteResponse interface{}
PluginRunnerExecuteError error
}

func (pm PluginManager) Get(ctx context.Context, name string) (plugin.Plugin, error) {
func (pm PluginManager) Get(ctx context.Context, name string) (pluginframework.Plugin, error) {
return &PluginMock{
Metadata: proto.GetMetadataResponse{
Metadata: pluginframework.GetMetadataResponse{
Name: "plugin-name",
Description: "for mocking in unit tests",
Version: "1.0.0",
Expand Down
20 changes: 1 addition & 19 deletions internal/testdata/oci-layout/index.json
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
{
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"digest": "sha256:19dbd2e48e921426ee8ace4dc892edfb2ecdc1d1a72d5416c83670c30acecef0",
"size": 481,
"annotations": {
"io.containerd.image.name": "docker.io/library/alpine:v2",
"org.opencontainers.image.created": "2023-03-13T02:31:43Z",
"org.opencontainers.image.ref.name": "v2"
},
"platform": {
"architecture": "amd64",
"os": "linux"
}
}
]
}
{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:19dbd2e48e921426ee8ace4dc892edfb2ecdc1d1a72d5416c83670c30acecef0","size":481,"annotations":{"io.containerd.image.name":"docker.io/library/alpine:v2","org.opencontainers.image.created":"2023-03-13T02:31:43Z","org.opencontainers.image.ref.name":"v2"},"platform":{"architecture":"amd64","os":"linux"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:1a0a67c6f7cfea22d50a18f48d857ed36cf8806e6542db1372675100db62d09b","size":723,"annotations":{"io.cncf.notary.x509chain.thumbprint#S256":"[\"9f5f5aecee24b5cfdc7a91f6d5ac5c3a5348feb17c934d403f59ac251549ea0d\"]","org.opencontainers.image.created":"2023-03-14T04:45:22Z"}},{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:baeaea44f55c94499b7e082bd3c98ad5ec40fdf23ef89cdf4e5db6b83e4f18f5","size":728,"annotations":{"io.cncf.notary.x509chain.thumbprint#S256":"[\"9f5f5aecee24b5cfdc7a91f6d5ac5c3a5348feb17c934d403f59ac251549ea0d\"]","org.opencontainers.image.created":"2023-03-14T08:10:02Z"}}]}
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")
actualPlugin, err := mgr.Get(context.Background(), "foo")
if err != nil {
t.Fatal(err)
}
metadata, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
metadata, err := actualPlugin.GetMetadata(context.Background(), &plugin.GetMetadataRequest{})
if err != nil {
t.Fatal(err)
}
Expand Down
21 changes: 11 additions & 10 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/internal/file"
"github.com/notaryproject/notation-go/internal/semver"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-plugin-framework-go/plugin"
pluginframework "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) (pluginframework.Plugin, error)
List(ctx context.Context) ([]string, error)
}

Expand All @@ -47,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) (pluginframework.Plugin, error) {
pluginPath := path.Join(name, binName(name))
path, err := m.pluginFS.SysPath(pluginPath)
if err != nil {
Expand All @@ -59,7 +60,7 @@ func (m *CLIManager) Get(ctx context.Context, name string) (Plugin, error) {
}

// List produces a list of the plugin names on the system.
func (m *CLIManager) List(ctx context.Context) ([]string, error) {
func (m *CLIManager) List(_ context.Context) ([]string, error) {
var plugins []string
fs.WalkDir(m.pluginFS, ".", func(dir string, d fs.DirEntry, err error) error {
if err != nil {
Expand Down Expand Up @@ -112,7 +113,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
overwrite := installOpts.Overwrite
if installOpts.PluginPath == "" {
Expand Down Expand Up @@ -147,20 +148,20 @@ func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions)
if err != nil {
return nil, nil, fmt.Errorf("failed to create new CLI plugin: %w", 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 Expand Up @@ -203,7 +204,7 @@ func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions)

// Uninstall uninstalls a plugin on the system by its name.
// If the plugin dir does not exist, os.ErrNotExist is returned.
func (m *CLIManager) Uninstall(ctx context.Context, name string) error {
func (m *CLIManager) Uninstall(_ context.Context, name string) error {
pluginDirPath, err := m.pluginFS.SysPath(name)
if err != nil {
return err
Expand All @@ -216,7 +217,7 @@ func (m *CLIManager) Uninstall(ctx context.Context, name string) error {

// parsePluginFromDir checks if a dir is a valid plugin dir which contains
// one and only one plugin executable file. The dir may contain extra lib files
// and LICENSE files. Sub-directories are ignored.
// and LICENSE files. Subdirectories are ignored.
//
// On success, the plugin executable file path, plugin name and
// nil error are returned.
Expand Down
32 changes: 16 additions & 16 deletions plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"testing/fstest"

"github.com/notaryproject/notation-go/internal/mock/mockfs"
"github.com/notaryproject/notation-go/plugin/proto"
pluginframework "github.com/notaryproject/notation-plugin-framework-go/plugin"
)

type testCommander struct {
Expand All @@ -34,7 +34,7 @@ type testCommander struct {
err error
}

func (t testCommander) Output(ctx context.Context, path string, command proto.Command, req []byte) ([]byte, []byte, error) {
func (t testCommander) Output(ctx context.Context, path string, command pluginframework.Command, req []byte) ([]byte, []byte, error) {
return t.stdout, t.stderr, t.err
}

Expand All @@ -50,7 +50,7 @@ type testInstallCommander struct {
err error
}

func (t testInstallCommander) Output(ctx context.Context, path string, command proto.Command, req []byte) ([]byte, []byte, error) {
func (t testInstallCommander) Output(ctx context.Context, path string, command pluginframework.Command, req []byte) ([]byte, []byte, error) {
if path == t.existedPluginFilePath {
return t.existedPluginStdout, t.existedPluginStderr, t.existedPluginErr
}
Expand All @@ -60,34 +60,34 @@ func (t testInstallCommander) Output(ctx context.Context, path string, command p
return nil, nil, t.err
}

var validMetadata = proto.GetMetadataResponse{
var validMetadata = pluginframework.GetMetadataResponse{
Name: "foo", Description: "friendly", Version: "1.0.0", URL: "example.com",
SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
SupportedContractVersions: []string{"1.0"}, Capabilities: []pluginframework.Capability{pluginframework.CapabilitySignatureGenerator},
}

var validMetadataHigherVersion = proto.GetMetadataResponse{
var validMetadataHigherVersion = pluginframework.GetMetadataResponse{
Name: "foo", Description: "friendly", Version: "1.1.0", URL: "example.com",
SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
SupportedContractVersions: []string{"1.0"}, Capabilities: []pluginframework.Capability{pluginframework.CapabilitySignatureGenerator},
}

var validMetadataLowerVersion = proto.GetMetadataResponse{
var validMetadataLowerVersion = pluginframework.GetMetadataResponse{
Name: "foo", Description: "friendly", Version: "0.1.0", URL: "example.com",
SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
SupportedContractVersions: []string{"1.0"}, Capabilities: []pluginframework.Capability{pluginframework.CapabilitySignatureGenerator},
}

var validMetadataBar = proto.GetMetadataResponse{
var validMetadataBar = pluginframework.GetMetadataResponse{
Name: "bar", Description: "friendly", Version: "1.0.0", URL: "example.com",
SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
SupportedContractVersions: []string{"1.0"}, Capabilities: []pluginframework.Capability{pluginframework.CapabilitySignatureGenerator},
}

var invalidMetadataName = proto.GetMetadataResponse{
var invalidMetadataName = pluginframework.GetMetadataResponse{
Name: "foobar", Description: "friendly", Version: "1", URL: "example.com",
SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
SupportedContractVersions: []string{"1.0"}, Capabilities: []pluginframework.Capability{pluginframework.CapabilitySignatureGenerator},
}

var invalidContractVersionMetadata = proto.GetMetadataResponse{
var invalidContractVersionMetadata = pluginframework.GetMetadataResponse{
Name: "foo", Description: "friendly", Version: "1", URL: "example.com",
SupportedContractVersions: []string{"110.0"}, Capabilities: []proto.Capability{proto.CapabilitySignatureGenerator},
SupportedContractVersions: []string{"110.0"}, Capabilities: []pluginframework.Capability{pluginframework.CapabilitySignatureGenerator},
}

func TestManager_Get(t *testing.T) {
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestManager_Uninstall(t *testing.T) {
}
}

func metadataJSON(m proto.GetMetadataResponse) []byte {
func metadataJSON(m pluginframework.GetMetadataResponse) []byte {
d, err := json.Marshal(m)
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 5f289b6

Please sign in to comment.