-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_source_test.go
More file actions
80 lines (75 loc) · 2.64 KB
/
Copy pathdata_source_test.go
File metadata and controls
80 lines (75 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
*
* This product includes software developed at Datadog (https://www.datadoghq.com) Copyright 2024 Datadog, Inc.
*/
package terraform
import (
"context"
"path/filepath"
"testing"
"github.com/DataDog/datadog-iac-scanner/pkg/parser/terraform/converter"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty/gocty"
)
func Test_getDataSourcePolicy(t *testing.T) {
type args struct {
currentPath string
resourceName string
}
tests := []struct {
name string
args args
want string
}{
{
name: "should load data source as json without errors 1",
args: args{
currentPath: filepath.Join("..", "..", "..", "test", "fixtures", "test_terraform_data_source"),
resourceName: "test_destination_policy",
},
want: `{"Statement":[{"Actions":["logs:*"],"Effect":"Allow","Principals":{"AWS":["data.aws_caller_identity.current.id"]}}]}
`,
},
{
name: "should load data source as json without errors 2",
args: args{
currentPath: filepath.Join("..", "..", "..", "test", "fixtures", "test_terraform_data_source"),
resourceName: "test_example",
},
want: `{"Id":"lala","Statement":[{"Actions":["s3:ListAllMyBuckets","s3:GetBucketLocation"],"Resources":["arn:aws:s3:::*"],"Sid":"1"},{"Actions":["s3:ListBucket"],"Condition":{"StringLike":{"s3:prefix":["","home/","home/&{aws:username}/"]}},"Resources":["arn:aws:s3:::test"]},{"Actions":["s3:*"],"Resources":["arn:aws:s3:::test/home/&{aws:username}","arn:aws:s3:::test/home/&{aws:username}/*"]}]}
`,
},
{
name: "should not drop policy when scalar fields reference unknown variables",
args: args{
currentPath: filepath.Join("..", "..", "..", "test", "fixtures", "test_terraform_data_source_unknown_vars"),
resourceName: "partial_unknowns",
},
want: `{"Statement":[{"Actions":["s3:GetObject"],"Effect":"Allow","Resources":["arn:aws:s3:::my-bucket/*"]}]}
`,
},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputVars := make(converter.VariableMap)
result := getDataSourcePolicy(ctx, tt.args.currentPath, inputVars)
data, ok := result["data"]
if !ok {
t.FailNow()
}
var awsPolicyMap map[string]map[string]map[string]string
err := gocty.FromCtyValue(data, &awsPolicyMap)
if err != nil {
t.Errorf("getDataSourcePolicy() error = %v", err)
}
got, ok := awsPolicyMap["aws_iam_policy_document"][tt.args.resourceName]["json"]
if !ok {
t.FailNow()
}
require.Equal(t, tt.want, got)
})
}
// No cleanup needed since we're not using global variables anymore
}