github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/google/kms/rotate_kms_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/google/kms"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckRotateKmsKeys(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    kms.KMS
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "KMS key rotation period of 91 days",
    24  			input: kms.KMS{
    25  				KeyRings: []kms.KeyRing{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						Keys: []kms.Key{
    29  							{
    30  								Metadata:              defsecTypes.NewTestMetadata(),
    31  								RotationPeriodSeconds: defsecTypes.Int(7862400, defsecTypes.NewTestMetadata()),
    32  							},
    33  						},
    34  					},
    35  				},
    36  			},
    37  			expected: true,
    38  		},
    39  		{
    40  			name: "KMS key rotation period of 30 days",
    41  			input: kms.KMS{
    42  				KeyRings: []kms.KeyRing{
    43  					{
    44  						Metadata: defsecTypes.NewTestMetadata(),
    45  						Keys: []kms.Key{
    46  							{
    47  								Metadata:              defsecTypes.NewTestMetadata(),
    48  								RotationPeriodSeconds: defsecTypes.Int(2592000, defsecTypes.NewTestMetadata()),
    49  							},
    50  						},
    51  					},
    52  				},
    53  			},
    54  			expected: false,
    55  		},
    56  	}
    57  	for _, test := range tests {
    58  		t.Run(test.name, func(t *testing.T) {
    59  			var testState state.State
    60  			testState.Google.KMS = test.input
    61  			results := CheckRotateKmsKeys.Evaluate(&testState)
    62  			var found bool
    63  			for _, result := range results {
    64  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckRotateKmsKeys.Rule().LongID() {
    65  					found = true
    66  				}
    67  			}
    68  			if test.expected {
    69  				assert.True(t, found, "Rule should have been found")
    70  			} else {
    71  				assert.False(t, found, "Rule should not have been found")
    72  			}
    73  		})
    74  	}
    75  }