Skip to content

Commit

Permalink
Cleanup tmp directory in the vulnerability ingestion service (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
breadchris authored Dec 7, 2022
1 parent a91c3a2 commit 0e56c9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
15 changes: 0 additions & 15 deletions lunatrace/bsl/ingest-worker/cmd/ingestworker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -49,12 +47,6 @@ func main() {
dbfx.Module,
registry.NPMModule,

fx.Invoke(func() {
util.RunOnProcessExit(func() {
util.RemoveCleanupDirs()
})
}),

fx.Provide(
cwe2.NewCWEIngester,
),
Expand Down Expand Up @@ -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
}})
}),
)
}
4 changes: 3 additions & 1 deletion lunatrace/bsl/ingest-worker/pkg/vulnerability/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 17 additions & 9 deletions lunatrace/bsl/ingest-worker/pkg/vulnerability/source.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
// 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
//
// https://github.com/lunasec-io/lunasec/blob/master/licenses/BSL-LunaTrace.txt
//
// 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"
Expand All @@ -38,19 +36,29 @@ 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")
if err != nil {
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).
Expand All @@ -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
}

0 comments on commit 0e56c9c

Please sign in to comment.