From f64200f42f4959ce9689726a9da692e9aa78bb2d Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Fri, 9 Dec 2022 23:44:04 +0530 Subject: [PATCH] Small Changes --- core/pkg/fixhandler/yamlhandler.go | 26 +++++++------------ core/pkg/fixhandler/yamlhelper.go | 21 +++++++++++++++ .../fixed_yaml_scenario_1.yml | 2 +- .../fixed_yaml_scenario_2.yml | 4 +-- .../fixed_yaml_scenario_3.yml | 4 +-- .../fixed_yaml_scenario_5.yml | 2 +- .../fixed_yaml_scenario_6.yml | 2 +- .../original_yaml_scenario_3.yml | 2 +- 8 files changed, 39 insertions(+), 24 deletions(-) diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index ab7efeb8..67b24688 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -6,6 +6,7 @@ import ( "container/list" "fmt" "io/ioutil" + "math" "os" logger "github.com/kubescape/go-logger" @@ -132,7 +133,7 @@ func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]Conten } for fixedListTracker < len(*fixedList) { - fixInfoMetadata.originalListTracker = len(*originalList) - 1 + fixInfoMetadata.originalListTracker = -1 fixInfoMetadata.fixedListTracker = fixedListTracker fixedListTracker = addLinesToInsert(fixInfoMetadata) } @@ -156,7 +157,14 @@ func addLinesToRemove(fixInfoMetadata *FixInfoMetadata) int { // Adds the lines to insert and returns the updated fixedListTracker func addLinesToInsert(fixInfoMetadata *FixInfoMetadata) int { currentDFSNode := (*fixInfoMetadata.fixedList)[fixInfoMetadata.fixedListTracker] - lineToInsert := (*fixInfoMetadata.originalList)[fixInfoMetadata.originalListTracker].node.Line - 1 + + var lineToInsert int + if fixInfoMetadata.originalListTracker == -1 { + lineToInsert = int(math.Inf(1)) + } else { + lineToInsert = (*fixInfoMetadata.originalList)[fixInfoMetadata.originalListTracker].node.Line - 1 + } + contentToInsert := getContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) newTracker := updateTracker(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) @@ -184,20 +192,6 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { return updatedOriginalTracker, updatedFixedTracker } -// Line numbers are readjusted such that there are no empty lines or comment lines before them -func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) { - for contentIdx, content := range *contentToAdd { - line := content.Line - for idx := line - 1; idx >= 0; idx-- { - if isEmptyLineOrComment((*linesSlice)[idx]) { - (*contentToAdd)[contentIdx].Line -= 1 - } else { - break - } - } - } -} - func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]ContentToRemove, contentAtHead string) error { linesSlice, err := getLinesSlice(filePath) diff --git a/core/pkg/fixhandler/yamlhelper.go b/core/pkg/fixhandler/yamlhelper.go index 8e1ecb96..511fcb9d 100644 --- a/core/pkg/fixhandler/yamlhelper.go +++ b/core/pkg/fixhandler/yamlhelper.go @@ -292,6 +292,27 @@ func truncateContentAtHead(filePath string) (string, error) { return contentAtHead, nil } +func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) { + for contentIdx, content := range *contentToAdd { + line := content.Line + + // Update Line number to last line if their value is math.Inf + if line == int(math.Inf(1)) { + (*contentToAdd)[contentIdx].Line = len(*linesSlice) + continue + } + + // Adjust line numbers such that there are no "empty lines or comment lines of next nodes" before them + for idx := line - 1; idx >= 0; idx-- { + if isEmptyLineOrComment((*linesSlice)[idx]) { + (*contentToAdd)[contentIdx].Line -= 1 + } else { + break + } + } + } +} + // Get the lines of existing yaml in a slice func getLinesSlice(filePath string) ([]string, error) { lineSlice := make([]string, 0) diff --git a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_1.yml b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_1.yml index 55f3aeb5..dbe3c81e 100644 --- a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_1.yml +++ b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_1.yml @@ -11,4 +11,4 @@ spec: - name: nginx_container image: nginx securityContext: - allowPrivilegeEscalation: false \ No newline at end of file + allowPrivilegeEscalation: false diff --git a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_2.yml b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_2.yml index 6ffcf01a..f0d77a9f 100644 --- a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_2.yml +++ b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_2.yml @@ -7,9 +7,9 @@ metadata: name: insert_list spec: containers: - - name: nginx1 + - name: nginx_container image: nginx securityContext: capabilities: drop: - - NET_RAW \ No newline at end of file + - NET_RAW diff --git a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_3.yml b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_3.yml index 1dfcfd49..60b4f0ed 100644 --- a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_3.yml +++ b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_3.yml @@ -7,10 +7,10 @@ metadata: name: insert_list spec: containers: - - name: nginx1 + - name: nginx_container image: nginx securityContext: capabilities: drop: - NET_RAW - - SYS_ADM \ No newline at end of file + - SYS_ADM diff --git a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_5.yml b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_5.yml index c9917375..6b66f26b 100644 --- a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_5.yml +++ b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_5.yml @@ -15,4 +15,4 @@ spec: # Testing if comments are retained as intended securityContext: - runAsRoot: false \ No newline at end of file + runAsRoot: false diff --git a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_6.yml b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_6.yml index 6eea6f7f..36968eec 100644 --- a/examples/fix-command/insert_scenarios/fixed_yaml_scenario_6.yml +++ b/examples/fix-command/insert_scenarios/fixed_yaml_scenario_6.yml @@ -13,4 +13,4 @@ spec: capabilities: drop: - "NET_RAW" - - "SYS_ADM" \ No newline at end of file + - "SYS_ADM" diff --git a/examples/fix-command/insert_scenarios/original_yaml_scenario_3.yml b/examples/fix-command/insert_scenarios/original_yaml_scenario_3.yml index ec8f58e9..3d6c873a 100644 --- a/examples/fix-command/insert_scenarios/original_yaml_scenario_3.yml +++ b/examples/fix-command/insert_scenarios/original_yaml_scenario_3.yml @@ -7,7 +7,7 @@ metadata: name: insert_list spec: containers: - - name: nginx1 + - name: nginx_container image: nginx securityContext: capabilities: