From e02086e90cf4024dbb34f70a9fc61df633e0475f Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Sat, 10 Dec 2022 18:11:17 +0530 Subject: [PATCH] Handle Single Line Sequence Node in Removal --- core/pkg/fixhandler/fixhandler_test.go | 2 +- core/pkg/fixhandler/yamlhandler.go | 41 +++++++++++++++---- .../fixed_yaml_scenario_1.yml | 2 +- .../fixed_yaml_scenario_2.yml | 3 +- .../fixed_yaml_scenario_3.yml | 3 +- .../original_yaml_scenario_2.yml | 2 +- 6 files changed, 40 insertions(+), 13 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 4ef1aa77..cb54a415 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -140,7 +140,7 @@ func TestFixHandler_applyFixToFile(t *testing.T) { testDirectoryApplyFix(t, "insert_scenarios") // Tests for Removal scenarios - // testDirectoryApplyFix(t, "remove_scenarios") + testDirectoryApplyFix(t, "remove_scenarios") // Tests for Replace scenarios // testDirectoryApplyFix(t, "replace_scenarios") diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index a1f7a318..3ed45e27 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -114,7 +114,7 @@ func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]Conten fixedListTracker += 1 case int(removedNode): - originalListTracker = addLinesToRemove(fixInfoMetadata) + originalListTracker, fixedListTracker = addLinesToRemove(fixInfoMetadata) case int(insertedNode): originalListTracker, fixedListTracker = addLinesToInsert(fixInfoMetadata) @@ -127,7 +127,7 @@ func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]Conten for originalListTracker < len(*originalList) { fixInfoMetadata.originalListTracker = originalListTracker fixInfoMetadata.fixedListTracker = len(*fixedList) - 1 - originalListTracker = addLinesToRemove(fixInfoMetadata) + originalListTracker, _ = addLinesToRemove(fixInfoMetadata) } for fixedListTracker < len(*fixedList) { @@ -141,15 +141,43 @@ func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]Conten } // Adds the lines to remove and returns the updated originalListTracker -func addLinesToRemove(fixInfoMetadata *FixInfoMetadata) int { +func addLinesToRemove(fixInfoMetadata *FixInfoMetadata) (int, int) { currentDFSNode := (*fixInfoMetadata.originalList)[fixInfoMetadata.originalListTracker] + + isOneLine, line := isOneLineSequenceNode(fixInfoMetadata.originalList, fixInfoMetadata.originalListTracker) + + if isOneLine { + originalListTracker := getFirstNodeInLine(fixInfoMetadata.originalList, line) + fixedListTracker := getFirstNodeInLine(fixInfoMetadata.fixedList, line) + + currentDFSNode = (*fixInfoMetadata.fixedList)[fixedListTracker] + content := getContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixedListTracker) + + // Remove the Single line + *fixInfoMetadata.contentToRemove = append(*fixInfoMetadata.contentToRemove, ContentToRemove{ + startLine: line, + endLine: line, + }) + + // Encode entire Sequence Node and Insert + *fixInfoMetadata.contentToAdd = append(*fixInfoMetadata.contentToAdd, ContentToAdd{ + Line: line, + Content: content, + }) + + originalListTracker = updateTracker(fixInfoMetadata.originalList, originalListTracker) + fixedListTracker = updateTracker(fixInfoMetadata.fixedList, fixedListTracker) + + return originalListTracker, fixedListTracker + } + newTracker := updateTracker(fixInfoMetadata.originalList, fixInfoMetadata.originalListTracker) *fixInfoMetadata.contentToRemove = append(*fixInfoMetadata.contentToRemove, ContentToRemove{ startLine: currentDFSNode.node.Line, endLine: getNodeLine(fixInfoMetadata.originalList, newTracker) - 1, }) - return newTracker + return newTracker, fixInfoMetadata.fixedListTracker } // Adds the lines to insert and returns the updated fixedListTracker @@ -181,7 +209,6 @@ func addLinesToInsert(fixInfoMetadata *FixInfoMetadata) (int, int) { fixedListTracker = updateTracker(fixInfoMetadata.fixedList, fixedListTracker) return originalListTracker, fixedListTracker - } var lineToInsert int @@ -212,8 +239,8 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { fixInfoMetadata.fixedListTracker -= 1 } - updatedOriginalTracker := addLinesToRemove(fixInfoMetadata) - updatedOriginalTracker, updatedFixedTracker := addLinesToInsert(fixInfoMetadata) + updatedOriginalTracker, updatedFixedTracker := addLinesToRemove(fixInfoMetadata) + updatedOriginalTracker, updatedFixedTracker = addLinesToInsert(fixInfoMetadata) return updatedOriginalTracker, updatedFixedTracker } diff --git a/examples/fix-command/remove_scenarios/fixed_yaml_scenario_1.yml b/examples/fix-command/remove_scenarios/fixed_yaml_scenario_1.yml index 8ecc8724..5c5f896c 100644 --- a/examples/fix-command/remove_scenarios/fixed_yaml_scenario_1.yml +++ b/examples/fix-command/remove_scenarios/fixed_yaml_scenario_1.yml @@ -9,4 +9,4 @@ metadata: spec: containers: - name: nginx_container - image: nginx \ No newline at end of file + image: nginx diff --git a/examples/fix-command/remove_scenarios/fixed_yaml_scenario_2.yml b/examples/fix-command/remove_scenarios/fixed_yaml_scenario_2.yml index 55cf481a..43b80fa1 100644 --- a/examples/fix-command/remove_scenarios/fixed_yaml_scenario_2.yml +++ b/examples/fix-command/remove_scenarios/fixed_yaml_scenario_2.yml @@ -9,4 +9,5 @@ metadata: spec: containers: - name: nginx_container - image: nginx \ No newline at end of file + image: nginx + diff --git a/examples/fix-command/remove_scenarios/fixed_yaml_scenario_3.yml b/examples/fix-command/remove_scenarios/fixed_yaml_scenario_3.yml index 254affe4..bc2225bd 100644 --- a/examples/fix-command/remove_scenarios/fixed_yaml_scenario_3.yml +++ b/examples/fix-command/remove_scenarios/fixed_yaml_scenario_3.yml @@ -11,5 +11,4 @@ spec: image: nginx securityContext: capabilities: - drop: - - NET_RAW \ No newline at end of file + drop: ["NET_RAW"] diff --git a/examples/fix-command/remove_scenarios/original_yaml_scenario_2.yml b/examples/fix-command/remove_scenarios/original_yaml_scenario_2.yml index 4b1f39d5..29ebfbf9 100644 --- a/examples/fix-command/remove_scenarios/original_yaml_scenario_2.yml +++ b/examples/fix-command/remove_scenarios/original_yaml_scenario_2.yml @@ -10,6 +10,6 @@ spec: containers: - name: nginx_container image: nginx - + - name: container_with_security_issues image: image_with_security_issues \ No newline at end of file