github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/rds/encrypt_instance_storage_data_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 TestCheckEncryptInstanceStorageData(t *testing.T) { 17 tests := []struct { 18 name string 19 input rds.RDS 20 expected bool 21 }{ 22 { 23 name: "RDS Instance with unencrypted storage", 24 input: rds.RDS{ 25 Instances: []rds.Instance{ 26 { 27 Metadata: defsecTypes.NewTestMetadata(), 28 ReplicationSourceARN: defsecTypes.String("", defsecTypes.NewTestMetadata()), 29 Encryption: rds.Encryption{ 30 Metadata: defsecTypes.NewTestMetadata(), 31 EncryptStorage: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()), 32 }, 33 }, 34 }, 35 }, 36 expected: true, 37 }, 38 { 39 name: "RDS Instance with encrypted storage", 40 input: rds.RDS{ 41 Instances: []rds.Instance{ 42 { 43 Metadata: defsecTypes.NewTestMetadata(), 44 ReplicationSourceARN: defsecTypes.String("", defsecTypes.NewTestMetadata()), 45 Encryption: rds.Encryption{ 46 Metadata: defsecTypes.NewTestMetadata(), 47 EncryptStorage: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()), 48 }, 49 }, 50 }, 51 }, 52 expected: false, 53 }, 54 } 55 for _, test := range tests { 56 t.Run(test.name, func(t *testing.T) { 57 var testState state.State 58 testState.AWS.RDS = test.input 59 results := CheckEncryptInstanceStorageData.Evaluate(&testState) 60 var found bool 61 for _, result := range results { 62 if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEncryptInstanceStorageData.Rule().LongID() { 63 found = true 64 } 65 } 66 if test.expected { 67 assert.True(t, found, "Rule should have been found") 68 } else { 69 assert.False(t, found, "Rule should not have been found") 70 } 71 }) 72 } 73 }