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): generate placeholders for random provider resources #8051

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
44 changes: 44 additions & 0 deletions pkg/iac/scanners/terraform/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"testing"
"testing/fstest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -951,3 +952,46 @@ resource "aws_s3_bucket" "test" {}
assert.Len(t, results, 2)
})
}

func TestUseRandomProvider(t *testing.T) {
fsys := fstest.MapFS{
"main.tf": &fstest.MapFile{Data: []byte(`resource "random_id" "suffix" {}

locals {
bucket = "test-${random_id.suffix.hex}"
}

resource "aws_s3_bucket" "test" {
bucket = local.bucket
}

resource "aws_s3_bucket_versioning" "test" {
bucket = local.bucket
versioning_configuration {
status = "Enabled"
}
}
`)},
}

check := `package test
import rego.v1

deny contains res if {
some bucket in input.aws.s3.buckets
bucket.versioning.enabled.value
res := result.new("Bucket versioning is enabled", bucket)
}
`

scanner := New(
ScannerWithAllDirectories(true),
rego.WithPolicyReader(strings.NewReader(check)),
rego.WithPolicyNamespaces("test"),
)

results, err := scanner.ScanFS(context.TODO(), fsys, ".")
require.NoError(t, err)

assert.Len(t, results.GetFailed(), 1)
}
24 changes: 20 additions & 4 deletions pkg/iac/terraform/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ package terraform

import (
"fmt"
"math/rand/v2"
"strings"

"github.com/google/uuid"
"github.com/zclconf/go-cty/cty"
)

var resourceRandomAttributes = map[string][]string{
// If the user leaves the name blank, Terraform will automatically generate a unique name
Copy link
Member

Choose a reason for hiding this comment

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

You left random_pet behind 😿

Copy link
Contributor Author

Choose a reason for hiding this comment

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

random_pet has only an id attribute, which we fill in for all resources :)

"aws_launch_template": {"name"},
"random_id": {"hex", "dec", "b64_url", "b64_std"},
"random_password": {"result", "bcrypt_hash"},
"random_string": {"result"},
"random_bytes": {"base64", "hex"},
"random_uuid": {"result"},
}

func createPresetValues(b *Block) map[string]cty.Value {
presets := make(map[string]cty.Value)

Expand All @@ -23,16 +34,21 @@ func createPresetValues(b *Block) map[string]cty.Value {
// workaround for weird iam feature
case "aws_iam_policy_document":
presets["json"] = cty.StringVal(b.ID())
// If the user leaves the name blank, Terraform will automatically generate a unique name
case "aws_launch_template":
presets["name"] = cty.StringVal(uuid.New().String())
// allow referencing the current region name
case "aws_region":
presets["name"] = cty.StringVal("current-region")
case "random_integer":
//nolint:gosec
presets["result"] = cty.NumberIntVal(rand.Int64())
}

return presets
if attrs, exists := resourceRandomAttributes[b.TypeLabel()]; exists {
for _, attr := range attrs {
presets[attr] = cty.StringVal(uuid.New().String())
}
}

return presets
}

func postProcessValues(b *Block, input map[string]cty.Value) map[string]cty.Value {
Expand Down
Loading