-
Notifications
You must be signed in to change notification settings - Fork 51
maintained actions #2515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
varunsh-coder
merged 7 commits into
step-security:int
from
vamshi-stepsecurity:stepsecurityActions
May 14, 2025
Merged
maintained actions #2515
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4b2c48
maintained actions
28d68b9
del uneccessary stuff
703e794
add change for tag
fc469a1
add changes for exempted actions
e9bfbb3
add actionMap as a argument to secure workflow
59f33da
Merge branch 'int' into stepsecurityActions
vamshi-stepsecurity b1012ad
add name changes to variables
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
remediation/workflow/maintainedactions/getlatestrelease.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package maintainedactions | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/google/go-github/v40/github" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
type Release struct { | ||
TagName string `json:"tag_name"` | ||
} | ||
|
||
func getMajorVersion(version string) string { | ||
hasVPrefix := strings.HasPrefix(version, "v") | ||
version = strings.TrimPrefix(version, "v") | ||
parts := strings.Split(version, ".") | ||
if len(parts) > 0 { | ||
if hasVPrefix { | ||
return "v" + parts[0] | ||
} | ||
return parts[0] | ||
} | ||
if hasVPrefix { | ||
return "v" + version | ||
} | ||
return version | ||
} | ||
|
||
func GetLatestRelease(ownerRepo string) (string, error) { | ||
splitOnSlash := strings.Split(ownerRepo, "/") | ||
if len(splitOnSlash) != 2 { | ||
return "", fmt.Errorf("invalid owner/repo format: %s", ownerRepo) | ||
} | ||
owner := splitOnSlash[0] | ||
repo := splitOnSlash[1] | ||
|
||
ctx := context.Background() | ||
|
||
// First try without token | ||
client := github.NewClient(nil) | ||
release, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo) | ||
if err != nil { | ||
// If failed, try with token | ||
token := os.Getenv("PAT") | ||
if token == "" { | ||
return "", fmt.Errorf("failed to get latest release and no GITHUB_TOKEN available: %w", err) | ||
} | ||
|
||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
client = github.NewClient(tc) | ||
|
||
release, _, err = client.Repositories.GetLatestRelease(ctx, owner, repo) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get latest release with token: %w", err) | ||
} | ||
} | ||
|
||
return getMajorVersion(release.GetTagName()), nil | ||
} |
159 changes: 159 additions & 0 deletions
159
remediation/workflow/maintainedactions/maintainedActions.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package maintainedactions | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/step-security/secure-repo/remediation/workflow/metadata" | ||
"github.com/step-security/secure-repo/remediation/workflow/permissions" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Action represents a GitHub Action in the maintained actions list | ||
type Action struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
ForkedFrom struct { | ||
Name string `json:"name"` | ||
} `json:"forkedFrom"` | ||
Score int `json:"score"` | ||
Image string `json:"image"` | ||
} | ||
|
||
type replacement struct { | ||
jobName string | ||
stepIdx int | ||
newAction string | ||
originalAction string | ||
latestVersion string | ||
} | ||
|
||
// LoadMaintainedActions loads the maintained actions from the JSON file | ||
func LoadMaintainedActions() (map[string]string, error) { | ||
// Read the JSON file | ||
jsonPath := filepath.Join("maintainedactions", "maintainedActions.json") | ||
// jsonPath := filepath.Join("maintainedActions.json") | ||
data, err := ioutil.ReadFile(jsonPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read maintained actions file: %v", err) | ||
} | ||
|
||
// Parse the JSON | ||
var actions []Action | ||
if err := json.Unmarshal(data, &actions); err != nil { | ||
return nil, fmt.Errorf("failed to parse maintained actions JSON: %v", err) | ||
} | ||
|
||
// Create a map of original actions to their Step Security replacements | ||
actionMap := make(map[string]string) | ||
for _, action := range actions { | ||
if action.ForkedFrom.Name != "" { | ||
actionMap[action.ForkedFrom.Name] = action.Name | ||
} | ||
} | ||
|
||
return actionMap, nil | ||
} | ||
|
||
// ReplaceActions replaces original actions with Step Security actions in a workflow | ||
func ReplaceActions(inputYaml string, customerMaintainedActions []string) (string, bool, error) { | ||
workflow := metadata.Workflow{} | ||
updated := false | ||
actionMap, err := LoadMaintainedActions() | ||
if err != nil { | ||
return "", updated, fmt.Errorf("unable to load maintained actions: %v", err) | ||
} | ||
err = yaml.Unmarshal([]byte(inputYaml), &workflow) | ||
if err != nil { | ||
return "", updated, fmt.Errorf("unable to parse yaml: %v", err) | ||
} | ||
|
||
// Step 1: Check if anything needs to be replaced | ||
|
||
var replacements []replacement | ||
|
||
for jobName, job := range workflow.Jobs { | ||
if metadata.IsCallingReusableWorkflow(job) { | ||
continue | ||
} | ||
for stepIdx, step := range job.Steps { | ||
// fmt.Println("step ", step.Uses) | ||
actionName := strings.Split(step.Uses, "@")[0] | ||
if newAction, ok := actionMap[actionName]; ok { | ||
if isMaintained(newAction, customerMaintainedActions) { | ||
continue | ||
} | ||
latestVersion, err := GetLatestRelease(newAction) | ||
if err != nil { | ||
return "", updated, fmt.Errorf("unable to get latest release: %v", err) | ||
} | ||
replacements = append(replacements, replacement{ | ||
jobName: jobName, | ||
stepIdx: stepIdx, | ||
newAction: newAction, | ||
originalAction: step.Uses, | ||
latestVersion: latestVersion, | ||
}) | ||
} | ||
} | ||
} | ||
if len(replacements) == 0 { | ||
// No changes needed | ||
return inputYaml, false, nil | ||
} | ||
|
||
// Step 2: Now modify the YAML lines manually | ||
t := yaml.Node{} | ||
err = yaml.Unmarshal([]byte(inputYaml), &t) | ||
if err != nil { | ||
return "", updated, fmt.Errorf("unable to parse yaml: %v", err) | ||
} | ||
|
||
inputLines := strings.Split(inputYaml, "\n") | ||
inputLines, updated = replaceAction(&t, inputLines, replacements, updated) | ||
|
||
output := strings.Join(inputLines, "\n") | ||
|
||
return output, updated, nil | ||
} | ||
|
||
func replaceAction(t *yaml.Node, inputLines []string, replacements []replacement, updated bool) ([]string, bool) { | ||
for _, r := range replacements { | ||
jobsNode := permissions.IterateNode(t, "jobs", "!!map", 0) | ||
jobNode := permissions.IterateNode(jobsNode, r.jobName, "!!map", 0) | ||
stepsNode := permissions.IterateNode(jobNode, "steps", "!!seq", 0) | ||
if stepsNode == nil { | ||
continue | ||
} | ||
|
||
// Now get the specific step | ||
stepNode := stepsNode.Content[r.stepIdx] | ||
usesNode := permissions.IterateNode(stepNode, "uses", "!!str", 0) | ||
if usesNode == nil { | ||
continue | ||
} | ||
|
||
lineNum := usesNode.Line - 1 // 0-based indexing | ||
columnNum := usesNode.Column - 1 | ||
|
||
// Replace the line | ||
oldLine := inputLines[lineNum] | ||
prefix := oldLine[:columnNum] | ||
inputLines[lineNum] = prefix + r.newAction + "@" + r.latestVersion | ||
updated = true | ||
|
||
} | ||
return inputLines, updated | ||
} | ||
|
||
func isMaintained(actionName string, maintainedActions []string) bool { | ||
for _, maintainedAction := range maintainedActions { | ||
if maintainedAction == actionName { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the plan is to get this list from the API now, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes