diff --git a/lunatrace/bsl/ingest-worker/cmd/ingestworker/main.go b/lunatrace/bsl/ingest-worker/cmd/ingestworker/main.go index ee1a90364..e166fa9c4 100644 --- a/lunatrace/bsl/ingest-worker/cmd/ingestworker/main.go +++ b/lunatrace/bsl/ingest-worker/cmd/ingestworker/main.go @@ -11,7 +11,6 @@ package main import ( - "context" "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/cmd/ingestworker/cwe" packageCommand "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/cmd/ingestworker/package" "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/cmd/ingestworker/vulnerability" @@ -23,7 +22,6 @@ import ( "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/metadata/replicator" "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/scanner/licensecheck" "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/scanner/packagejson" - "github.com/lunasec-io/lunasec/lunatrace/cli/pkg/util" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "net/http" @@ -49,12 +47,6 @@ func main() { dbfx.Module, registry.NPMModule, - fx.Invoke(func() { - util.RunOnProcessExit(func() { - util.RemoveCleanupDirs() - }) - }), - fx.Provide( cwe2.NewCWEIngester, ), @@ -86,12 +78,5 @@ func main() { fx.Provide( packageCommand.NewCommand, ), - - fx.Invoke(func(lc fx.Lifecycle) { - lc.Append(fx.Hook{OnStop: func(_ context.Context) error { - util.RemoveCleanupDirs() - return nil - }}) - }), ) } diff --git a/lunatrace/bsl/ingest-worker/pkg/vulnerability/ingester.go b/lunatrace/bsl/ingest-worker/pkg/vulnerability/ingester.go index f9ae730f0..0b1a98bbb 100644 --- a/lunatrace/bsl/ingest-worker/pkg/vulnerability/ingester.go +++ b/lunatrace/bsl/ingest-worker/pkg/vulnerability/ingester.go @@ -171,7 +171,9 @@ func (f FileAdvisoryIngester) upsertVulnerabilities( } func (f FileAdvisoryIngester) IngestVulnerabilitiesFromSource(advisoryLocation, source, sourceRelativePath string) error { - advisoryLocation, err := ensureAdvisoriesExistFromSource(source, advisoryLocation) + advisoryLocation, cleanup, err := ensureAdvisoriesExistFromSource(source, advisoryLocation) + + defer cleanup() if err != nil { return err } diff --git a/lunatrace/bsl/ingest-worker/pkg/vulnerability/source.go b/lunatrace/bsl/ingest-worker/pkg/vulnerability/source.go index 20044d8a7..757090c4c 100644 --- a/lunatrace/bsl/ingest-worker/pkg/vulnerability/source.go +++ b/lunatrace/bsl/ingest-worker/pkg/vulnerability/source.go @@ -1,6 +1,6 @@ // Copyright by LunaSec (owned by Refinery Labs, Inc) // -// Licensed under the Business Source License v1.1 +// Licensed under the Business Source License v1.1 // (the "License"); you may not use this file except in compliance with the // License. You may obtain a copy of the License at // @@ -8,13 +8,11 @@ // // See the License for the specific language governing permissions and // limitations under the License. -// package vulnerability import ( "fmt" "github.com/go-git/go-git/v5" - "github.com/lunasec-io/lunasec/lunatrace/cli/pkg/util" "github.com/rs/zerolog/log" "io/ioutil" "os" @@ -38,9 +36,11 @@ func pullVulnerabilitiesFromSource(source, dst string) error { return fmt.Errorf("cannot pull vulnerabilities for source: %s", source) } -func ensureAdvisoriesExistFromSource(source, advisoryDir string) (string, error) { +func ensureAdvisoriesExistFromSource(source, advisoryDir string) (string, func(), error) { + cleanup := func() {} + if advisoryDir != "" { - return advisoryDir, nil + return advisoryDir, cleanup, nil } advisoryDir, err := ioutil.TempDir("", source+"-advisories") @@ -48,9 +48,17 @@ func ensureAdvisoriesExistFromSource(source, advisoryDir string) (string, error) log.Error(). Err(err). Msg("unable to create temporary directory for advisories") - return "", err + return "", cleanup, err + } + cleanup = func() { + err = os.RemoveAll(advisoryDir) + if err != nil { + log.Error(). + Err(err). + Str("advisory dir", advisoryDir). + Msg("failed to remove temporary advisory location") + } } - util.EnsureDirIsCleanedUp(advisoryDir) log.Info(). Str("source", source). @@ -61,12 +69,12 @@ func ensureAdvisoriesExistFromSource(source, advisoryDir string) (string, error) log.Error(). Err(err). Msg("unable to pull vulnerabilities from source") - return "", err + return "", cleanup, err } log.Info(). Str("source", source). Msg("collected advisories from source") - return advisoryDir, nil + return advisoryDir, cleanup, nil }