-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpolicy.go
More file actions
124 lines (119 loc) · 3.01 KB
/
Copy pathpolicy.go
File metadata and controls
124 lines (119 loc) · 3.01 KB
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
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v40/github"
"gopkg.in/yaml.v2"
)
type Policy struct {
Repo string
BuildMonitor *BuildMonitor `yaml:"build_monitor"`
Rebuilder *Rebuilder `yaml:"rebuilder"`
ProvenanceUpload *ProvenanceUpload `yaml:"provenance_upload"`
Digest string
Scope string
Package string
}
type Rebuilder struct {
PackageRoot string `yaml:"package_root"`
}
type ProvenanceUpload struct {
AuthorizedBuilders []string `yaml:"authorized_builders"`
}
type BuildMonitor struct {
GitHubActions `yaml:"github_actions"`
}
type GitHubActions struct {
Workflow string
Artifacts []ArtifactSpec
RequireSucceeded *CompletionSpec `yaml:"require_succeeded"`
}
type ArtifactSpec struct {
Name string
Patterns []string
}
type CompletionSpec struct {
Job string
Step string
}
func fetchPolicy(c *github.Client, scope, pkg, ref string) (*Policy, error) {
file, _, _, err := c.Repositories.GetContents(
context.Background(), *policyRepoOwner, *policyRepoName, filepath.Join(*policyRepoDir, scope, pkg, "policy.yaml"), &github.RepositoryContentGetOptions{Ref: ref})
if err != nil {
return nil, err
}
content, err := file.GetContent()
if err != nil {
return nil, err
}
var np Policy
if err := yaml.Unmarshal([]byte(content), &np); err != nil {
return nil, err
}
h := sha256.Sum256([]byte(content))
np.Digest = hex.EncodeToString(h[:])
np.Scope = scope
np.Package = pkg
return &np, nil
}
func fetchPolicies(ref string) (*[]Policy, error) {
gitfs := memfs.New()
storer := memory.NewStorage()
_, err := git.Clone(storer, gitfs, &git.CloneOptions{
URL: fmt.Sprintf("https://github.com/%s/%s.git", *policyRepoOwner, *policyRepoName),
SingleBranch: true,
ReferenceName: plumbing.NewBranchReferenceName(ref),
})
if err != nil {
return nil, err
}
dirs := []string{*policyRepoDir}
var paths []string
for len(dirs) > 0 {
dir := dirs[len(dirs)-1]
dirs = dirs[:len(dirs)-1]
files, err := gitfs.ReadDir(dir)
if err != nil {
return nil, err
}
for _, f := range files {
switch {
case f.IsDir():
dirs = append(dirs, filepath.Join(dir, f.Name()))
default:
if f.Name() == "policy.yaml" {
paths = append(paths, filepath.Join(dir, f.Name()))
}
}
}
}
var policies []Policy
for _, path := range paths {
f, err := gitfs.Open(path)
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var np Policy
if err := yaml.Unmarshal(content, &np); err != nil {
return nil, err
}
h := sha256.Sum256([]byte(content))
np.Digest = hex.EncodeToString(h[:])
parts := strings.Split(path, string(os.PathSeparator))
np.Scope = parts[0]
np.Package = parts[1]
policies = append(policies, np)
}
return &policies, nil
}