Skip to content

Commit fc469a1

Browse files
Balijepalli Vamshi KrishnaBalijepalli Vamshi Krishna
Balijepalli Vamshi Krishna
authored and
Balijepalli Vamshi Krishna
committed
add changes for exempted actions
1 parent 703e794 commit fc469a1

File tree

5 files changed

+107
-27
lines changed

5 files changed

+107
-27
lines changed

remediation/workflow/maintainedactions/maintainedActions.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func LoadMaintainedActions() (map[string]string, error) {
5959
}
6060

6161
// ReplaceActions replaces original actions with Step Security actions in a workflow
62-
func ReplaceActions(inputYaml string) (string, bool, error) {
62+
func ReplaceActions(inputYaml string, customerMaintainedActions []string) (string, bool, error) {
6363
workflow := metadata.Workflow{}
6464
updated := false
6565
actionMap, err := LoadMaintainedActions()
@@ -83,6 +83,9 @@ func ReplaceActions(inputYaml string) (string, bool, error) {
8383
// fmt.Println("step ", step.Uses)
8484
actionName := strings.Split(step.Uses, "@")[0]
8585
if newAction, ok := actionMap[actionName]; ok {
86+
if isMaintained(newAction, customerMaintainedActions) {
87+
continue
88+
}
8689
latestVersion, err := GetLatestRelease(newAction)
8790
if err != nil {
8891
return "", updated, fmt.Errorf("unable to get latest release: %v", err)
@@ -145,3 +148,12 @@ func replaceAction(t *yaml.Node, inputLines []string, replacements []replacement
145148
}
146149
return inputLines, updated
147150
}
151+
152+
func isMaintained(actionName string, maintainedActions []string) bool {
153+
for _, maintainedAction := range maintainedActions {
154+
if maintainedAction == actionName {
155+
return true
156+
}
157+
}
158+
return false
159+
}

remediation/workflow/maintainedactions/maintainedactions_test.go

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
package maintainedactions
22

33
import (
4-
"fmt"
54
"io/ioutil"
6-
"os"
75
"path"
86
"testing"
97

108
"github.com/jarcoal/httpmock"
119
)
1210

13-
// WriteYAML writes the given string content to a YAML file with the specified filename.
14-
func WriteYAML(filename string, content string) error {
15-
// Create or truncate the file
16-
file, err := os.Create(filename)
17-
if err != nil {
18-
return fmt.Errorf("failed to create file %s: %w", filename, err)
19-
}
20-
defer file.Close()
21-
22-
// Write the string content to the file
23-
_, err = file.WriteString(content)
24-
if err != nil {
25-
return fmt.Errorf("failed to write to file %s: %w", filename, err)
26-
}
27-
28-
return nil
29-
}
30-
3111
func TestReplaceActions(t *testing.T) {
3212
const inputDirectory = "../../../testfiles/maintainedactions/input"
3313
const outputDirectory = "../../../testfiles/maintainedactions/output"
@@ -89,6 +69,12 @@ func TestReplaceActions(t *testing.T) {
8969
wantUpdated: true,
9070
wantErr: false,
9171
},
72+
{
73+
name: "exemtedMaintainedActions.yml",
74+
inputFile: "exemtedMaintainedActions.yml",
75+
outputFile: "exemtedMaintainedActions.yml",
76+
wantUpdated: true,
77+
},
9278
}
9379

9480
for _, tt := range tests {
@@ -100,11 +86,18 @@ func TestReplaceActions(t *testing.T) {
10086
}
10187

10288
// Call ReplaceActions
103-
got, updated, err := ReplaceActions(string(input))
89+
var got string
90+
var updated bool
91+
var replaceErr error
92+
if tt.inputFile == "exemtedMaintainedActions.yml" {
93+
got, updated, replaceErr = ReplaceActions(string(input), []string{"step-security/git-restore-mtime-action"})
94+
} else {
95+
got, updated, replaceErr = ReplaceActions(string(input), []string{})
96+
}
10497

10598
// Check error
106-
if (err != nil) != tt.wantErr {
107-
t.Errorf("ReplaceActions() error = %v, wantErr %v", err, tt.wantErr)
99+
if (replaceErr != nil) != tt.wantErr {
100+
t.Errorf("ReplaceActions() error = %v, wantErr %v", replaceErr, tt.wantErr)
108101
return
109102
}
110103

@@ -121,7 +114,7 @@ func TestReplaceActions(t *testing.T) {
121114

122115
// Compare output with expected
123116
if got != string(expectedOutput) {
124-
WriteYAML(tt.outputFile+"second", got)
117+
// WriteYAML(tt.outputFile+"second", got)
125118
t.Errorf("ReplaceActions() = %v, want %v", got, string(expectedOutput))
126119
}
127120
})

remediation/workflow/secureworkflow.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc d
1818
pinActions, addHardenRunner, addPermissions, addProjectComment, addMaintainedActions := true, true, true, true, true
1919
pinnedActions, addedHardenRunner, addedPermissions, addedMaintainedActions := false, false, false, false
2020
ignoreMissingKBs := false
21-
exemptedActions, pinToImmutable := []string{}, false
21+
exemptedActions, pinToImmutable, customerMaintainedActions := []string{}, false, []string{}
2222
if len(params) > 0 {
2323
if v, ok := params[0].([]string); ok {
2424
exemptedActions = v
@@ -29,6 +29,11 @@ func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc d
2929
pinToImmutable = v
3030
}
3131
}
32+
if len(params) > 2 {
33+
if v, ok := params[2].([]string); ok {
34+
customerMaintainedActions = v
35+
}
36+
}
3237

3338
if queryStringParams["pinActions"] == "false" {
3439
pinActions = false
@@ -79,7 +84,7 @@ func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc d
7984
}
8085

8186
if addMaintainedActions {
82-
secureWorkflowReponse.FinalOutput, addedMaintainedActions, err = maintainedactions.ReplaceActions(secureWorkflowReponse.FinalOutput)
87+
secureWorkflowReponse.FinalOutput, addedMaintainedActions, err = maintainedactions.ReplaceActions(secureWorkflowReponse.FinalOutput, customerMaintainedActions)
8388
if err != nil {
8489
secureWorkflowReponse.HasErrors = true
8590
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test Workflow
2+
on: push
3+
4+
jobs:
5+
test:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v3
9+
- uses: amannn/action-semantic-pull-request@v5
10+
with:
11+
types: feat,fix,chore
12+
- uses: fkirc/skip-duplicate-actions@v5
13+
with:
14+
do_not_skip: '["release"]'
15+
- uses: chetan/git-restore-mtime-action@v1
16+
with:
17+
pattern: '**/*'
18+
19+
build:
20+
runs-on: ubuntu-latest
21+
needs: test
22+
steps:
23+
- uses: actions/checkout@v3
24+
- uses: actions/setup-node@v3
25+
with:
26+
node-version: '16'
27+
- uses: actions/cache@v3
28+
with:
29+
path: ~/.npm
30+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
31+
restore-keys: |
32+
${{ runner.os }}-node-
33+
- uses: amannn/action-semantic-pull-request@v5
34+
with:
35+
types: feat,fix,chore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test Workflow
2+
on: push
3+
4+
jobs:
5+
test:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v3
9+
- uses: step-security/action-semantic-pull-request@v5
10+
with:
11+
types: feat,fix,chore
12+
- uses: step-security/skip-duplicate-actions@v5
13+
with:
14+
do_not_skip: '["release"]'
15+
- uses: chetan/git-restore-mtime-action@v1
16+
with:
17+
pattern: '**/*'
18+
19+
build:
20+
runs-on: ubuntu-latest
21+
needs: test
22+
steps:
23+
- uses: actions/checkout@v3
24+
- uses: actions/setup-node@v3
25+
with:
26+
node-version: '16'
27+
- uses: actions/cache@v3
28+
with:
29+
path: ~/.npm
30+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
31+
restore-keys: |
32+
${{ runner.os }}-node-
33+
- uses: step-security/action-semantic-pull-request@v5
34+
with:
35+
types: feat,fix,chore

0 commit comments

Comments
 (0)