Skip to content

Commit

Permalink
🐛 ensure gitlab discovered assets have unique platform ids (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey authored Sep 18, 2023
1 parent 602bef1 commit 71c6349
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions motor/discovery/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package gitlab
import (
"context"
"errors"
"github.com/rs/zerolog/log"
terraform_resolver "go.mondoo.com/cnquery/motor/discovery/terraform"
"os"
"strings"

"github.com/rs/zerolog/log"
terraform_resolver "go.mondoo.com/cnquery/motor/discovery/terraform"

"github.com/xanzy/go-gitlab"
gitlab_lib "github.com/xanzy/go-gitlab"
"go.mondoo.com/cnquery/motor/asset"
Expand Down Expand Up @@ -47,11 +48,6 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
return nil, errors.New("could not initialize gitlab transport")
}

identifier, err := p.Identifier()
if err != nil {
return nil, err
}

pf, err := m.Platform()
if err != nil {
return nil, err
Expand All @@ -72,7 +68,10 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
name = project.NameWithNamespace
}
}

identifier, err := p.Identifier()
if err != nil {
return nil, err
}
projectAsset := &asset.Asset{
PlatformIds: []string{identifier},
Name: name,
Expand Down Expand Up @@ -133,7 +132,10 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
name = "GitLab Group " + grp.Name
}
}

identifier, err := p.Identifier()
if err != nil {
return nil, err
}
list = append(list, &asset.Asset{
PlatformIds: []string{identifier},
Name: name,
Expand All @@ -153,8 +155,9 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
clonedConfig.Options["group"] = grp.Path
clonedConfig.Options["project"] = project.Path

id := gitlab_provider.NewGitLabProjectIdentifier(grp.Name, project.Name)
projectAsset := &asset.Asset{
PlatformIds: []string{identifier},
PlatformIds: []string{id},
Name: project.NameWithNamespace,
Platform: gitlab_provider.GitLabProjectPlatform,
Connections: []*providers.Config{clonedConfig}, // pass-in the current config
Expand All @@ -166,9 +169,17 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
terraformFiles, err := discoverTerraformHcl(ctx, p.Client(), grp.Path, project.Path)
if err == nil && len(terraformFiles) > 0 {
terraformCfg := pCfg.Clone()

assets, err := (&terraform_resolver.Resolver{}).Resolve(ctx, projectAsset, terraformCfg, credsResolver, sfn, userIdDetectors...)
terraformCfg.Backend = providers.ProviderType_TERRAFORM
assets, err := (&terraform_resolver.Resolver{}).Resolve(ctx, projectAsset, terraformCfg, credsResolver, sfn)
if err == nil && len(assets) > 0 {
for i := range assets {
if len(assets[i].PlatformIds) > 0 {
assets[i].PlatformIds[0] = assets[i].PlatformIds[0] + "/" + project.Name
} else {
log.Debug().Msg("missing platform id for asset")
continue
}
}
list = append(list, assets...)
}
}
Expand Down

0 comments on commit 71c6349

Please sign in to comment.