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

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