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

     1  package kms
     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/kms"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckAutoRotateKeys(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    kms.KMS
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "ENCRYPT_DECRYPT KMS Key with auto-rotation disabled",
    24  			input: kms.KMS{
    25  				Keys: []kms.Key{
    26  					{
    27  						Usage:           defsecTypes.String("ENCRYPT_DECRYPT", defsecTypes.NewTestMetadata()),
    28  						RotationEnabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    29  					},
    30  				},
    31  			},
    32  			expected: true,
    33  		},
    34  		{
    35  			name: "ENCRYPT_DECRYPT KMS Key with auto-rotation enabled",
    36  			input: kms.KMS{
    37  				Keys: []kms.Key{
    38  					{
    39  						Usage:           defsecTypes.String("ENCRYPT_DECRYPT", defsecTypes.NewTestMetadata()),
    40  						RotationEnabled: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
    41  					},
    42  				},
    43  			},
    44  			expected: false,
    45  		},
    46  		{
    47  			name: "SIGN_VERIFY KMS Key with auto-rotation disabled",
    48  			input: kms.KMS{
    49  				Keys: []kms.Key{
    50  					{
    51  						Usage:           defsecTypes.String(kms.KeyUsageSignAndVerify, defsecTypes.NewTestMetadata()),
    52  						RotationEnabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
    53  					},
    54  				},
    55  			},
    56  			expected: false,
    57  		},
    58  	}
    59  	for _, test := range tests {
    60  		t.Run(test.name, func(t *testing.T) {
    61  			var testState state.State
    62  			testState.AWS.KMS = test.input
    63  			results := CheckAutoRotateKeys.Evaluate(&testState)
    64  			var found bool
    65  			for _, result := range results {
    66  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckAutoRotateKeys.Rule().LongID() {
    67  					found = true
    68  				}
    69  			}
    70  			if test.expected {
    71  				assert.True(t, found, "Rule should have been found")
    72  			} else {
    73  				assert.False(t, found, "Rule should not have been found")
    74  			}
    75  		})
    76  	}
    77  }