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

Add EPSS Ingestor command to ingest-worker #1070

Merged
merged 9 commits into from
Jan 18, 2023
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

51 changes: 51 additions & 0 deletions lunatrace/bsl/ingest-worker/cmd/ingestworker/epss/epss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright by LunaSec (owned by Refinery Labs, Inc)
//
// 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 epss

import (
"github.com/ajvpot/clifx"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/epss"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
)

type Params struct {
fx.In

Ingester epss.EPSSIngester
}

func NewCommand(p Params) clifx.CommandResult {
return clifx.CommandResult{
Command: &cli.Command{
Name: "epss",
Subcommands: []*cli.Command{
{
Name: "ingest",
Usage: "[file or directory]",
Flags: []cli.Flag{},
Subcommands: []*cli.Command{},
Action: func(ctx *cli.Context) error {
log.Info().
Msg("Updating EPSS Scores")
err := p.Ingester.Ingest(ctx.Context)
if err == nil {
log.Info().
Msg("Updated EPSS Scores")
}
return err
},
},
},
},
}
}
80 changes: 42 additions & 38 deletions lunatrace/bsl/ingest-worker/cmd/ingestworker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ package main

import (
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/cmd/ingestworker/cwe"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/cmd/ingestworker/epss"
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"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/config/ingestworker"
cwe2 "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/cwe"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/dbfx"
epss2 "github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/epss"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/graphqlfx"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/metadata/registry"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/metadata/replicator"
Expand All @@ -37,46 +39,48 @@ import (
)

func main() {
// TODO (cthompson) this should be configured with an fx module
log.Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
// TODO (cthompson) this should be configured with an fx module
log.Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()

clifx2.Main(
fx.Supply(http.DefaultClient),
clifx2.Main(
fx.Supply(http.DefaultClient),

graphqlfx.Module,
dbfx.Module,
registry.NPMModule,
graphqlfx.Module,
dbfx.Module,
registry.NPMModule,

fx.Provide(
cwe2.NewCWEIngester,
),
fx.Provide(
cwe2.NewCWEIngester,
epss2.NewEPSSIngester,
),

// todo make a module
fx.Supply(&clifx2.AppConfig{
Name: "ingestworker",
Usage: "LunaTrace Ingest Worker",
Version: "0.0.1",
}),
fx.Provide(
ingester.NewPackageSqlIngester,
ingester.NewNPMPackageIngester,
replicator.NewNPMReplicator,
),
fx.Provide(
ingestworker.NewConfigProvider,
),
fx.Provide(
licensecheck.NewScanner,
packagejson.NewScanner,
license.NewCommand,
vulnmanager.NewFileIngester,
),
fx.Provide(
vulnerability.NewCommand,
cwe.NewCommand,
),
fx.Provide(
packageCommand.NewCommand,
),
)
// todo make a module
fx.Supply(&clifx2.AppConfig{
Name: "ingestworker",
Usage: "LunaTrace Ingest Worker",
Version: "0.0.1",
}),
fx.Provide(
ingester.NewPackageSqlIngester,
ingester.NewNPMPackageIngester,
replicator.NewNPMReplicator,
),
fx.Provide(
ingestworker.NewConfigProvider,
),
fx.Provide(
licensecheck.NewScanner,
packagejson.NewScanner,
license.NewCommand,
vulnmanager.NewFileIngester,
),
fx.Provide(
vulnerability.NewCommand,
cwe.NewCommand,
epss.NewCommand,
),
fx.Provide(
packageCommand.NewCommand,
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
package vulnerability

import (
"context"
"github.com/go-co-op/gocron"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/cwe"
"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/epss"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
Expand All @@ -27,8 +29,9 @@ import (
type Params struct {
fx.In

Ingester vulnerability.FileAdvisoryIngester
CWEIngester cwe.CWEIngester
Ingester vulnerability.FileAdvisoryIngester
CWEIngester cwe.CWEIngester
EPSSIngester epss.EPSSIngester
}

func NewCommand(p Params) clifx.CommandResult {
Expand Down Expand Up @@ -62,22 +65,52 @@ func NewCommand(p Params) clifx.CommandResult {
sourceRelativePath := ctx.String("source-relative-path")
cron := ctx.String("cron")

log.Info().
Msg("Updating CWEs")
err := p.CWEIngester.Ingest(ctx.Context)
if err == nil {
runIngestion := func() error {
log.Info().
Msg("Updating CWEs")

err := p.CWEIngester.Ingest(ctx.Context)

if err != nil {
freeqaz marked this conversation as resolved.
Show resolved Hide resolved
log.Error().
Err(err).
Msg("failed to update CWEs")
}

log.Info().
Msg("Updated CWEs")
} else {
return err
}

runIngestion := func() error {
log.Info().
Str("source", source).
Str("cron", cron).
Msg("starting vulnerability ingestion")
return p.Ingester.IngestVulnerabilitiesFromSource(advisoryLocation, source, sourceRelativePath)
err = p.Ingester.IngestVulnerabilitiesFromSource(advisoryLocation, source, sourceRelativePath)

if err != nil {
log.Error().
Err(err).
Str("source", source).
Str("cron", cron).
Msg("failed to ingest vulnerabilities")
}

log.Info().
Str("source", source).
Str("cron", cron).
Msg("starting epss ingestion")

epssContext := context.Background()
err = p.EPSSIngester.Ingest(epssContext)

if err != nil {
log.Error().
Err(err).
Str("source", source).
Str("cron", cron).
Msg("failed to ingest epss")
}

return nil
}

if cron != "" {
Expand Down
19 changes: 19 additions & 0 deletions lunatrace/bsl/ingest-worker/pkg/cwe/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (s *cweIngester) Ingest(ctx context.Context) error {
return err
}

log.Info().
Int("count", len(cwes.Weaknesses)).
Msg("fetched CWEs from MITRE successfully")

for _, weakness := range cwes.Weaknesses {
weaknessIdStr := weakness.ID

Expand All @@ -73,7 +77,22 @@ func (s *cweIngester) Ingest(ctx context.Context) error {
gql.Vulnerability_cwe_update_columnExtendedDescription,
gql.Vulnerability_cwe_update_columnCommonName,
})

if err != nil {
log.Error().
Err(err).
Msg("error inserting CWE")
return err
}

log.Info().
Int("id", weaknessId).
Msg("inserted CWE")
}

log.Info().
Msg("ingested CWEs successfully")

return nil
}

Expand Down