From 71c634950c37cb7a091f89b80c396ece69f76434 Mon Sep 17 00:00:00 2001 From: vjeffrey Date: Mon, 18 Sep 2023 13:45:12 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20ensure=20gitlab=20discovered=20a?= =?UTF-8?q?ssets=20have=20unique=20platform=20ids=20(#1778)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- motor/discovery/gitlab/gitlab.go | 35 +++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/motor/discovery/gitlab/gitlab.go b/motor/discovery/gitlab/gitlab.go index de754f94f4..49ef9ae4fe 100644 --- a/motor/discovery/gitlab/gitlab.go +++ b/motor/discovery/gitlab/gitlab.go @@ -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" @@ -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 @@ -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, @@ -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, @@ -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 @@ -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...) } }