github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/azure/storage/allow_microsoft_service_bypass_test.go (about)

     1  package storage
     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/azure/storage"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckAllowMicrosoftServiceBypass(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    storage.Storage
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "Azure storage rule doesn't allow bypass access",
    24  			input: storage.Storage{
    25  				Accounts: []storage.Account{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						NetworkRules: []storage.NetworkRule{
    29  							{
    30  								Metadata: defsecTypes.NewTestMetadata(),
    31  								Bypass:   []defsecTypes.StringValue{},
    32  							},
    33  						},
    34  					},
    35  				},
    36  			},
    37  			expected: true,
    38  		},
    39  		{
    40  			name: "Azure storage rule allows bypass access to Microsoft services",
    41  			input: storage.Storage{
    42  				Accounts: []storage.Account{
    43  					{
    44  						Metadata: defsecTypes.NewTestMetadata(),
    45  						NetworkRules: []storage.NetworkRule{
    46  							{
    47  								Metadata: defsecTypes.NewTestMetadata(),
    48  								Bypass: []defsecTypes.StringValue{
    49  									defsecTypes.String("AzureServices", defsecTypes.NewTestMetadata()),
    50  								},
    51  							},
    52  						},
    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.Azure.Storage = test.input
    63  			results := CheckAllowMicrosoftServiceBypass.Evaluate(&testState)
    64  			var found bool
    65  			for _, result := range results {
    66  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckAllowMicrosoftServiceBypass.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  }