Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log and skip adapter ping error when retrieve adapter capability #20314

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/controller/scanner/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (
proScannerMetaKey = "projectScanner"
statusUnhealthy = "unhealthy"
statusHealthy = "healthy"
// RetrieveCapFailMsg the message indicate failed to retrieve the scanner capabilities
RetrieveCapFailMsg = "failed to retrieve scanner capabilities, error %v"
)

// DefaultController is a singleton api controller for plug scanners
Expand Down Expand Up @@ -80,8 +82,9 @@ func (bc *basicController) ListRegistrations(ctx context.Context, query *q.Query
return nil, errors.Wrap(err, "api controller: list registrations")
}
for _, r := range l {
if err := bc.appendCap(ctx, r); err != nil {
return nil, err
if err := bc.RetrieveCap(ctx, r); err != nil {
log.Warningf(RetrieveCapFailMsg, err)
return l, nil
}
}
return l, nil
Expand Down Expand Up @@ -129,13 +132,14 @@ func (bc *basicController) GetRegistration(ctx context.Context, registrationUUID
if r == nil {
return nil, nil
}
if err := bc.appendCap(ctx, r); err != nil {
return nil, err
if err := bc.RetrieveCap(ctx, r); err != nil {
log.Warningf(RetrieveCapFailMsg, err)
return r, nil
}
return r, nil
}

func (bc *basicController) appendCap(ctx context.Context, r *scanner.Registration) error {
func (bc *basicController) RetrieveCap(ctx context.Context, r *scanner.Registration) error {
mt, err := bc.Ping(ctx, r)
if err != nil {
logger.Errorf("Get registration error: %s", err)
Expand Down
3 changes: 3 additions & 0 deletions src/controller/scanner/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,7 @@ type Controller interface {
// *v1.ScannerAdapterMetadata : metadata returned by the scanner if successfully ping
// error : non nil error if any errors occurred
GetMetadata(ctx context.Context, registrationUUID string) (*v1.ScannerAdapterMetadata, error)

// RetrieveCap retrieve scanner capabilities
RetrieveCap(ctx context.Context, r *scanner.Registration) error
}
12 changes: 5 additions & 7 deletions src/server/v2.0/handler/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,18 +590,16 @@ func (a *projectAPI) GetScannerOfProject(ctx context.Context, params operation.G
return a.SendError(ctx, err)
}

scanner, err := a.scannerCtl.GetRegistrationByProject(ctx, p.ProjectID)
s, err := a.scannerCtl.GetRegistrationByProject(ctx, p.ProjectID)
if err != nil {
return a.SendError(ctx, err)
}
if scanner != nil {
metadata, err := a.scannerCtl.GetMetadata(ctx, scanner.UUID)
if err != nil {
return a.SendError(ctx, err)
if s != nil {
if err := a.scannerCtl.RetrieveCap(ctx, s); err != nil {
log.Warningf(scanner.RetrieveCapFailMsg, err)
}
scanner.Capabilities = metadata.ConvertCapability()
}
return operation.NewGetScannerOfProjectOK().WithPayload(model.NewScannerRegistration(scanner).ToSwagger(ctx))
return operation.NewGetScannerOfProjectOK().WithPayload(model.NewScannerRegistration(s).ToSwagger(ctx))
}

func (a *projectAPI) ListScannerCandidatesOfProject(ctx context.Context, params operation.ListScannerCandidatesOfProjectParams) middleware.Responder {
Expand Down
3 changes: 3 additions & 0 deletions src/server/v2.0/handler/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (suite *ProjectTestSuite) TestGetScannerOfProject() {
mock.OnAnything(suite.projectCtl, "Get").Return(suite.project, nil).Once()
mock.OnAnything(suite.scannerCtl, "GetRegistrationByProject").Return(nil, nil).Once()
mock.OnAnything(suite.scannerCtl, "GetMetadata").Return(suite.metadata, nil).Once()
mock.OnAnything(suite.scannerCtl, "RetrieveCap").Return(nil).Once()
res, err := suite.Get("/projects/1/scanner")
suite.NoError(err)
suite.Equal(200, res.StatusCode)
Expand All @@ -98,6 +99,7 @@ func (suite *ProjectTestSuite) TestGetScannerOfProject() {
mock.OnAnything(suite.projectCtl, "Get").Return(suite.project, nil).Once()
mock.OnAnything(suite.scannerCtl, "GetRegistrationByProject").Return(suite.reg, nil).Once()
mock.OnAnything(suite.scannerCtl, "GetMetadata").Return(suite.metadata, nil).Once()
mock.OnAnything(suite.scannerCtl, "RetrieveCap").Return(nil).Once()
var scanner scanner.Registration
res, err := suite.GetJSON("/projects/1/scanner", &scanner)
suite.NoError(err)
Expand All @@ -110,6 +112,7 @@ func (suite *ProjectTestSuite) TestGetScannerOfProject() {
mock.OnAnything(suite.projectCtl, "Get").Return(suite.project, nil).Once()
mock.OnAnything(suite.scannerCtl, "GetMetadata").Return(suite.metadata, nil).Once()
mock.OnAnything(suite.scannerCtl, "GetRegistrationByProject").Return(suite.reg, nil).Once()
mock.OnAnything(suite.scannerCtl, "RetrieveCap").Return(nil).Once()

var scanner scanner.Registration
res, err := suite.GetJSON("/projects/library/scanner", &scanner)
Expand Down
18 changes: 18 additions & 0 deletions src/testing/controller/scanner/controller.go

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

Loading