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

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