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

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