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

     1  package athena
     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/athena"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckEnableAtRestEncryption(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    athena.Athena
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "AWS Athena database unencrypted",
    24  			input: athena.Athena{
    25  				Databases: []athena.Database{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						Encryption: athena.EncryptionConfiguration{
    29  							Metadata: defsecTypes.NewTestMetadata(),
    30  							Type:     defsecTypes.String(athena.EncryptionTypeNone, defsecTypes.NewTestMetadata()),
    31  						},
    32  					},
    33  				},
    34  			},
    35  			expected: true,
    36  		},
    37  		{
    38  			name: "AWS Athena workgroup unencrypted",
    39  			input: athena.Athena{
    40  				Workgroups: []athena.Workgroup{
    41  					{
    42  						Metadata: defsecTypes.NewTestMetadata(),
    43  						Encryption: athena.EncryptionConfiguration{
    44  							Metadata: defsecTypes.NewTestMetadata(),
    45  							Type:     defsecTypes.String(athena.EncryptionTypeNone, defsecTypes.NewTestMetadata()),
    46  						},
    47  					},
    48  				},
    49  			},
    50  			expected: true,
    51  		},
    52  		{
    53  			name: "AWS Athena database and workgroup encrypted",
    54  			input: athena.Athena{
    55  				Databases: []athena.Database{
    56  					{
    57  						Metadata: defsecTypes.NewTestMetadata(),
    58  						Encryption: athena.EncryptionConfiguration{
    59  							Metadata: defsecTypes.NewTestMetadata(),
    60  							Type:     defsecTypes.String(athena.EncryptionTypeSSEKMS, defsecTypes.NewTestMetadata()),
    61  						},
    62  					},
    63  				},
    64  				Workgroups: []athena.Workgroup{
    65  					{
    66  						Metadata: defsecTypes.NewTestMetadata(),
    67  						Encryption: athena.EncryptionConfiguration{
    68  							Metadata: defsecTypes.NewTestMetadata(),
    69  							Type:     defsecTypes.String(athena.EncryptionTypeSSEKMS, defsecTypes.NewTestMetadata()),
    70  						},
    71  					},
    72  				},
    73  			},
    74  			expected: false,
    75  		},
    76  	}
    77  	for _, test := range tests {
    78  		t.Run(test.name, func(t *testing.T) {
    79  			var testState state.State
    80  			testState.AWS.Athena = test.input
    81  			results := CheckEnableAtRestEncryption.Evaluate(&testState)
    82  			var found bool
    83  			for _, result := range results {
    84  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnableAtRestEncryption.Rule().LongID() {
    85  					found = true
    86  				}
    87  			}
    88  			if test.expected {
    89  				assert.True(t, found, "Rule should have been found")
    90  			} else {
    91  				assert.False(t, found, "Rule should not have been found")
    92  			}
    93  		})
    94  	}
    95  }