Skip to content

Commit

Permalink
fix: change the wording of the logs and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: nikpivkin <[email protected]>
  • Loading branch information
nikpivkin committed Aug 20, 2024
1 parent 2533267 commit 1cb0263
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/iac/scanners/cloudformation/parser/file_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (t *FileContext) overrideParameters(params map[string]any) {
}
}

func (t *FileContext) missingParameters() []string {
func (t *FileContext) missingParameterValues() []string {
var missing []string
for key := range t.Parameters {
if t.Parameters[key].inner.Default == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/scanners/cloudformation/parser/file_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestFileContext_OverrideParameters(t *testing.T) {
}
}

func TestFileContext_MissingParameters(t *testing.T) {
func TestFileContext_MissingParameterValues(t *testing.T) {

tests := []struct {
name string
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestFileContext_MissingParameters(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.ctx.missingParameters()
got := tt.ctx.missingParameterValues()
slices.Sort(got)
assert.Equal(t, tt.expected, got)
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/scanners/cloudformation/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func (p *Parser) ParseFile(ctx context.Context, fsys fs.FS, path string) (fctx *

fctx.overrideParameters(p.overridedParameters)

if params := fctx.missingParameters(); len(params) > 0 {
p.logger.Warn("Missing parameters", log.FilePath(path), log.String("parameters", strings.Join(params, ", ")))
if params := fctx.missingParameterValues(); len(params) > 0 {
p.logger.Warn("Missing parameter values", log.FilePath(path), log.String("parameters", strings.Join(params, ", ")))
}

fctx.lines = lines
Expand Down
27 changes: 18 additions & 9 deletions pkg/iac/scanners/terraform/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,11 @@ func (p *Parser) Load(ctx context.Context) (*evaluator, error) {
}
p.logger.Debug("Added input variables from tfvars", log.Int("count", len(inputVars)))

for _, varBlock := range blocks.OfType("variable") {
if varBlock.GetAttribute("default") == nil {
if _, ok := inputVars[varBlock.TypeLabel()]; !ok {
p.logger.Warn(
"Variable was not found in the environment or variable files. Evaluating may not work correctly.",
log.String("variable", varBlock.TypeLabel()),
)
}
}
if missingVars := missingVariableValues(blocks, inputVars); len(missingVars) > 0 {
p.logger.Warn(
"Variable values was not found in the environment or variable files. Evaluating may not work correctly.",
log.String("variables", strings.Join(missingVars, ", ")),
)
}
}

Expand Down Expand Up @@ -245,6 +241,19 @@ func (p *Parser) Load(ctx context.Context) (*evaluator, error) {
), nil
}

func missingVariableValues(blocks terraform.Blocks, inputVars map[string]cty.Value) []string {
var missing []string
for _, varBlock := range blocks.OfType("variable") {
if varBlock.GetAttribute("default") == nil {
if _, ok := inputVars[varBlock.TypeLabel()]; !ok {
missing = append(missing, varBlock.TypeLabel())
}
}
}

return missing
}

func (p *Parser) EvaluateAll(ctx context.Context) (terraform.Modules, cty.Value, error) {

e, err := p.Load(ctx)
Expand Down
38 changes: 38 additions & 0 deletions pkg/iac/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package parser

import (
"bytes"
"context"
"log/slog"
"os"
"path/filepath"
"sort"
Expand All @@ -14,6 +16,7 @@ import (

"github.com/aquasecurity/trivy/internal/testutil"
"github.com/aquasecurity/trivy/pkg/iac/terraform"
"github.com/aquasecurity/trivy/pkg/log"
)

func Test_BasicParsing(t *testing.T) {
Expand Down Expand Up @@ -1819,3 +1822,38 @@ resource "something" "blah" {
assert.True(t, r2.IsResolvable())
assert.Equal(t, "us-east-2", r2.Value().AsString())
}

func TestLogAboutMissingVariableValues(t *testing.T) {
var buf bytes.Buffer
slog.SetDefault(slog.New(log.NewHandler(&buf, nil)))

fsys := fstest.MapFS{
"main.tf": &fstest.MapFile{
Data: []byte(`
variable "foo" {}
variable "bar" {
default = "bar"
}
variable "baz" {}
`),
},
"main.tfvars": &fstest.MapFile{
Data: []byte(`baz = "baz"`),
},
}

parser := New(
fsys, "",
OptionStopOnHCLError(true),
OptionWithTFVarsPaths("main.tfvars"),
)
require.NoError(t, parser.ParseFS(context.TODO(), "."))

_, err := parser.Load(context.TODO())
require.NoError(t, err)

assert.Contains(t, buf.String(), "Variable values was not found in the environment or variable files.")
assert.Contains(t, buf.String(), "variables=\"foo\"")
}

0 comments on commit 1cb0263

Please sign in to comment.