Skip to content

Commit c988535

Browse files
update pattern matching & add test cases
1 parent 3ba8fe3 commit c988535

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-11
lines changed

remediation/workflow/pin/pinactions.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"regexp"
89
"strings"
910

@@ -16,11 +17,6 @@ import (
1617
func PinActions(inputYaml string, exemptedActions []string) (string, bool, error) {
1718
workflow := metadata.Workflow{}
1819
updated := false
19-
exemptedActionsMap := make(map[string]bool)
20-
for _, exemptedAction := range exemptedActions {
21-
exemptedAction = strings.TrimRight(exemptedAction, "/")
22-
exemptedActionsMap[exemptedAction] = true
23-
}
2420
err := yaml.Unmarshal([]byte(inputYaml), &workflow)
2521
if err != nil {
2622
return inputYaml, updated, fmt.Errorf("unable to parse yaml %v", err)
@@ -33,7 +29,7 @@ func PinActions(inputYaml string, exemptedActions []string) (string, bool, error
3329
for _, step := range job.Steps {
3430
if len(step.Uses) > 0 {
3531
localUpdated := false
36-
out, localUpdated = PinAction(step.Uses, out, exemptedActionsMap)
32+
out, localUpdated = PinAction(step.Uses, out, exemptedActions)
3733
updated = updated || localUpdated
3834
}
3935
}
@@ -42,7 +38,7 @@ func PinActions(inputYaml string, exemptedActions []string) (string, bool, error
4238
return out, updated, nil
4339
}
4440

45-
func PinAction(action, inputYaml string, exemptedActionsMap map[string]bool) (string, bool) {
41+
func PinAction(action, inputYaml string, exemptedActions []string) (string, bool) {
4642

4743
updated := false
4844
if !strings.Contains(action, "@") || strings.HasPrefix(action, "docker://") {
@@ -56,7 +52,7 @@ func PinAction(action, inputYaml string, exemptedActionsMap map[string]bool) (st
5652
tagOrBranch := leftOfAt[1]
5753

5854
// skip pinning for exempted actions
59-
if exemptedActionsMap[leftOfAt[0]] {
55+
if actionExists(leftOfAt[0], exemptedActions) {
6056
return inputYaml, updated
6157
}
6258

@@ -198,3 +194,20 @@ func getSemanticVersion(client *github.Client, owner, repo, tagOrBranch, commitS
198194
}
199195
return tagOrBranch, nil
200196
}
197+
198+
// Function to check if an action matches any pattern in the list
199+
func actionExists(actionName string, patterns []string) bool {
200+
for _, pattern := range patterns {
201+
// Use filepath.Match to match the pattern
202+
matched, err := filepath.Match(pattern, actionName)
203+
if err != nil {
204+
// Handle invalid patterns
205+
fmt.Printf("Error matching pattern: %v\n", err)
206+
continue
207+
}
208+
if matched {
209+
return true
210+
}
211+
}
212+
return false
213+
}

remediation/workflow/pin/pinactions_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ func TestPinActions(t *testing.T) {
263263
})
264264

265265
tests := []struct {
266-
fileName string
267-
wantUpdated bool
266+
fileName string
267+
wantUpdated bool
268+
exemptedActions []string
268269
}{
269270
{fileName: "alreadypinned.yml", wantUpdated: false},
270271
{fileName: "branch.yml", wantUpdated: true},
@@ -276,6 +277,7 @@ func TestPinActions(t *testing.T) {
276277
{fileName: "actionwithcomment.yml", wantUpdated: true},
277278
{fileName: "repeatedactionwithcomment.yml", wantUpdated: true},
278279
{fileName: "immutableaction-1.yml", wantUpdated: true},
280+
{fileName: "exemptaction.yml", wantUpdated: true, exemptedActions: []string{"actions/checkout", "rohith/*"}},
279281
}
280282
for _, tt := range tests {
281283
input, err := ioutil.ReadFile(path.Join(inputDirectory, tt.fileName))
@@ -284,7 +286,7 @@ func TestPinActions(t *testing.T) {
284286
log.Fatal(err)
285287
}
286288

287-
output, gotUpdated, err := PinActions(string(input), nil)
289+
output, gotUpdated, err := PinActions(string(input), tt.exemptedActions)
288290
if tt.wantUpdated != gotUpdated {
289291
t.Errorf("test failed wantUpdated %v did not match gotUpdated %v", tt.wantUpdated, gotUpdated)
290292
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: publish to nuget
2+
on:
3+
push:
4+
branches:
5+
- master # Default release branch
6+
jobs:
7+
publish:
8+
name: build, pack & publish
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v1
12+
13+
# - name: Setup dotnet
14+
# uses: actions/setup-dotnet@v1
15+
# with:
16+
# dotnet-version: 3.1.200
17+
18+
# Publish
19+
- name: publish on version change
20+
id: publish_nuget
21+
uses: brandedoutcast/publish-nuget@v2
22+
with:
23+
PROJECT_FILE_PATH: Core/Core.csproj
24+
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
25+
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json
26+
publish1:
27+
name: build, pack & publish
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v1
31+
32+
# - name: Setup dotnet
33+
# uses: actions/setup-dotnet@v1
34+
# with:
35+
# dotnet-version: 3.1.200
36+
37+
# Publish
38+
- name: publish on version change
39+
id: publish_nuget
40+
uses: rohith/publish-nuget@v2
41+
with:
42+
PROJECT_FILE_PATH: Core/Core.csproj
43+
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
44+
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: publish to nuget
2+
on:
3+
push:
4+
branches:
5+
- master # Default release branch
6+
jobs:
7+
publish:
8+
name: build, pack & publish
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v1
12+
13+
# - name: Setup dotnet
14+
# uses: actions/setup-dotnet@v1
15+
# with:
16+
# dotnet-version: 3.1.200
17+
18+
# Publish
19+
- name: publish on version change
20+
id: publish_nuget
21+
uses: brandedoutcast/publish-nuget@c12b8546b67672ee38ac87bea491ac94a587f7cc # v2.5.5
22+
with:
23+
PROJECT_FILE_PATH: Core/Core.csproj
24+
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
25+
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json
26+
publish1:
27+
name: build, pack & publish
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v1
31+
32+
# - name: Setup dotnet
33+
# uses: actions/setup-dotnet@v1
34+
# with:
35+
# dotnet-version: 3.1.200
36+
37+
# Publish
38+
- name: publish on version change
39+
id: publish_nuget
40+
uses: rohith/publish-nuget@v2
41+
with:
42+
PROJECT_FILE_PATH: Core/Core.csproj
43+
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
44+
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

0 commit comments

Comments
 (0)