Skip to content

Commit

Permalink
🧹 Add shutdown to v9 plugins (#1632)
Browse files Browse the repository at this point in the history
This allows us to call clean-up funcs, when we close the connection.

Fixes #1628

---------

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Sep 8, 2023
1 parent 25b6c12 commit f3781a2
Show file tree
Hide file tree
Showing 50 changed files with 3,028 additions and 368 deletions.
7 changes: 7 additions & 0 deletions apps/provider-scaffold/template/provider/provider.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}, nil
}

// Shutdown is automatically called when the shell closes.
// It is not necessary to implement this method.
// If you want to do some cleanup, you can do it here.
func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) {
return &plugin.ShutdownRes{}, nil
}

func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*connection.{{ .CamelcaseProviderID }}Connection, error) {
if len(req.Asset.Connections) == 0 {
return nil, errors.New("no connection options for asset")
Expand Down
1 change: 1 addition & 0 deletions cli/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ func setConnector(provider *plugin.Provider, connector *plugin.Connector, run fu
}
}

// TODO: add flag to set timeout and then use RuntimeWithShutdownTimeout
runtime := providers.Coordinator.NewRuntime()

// TODO: read from config
Expand Down
2 changes: 1 addition & 1 deletion explorer/cnquery_explorer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion explorer/scan/cnquery_explorer_scan.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion llx/llx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions providers-sdk/v1/plugin/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (m *GRPCClient) Connect(req *ConnectReq, callback ProviderCallback) (*Conne
return res, err
}

func (m *GRPCClient) Shutdown(req *ShutdownReq) (*ShutdownRes, error) {
return m.client.Shutdown(context.Background(), req)
}

func (m *GRPCClient) GetData(req *DataReq) (*DataRes, error) {
return m.client.GetData(context.Background(), req)
}
Expand Down
1 change: 1 addition & 0 deletions providers-sdk/v1/plugin/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ProviderCallback interface {
type ProviderPlugin interface {
ParseCLI(req *ParseCLIReq) (*ParseCLIRes, error)
Connect(req *ConnectReq, callback ProviderCallback) (*ConnectRes, error)
Shutdown(req *ShutdownReq) (*ShutdownRes, error)
GetData(req *DataReq) (*DataRes, error)
StoreData(req *StoreReq) (*StoreRes, error)
}
Expand Down
351 changes: 231 additions & 120 deletions providers-sdk/v1/plugin/plugin.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions providers-sdk/v1/plugin/plugin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ message ConnectRes {
Inventory inventory = 4;
}

message ShutdownReq {
}

message ShutdownRes {
}

message DataReq {
uint32 connection = 1;
string resource = 3;
Expand Down Expand Up @@ -71,6 +77,7 @@ message StoreRes {}
service ProviderPlugin {
rpc ParseCLI(ParseCLIReq) returns (ParseCLIRes);
rpc Connect(ConnectReq) returns (ConnectRes);
rpc Shutdown(ShutdownReq) returns (ShutdownRes);
rpc GetData(DataReq) returns (DataRes);
rpc StoreData(StoreReq) returns (StoreRes);
}
Expand Down
37 changes: 37 additions & 0 deletions providers-sdk/v1/plugin/plugin_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion providers-sdk/v1/upstream/health/health.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion providers-sdk/v1/upstream/mvd/cvss/cvss.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion providers-sdk/v1/upstream/mvd/mvd.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion providers-sdk/v1/upstream/upstream.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions providers/arista/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}, nil
}

// Shutdown is automatically called when the shell closes.
// It is not necessary to implement this method.
// If you want to do some cleanup, you can do it here.
func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) {
return &plugin.ShutdownRes{}, nil
}

func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*connection.AristaConnection, error) {
if len(req.Asset.Connections) == 0 {
return nil, errors.New("no connection options for asset")
Expand Down
3 changes: 3 additions & 0 deletions providers/arista/resources/arista.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ resources:
name: {}
physicalAddress: {}
status: {}
is_private: true
min_mondoo_version: 5.15.0
platform:
name:
Expand All @@ -58,6 +59,7 @@ resources:
address: {}
mtu: {}
name: {}
is_private: true
min_mondoo_version: 5.15.0
platform:
name:
Expand All @@ -74,6 +76,7 @@ resources:
default: {}
name: {}
rules: {}
is_private: true
min_mondoo_version: 5.15.0
platform:
name:
Expand Down
7 changes: 7 additions & 0 deletions providers/aws/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ func (s *Service) detect(asset *inventory.Asset, conn *connection.AwsConnection)
return nil
}

// Shutdown is automatically called when the shell closes.
// It is not necessary to implement this method.
// If you want to do some cleanup, you can do it here.
func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) {
return &plugin.ShutdownRes{}, nil
}

func (s *Service) GetData(req *plugin.DataReq) (*plugin.DataRes, error) {
runtime, ok := s.runtimes[req.Connection]
if !ok {
Expand Down
7 changes: 7 additions & 0 deletions providers/azure/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}, nil
}

// Shutdown is automatically called when the shell closes.
// It is not necessary to implement this method.
// If you want to do some cleanup, you can do it here.
func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) {
return &plugin.ShutdownRes{}, nil
}

func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*connection.AzureConnection, error) {
if len(req.Asset.Connections) == 0 {
return nil, errors.New("no connection options for asset")
Expand Down
1 change: 1 addition & 0 deletions providers/azure/resources/azure.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ resources:
name: {}
osDisk: {}
properties: {}
publicIpAddresses: {}
tags: {}
type: {}
min_mondoo_version: latest
Expand Down
7 changes: 7 additions & 0 deletions providers/core/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}, nil
}

// Shutdown is automatically called when the shell closes.
// It is not necessary to implement this method.
// If you want to do some cleanup, you can do it here.
func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) {
return &plugin.ShutdownRes{}, nil
}

func (s *Service) GetData(req *plugin.DataReq) (*plugin.DataRes, error) {
runtime, ok := s.runtimes[req.Connection]
if !ok {
Expand Down
22 changes: 10 additions & 12 deletions providers/core/resources/core.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
resources:
asset:
fields:
name: {}
ids: {}
platform: {}
kind: {}
runtime: {}
version: {}
arch: {}
title: {}
build: {}
family: {}
fqdn: {}
build: {}
ids: {}
kind: {}
labels: {}
name: {}
platform: {}
runtime: {}
title: {}
version: {}
vulnerabilityReport: {}
min_mondoo_version: 6.13.0
audit.advisory:
Expand All @@ -27,7 +27,6 @@ resources:
published: {}
title: {}
worstScore: {}
is_private: true
min_mondoo_version: 5.15.0
audit.cve:
fields:
Expand All @@ -39,21 +38,18 @@ resources:
summary: {}
unscored: {}
worstScore: {}
is_private: true
min_mondoo_version: 5.15.0
audit.cvss:
fields:
score: {}
vector: {}
is_private: true
min_mondoo_version: 5.15.0
audit.exploit:
fields:
id: {}
modified: {}
mrn: {}
worstScore: {}
is_private: true
min_mondoo_version: 5.15.0
authorizedkeys:
fields:
Expand Down Expand Up @@ -146,6 +142,8 @@ resources:
min_mondoo_version: 5.15.0
mondoo:
fields:
arch:
min_mondoo_version: latest
build: {}
capabilities: {}
jobEnvironment: {}
Expand Down
Loading

0 comments on commit f3781a2

Please sign in to comment.