github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/iam/require_support_role_test.go (about)

     1  package iam
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/providers/aws/iam"
     9  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    10  	"github.com/khulnasoft-lab/defsec/pkg/state"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestCheckRequireSupportRole(t *testing.T) {
    16  	tests := []struct {
    17  		name     string
    18  		input    iam.IAM
    19  		expected bool
    20  	}{
    21  		{
    22  			name:     "No support role",
    23  			input:    iam.IAM{},
    24  			expected: true,
    25  		},
    26  		{
    27  			name: "Has support role",
    28  			input: iam.IAM{
    29  				Roles: []iam.Role{
    30  					{
    31  						Metadata: defsecTypes.NewTestMetadata(),
    32  						Name:     defsecTypes.String("example", defsecTypes.NewTestMetadata()),
    33  						Policies: []iam.Policy{
    34  							{
    35  								Metadata: defsecTypes.NewTestMetadata(),
    36  								Builtin:  defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    37  								Name:     defsecTypes.String("AWSSupportRole", defsecTypes.NewTestMetadata()),
    38  							},
    39  						},
    40  					},
    41  				},
    42  			},
    43  			expected: true,
    44  		},
    45  	}
    46  	for _, test := range tests {
    47  		t.Run(test.name, func(t *testing.T) {
    48  			var testState state.State
    49  			testState.AWS.IAM = test.input
    50  			results := CheckRequireSupportRole.Evaluate(&testState)
    51  			var found bool
    52  			for _, result := range results {
    53  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckRequireSupportRole.Rule().LongID() {
    54  					found = true
    55  				}
    56  			}
    57  			if test.expected {
    58  				assert.True(t, found, "Rule should have been found")
    59  			} else {
    60  				assert.False(t, found, "Rule should not have been found")
    61  			}
    62  		})
    63  	}
    64  }