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

     1  package emr
     2  
     3  import (
     4  	"testing"
     5  
     6  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/providers/aws/emr"
     9  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    10  	"github.com/khulnasoft-lab/defsec/pkg/state"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestEnableAtRestEncryption(t *testing.T) {
    15  	tests := []struct {
    16  		name     string
    17  		input    emr.EMR
    18  		expected bool
    19  	}{
    20  		{
    21  			name: "EMR cluster with at-rest encryption disabled",
    22  			input: emr.EMR{
    23  				SecurityConfiguration: []emr.SecurityConfiguration{
    24  					{
    25  						Name: defsecTypes.String("test", defsecTypes.NewTestMetadata()),
    26  						Configuration: defsecTypes.String(`{
    27  							"EncryptionConfiguration": {
    28  							  "AtRestEncryptionConfiguration": {
    29  								"S3EncryptionConfiguration": {
    30  								  "EncryptionMode": "SSE-S3"
    31  								},
    32  								"LocalDiskEncryptionConfiguration": {
    33  								  "EncryptionKeyProviderType": "AwsKms",
    34  								  "AwsKmsKey": "arn:aws:kms:us-west-2:187416307283:alias/tf_emr_test_key"
    35  								}
    36  							  },
    37  							  "EnableAtRestEncryption": false
    38  							}
    39  						  }`, defsecTypes.NewTestMetadata()),
    40  					},
    41  				},
    42  			},
    43  			expected: true,
    44  		},
    45  		{
    46  			name: "EMR cluster with at-rest encryption enabled",
    47  			input: emr.EMR{
    48  				SecurityConfiguration: []emr.SecurityConfiguration{
    49  					{
    50  						Name: defsecTypes.String("test", defsecTypes.NewTestMetadata()),
    51  						Configuration: defsecTypes.String(`{
    52  							"EncryptionConfiguration": {
    53  							  "AtRestEncryptionConfiguration": {
    54  								"S3EncryptionConfiguration": {
    55  								  "EncryptionMode": "SSE-S3"
    56  								},
    57  								"LocalDiskEncryptionConfiguration": {
    58  								  "EncryptionKeyProviderType": "AwsKms",
    59  								  "AwsKmsKey": "arn:aws:kms:us-west-2:187416307283:alias/tf_emr_test_key"
    60  								}
    61  							  },
    62  							  "EnableAtRestEncryption": true
    63  							}
    64  						  }`, defsecTypes.NewTestMetadata()),
    65  					},
    66  				},
    67  			},
    68  			expected: false,
    69  		},
    70  	}
    71  	for _, test := range tests {
    72  		t.Run(test.name, func(t *testing.T) {
    73  			var testState state.State
    74  			testState.AWS.EMR = test.input
    75  			results := CheckEnableAtRestEncryption.Evaluate(&testState)
    76  			var found bool
    77  			for _, result := range results {
    78  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckEnableAtRestEncryption.Rule().LongID() {
    79  					found = true
    80  				}
    81  			}
    82  			if test.expected {
    83  				assert.True(t, found, "Rule should have been found")
    84  			} else {
    85  				assert.False(t, found, "Rule should not have been found")
    86  			}
    87  		})
    88  	}
    89  }