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

     1  package rds
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/state"
     9  
    10  	"github.com/khulnasoft-lab/defsec/pkg/providers/aws/rds"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckEnablePerformanceInsights(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    rds.RDS
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "RDS Instance with performance insights disabled",
    24  			input: rds.RDS{
    25  				Instances: []rds.Instance{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						PerformanceInsights: rds.PerformanceInsights{
    29  							Metadata: defsecTypes.NewTestMetadata(),
    30  							Enabled:  defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    31  							KMSKeyID: defsecTypes.String("some-kms-key", defsecTypes.NewTestMetadata()),
    32  						},
    33  					},
    34  				},
    35  			},
    36  			expected: true,
    37  		},
    38  
    39  		{
    40  			name: "RDS Instance with performance insights enabled and KMS key provided",
    41  			input: rds.RDS{
    42  				Instances: []rds.Instance{
    43  					{
    44  						Metadata: defsecTypes.NewTestMetadata(),
    45  						PerformanceInsights: rds.PerformanceInsights{
    46  							Metadata: defsecTypes.NewTestMetadata(),
    47  							Enabled:  defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    48  							KMSKeyID: defsecTypes.String("some-kms-key", defsecTypes.NewTestMetadata()),
    49  						},
    50  					},
    51  				},
    52  			},
    53  			expected: false,
    54  		},
    55  	}
    56  	for _, test := range tests {
    57  		t.Run(test.name, func(t *testing.T) {
    58  			var testState state.State
    59  			testState.AWS.RDS = test.input
    60  			results := CheckEnablePerformanceInsights.Evaluate(&testState)
    61  			var found bool
    62  			for _, result := range results {
    63  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnablePerformanceInsights.Rule().LongID() {
    64  					found = true
    65  				}
    66  			}
    67  			if test.expected {
    68  				assert.True(t, found, "Rule should have been found")
    69  			} else {
    70  				assert.False(t, found, "Rule should not have been found")
    71  			}
    72  		})
    73  	}
    74  }