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

     1  package monitor
     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/monitor"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scan"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCheckCaptureAllRegions(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		input    monitor.Monitor
    20  		expected bool
    21  	}{
    22  		{
    23  			name: "Log profile captures only eastern US region",
    24  			input: monitor.Monitor{
    25  				LogProfiles: []monitor.LogProfile{
    26  					{
    27  						Metadata: defsecTypes.NewTestMetadata(),
    28  						Locations: []defsecTypes.StringValue{
    29  							defsecTypes.String("eastus", defsecTypes.NewTestMetadata()),
    30  						},
    31  					},
    32  				},
    33  			},
    34  			expected: true,
    35  		},
    36  		{
    37  			name: "Log profile captures all regions",
    38  			input: monitor.Monitor{
    39  				LogProfiles: []monitor.LogProfile{
    40  					{
    41  						Metadata: defsecTypes.NewTestMetadata(),
    42  						Locations: []defsecTypes.StringValue{
    43  							defsecTypes.String("eastus", defsecTypes.NewTestMetadata()),
    44  							defsecTypes.String("eastus2", defsecTypes.NewTestMetadata()),
    45  							defsecTypes.String("southcentralus", defsecTypes.NewTestMetadata()),
    46  							defsecTypes.String("westus2", defsecTypes.NewTestMetadata()),
    47  							defsecTypes.String("westus3", defsecTypes.NewTestMetadata()),
    48  							defsecTypes.String("australiaeast", defsecTypes.NewTestMetadata()),
    49  							defsecTypes.String("southeastasia", defsecTypes.NewTestMetadata()),
    50  							defsecTypes.String("northeurope", defsecTypes.NewTestMetadata()),
    51  							defsecTypes.String("swedencentral", defsecTypes.NewTestMetadata()),
    52  							defsecTypes.String("uksouth", defsecTypes.NewTestMetadata()),
    53  							defsecTypes.String("westeurope", defsecTypes.NewTestMetadata()),
    54  							defsecTypes.String("centralus", defsecTypes.NewTestMetadata()),
    55  							defsecTypes.String("northcentralus", defsecTypes.NewTestMetadata()),
    56  							defsecTypes.String("westus", defsecTypes.NewTestMetadata()),
    57  							defsecTypes.String("southafricanorth", defsecTypes.NewTestMetadata()),
    58  							defsecTypes.String("centralindia", defsecTypes.NewTestMetadata()),
    59  							defsecTypes.String("eastasia", defsecTypes.NewTestMetadata()),
    60  							defsecTypes.String("japaneast", defsecTypes.NewTestMetadata()),
    61  							defsecTypes.String("jioindiawest", defsecTypes.NewTestMetadata()),
    62  							defsecTypes.String("koreacentral", defsecTypes.NewTestMetadata()),
    63  							defsecTypes.String("canadacentral", defsecTypes.NewTestMetadata()),
    64  							defsecTypes.String("francecentral", defsecTypes.NewTestMetadata()),
    65  							defsecTypes.String("germanywestcentral", defsecTypes.NewTestMetadata()),
    66  							defsecTypes.String("norwayeast", defsecTypes.NewTestMetadata()),
    67  							defsecTypes.String("switzerlandnorth", defsecTypes.NewTestMetadata()),
    68  							defsecTypes.String("uaenorth", defsecTypes.NewTestMetadata()),
    69  							defsecTypes.String("brazilsouth", defsecTypes.NewTestMetadata()),
    70  							defsecTypes.String("centralusstage", defsecTypes.NewTestMetadata()),
    71  							defsecTypes.String("eastusstage", defsecTypes.NewTestMetadata()),
    72  							defsecTypes.String("eastus2stage", defsecTypes.NewTestMetadata()),
    73  							defsecTypes.String("northcentralusstage", defsecTypes.NewTestMetadata()),
    74  							defsecTypes.String("southcentralusstage", defsecTypes.NewTestMetadata()),
    75  							defsecTypes.String("westusstage", defsecTypes.NewTestMetadata()),
    76  							defsecTypes.String("westus2stage", defsecTypes.NewTestMetadata()),
    77  							defsecTypes.String("asia", defsecTypes.NewTestMetadata()),
    78  							defsecTypes.String("asiapacific", defsecTypes.NewTestMetadata()),
    79  							defsecTypes.String("australia", defsecTypes.NewTestMetadata()),
    80  							defsecTypes.String("brazil", defsecTypes.NewTestMetadata()),
    81  							defsecTypes.String("canada", defsecTypes.NewTestMetadata()),
    82  							defsecTypes.String("europe", defsecTypes.NewTestMetadata()),
    83  							defsecTypes.String("global", defsecTypes.NewTestMetadata()),
    84  							defsecTypes.String("india", defsecTypes.NewTestMetadata()),
    85  							defsecTypes.String("japan", defsecTypes.NewTestMetadata()),
    86  							defsecTypes.String("uk", defsecTypes.NewTestMetadata()),
    87  							defsecTypes.String("unitedstates", defsecTypes.NewTestMetadata()),
    88  							defsecTypes.String("eastasiastage", defsecTypes.NewTestMetadata()),
    89  							defsecTypes.String("southeastasiastage", defsecTypes.NewTestMetadata()),
    90  							defsecTypes.String("centraluseuap", defsecTypes.NewTestMetadata()),
    91  							defsecTypes.String("eastus2euap", defsecTypes.NewTestMetadata()),
    92  							defsecTypes.String("westcentralus", defsecTypes.NewTestMetadata()),
    93  							defsecTypes.String("southafricawest", defsecTypes.NewTestMetadata()),
    94  							defsecTypes.String("australiacentral", defsecTypes.NewTestMetadata()),
    95  							defsecTypes.String("australiacentral2", defsecTypes.NewTestMetadata()),
    96  							defsecTypes.String("australiasoutheast", defsecTypes.NewTestMetadata()),
    97  							defsecTypes.String("japanwest", defsecTypes.NewTestMetadata()),
    98  							defsecTypes.String("jioindiacentral", defsecTypes.NewTestMetadata()),
    99  							defsecTypes.String("koreasouth", defsecTypes.NewTestMetadata()),
   100  							defsecTypes.String("southindia", defsecTypes.NewTestMetadata()),
   101  							defsecTypes.String("westindia", defsecTypes.NewTestMetadata()),
   102  							defsecTypes.String("canadaeast", defsecTypes.NewTestMetadata()),
   103  							defsecTypes.String("francesouth", defsecTypes.NewTestMetadata()),
   104  							defsecTypes.String("germanynorth", defsecTypes.NewTestMetadata()),
   105  							defsecTypes.String("norwaywest", defsecTypes.NewTestMetadata()),
   106  							defsecTypes.String("swedensouth", defsecTypes.NewTestMetadata()),
   107  							defsecTypes.String("switzerlandwest", defsecTypes.NewTestMetadata()),
   108  							defsecTypes.String("ukwest", defsecTypes.NewTestMetadata()),
   109  							defsecTypes.String("uaecentral", defsecTypes.NewTestMetadata()),
   110  							defsecTypes.String("brazilsoutheast", defsecTypes.NewTestMetadata()),
   111  						},
   112  					},
   113  				},
   114  			},
   115  			expected: false,
   116  		},
   117  	}
   118  	for _, test := range tests {
   119  		t.Run(test.name, func(t *testing.T) {
   120  			var testState state.State
   121  			testState.Azure.Monitor = test.input
   122  			results := CheckCaptureAllRegions.Evaluate(&testState)
   123  			var found bool
   124  			for _, result := range results {
   125  				if result.Status() == scan.StatusFailed && result.Rule().LongID() == CheckCaptureAllRegions.Rule().LongID() {
   126  					found = true
   127  				}
   128  			}
   129  			if test.expected {
   130  				assert.True(t, found, "Rule should have been found")
   131  			} else {
   132  				assert.False(t, found, "Rule should not have been found")
   133  			}
   134  		})
   135  	}
   136  }