github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/google/compute/no_default_service_account_test.go (about)

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