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

solved locked routine when image has more than 1 tag #218

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
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: 16 additions & 21 deletions mainhandler/imageregistryhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (reg *registryAuth) initDefaultValues(ctx context.Context) error {
switch reg.AuthMethod {
case string(accessTokenAuth), "", "credentials":
if reg.Password == "" || reg.Username == "" {
return errorWithDocumentationRef("auth_method accesstoken requirers username and password")
return errorWithDocumentationRef("auth_method accesstoken requires username and password")
}
case "public":
//do nothing
Expand Down Expand Up @@ -209,7 +209,7 @@ func (reg *registryAuth) initDefaultValues(ctx context.Context) error {
return err
}
} else {
//try to get the kind from the reg name - if not found it will fallback to default kind
//try to get the kind from the reg name - if not found it will fall back to default kind
reg.Kind, _ = regCommon.GetRegistryKind(strings.Split(reg.Registry, "/")[0])
}
return err
Expand All @@ -219,7 +219,7 @@ func (rs *registryScan) filterRepositories(ctx context.Context, repos []string)
if len(rs.registryInfo.Include) == 0 && len(rs.registryInfo.Exclude) == 0 {
return repos
}
filteredRepos := []string{}
var filteredRepos []string
for _, repo := range repos {
// if rs.registry.projectID != "" {
// if !strings.Contains(repo, rs.registry.projectID+"/") {
Expand Down Expand Up @@ -279,7 +279,7 @@ func (registryScan *registryScan) createTriggerRequestSecret(k8sAPI *k8sinterfac
return nil
}

func (registryScan *registryScan) createTriggerRequestConfigMap(k8sAPI *k8sinterface.KubernetesApi, name, registryName string, webSocketScanCMD apis.Command) error {
func (registryScan *registryScan) createTriggerRequestConfigMap(k8sAPI *k8sinterface.KubernetesApi, name string) error {
configMap := corev1.ConfigMap{}
configMap.Name = name
if configMap.Labels == nil {
Expand All @@ -298,7 +298,7 @@ func (registryScan *registryScan) createTriggerRequestConfigMap(k8sAPI *k8sinter
}

// command will be mounted into cronjob by using this configmap
configMap.Data[requestBodyFile] = string(command)
configMap.Data[requestBodyFile] = command

if _, err := k8sAPI.KubernetesClient.CoreV1().ConfigMaps(registryScan.config.Namespace()).Create(context.Background(), &configMap, metav1.CreateOptions{}); err != nil {
return err
Expand All @@ -307,7 +307,7 @@ func (registryScan *registryScan) createTriggerRequestConfigMap(k8sAPI *k8sinter
}

func (registryScan *registryScan) getImagesForScanning(ctx context.Context, reporter beClientV1.IReportSender) error {
logger.L().Info("getImagesForScanning: enumerating repoes...")
logger.L().Info("getImagesForScanning: enumerating repos...")
errChan := make(chan error)
repos, err := registryScan.enumerateRepos(ctx)
if err != nil {
Expand Down Expand Up @@ -348,13 +348,13 @@ func (registryScan *registryScan) setImageToTagsMap(ctx context.Context, repo st
firstPage := regCommon.MakePagination(tagsPageSize)
latestTagFound := false
tagsDepth := registryScan.registryInfo.Depth
tags := []string{}
options := []remote.Option{}
var tags []string
var options []remote.Option
if registryScan.isPrivate() {
options = append(options, remote.WithAuth(registryScan.registryCredentials()))
}
if latestTags, err := iRegistry.GetLatestTags(repo, *tagsDepth, options...); err == nil {
tags := []string{}
var tags []string
for _, tag := range latestTags {
// filter out signature tags
if strings.HasSuffix(tag, ".sig") {
Expand All @@ -367,15 +367,10 @@ func (registryScan *registryScan) setImageToTagsMap(ctx context.Context, repo st
} else {
if tagsForDigestLen > *tagsDepth {
tags = append(tags, tagsForDigest[:*tagsDepth]...)
errMsg := fmt.Sprintf("image %s has %d tags. scanning only first %d tags - %s", repo, tagsForDigestLen, tagsDepth, strings.Join(tagsForDigest[:*tagsDepth], ","))
errMsg := fmt.Sprintf("image %s has %d tags. scanning only first %d tags - %s", repo, tagsForDigestLen, *tagsDepth, strings.Join(tagsForDigest[:*tagsDepth], ","))
if sender != nil {
errChan := make(chan error)
err := errorWithDocumentationRef(errMsg)
sender.SendWarning(err.Error(), registryScan.sendReport, true)
if err := <-errChan; err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

logger.L().Ctx(ctx).Error("GetLatestTags failed to send error report",
helpers.String("registry", registryScan.registry.hostname), helpers.Error(err))
}
}
logger.L().Ctx(ctx).Warning("GetImagesForScanning: " + errMsg)
} else {
Expand Down Expand Up @@ -538,7 +533,7 @@ func (registryScan *registryScan) getCommandForConfigMap() (string, error) {
return string(scanV1Bytes), nil
}

func (registryScan *registryScan) setCronJobTemplate(jobTemplateObj *v1.CronJob, name, schedule, jobID, registryName string) error {
func (registryScan *registryScan) setCronJobTemplate(jobTemplateObj *v1.CronJob, name, schedule, registryName string) error {
jobTemplateObj.Name = name
if schedule == "" {
return fmt.Errorf("schedule cannot be empty")
Expand Down Expand Up @@ -646,7 +641,7 @@ func (registryScan *registryScan) createTriggerRequestCronJob(k8sAPI *k8sinterfa
return err
}

err = registryScan.setCronJobTemplate(jobTemplateObj, name, getCronTabSchedule(command), command.JobTracking.JobID, registryName)
err = registryScan.setCronJobTemplate(jobTemplateObj, name, getCronTabSchedule(command), registryName)
if err != nil {
return err
}
Expand Down Expand Up @@ -769,9 +764,9 @@ func (registryScan *registryScan) getRegistryConfig(registryInfo *armotypes.Regi
if err != nil {
return string(cmDefaultMode), fmt.Errorf("error parsing ConfigMap: %s", err.Error())
}
for _, config := range registriesConfigs {
if config.Registry == registryInfo.RegistryName {
registryScan.setRegistryInfoFromConfigMap(registryInfo, config)
for _, c := range registriesConfigs {
if c.Registry == registryInfo.RegistryName {
registryScan.setRegistryInfoFromConfigMap(registryInfo, c)
return string(cmLoadedMode), nil
}
}
Expand Down Expand Up @@ -805,7 +800,7 @@ func getRegistryScanSecrets(k8sAPI IWorkloadsGetter, namespace, secretName strin
}

// when secret name is not provided, we will try to find all secrets starting with kubescape-registry-scan
registryScanSecrets := []k8sinterface.IWorkload{}
var registryScanSecrets []k8sinterface.IWorkload
all, err := k8sAPI.ListWorkloads2(namespace, "Secret")
if err == nil {
for _, secret := range all {
Expand Down
2 changes: 1 addition & 1 deletion mainhandler/imageregistryhandlerhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (actionHandler *ActionHandler) setRegistryScanCronJob(ctx context.Context,
}

// create configmap with POST data to trigger websocket
err = registryScan.createTriggerRequestConfigMap(actionHandler.k8sAPI, name, registryScan.registryInfo.RegistryName, sessionObj.Command)
err = registryScan.createTriggerRequestConfigMap(actionHandler.k8sAPI, name)
if err != nil {
logger.L().Info("In setRegistryScanCronJob: createTriggerRequestConfigMap failed", helpers.Error(err))
sessionObj.Reporter.SetDetails("createTriggerRequestConfigMap")
Expand Down
Loading