github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/aws/sns/topic_encryption_with_cmk_test.go (about) 1 package sns 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/sns" 11 "github.com/khulnasoft-lab/defsec/pkg/scan" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestCheckTopicEncryptionUsesCMK(t *testing.T) { 17 tests := []struct { 18 name string 19 input sns.SNS 20 expected bool 21 }{ 22 { 23 name: "AWS SNS Topic without encryption", 24 input: sns.SNS{ 25 Topics: []sns.Topic{ 26 { 27 Metadata: defsecTypes.NewTestMetadata(), 28 Encryption: sns.Encryption{ 29 Metadata: defsecTypes.NewTestMetadata(), 30 KMSKeyID: defsecTypes.String("", defsecTypes.NewTestMetadata()), 31 }, 32 }, 33 }, 34 }, 35 expected: false, 36 }, 37 { 38 name: "AWS SNS Topic encrypted with default key", 39 input: sns.SNS{ 40 Topics: []sns.Topic{ 41 { 42 Metadata: defsecTypes.NewTestMetadata(), 43 Encryption: sns.Encryption{ 44 Metadata: defsecTypes.NewTestMetadata(), 45 KMSKeyID: defsecTypes.String("alias/aws/sns", defsecTypes.NewTestMetadata()), 46 }, 47 }, 48 }, 49 }, 50 expected: true, 51 }, 52 { 53 name: "AWS SNS Topic properly encrypted", 54 input: sns.SNS{ 55 Topics: []sns.Topic{ 56 { 57 Metadata: defsecTypes.NewTestMetadata(), 58 Encryption: sns.Encryption{ 59 Metadata: defsecTypes.NewTestMetadata(), 60 KMSKeyID: defsecTypes.String("some-ok-key", defsecTypes.NewTestMetadata()), 61 }, 62 }, 63 }, 64 }, 65 expected: false, 66 }, 67 } 68 for _, test := range tests { 69 t.Run(test.name, func(t *testing.T) { 70 var testState state.State 71 testState.AWS.SNS = test.input 72 results := CheckTopicEncryptionUsesCMK.Evaluate(&testState) 73 var found bool 74 for _, result := range results { 75 if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckTopicEncryptionUsesCMK.Rule().LongID() { 76 found = true 77 } 78 } 79 if test.expected { 80 assert.True(t, found, "Rule should have been found") 81 } else { 82 assert.False(t, found, "Rule should not have been found") 83 } 84 }) 85 } 86 }