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

feat(misconf): support of selectors for all providers for Rego #6905

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions pkg/iac/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const (
CloudStackProvider Provider = "cloudstack"
)

func AllProviders() []Provider {
return []Provider{
AWSProvider, AzureProvider, DigitalOceanProvider, GitHubProvider, GoogleProvider,
KubernetesProvider, OracleProvider, OpenStackProvider, NifcloudProvider, CloudStackProvider,
}
}

func RuleProviderToString(provider Provider) string {
return strings.ToUpper(string(provider))
}
Expand Down
37 changes: 25 additions & 12 deletions pkg/iac/rego/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@ import (

"github.com/aquasecurity/trivy/pkg/iac/debug"
"github.com/aquasecurity/trivy/pkg/iac/framework"
"github.com/aquasecurity/trivy/pkg/iac/providers"
"github.com/aquasecurity/trivy/pkg/iac/rego/schemas"
"github.com/aquasecurity/trivy/pkg/iac/scan"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/iac/types"
)

var checkTypesWithSubtype = map[types.Source]struct{}{
types.SourceCloud: {},
types.SourceDefsec: {},
types.SourceKubernetes: {},
}

var supportedProviders map[string]struct{}

func init() {
supportedProviders = make(map[string]struct{})
for _, p := range providers.AllProviders() {
supportedProviders[string(p)] = struct{}{}
}
supportedProviders["kind"] = struct{}{} // kubernetes
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: Do we need this to be an init func? The abstraction into the providers package is nice but IMO not at the cost of keeping an init func. I usually try to avoid having init funcs as they can get a bit problematic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: 9d2240a


var _ options.ConfigurableScanner = (*Scanner)(nil)

type Scanner struct {
Expand Down Expand Up @@ -295,12 +312,8 @@ func (s *Scanner) ScanInput(ctx context.Context, inputs ...Input) (scan.Results,
}

func isPolicyWithSubtype(sourceType types.Source) bool {
for _, s := range []types.Source{types.SourceCloud, types.SourceDefsec, types.SourceKubernetes} {
if sourceType == s {
return true
}
}
return false
_, exists := checkTypesWithSubtype[sourceType]
return exists
}

func checkSubtype(ii map[string]any, provider string, subTypes []SubType) bool {
Expand All @@ -311,10 +324,11 @@ func checkSubtype(ii map[string]any, provider string, subTypes []SubType) bool {
for _, st := range subTypes {
switch services := ii[provider].(type) {
case map[string]any:
for service := range services {
if (service == st.Service) && (st.Provider == provider) {
return true
}
if st.Provider != provider {
continue
}
if _, exists := services[st.Service]; exists {
return true
}
case string: // k8s - logic can be improved
if strings.EqualFold(services, st.Group) ||
Expand All @@ -331,8 +345,7 @@ func isPolicyApplicable(staticMetadata *StaticMetadata, inputs ...Input) bool {
for _, input := range inputs {
if ii, ok := input.Contents.(map[string]any); ok {
for provider := range ii {
// TODO(simar): Add other providers
if !strings.Contains(strings.Join([]string{"kind", "aws", "azure"}, ","), provider) {
if _, exists := supportedProviders[provider]; !exists {
continue
}

Expand Down