-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpinactions.go
137 lines (113 loc) · 3.3 KB
/
pinactions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package pin
import (
"context"
"fmt"
"os"
"strings"
"github.com/google/go-github/v40/github"
metadata "github.com/step-security/secure-workflows/remediation/workflow/metadata"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
)
func PinActions(inputYaml string) (string, bool, error) {
workflow := metadata.Workflow{}
updated := false
err := yaml.Unmarshal([]byte(inputYaml), &workflow)
if err != nil {
return inputYaml, updated, fmt.Errorf("unable to parse yaml %v", err)
}
out := inputYaml
for jobName, job := range workflow.Jobs {
for _, step := range job.Steps {
if len(step.Uses) > 0 {
localUpdated := false
out, localUpdated = pinAction(step.Uses, jobName, out)
updated = updated || localUpdated
}
}
}
return out, updated, nil
}
func pinAction(action, jobName, inputYaml string) (string, bool) {
updated := false
if !strings.Contains(action, "@") || strings.HasPrefix(action, "docker://") {
return inputYaml, updated // Cannot pin local actions and docker actions
}
if isAbsolute(action) {
return inputYaml, updated
}
leftOfAt := strings.Split(action, "@")
tagOrBranch := leftOfAt[1]
splitOnSlash := strings.Split(leftOfAt[0], "/")
owner := splitOnSlash[0]
repo := splitOnSlash[1]
PAT := os.Getenv("PAT")
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: PAT},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
commitSHA, _, err := client.Repositories.GetCommitSHA1(ctx, owner, repo, tagOrBranch, "")
if err != nil {
return inputYaml, updated
}
tagOrBranch, err = getSemanticVersion(client, owner, repo, tagOrBranch, commitSHA)
if err != nil {
return inputYaml, updated
}
pinnedAction := fmt.Sprintf("%s@%s # %s", leftOfAt[0], commitSHA, tagOrBranch)
updated = !strings.EqualFold(action, pinnedAction)
inputYaml = strings.ReplaceAll(inputYaml, action, pinnedAction)
return inputYaml, updated
}
// https://github.com/sethvargo/ratchet/blob/3524c5cfde0439099b3a37274e683af4c779b0d1/parser/refs.go#L56
func isAbsolute(ref string) bool {
parts := strings.Split(ref, "@")
last := parts[len(parts)-1]
if len(last) == 40 && isAllHex(last) {
return true
}
if len(last) == 71 && last[:6] == "sha256" && isAllHex(last[7:]) {
return true
}
return false
}
// isAllHex returns true if the given string is all hex characters, false
// otherwise.
func isAllHex(s string) bool {
for _, ch := range s {
if (ch < '0' || ch > '9') && (ch < 'a' || ch > 'f') && (ch < 'A' || ch > 'F') {
return false
}
}
return true
}
func getSemanticVersion(client *github.Client, owner, repo, tagOrBranch, commitSHA string) (string, error) {
tags, _, err := client.Git.ListMatchingRefs(context.Background(), owner, repo, &github.ReferenceListOptions{
Ref: fmt.Sprintf("tags/%s.", tagOrBranch),
ListOptions: github.ListOptions{
PerPage: 100,
},
})
if err != nil {
return "", err
}
for i := len(tags) - 1; i >= 0; i-- {
tag := strings.TrimPrefix(*tags[i].Ref, "refs/tags/")
if *tags[i].Object.Type == "commit" {
if commitSHA == *tags[i].Object.SHA {
return tag, nil
}
} else {
commitsha, _, err := client.Repositories.GetCommitSHA1(context.Background(), owner, repo, tag, "")
if err != nil {
return "", err
}
if commitSHA == commitsha {
return tag, nil
}
}
}
return tagOrBranch, nil
}