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

🐛 Compute upstream config correctly with an inventory passed in #1828

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func (s *LocalScanner) Run(ctx context.Context, job *Job) (*explorer.ReportColle
return nil, errors.New("no context provided to run job with local scanner")
}

reports, _, err := s.distributeJob(job, ctx, s.upstream)
upstreamConfig, err := s.getUpstreamConfig(job.Inventory, false)
if err != nil {
return nil, err
}
reports, _, err := s.distributeJob(job, ctx, upstreamConfig)
if err != nil {
if code := status.Code(err); code == codes.Unauthenticated {
return nil, multierr.Wrap(err,
Expand All @@ -104,6 +108,23 @@ func (s *LocalScanner) Run(ctx context.Context, job *Job) (*explorer.ReportColle
return reports, nil
}

// returns the upstream config for the job. If the job has a specified config, it has precedence
// over the automatically detected one
func (s *LocalScanner) getUpstreamConfig(inv *inventory.Inventory, incognito bool) (*upstream.UpstreamConfig, error) {
jobCreds := inv.GetSpec().GetUpstreamCredentials()
if s.upstream == nil && jobCreds == nil {
return nil, errors.New("no default or job upstream config provided")
}
u := proto.Clone(s.upstream).(*upstream.UpstreamConfig)
u.Incognito = incognito
if jobCreds != nil {
u.ApiEndpoint = jobCreds.GetApiEndpoint()
u.Creds = jobCreds
u.SpaceMrn = jobCreds.GetParentMrn()
}
return u, nil
}

func (s *LocalScanner) RunIncognito(ctx context.Context, job *Job) (*explorer.ReportCollection, error) {
if job == nil {
return nil, status.Errorf(codes.InvalidArgument, "missing scan job")
Expand All @@ -117,13 +138,11 @@ func (s *LocalScanner) RunIncognito(ctx context.Context, job *Job) (*explorer.Re
return nil, errors.New("no context provided to run job with local scanner")
}

var upstreamConf *upstream.UpstreamConfig
if s.upstream != nil {
upstreamConf = proto.Clone(s.upstream).(*upstream.UpstreamConfig)
upstreamConf.Incognito = true
upstreamConfig, err := s.getUpstreamConfig(job.Inventory, true)
if err != nil {
return nil, err
}

reports, _, err := s.distributeJob(job, ctx, upstreamConf)
reports, _, err := s.distributeJob(job, ctx, upstreamConfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -393,8 +412,8 @@ func (s *LocalScanner) runMotorizedAsset(job *AssetJob) (*AssetReport, error) {

runtimeErr := inmemory.WithDb(job.runtime, func(db *inmemory.Db, services *explorer.LocalServices) error {
if job.UpstreamConfig != nil && job.UpstreamConfig.ApiEndpoint != "" && !job.UpstreamConfig.Incognito {
log.Debug().Msg("using API endpoint " + s.upstream.ApiEndpoint)
client, err := s.upstream.InitClient()
log.Debug().Msg("using API endpoint " + job.UpstreamConfig.ApiEndpoint)
client, err := job.UpstreamConfig.InitClient()
if err != nil {
return err
}
Expand Down