replace AnalysePackage with QueryPackageAnalysis API & use community API

This commit is contained in:
Sahilb315
2025-05-09 03:05:50 +05:30
parent 6a28fb16a1
commit 1dd07f13bb
5 changed files with 15 additions and 48 deletions
-4
View File
@@ -24,10 +24,6 @@ func NewNpmCommand() *cobra.Command {
}
if len(args) >= 2 && utils.IsInstallCommand(string(registry.RegistryNPM), args[0]) {
if err := utils.ValidateEnvVars(); err != nil {
return err
}
pmw := wrapper.NewPackageManagerWrapper(registry.RegistryNPM)
pmw.Action = args[0]
pmw.PackageName = args[1]
-4
View File
@@ -24,10 +24,6 @@ func NewPnpmCommand() *cobra.Command {
}
if len(args) >= 2 && utils.IsInstallCommand(string(registry.RegistryPNPM), args[0]) {
if err := utils.ValidateEnvVars(); err != nil {
return err
}
pmw := wrapper.NewPackageManagerWrapper(registry.RegistryPNPM)
pmw.Action = args[0]
pmw.PackageName = args[1]
+12 -29
View File
@@ -21,31 +21,26 @@ type PackageAnalyser struct {
Ctx context.Context
MaliciousPkgsMutex sync.Mutex
ProgressTracker ui.ProgressTracker
Ecosystem packagev1.Ecosystem
}
func New(client malysisv1grpc.MalwareAnalysisServiceClient, ctx context.Context) *PackageAnalyser {
func New(client malysisv1grpc.MalwareAnalysisServiceClient, ctx context.Context, ecosystem packagev1.Ecosystem) *PackageAnalyser {
return &PackageAnalyser{
MaliciousPkgs: make(map[string]string),
Client: client,
Ctx: ctx,
MaliciousPkgsMutex: sync.Mutex{},
Ecosystem: ecosystem,
}
}
func (ap *PackageAnalyser) Handler() vetUtils.WorkQueueFn[models.Package] {
return func(q *vetUtils.WorkQueue[models.Package], item models.Package) error {
resp, err := SubmitPackageForAnalysis(ap.Ctx, ap.Client,
packagev1.Ecosystem_ECOSYSTEM_NPM, item.Name, item.Version)
reportResp, err := QueryPackageAnalysis(ap.Ctx, ap.Client,
ap.Ecosystem, item.Name, item.Version)
if err != nil {
log.Debugf("Failed to analyze %s@%s: %v", item.Name, item.Version, err)
return err
}
reportResp, err := GetAnalysisReport(ap.Ctx, ap.Client, resp.GetAnalysisId())
if err != nil {
log.Debugf("Failed to get analysis report for %s:%s %v",
item.Name, resp.GetAnalysisId(), err)
return err
return nil
}
report := reportResp.GetReport()
@@ -73,10 +68,9 @@ func (ap *PackageAnalyser) Handler() vetUtils.WorkQueueFn[models.Package] {
}
}
func SubmitPackageForAnalysis(ctx context.Context, client malysisv1grpc.MalwareAnalysisServiceClient,
ecosystem packagev1.Ecosystem, name string,
version string) (*malysisv1.AnalyzePackageResponse, error) {
req := &malysisv1.AnalyzePackageRequest{
func QueryPackageAnalysis(ctx context.Context, client malysisv1grpc.MalwareAnalysisServiceClient, ecosystem packagev1.Ecosystem, name string,
version string) (*malysisv1.QueryPackageAnalysisResponse, error) {
resp, err := client.QueryPackageAnalysis(ctx, &malysisv1.QueryPackageAnalysisRequest{
Target: &malysisv1pb.PackageAnalysisTarget{
PackageVersion: &packagev1.PackageVersion{
Package: &packagev1.Package{
@@ -86,22 +80,11 @@ func SubmitPackageForAnalysis(ctx context.Context, client malysisv1grpc.MalwareA
Version: version,
},
},
}
resp, err := client.AnalyzePackage(ctx, req)
})
if err != nil {
return nil, fmt.Errorf("failed to analyze %s@%s: %w", name, version, err)
}
return resp, nil
}
func GetAnalysisReport(ctx context.Context, client malysisv1grpc.MalwareAnalysisServiceClient,
analysisId string) (*malysisv1.GetAnalysisReportResponse, error) {
analysisReportReq := &malysisv1.GetAnalysisReportRequest{
AnalysisId: analysisId,
}
reportResp, err := client.GetAnalysisReport(ctx, analysisReportReq)
if err != nil {
return nil, fmt.Errorf("failed to get analysis report: %w", err)
}
return reportResp, nil
}
+1 -10
View File
@@ -5,20 +5,11 @@ import (
"net/http"
drygrpc "github.com/safedep/dry/adapters/grpc"
"github.com/safedep/pmg/pkg/common/utils"
"google.golang.org/grpc"
)
func NewCloudClientConnection() (*grpc.ClientConn, error) {
tok := utils.ApiKey()
tenantId := utils.TenantDomain()
if tok == "" || tenantId == "" {
return nil, fmt.Errorf("SAFEDEP_API_KEY and SAFEDEP_TENANT_ID must be set")
}
headers := http.Header{}
headers.Set("x-tenant-id", tenantId)
cc, err := newGrpcClient(headers, tok, "pmg-pkg-scan", "api.safedep.io", "443")
cc, err := newGrpcClient(http.Header{}, "", "pmg-pkg-scan", "community-api.safedep.io", "443")
if err != nil {
return nil, fmt.Errorf("failed to create gRPC client: %v", err)
}
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"time"
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
"github.com/fatih/color"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/internal/ui"
@@ -100,7 +101,7 @@ func (pmw *PackageManagerWrapper) analyzeDependencies(ctx context.Context, deps
return fmt.Errorf("error while creating a malware analysis client: %w", err)
}
pkgAnalyser := analyser.New(client, ctx)
pkgAnalyser := analyser.New(client, ctx, packagev1.Ecosystem_ECOSYSTEM_NPM)
pkgAnalyser.ProgressTracker = progressTracker
handler := pkgAnalyser.Handler()