Skip to content

Bug replaceable actions #2531

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion remediation/workflow/maintainedactions/getlatestrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getMajorVersion(version string) string {

func GetLatestRelease(ownerRepo string) (string, error) {
splitOnSlash := strings.Split(ownerRepo, "/")
if len(splitOnSlash) != 2 {
if len(splitOnSlash) < 2 {
return "", fmt.Errorf("invalid owner/repo format: %s", ownerRepo)
}
owner := splitOnSlash[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func ReplaceActions(inputYaml string, customerMaintainedActions map[string]strin
if newAction, ok := actionMap[actionName]; ok {
latestVersion, err := GetLatestRelease(newAction)
if err != nil {
return "", updated, fmt.Errorf("unable to get latest release: %v", err)
return inputYaml, updated, fmt.Errorf("unable to get latest release: %v", err)
}
replacements = append(replacements, replacement{
jobName: jobName,
Expand Down
9 changes: 9 additions & 0 deletions remediation/workflow/maintainedactions/maintainedActions.json
Original file line number Diff line number Diff line change
Expand Up @@ -493,5 +493,14 @@
},
"score": 10,
"image": "https://avatars.githubusercontent.com/u/88700172?v=4"
},
{
"name": "step-security/actions-cache/restore",
"description": "GitHub Action to restore cache",
"forkedFrom": {
"name": "tespkg/actions-cache/restore"
},
"score": 10,
"image": "https://avatars.githubusercontent.com/u/88700172?v=4"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func TestReplaceActions(t *testing.T) {
"created_at": "2023-01-01T00:00:00Z"
}`))

httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/releases/latest",
httpmock.NewStringResponder(200, `{
"tag_name": "v1.0.0",
"name": "v1.0.0",
"body": "Release notes",
"created_at": "2023-01-01T00:00:00Z"
}`))

tests := []struct {
name string
inputFile string
Expand Down
1 change: 1 addition & 0 deletions remediation/workflow/secureworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc d
if replaceMaintainedActions {
secureWorkflowReponse.FinalOutput, replacedMaintainedActions, err = maintainedactions.ReplaceActions(secureWorkflowReponse.FinalOutput, maintainedActionsMap)
if err != nil {
log.Printf("Error replacing maintained actions: %v", err)
secureWorkflowReponse.HasErrors = true
}
}
Expand Down
27 changes: 26 additions & 1 deletion remediation/workflow/secureworkflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ func TestSecureWorkflow(t *testing.T) {
}
]`))

httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/commits/v1",
httpmock.NewStringResponder(200, `d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0a1b2c3`))

httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/git/matching-refs/tags/v1.",
httpmock.NewStringResponder(200, `[
{
"ref": "refs/tags/v1.0.0",
"object": {
"sha": "d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0a1b2c3",
"type": "commit"
}
}
]`))

httpmock.RegisterResponder("GET", "https://api.github.com/repos/step-security/actions-cache/releases/latest",
httpmock.NewStringResponder(200, `{
"tag_name": "v1.0.0",
"name": "v1.0.0",
"body": "Release notes",
"created_at": "2023-01-01T00:00:00Z"
}`))

tests := []struct {
fileName string
wantPinnedActions bool
Expand Down Expand Up @@ -244,7 +266,6 @@ func TestSecureWorkflow(t *testing.T) {
t.Errorf("unable to load the file %s", err)
}
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, []string{}, false, actionMap)

} else {
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{})
}
Expand Down Expand Up @@ -275,6 +296,10 @@ func TestSecureWorkflow(t *testing.T) {
t.Errorf("test failed %s did not match expected PinnedActions value. Expected:%v Actual:%v", test.fileName, test.wantPinnedActions, output.PinnedActions)
}

if output.AddedMaintainedActions != test.wantAddedMaintainedActions {
t.Errorf("test failed %s did not match expected AddedMaintainedActions value. Expected:%v Actual:%v", test.fileName, test.wantAddedMaintainedActions, output.AddedMaintainedActions)
}

}
}

Expand Down
8 changes: 7 additions & 1 deletion testfiles/maintainedActions/input/oneJob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ jobs:
do_not_skip: '["release"]'
- uses: chetan/git-restore-mtime-action@v1
with:
pattern: '**/*'
pattern: '**/*'
- uses: tespkg/actions-cache/restore@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
8 changes: 7 additions & 1 deletion testfiles/maintainedActions/output/oneJob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ jobs:
do_not_skip: '["release"]'
- uses: step-security/git-restore-mtime-action@v2
with:
pattern: '**/*'
pattern: '**/*'
- uses: step-security/actions-cache/restore@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
9 changes: 8 additions & 1 deletion testfiles/secureworkflow/input/replaceactions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ jobs:
- uses: github/super-linter@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DISABLE_ERRORS: true
DISABLE_ERRORS: true
- uses: tespkg/actions-cache/restore@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

9 changes: 8 additions & 1 deletion testfiles/secureworkflow/output/replaceactions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ jobs:
- uses: github/super-linter@34b2f8032d759425f6b42ea2e52231b33ae05401 # v3.17.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DISABLE_ERRORS: true
DISABLE_ERRORS: true
- uses: step-security/actions-cache/restore@d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0a1b2c3 # v1.0.0
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-