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

fix(misconf): skip Azure CreateUiDefinition #8503

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion pkg/fanal/analyzer/config/azurearm/azurearm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurearm
import (
"os"
"path/filepath"
"strings"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/config"
Expand Down Expand Up @@ -34,5 +35,8 @@ func newAzureARMConfigAnalyzer(opts analyzer.AnalyzerOptions) (analyzer.PostAnal

// Required overrides config.Analyzer.Required() and check if the given file is JSON.
func (a *azureARMConfigAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return filepath.Ext(filePath) == ".json"
return filepath.Ext(filePath) == ".json" &&
// skip CreateUiDefinition
// https://learn.microsoft.com/en-us/azure/azure-resource-manager/managed-applications/create-uidefinition-overview
strings.ToLower(filepath.Base(filePath)) != "createuidefinition.json"
}
10 changes: 10 additions & 0 deletions pkg/fanal/analyzer/config/azurearm/azurearm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func Test_azureARMConfigAnalyzer_Required(t *testing.T) {
filePath: "test.yaml",
want: false,
},
{
name: "CreateUiDefinition",
filePath: "CreateUiDefinition.json",
want: false,
},
{
name: "CreateUiDefinition lowercase",
filePath: "createuidefinition.json",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/iac/detection/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func init() {

sniff := struct {
Schema string `json:"$schema"`
Handler string `json:"handler"`
Parameters map[string]any `json:"parameters"`
Resources []any `json:"resources"`
}{}
Expand All @@ -155,6 +156,12 @@ func init() {
return false
}

// skip CreateUiDefinition
// https://learn.microsoft.com/en-us/azure/azure-resource-manager/managed-applications/create-uidefinition-overview
if sniff.Handler != "" {
return false
}

return len(sniff.Parameters) > 0 || len(sniff.Resources) > 0
}

Expand Down
31 changes: 24 additions & 7 deletions pkg/iac/detection/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -420,6 +421,28 @@ rules:
FileTypeJSON,
},
},
{
name: "CreateUiDefinition",
path: "CreateUiDefinition.json",
r: strings.NewReader(`{
"$schema": "https://schema.management.azure.com/schemas/0.1.2-preview/CreateUIDefinition.MultiVm.json#",
"handler": "Microsoft.Azure.CreateUIDef",
"version": "0.1.2-preview",
"parameters": {
"config": {
"isWizard": false,
"basics": {}
},
"basics": [],
"steps": [],
"outputs": {},
"resourceTypes": []
}
}`),
expected: []FileType{
FileTypeJSON,
},
},
}

for _, test := range tests {
Expand All @@ -429,13 +452,7 @@ rules:
assert.Equal(t, len(test.expected), len(actualDetections))
for _, expected := range test.expected {
resetReader(test.r)
var found bool
for _, actual := range actualDetections {
if actual == expected {
found = true
break
}
}
found := slices.Contains(actualDetections, expected)
assert.True(t, found, "%s should be detected", expected)
}
})
Expand Down
4 changes: 3 additions & 1 deletion pkg/iac/scanners/azure/arm/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func (p *Parser) ParseFS(ctx context.Context, dir string) ([]azure.Deployment, e

deployment, err := p.parseFile(f, path)
if err != nil {
return err
p.logger.Error("Failed to parse file", log.FilePath(path), log.Err(err))
return nil
}

deployments = append(deployments, *deployment)
return nil
}); err != nil {
Expand Down
46 changes: 23 additions & 23 deletions pkg/iac/scanners/azure/arm/parser/template.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package parser

import (
types2 "github.com/aquasecurity/trivy/pkg/iac/scanners/azure"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure/arm/parser/armjson"
"github.com/aquasecurity/trivy/pkg/iac/types"
)

type Template struct {
Metadata types.Metadata `json:"-"`
Schema types2.Value `json:"$schema"`
ContentVersion types2.Value `json:"contentVersion"`
APIProfile types2.Value `json:"apiProfile"`
Parameters map[string]Parameter `json:"parameters"`
Variables map[string]types2.Value `json:"variables"`
Functions []Function `json:"functions"`
Resources []Resource `json:"resources"`
Outputs map[string]types2.Value `json:"outputs"`
Metadata types.Metadata `json:"-"`
Schema azure.Value `json:"$schema"`
ContentVersion azure.Value `json:"contentVersion"`
APIProfile azure.Value `json:"apiProfile"`
Parameters map[string]Parameter `json:"parameters"`
Variables map[string]azure.Value `json:"variables"`
Functions []Function `json:"functions"`
Resources []Resource `json:"resources"`
Outputs map[string]azure.Value `json:"outputs"`
}

type Parameter struct {
Metadata types.Metadata
Type types2.Value `json:"type"`
DefaultValue types2.Value `json:"defaultValue"`
MaxLength types2.Value `json:"maxLength"`
MinLength types2.Value `json:"minLength"`
Type azure.Value `json:"type"`
DefaultValue azure.Value `json:"defaultValue"`
MaxLength azure.Value `json:"maxLength"`
MinLength azure.Value `json:"minLength"`
}

type Function struct{}
Expand All @@ -46,15 +46,15 @@ func (p *Parameter) SetMetadata(m *types.Metadata) {
}

type innerResource struct {
APIVersion types2.Value `json:"apiVersion"`
Type types2.Value `json:"type"`
Kind types2.Value `json:"kind"`
Name types2.Value `json:"name"`
Location types2.Value `json:"location"`
Tags types2.Value `json:"tags"`
Sku types2.Value `json:"sku"`
Properties types2.Value `json:"properties"`
Resources []Resource `json:"resources"`
APIVersion azure.Value `json:"apiVersion"`
Type azure.Value `json:"type"`
Kind azure.Value `json:"kind"`
Name azure.Value `json:"name"`
Location azure.Value `json:"location"`
Tags azure.Value `json:"tags"`
Sku azure.Value `json:"sku"`
Properties azure.Value `json:"properties"`
Resources []Resource `json:"resources"`
}

func (v *Resource) UnmarshalJSONWithMetadata(node armjson.Node) error {
Expand Down
10 changes: 5 additions & 5 deletions pkg/iac/scanners/azure/arm/parser/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

types2 "github.com/aquasecurity/trivy/pkg/iac/scanners/azure"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure/arm/parser/armjson"
"github.com/aquasecurity/trivy/pkg/iac/types"
)
Expand Down Expand Up @@ -40,22 +40,22 @@ func Test_JSONUnmarshal(t *testing.T) {
"minLength": 3
*/
assert.Equal(t, "string", prefix.Type.Raw())
assert.Equal(t, types2.KindString, prefix.Type.Kind)
assert.Equal(t, azure.KindString, prefix.Type.Kind)
assert.Equal(t, 8, prefix.Type.Metadata.Range().GetStartLine())
assert.Equal(t, 8, prefix.Type.Metadata.Range().GetEndLine())

assert.Equal(t, "x", prefix.DefaultValue.Raw())
assert.Equal(t, types2.KindString, prefix.DefaultValue.Kind)
assert.Equal(t, azure.KindString, prefix.DefaultValue.Kind)
assert.Equal(t, 9, prefix.DefaultValue.Metadata.Range().GetStartLine())
assert.Equal(t, 9, prefix.DefaultValue.Metadata.Range().GetEndLine())

assert.Equal(t, int64(11), prefix.MaxLength.Raw())
assert.Equal(t, types2.KindNumber, prefix.MaxLength.Kind)
assert.Equal(t, azure.KindNumber, prefix.MaxLength.Kind)
assert.Equal(t, 10, prefix.MaxLength.Metadata.Range().GetStartLine())
assert.Equal(t, 10, prefix.MaxLength.Metadata.Range().GetEndLine())

assert.Equal(t, int64(3), prefix.MinLength.Raw())
assert.Equal(t, types2.KindNumber, prefix.MinLength.Kind)
assert.Equal(t, azure.KindNumber, prefix.MinLength.Kind)
assert.Equal(t, 11, prefix.MinLength.Metadata.Range().GetStartLine())
assert.Equal(t, 11, prefix.MinLength.Metadata.Range().GetEndLine())
}
Loading