mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-03 14:37:44 +01:00
refactor(scanners): Allow RemoveImmutableScanners() to accept multiple endpoint URLs
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
This commit is contained in:
parent
1b60bb255c
commit
93f05b3643
@ -208,52 +208,7 @@ func main() {
|
||||
log.Fatalf("Failed to initialize API handlers with error: %s", err.Error())
|
||||
}
|
||||
|
||||
if config.WithTrivy() {
|
||||
log.Debug("Registering Trivy scanner")
|
||||
reg := &scanner.Registration{
|
||||
Name: "Trivy",
|
||||
Description: "The Trivy scanner adapter",
|
||||
URL: config.TrivyAdapterURL(),
|
||||
UseInternalAddr: true,
|
||||
Immutable: true,
|
||||
}
|
||||
if err := scan.EnsureScanner(reg, true); err != nil {
|
||||
log.Fatalf("failed to register Trivy scanner: %v", err)
|
||||
}
|
||||
} else {
|
||||
log.Debug("Removing Trivy scanner")
|
||||
if err := scan.RemoveImmutableScanner(config.TrivyAdapterURL()); err != nil {
|
||||
log.Warningf("failed to remove Trivy scanner: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if config.WithClair() {
|
||||
clairDB, err := config.ClairDB()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to load clair database information: %v", err)
|
||||
}
|
||||
if err := dao.InitClairDB(clairDB); err != nil {
|
||||
log.Fatalf("failed to initialize clair database: %v", err)
|
||||
}
|
||||
|
||||
log.Debug("Registering Clair scanner")
|
||||
reg := &scanner.Registration{
|
||||
Name: "Clair",
|
||||
Description: "The clair scanner adapter",
|
||||
URL: config.ClairAdapterEndpoint(),
|
||||
UseInternalAddr: true,
|
||||
Immutable: true,
|
||||
}
|
||||
|
||||
if err := scan.EnsureScanner(reg, true); err != nil {
|
||||
log.Fatalf("failed to register Clair scanner: %v", err)
|
||||
}
|
||||
} else {
|
||||
log.Debug("Removing Clair scanner")
|
||||
if err := scan.RemoveImmutableScanner(config.ClairAdapterEndpoint()); err != nil {
|
||||
log.Warningf("failed to remove Clair scanners: %v", err)
|
||||
}
|
||||
}
|
||||
registerScanners()
|
||||
|
||||
closing := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
@ -312,3 +267,55 @@ func main() {
|
||||
|
||||
beego.RunWithMiddleWares("", middlewares.MiddleWares()...)
|
||||
}
|
||||
|
||||
func registerScanners() {
|
||||
uninstallURLs := make([]string, 0)
|
||||
|
||||
if config.WithTrivy() {
|
||||
log.Debug("Registering Trivy scanner")
|
||||
reg := &scanner.Registration{
|
||||
Name: "Trivy",
|
||||
Description: "The Trivy scanner adapter",
|
||||
URL: config.TrivyAdapterURL(),
|
||||
UseInternalAddr: true,
|
||||
Immutable: true,
|
||||
}
|
||||
if err := scan.EnsureScanner(reg, true); err != nil {
|
||||
log.Fatalf("failed to register Trivy scanner: %v", err)
|
||||
}
|
||||
} else {
|
||||
log.Debug("Removing Trivy scanner")
|
||||
uninstallURLs = append(uninstallURLs, config.TrivyAdapterURL())
|
||||
}
|
||||
|
||||
if config.WithClair() {
|
||||
clairDB, err := config.ClairDB()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to load clair database information: %v", err)
|
||||
}
|
||||
if err := dao.InitClairDB(clairDB); err != nil {
|
||||
log.Fatalf("failed to initialize clair database: %v", err)
|
||||
}
|
||||
|
||||
log.Debug("Registering Clair scanner")
|
||||
reg := &scanner.Registration{
|
||||
Name: "Clair",
|
||||
Description: "The clair scanner adapter",
|
||||
URL: config.ClairAdapterEndpoint(),
|
||||
UseInternalAddr: true,
|
||||
Immutable: true,
|
||||
}
|
||||
|
||||
if err := scan.EnsureScanner(reg, true); err != nil {
|
||||
log.Fatalf("failed to register Clair scanner: %v", err)
|
||||
}
|
||||
} else {
|
||||
log.Debug("Removing Clair scanner")
|
||||
uninstallURLs = append(uninstallURLs, config.ClairAdapterEndpoint())
|
||||
}
|
||||
|
||||
if err := scan.RemoveImmutableScanners(uninstallURLs); err != nil {
|
||||
log.Warningf("failed to remove scanners: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -86,23 +86,27 @@ func EnsureScanner(registration *scanner.Registration, resolveConflicts ...bool)
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveImmutableScanner removes an immutable scanner with the specified endpoint URL.
|
||||
func RemoveImmutableScanner(registrationURL string) error {
|
||||
// RemoveImmutableScanners removes immutable scanner Registrations with the specified endpoint URLs.
|
||||
func RemoveImmutableScanners(urls []string) error {
|
||||
if len(urls) == 0 {
|
||||
return nil
|
||||
}
|
||||
query := &q.Query{
|
||||
Keywords: map[string]interface{}{
|
||||
"immutable": true,
|
||||
"url": registrationURL,
|
||||
"ex_url__in": urls,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO Instead of executing 1 to N SQL queries we might want to delete multiple rows with scannerManager.DeleteByImmutableAndURLIn(true, []string{})
|
||||
registrations, err := scannerManager.List(query)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Errorf("listing scanners: %v", err)
|
||||
}
|
||||
|
||||
for _, reg := range registrations {
|
||||
if err := scannerManager.Delete(reg.UUID); err != nil {
|
||||
return err
|
||||
return errors.Errorf("deleting scanner: %s: %v", reg.UUID, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -213,3 +213,99 @@ func TestEnsureScannerWithResolveConflict(t *testing.T) {
|
||||
assert.Error(EnsureScanner(&scanner.Registration{Name: "Clair", URL: "reg2"}))
|
||||
assert.Nil(EnsureScanner(&scanner.Registration{Name: "Clair", URL: "reg2"}, true))
|
||||
}
|
||||
|
||||
func TestRemoveImmutableScanners(t *testing.T) {
|
||||
|
||||
t.Run("Should do nothing when list of URLs is empty", func(t *testing.T) {
|
||||
mgr := &mocks.Manager{}
|
||||
scannerManager = mgr
|
||||
|
||||
err := RemoveImmutableScanners([]string{})
|
||||
assert.NoError(t, err)
|
||||
mgr.AssertExpectations(t)
|
||||
})
|
||||
|
||||
t.Run("Should return error when listing scanners fails", func(t *testing.T) {
|
||||
mgr := &mocks.Manager{}
|
||||
scannerManager = mgr
|
||||
|
||||
mgr.On("List", &q.Query{
|
||||
Keywords: map[string]interface{}{
|
||||
"immutable": true,
|
||||
"ex_url__in": []string{"http://scanner:8080"},
|
||||
},
|
||||
}).Return(nil, errors.New("DB error"))
|
||||
|
||||
err := RemoveImmutableScanners([]string{"http://scanner:8080"})
|
||||
assert.EqualError(t, err, "listing scanners: DB error")
|
||||
mgr.AssertExpectations(t)
|
||||
})
|
||||
|
||||
t.Run("Should delete multiple scanners", func(t *testing.T) {
|
||||
mgr := &mocks.Manager{}
|
||||
scannerManager = mgr
|
||||
|
||||
registrations := []*scanner.Registration{
|
||||
{
|
||||
UUID: "uuid-1",
|
||||
URL: "http://scanner-1",
|
||||
},
|
||||
{
|
||||
UUID: "uuid-2",
|
||||
URL: "http://scanner-2",
|
||||
}}
|
||||
|
||||
mgr.On("List", &q.Query{
|
||||
Keywords: map[string]interface{}{
|
||||
"immutable": true,
|
||||
"ex_url__in": []string{
|
||||
"http://scanner-1",
|
||||
"http://scanner-2",
|
||||
},
|
||||
},
|
||||
}).Return(registrations, nil)
|
||||
mgr.On("Delete", "uuid-1").Return(nil)
|
||||
mgr.On("Delete", "uuid-2").Return(nil)
|
||||
|
||||
err := RemoveImmutableScanners([]string{
|
||||
"http://scanner-1",
|
||||
"http://scanner-2",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
mgr.AssertExpectations(t)
|
||||
})
|
||||
|
||||
t.Run("Should return error when deleting any scanner fails", func(t *testing.T) {
|
||||
mgr := &mocks.Manager{}
|
||||
scannerManager = mgr
|
||||
|
||||
registrations := []*scanner.Registration{
|
||||
{
|
||||
UUID: "uuid-1",
|
||||
URL: "http://scanner-1",
|
||||
},
|
||||
{
|
||||
UUID: "uuid-2",
|
||||
URL: "http://scanner-2",
|
||||
}}
|
||||
|
||||
mgr.On("List", &q.Query{
|
||||
Keywords: map[string]interface{}{
|
||||
"immutable": true,
|
||||
"ex_url__in": []string{
|
||||
"http://scanner-1",
|
||||
"http://scanner-2",
|
||||
},
|
||||
},
|
||||
}).Return(registrations, nil)
|
||||
mgr.On("Delete", "uuid-1").Return(nil)
|
||||
mgr.On("Delete", "uuid-2").Return(errors.New("DB error"))
|
||||
|
||||
err := RemoveImmutableScanners([]string{
|
||||
"http://scanner-1",
|
||||
"http://scanner-2",
|
||||
})
|
||||
assert.EqualError(t, err, "deleting scanner: uuid-2: DB error")
|
||||
mgr.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user