nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: datreeio/datree
Pull request #797 (2022-09-15 07:59:10)

fix: offline local mode without --policy-config failing


Patch:
diff --git a/pkg/cliClient/evaluation.go b/pkg/cliClient/evaluation.go
index a65827f..394238a 100644
--- a/pkg/cliClient/evaluation.go
+++ b/pkg/cliClient/evaluation.go
@@ -107,7 +107,7 @@ func (c *CliClient) RequestEvaluationPrerunData(tokenId string, isCi bool) (*Eva
 		}
 
 		if c.networkValidator.IsLocalMode() {
-			return &EvaluationPrerunDataResponse{IsPolicyAsCodeMode: true}, nil
+			return &EvaluationPrerunDataResponse{IsPolicyAsCodeMode: true, PoliciesJson: defaultPolicies.GetDefaultPoliciesStruct()}, nil
 		}
 
 		return &EvaluationPrerunDataResponse{}, err


Test patch:
diff --git a/pkg/cliClient/cliClient_test.go b/pkg/cliClient/cliClient_test.go
index 95513b7..b8fb829 100644
--- a/pkg/cliClient/cliClient_test.go
+++ b/pkg/cliClient/cliClient_test.go
@@ -551,10 +551,11 @@ func test_requestEvaluationPrerunData_network_error(offlineMode string, expected
 	emptyRes := &EvaluationPrerunDataResponse{}
 	if offlineMode == "local" {
 		emptyRes.IsPolicyAsCodeMode = true
+		emptyRes.PoliciesJson = defaultPolicies.GetDefaultPoliciesStruct()
 	}
 
 	return &RequestEvaluationPrerunDataTestCase{
-		name: "fail - get prerun data for evaluation",
+		name: "fail - get prerun data for evaluation network error",
 		args: struct {
 			token   string
 			offline string
Repository: datreeio/datree
Pull request #802 (2022-09-20 09:52:51)

feat: add crd extractor dir to default schema locations


Patch:
diff --git a/bl/validation/k8sValidator.go b/bl/validation/k8sValidator.go
index 0e05a73..b27c673 100644
--- a/bl/validation/k8sValidator.go
+++ b/bl/validation/k8sValidator.go
@@ -243,5 +243,16 @@ func getDefaultSchemaLocations() []string {
 		// notice: order here is important because this fallback doesn't have strict mode enabled (in contrast to "default")
 		"https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}/{{ .ResourceKind }}{{ .KindSuffix }}.json",
 		"https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{ .Group }}/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json",
+		getExtractedSchemasDir(),
 	}
 }
+
+// when using the crd-extractor(https://github.com/datreeio/CRDs-catalog#crd-extractor) extracted schemas are saved to a local dir, which should be used as a schema-location by default
+func getExtractedSchemasDir() string {
+	homeDir, err := os.UserHomeDir()
+	if err != nil {
+		return ""
+	}
+
+	return (homeDir + "/.datree/crdSchemas/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json")
+}


Test patch:
diff --git a/bl/validation/k8sValidator_test.go b/bl/validation/k8sValidator_test.go
index 1472944..4276848 100644
--- a/bl/validation/k8sValidator_test.go
+++ b/bl/validation/k8sValidator_test.go
@@ -3,6 +3,7 @@ package validation
 import (
 	"fmt"
 	"io"
+	"os"
 	"sync"
 	"testing"
 
@@ -201,11 +202,13 @@ func test_missing_schema_skipped(t *testing.T) {
 }
 
 func test_get_all_schema_locations_online(t *testing.T) {
+	homeDir, _ := os.UserHomeDir()
 	expectedOutput := []string{
 		"/my-local-schema-location",
 		"default",
 		"https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}/{{ .ResourceKind }}{{ .KindSuffix }}.json",
 		"https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{ .Group }}/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json",
+		homeDir + "/.datree/crdSchemas/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json",
 	}
 	actual := getAllSchemaLocations([]string{"/my-local-schema-location"}, false)
 	assert.Equal(t, expectedOutput, actual)
Repository: datreeio/datree
Pull request #835 (2022-11-11 05:16:13)

fix: improve offline mode detection
Compiled the datree binary using these changes and was able to successfully run schema validations in my airgapped environment. 


Patch:
diff --git a/pkg/utils/errors.go b/pkg/utils/errors.go
index 7368b5f..dd6141d 100644
--- a/pkg/utils/errors.go
+++ b/pkg/utils/errors.go
@@ -18,13 +18,13 @@ func ParseErrorToString(err interface{}) string {
 }
 
 func IsNetworkError(err error) bool {
-	networkErrors := []string{"network error", "connection refused", "no such host", "i/o timeout", "server misbehaving"}
+	networkErrors := []string{"network error", "connection refused", "no such host", "i/o timeout", "server misbehaving", "blocked"}
 	return stringInSliceContains(err.Error(), networkErrors) || isUrlErrorType(err)
 }
 
 func stringInSliceContains(a string, list []string) bool {
 	for _, b := range list {
-		if strings.Contains(a, b) {
+		if strings.Contains(strings.ToLower(a), b) {
 			return true
 		}
 	}


Test patch:
diff --git a/pkg/utils/errors_test.go b/pkg/utils/errors_test.go
new file mode 100644
index 0000000..cd006ad
--- /dev/null
+++ b/pkg/utils/errors_test.go
@@ -0,0 +1,19 @@
+package utils
+
+import (
+	"errors"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestIsNetworkError(t *testing.T) {
+	assert.Equal(t, IsNetworkError(errors.New("blocked")), true)
+	assert.Equal(t, IsNetworkError(errors.New("Blocked")), true)
+	assert.Equal(t, IsNetworkError(errors.New("network Error")), true)
+	assert.Equal(t, IsNetworkError(errors.New("CONNECTION REFUSED")), true)
+	assert.Equal(t, IsNetworkError(errors.New("no such host")), true)
+	assert.Equal(t, IsNetworkError(errors.New("i/o timeout")), true)
+	assert.Equal(t, IsNetworkError(errors.New("server misbehaving")), true)
+	assert.Equal(t, IsNetworkError(errors.New("this is not an error")), false)
+}
Repository: datreeio/datree
Pull request #870 (2023-01-25 10:16:05)

fix: rule 32 logic
rule 32 gave out false positives (rule should've failed, but it passed) whenever `annotations` had any other properties


Patch:
diff --git a/pkg/defaultRules/defaultRules.yaml b/pkg/defaultRules/defaultRules.yaml
index f88aaf0..da0b810 100644
--- a/pkg/defaultRules/defaultRules.yaml
+++ b/pkg/defaultRules/defaultRules.yaml
@@ -945,8 +945,8 @@ rules:
           metadata:
             properties:
               annotations:
-                not:
-                  propertyNames:
+                propertyNames:
+                  not:
                     pattern: ^.*server-snippet$
   - id: 33
     name: "Prevent container security vulnerability (CVE-2021-25741)"


Test patch:
diff --git a/pkg/policy/tests/32-fail.yaml b/pkg/policy/tests/32-fail.yaml
index 7dad316..88c81fe 100644
--- a/pkg/policy/tests/32-fail.yaml
+++ b/pkg/policy/tests/32-fail.yaml
@@ -4,6 +4,7 @@ metadata:
   name: simple-fanout-example
   annotations:
     nginx.ingress.kubernetes.io/server-snippet: dekljdkfj
+    aaa: bbb
 spec:
   rules:
   - host: foo.bar.com