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

🐛 ensure gitlab discovered assets have unique platform ids #1778

Merged
merged 1 commit into from
Sep 18, 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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the terraform platform ids in the group i was testing were coming back as the same, so i added the project name as a safeguard..

} else {
log.Debug().Msg("missing platform id for asset")
continue
}
}
list = append(list, assets...)
}
}
Expand Down