|
| 1 | +package maintainedactions |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/step-security/secure-repo/remediation/workflow/metadata" |
| 10 | + "github.com/step-security/secure-repo/remediation/workflow/permissions" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +// Action represents a GitHub Action in the maintained actions list |
| 15 | +type Action struct { |
| 16 | + Name string `json:"name"` |
| 17 | + Description string `json:"description"` |
| 18 | + ForkedFrom struct { |
| 19 | + Name string `json:"name"` |
| 20 | + } `json:"forkedFrom"` |
| 21 | + Score int `json:"score"` |
| 22 | + Image string `json:"image"` |
| 23 | +} |
| 24 | + |
| 25 | +type replacement struct { |
| 26 | + jobName string |
| 27 | + stepIdx int |
| 28 | + newAction string |
| 29 | + originalAction string |
| 30 | + latestVersion string |
| 31 | +} |
| 32 | + |
| 33 | +// LoadMaintainedActions loads the maintained actions from the JSON file |
| 34 | +func LoadMaintainedActions(jsonPath string) (map[string]string, error) { |
| 35 | + // Read the JSON file |
| 36 | + data, err := ioutil.ReadFile(jsonPath) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to read maintained actions file: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Parse the JSON |
| 42 | + var actions []Action |
| 43 | + if err := json.Unmarshal(data, &actions); err != nil { |
| 44 | + return nil, fmt.Errorf("failed to parse maintained actions JSON: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Create a map of original actions to their Step Security replacements |
| 48 | + actionMap := make(map[string]string) |
| 49 | + for _, action := range actions { |
| 50 | + if action.ForkedFrom.Name != "" { |
| 51 | + actionMap[action.ForkedFrom.Name] = action.Name |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return actionMap, nil |
| 56 | +} |
| 57 | + |
| 58 | +// ReplaceActions replaces original actions with Step Security actions in a workflow |
| 59 | +func ReplaceActions(inputYaml string, customerMaintainedActions map[string]string) (string, bool, error) { |
| 60 | + workflow := metadata.Workflow{} |
| 61 | + updated := false |
| 62 | + |
| 63 | + actionMap := customerMaintainedActions |
| 64 | + |
| 65 | + err := yaml.Unmarshal([]byte(inputYaml), &workflow) |
| 66 | + if err != nil { |
| 67 | + return "", updated, fmt.Errorf("unable to parse yaml: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Step 1: Check if anything needs to be replaced |
| 71 | + |
| 72 | + var replacements []replacement |
| 73 | + |
| 74 | + for jobName, job := range workflow.Jobs { |
| 75 | + if metadata.IsCallingReusableWorkflow(job) { |
| 76 | + continue |
| 77 | + } |
| 78 | + for stepIdx, step := range job.Steps { |
| 79 | + // fmt.Println("step ", step.Uses) |
| 80 | + actionName := strings.Split(step.Uses, "@")[0] |
| 81 | + if newAction, ok := actionMap[actionName]; ok { |
| 82 | + latestVersion, err := GetLatestRelease(newAction) |
| 83 | + if err != nil { |
| 84 | + return "", updated, fmt.Errorf("unable to get latest release: %v", err) |
| 85 | + } |
| 86 | + replacements = append(replacements, replacement{ |
| 87 | + jobName: jobName, |
| 88 | + stepIdx: stepIdx, |
| 89 | + newAction: newAction, |
| 90 | + originalAction: step.Uses, |
| 91 | + latestVersion: latestVersion, |
| 92 | + }) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + if len(replacements) == 0 { |
| 97 | + // No changes needed |
| 98 | + return inputYaml, false, nil |
| 99 | + } |
| 100 | + |
| 101 | + // Step 2: Now modify the YAML lines manually |
| 102 | + t := yaml.Node{} |
| 103 | + err = yaml.Unmarshal([]byte(inputYaml), &t) |
| 104 | + if err != nil { |
| 105 | + return "", updated, fmt.Errorf("unable to parse yaml: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + inputLines := strings.Split(inputYaml, "\n") |
| 109 | + inputLines, updated = replaceAction(&t, inputLines, replacements, updated) |
| 110 | + |
| 111 | + output := strings.Join(inputLines, "\n") |
| 112 | + |
| 113 | + return output, updated, nil |
| 114 | +} |
| 115 | + |
| 116 | +func replaceAction(t *yaml.Node, inputLines []string, replacements []replacement, updated bool) ([]string, bool) { |
| 117 | + for _, r := range replacements { |
| 118 | + jobsNode := permissions.IterateNode(t, "jobs", "!!map", 0) |
| 119 | + jobNode := permissions.IterateNode(jobsNode, r.jobName, "!!map", 0) |
| 120 | + stepsNode := permissions.IterateNode(jobNode, "steps", "!!seq", 0) |
| 121 | + if stepsNode == nil { |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + // Now get the specific step |
| 126 | + stepNode := stepsNode.Content[r.stepIdx] |
| 127 | + usesNode := permissions.IterateNode(stepNode, "uses", "!!str", 0) |
| 128 | + if usesNode == nil { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + lineNum := usesNode.Line - 1 // 0-based indexing |
| 133 | + columnNum := usesNode.Column - 1 |
| 134 | + |
| 135 | + // Replace the line |
| 136 | + oldLine := inputLines[lineNum] |
| 137 | + prefix := oldLine[:columnNum] |
| 138 | + inputLines[lineNum] = prefix + r.newAction + "@" + r.latestVersion |
| 139 | + updated = true |
| 140 | + |
| 141 | + } |
| 142 | + return inputLines, updated |
| 143 | +} |
0 commit comments