From c348d97fe0a57dd1b4d00180fba2c4ebad425367 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sat, 5 Sep 2020 11:16:46 -0400 Subject: [PATCH] not working Signed-off-by: Alex Goodman --- cmd/cmd.go | 2 +- go.mod | 2 +- go.sum | 3 +-- syft/cataloger/cataloger.go | 3 +-- syft/plugin/cataloger_plugin.go | 5 +---- syft/plugin/grpc/cataloger_client.go | 23 ++++++++++++++++++++--- syft/plugin/grpc/file_resolver_client.go | 3 +++ syft/plugin/grpc/file_resolver_server.go | 5 +++++ syft/plugin/plugin.go | 20 ++++++++++++++++---- syft/plugin/repository.go | 2 +- 10 files changed, 50 insertions(+), 18 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 8bcaa6768..be9328f50 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -84,7 +84,7 @@ func setGlobalCliOptions() { rootCmd.Flags().CountVarP(&cliOpts.Verbosity, "verbose", "v", "increase verbosity (-v = info, -vv = debug)") // plugins - defaultPluginDir := path.Join(xdg.ConfigHome, internal.ApplicationName, "plugins") + defaultPluginDir := path.Join(xdg.CacheHome, internal.ApplicationName, "plugins") flag = "plugins.dir" rootCmd.Flags().String( flag, defaultPluginDir, diff --git a/go.mod b/go.mod index 3088f9e6e..9337a254f 100644 --- a/go.mod +++ b/go.mod @@ -17,13 +17,13 @@ require ( github.com/google/uuid v1.1.1 github.com/gookit/color v1.2.7 github.com/gopherjs/gopherjs v0.0.0-20190910122728-9d188e94fb99 // indirect + github.com/hashicorp/go-hclog v0.9.2 github.com/hashicorp/go-multierror v1.1.0 github.com/hashicorp/go-plugin v1.3.0 github.com/hashicorp/go-version v1.2.0 github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.1 github.com/olekukonko/tablewriter v0.0.4 - github.com/opencontainers/runc v0.1.1 // indirect github.com/package-url/packageurl-go v0.1.0 github.com/pelletier/go-toml v1.8.0 github.com/rogpeppe/go-internal v1.5.2 diff --git a/go.sum b/go.sum index 59d0fe662..6f70f4e2e 100644 --- a/go.sum +++ b/go.sum @@ -131,8 +131,6 @@ github.com/anchore/go-testutils v0.0.0-20200624184116-66aa578126db/go.mod h1:D3r github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b h1:e1bmaoJfZVsCYMrIZBpFxwV26CbsuoEh5muXD5I1Ods= github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b/go.mod h1:Bkc+JYWjMCF8OyZ340IMSIi2Ebf3uwByOk6ho4wne1E= github.com/anchore/stereoscope v0.0.0-20200520221116-025e07f1c93e/go.mod h1:bkyLl5VITnrmgErv4S1vDfVz/TGAZ5il6161IQo7w2g= -github.com/anchore/stereoscope v0.0.0-20200813152757-548b22c8a0b3 h1:pl+txuYlhK8Mmio4d+4zQI/1xg8X6BtNErTASrx23Wk= -github.com/anchore/stereoscope v0.0.0-20200813152757-548b22c8a0b3/go.mod h1:WntReQTI/I27FOQ87UgLVVzWgku6+ZsqfOTLxpIZFCs= github.com/anchore/stereoscope v0.0.0-20200903223322-a3806f515470 h1:apXyPyfiaL/YQF7VeXRbDDB9TA7dA9rxdq8YbzPk9ko= github.com/anchore/stereoscope v0.0.0-20200903223322-a3806f515470/go.mod h1:WntReQTI/I27FOQ87UgLVVzWgku6+ZsqfOTLxpIZFCs= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= @@ -518,6 +516,7 @@ github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik= +github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a h1:GmsqmapfzSJkm28dhRoHz2tLRbJmqhU86IPgBtN3mmk= github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a/go.mod h1:xRskid8CManxVta/ALEhJha/pweKBaVG6fWgc0yH25s= diff --git a/syft/cataloger/cataloger.go b/syft/cataloger/cataloger.go index 11f7ec179..10d7fa109 100644 --- a/syft/cataloger/cataloger.go +++ b/syft/cataloger/cataloger.go @@ -11,7 +11,6 @@ import ( "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/syft/syft/cataloger/apkdb" "github.com/anchore/syft/syft/cataloger/bundler" - "github.com/anchore/syft/syft/cataloger/dpkg" "github.com/anchore/syft/syft/cataloger/golang" "github.com/anchore/syft/syft/cataloger/java" "github.com/anchore/syft/syft/cataloger/javascript" @@ -37,7 +36,7 @@ type Cataloger interface { // All returns a slice of all locally defined catalogers (defined in child packages). func BuiltIn() []Cataloger { return []Cataloger{ - dpkg.New(), + //dpkg.New(), bundler.New(), python.New(), rpmdb.New(), diff --git a/syft/plugin/cataloger_plugin.go b/syft/plugin/cataloger_plugin.go index 0d1a615ac..9d3ad7d2c 100644 --- a/syft/plugin/cataloger_plugin.go +++ b/syft/plugin/cataloger_plugin.go @@ -13,13 +13,10 @@ import ( // integrity check var _ plugin.GRPCPlugin = &CatalogerPlugin{} -// Cataloger is the interface exposed as a plugin. -type Cataloger cataloger.Cataloger - // CatalogerPlugin is the implementation of plugin.Plugin that is served/consumed. type CatalogerPlugin struct { plugin.NetRPCUnsupportedPlugin - Impl Cataloger + Impl cataloger.Cataloger } func (p *CatalogerPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error { diff --git a/syft/plugin/grpc/cataloger_client.go b/syft/plugin/grpc/cataloger_client.go index 818d815bf..4aef6fe57 100644 --- a/syft/plugin/grpc/cataloger_client.go +++ b/syft/plugin/grpc/cataloger_client.go @@ -3,6 +3,8 @@ package grpc import ( "context" "fmt" + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/cataloger" "io" "io/ioutil" @@ -14,6 +16,8 @@ import ( "google.golang.org/grpc" ) +var _ cataloger.Cataloger = &CatalogerClient{} + type CatalogerClient struct { broker *plugin.GRPCBroker client proto.CatalogerClient @@ -35,7 +39,7 @@ func (c *CatalogerClient) Name() string { return resp.Name } -func (c *CatalogerClient) SelectFiles(resolver scope.FileResolver) error { +func (c *CatalogerClient) SelectFiles(resolver scope.FileResolver) []file.Reference { fileResolverServer := &FileResolverServer{Impl: resolver} var s *grpc.Server @@ -49,12 +53,25 @@ func (c *CatalogerClient) SelectFiles(resolver scope.FileResolver) error { brokerID := c.broker.NextId() go c.broker.AcceptAndServe(brokerID, serverFunc) - _, err := c.client.SelectFiles(context.Background(), &proto.SelectFilesRequest{ + resp, err := c.client.SelectFiles(context.Background(), &proto.SelectFilesRequest{ FileResolverBrokerId: brokerID, }) + log.Debugf("Select Files Response: %+v", resp) + + if err != nil { + // TODO: nope + panic(err) + } + s.Stop() - return err + + var result []file.Reference + for _, f := range resp.Files { + result = append(result, file.NewFileReferenceWithID(file.Path(f.Path), uint64(f.Id))) + } + + return result } func (c *CatalogerClient) Catalog(contents map[file.Reference]io.Reader) ([]pkg.Package, error) { diff --git a/syft/plugin/grpc/file_resolver_client.go b/syft/plugin/grpc/file_resolver_client.go index cdc35a957..48627d6a3 100644 --- a/syft/plugin/grpc/file_resolver_client.go +++ b/syft/plugin/grpc/file_resolver_client.go @@ -2,6 +2,7 @@ package grpc import ( "context" + "github.com/anchore/syft/internal/log" "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/syft/syft/plugin/proto" @@ -37,6 +38,8 @@ func (m *FileResolverClient) FilesByGlob(patterns ...string) ([]file.Reference, return nil, err } + log.Debugf("FilesByGlob Response: %+v", resp) + var result []file.Reference for _, ref := range resp.Files { result = append(result, file.NewFileReferenceWithID(file.Path(ref.Path), uint64(ref.Id))) diff --git a/syft/plugin/grpc/file_resolver_server.go b/syft/plugin/grpc/file_resolver_server.go index 87c7096ec..186ec0c4f 100644 --- a/syft/plugin/grpc/file_resolver_server.go +++ b/syft/plugin/grpc/file_resolver_server.go @@ -2,6 +2,7 @@ package grpc import ( "context" + "github.com/anchore/syft/internal/log" "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/syft/syft/plugin/proto" @@ -36,11 +37,15 @@ func (m *FileResolverServer) FilesByPath(ctx context.Context, req *proto.FileRes } func (m *FileResolverServer) FilesByGlob(ctx context.Context, req *proto.FileResolverRequest) (resp *proto.FileResolverResponse, err error) { + log.Debugf("FilesByGlob Request: %+v", req) + r, err := m.Impl.FilesByGlob(req.Paths...) if err != nil { return nil, err } + log.Debugf("FilesByGlob Result: %+v", r) + var refs []*proto.FileReference for _, ref := range r { refs = append(refs, &proto.FileReference{ diff --git a/syft/plugin/plugin.go b/syft/plugin/plugin.go index 5e56febc0..413ceac2e 100644 --- a/syft/plugin/plugin.go +++ b/syft/plugin/plugin.go @@ -2,12 +2,14 @@ package plugin import ( "fmt" + "github.com/hashicorp/go-hclog" + "os" "os/exec" "github.com/hashicorp/go-plugin" ) -var plugins = map[int]plugin.PluginSet{ +var versionedPlugins = map[int]plugin.PluginSet{ 1: { TypeCataloger.String(): &CatalogerPlugin{}, }, @@ -19,8 +21,10 @@ type Plugin struct { client *plugin.Client } +// TODO: type should be in the name like with terraform "terraform--" + func NewPlugin(config Config) Plugin { - cmd := exec.Command(config.Command, config.Args...) + cmd := exec.Command("sh", "-c", config.Command) //, config.Args...) cmd.Env = append(cmd.Env, config.Env...) //secureConfig := &plugin.SecureConfig{ @@ -28,11 +32,19 @@ func NewPlugin(config Config) Plugin { // Hash: sha256.New(), //} + // TODO: temp? + logger := hclog.New(&hclog.LoggerOptions{ + Name: config.Name, + Level: hclog.Trace, + Output: os.Stderr, + }) + clientConfig := plugin.ClientConfig{ HandshakeConfig: config.Type.HandshakeConfig(), - VersionedPlugins: plugins, + VersionedPlugins: versionedPlugins, //SecureConfig: secureConfig, - Cmd: cmd, + Cmd: cmd, + Logger: logger, AllowedProtocols: []plugin.Protocol{ plugin.ProtocolGRPC, }, diff --git a/syft/plugin/repository.go b/syft/plugin/repository.go index fdad964d8..593f22749 100644 --- a/syft/plugin/repository.go +++ b/syft/plugin/repository.go @@ -63,7 +63,7 @@ func (c *Repository) ActivateCatalogers() ([]cataloger.Cataloger, func(), error) theCataloger, ok := raw.(cataloger.Cataloger) if !ok { - return nil, deactivateFn, fmt.Errorf("activation of cataloger did not return a cataloger object (name=%s)", plugin.Config.Name) + return nil, deactivateFn, fmt.Errorf("activation of cataloger did not return a cataloger object (name=%s type=%T)", plugin.Config.Name, theCataloger) } result = append(result, theCataloger)