github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/rds/enable_performance_insights_encryption_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 TestCheckEnablePerformanceInsightsEncryption(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: false,
    37  		},
    38  		{
    39  			name: "RDS Instance with performance insights enabled but missing KMS key",
    40  			input: rds.RDS{
    41  				Clusters: []rds.Cluster{
    42  					{
    43  						Metadata: defsecTypes.NewTestMetadata(),
    44  						Instances: []rds.ClusterInstance{
    45  							{
    46  								Instance: rds.Instance{
    47  									Metadata: defsecTypes.NewTestMetadata(),
    48  									PerformanceInsights: rds.PerformanceInsights{
    49  										Metadata: defsecTypes.NewTestMetadata(),
    50  										Enabled:  defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    51  										KMSKeyID: defsecTypes.String("", defsecTypes.NewTestMetadata()),
    52  									},
    53  								},
    54  							},
    55  						},
    56  					},
    57  				},
    58  			},
    59  			expected: true,
    60  		},
    61  		{
    62  			name: "RDS Instance with performance insights enabled and KMS key provided",
    63  			input: rds.RDS{
    64  				Instances: []rds.Instance{
    65  					{
    66  						Metadata: defsecTypes.NewTestMetadata(),
    67  						PerformanceInsights: rds.PerformanceInsights{
    68  							Metadata: defsecTypes.NewTestMetadata(),
    69  							Enabled:  defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    70  							KMSKeyID: defsecTypes.String("some-kms-key", defsecTypes.NewTestMetadata()),
    71  						},
    72  					},
    73  				},
    74  			},
    75  			expected: false,
    76  		},
    77  	}
    78  	for _, test := range tests {
    79  		t.Run(test.name, func(t *testing.T) {
    80  			var testState state.State
    81  			testState.AWS.RDS = test.input
    82  			results := CheckEnablePerformanceInsightsEncryption.Evaluate(&testState)
    83  			var found bool
    84  			for _, result := range results {
    85  				if result.Status() != scan.StatusPassed && result.Rule().LongID() == CheckEnablePerformanceInsightsEncryption.Rule().LongID() {
    86  					found = true
    87  				}
    88  			}
    89  			if test.expected {
    90  				assert.True(t, found, "Rule should have been found")
    91  			} else {
    92  				assert.False(t, found, "Rule should not have been found")
    93  			}
    94  		})
    95  	}
    96  }