This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
/
chain_of_trust.go
180 lines (159 loc) · 4.39 KB
/
chain_of_trust.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io"
"os"
"path/filepath"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/clearsign"
"github.com/taskcluster/taskcluster-base-go/scopes"
"github.com/taskcluster/taskcluster-client-go/queue"
)
type ChainOfTrustFeature struct {
}
type ArtifactHash struct {
SHA256 string `json:"sha256"`
}
type CoTEnvironment struct {
PublicIPAddress string `json:"publicIpAddress"`
PrivateIPAddress string `json:"privateIpAddress"`
InstanceID string `json:"instanceId"`
InstanceType string `json:"instanceType"`
Region string `json:"region"`
}
type ChainOfTrustData struct {
Version int `json:"chainOfTrustVersion"`
Artifacts map[string]ArtifactHash `json:"artifacts"`
Task queue.TaskDefinitionResponse `json:"task"`
TaskID string `json:"taskId"`
RunID uint `json:"runId"`
WorkerGroup string `json:"workerGroup"`
WorkerID string `json:"workerId"`
Environment CoTEnvironment `json:"environment"`
}
type ChainOfTrustTaskFeature struct {
task *TaskRun
}
func (feature *ChainOfTrustFeature) Name() string {
return "Chain of Trust"
}
func (feature *ChainOfTrustFeature) PersistState() error {
return nil
}
func (feature *ChainOfTrustFeature) Initialise() error {
return nil
}
func (feature *ChainOfTrustFeature) IsEnabled(fl EnabledFeatures) bool {
return fl.ChainOfTrust
}
func (feature *ChainOfTrustFeature) NewTaskFeature(task *TaskRun) TaskFeature {
return &ChainOfTrustTaskFeature{
task: task,
}
}
func (cot *ChainOfTrustTaskFeature) RequiredScopes() scopes.Required {
// let's not require any scopes, as I see no reason to control access to this feature
return scopes.Required{}
}
func (cot *ChainOfTrustTaskFeature) Start() *CommandExecutionError {
return nil
}
func (cot *ChainOfTrustTaskFeature) Stop() *CommandExecutionError {
logFile := filepath.Join(taskContext.TaskDir, "public", "logs", "live_backing.log")
certifiedLogFile := filepath.Join(taskContext.TaskDir, "public", "logs", "certified.log")
signedCert := filepath.Join(taskContext.TaskDir, "public", "chainOfTrust.json.asc")
e := copyFileContents(logFile, certifiedLogFile)
if e != nil {
panic(e)
}
err := cot.task.uploadLog("public/logs/certified.log")
if err != nil {
return err
}
artifactHashes := map[string]ArtifactHash{}
for _, artifact := range cot.task.Artifacts {
switch a := artifact.(type) {
case *S3Artifact:
// make sure SHA256 is calculated
hash, err := calculateHash(a)
if err != nil {
panic(err)
}
artifactHashes[a.CanonicalPath] = ArtifactHash{
SHA256: hash,
}
}
}
cotCert := &ChainOfTrustData{
Version: 1,
Artifacts: artifactHashes,
Task: cot.task.Definition,
TaskID: cot.task.TaskID,
RunID: cot.task.RunID,
WorkerGroup: config.WorkerGroup,
WorkerID: config.WorkerID,
Environment: CoTEnvironment{
PublicIPAddress: config.PublicIP.String(),
PrivateIPAddress: config.PrivateIP.String(),
InstanceID: config.InstanceID,
InstanceType: config.InstanceType,
Region: config.Region,
},
}
certBytes, e := json.MarshalIndent(cotCert, "", " ")
if e != nil {
panic(e)
}
// separate signature from json with a new line
certBytes = append(certBytes, '\n')
in := bytes.NewBuffer(certBytes)
out, e := os.Create(signedCert)
if e != nil {
panic(e)
}
defer out.Close()
privKeyFile, e := os.Open(config.SigningKeyLocation)
if e != nil {
panic(e)
}
defer privKeyFile.Close()
entityList, e := openpgp.ReadArmoredKeyRing(privKeyFile)
if e != nil {
panic(e)
}
privKey := entityList[0].PrivateKey
w, e := clearsign.Encode(out, privKey, nil)
if e != nil {
panic(e)
}
_, e = io.Copy(w, in)
if e != nil {
panic(e)
}
w.Close()
out.Write([]byte{'\n'})
out.Close()
err = cot.task.uploadLog("public/chainOfTrust.json.asc")
if err != nil {
return err
}
return nil
}
func calculateHash(artifact *S3Artifact) (hash string, err error) {
rawContentFile := filepath.Join(taskContext.TaskDir, artifact.CanonicalPath)
rawContent, err := os.Open(rawContentFile)
if err != nil {
return
}
defer rawContent.Close()
hasher := sha256.New()
_, err = io.Copy(hasher, rawContent)
if err != nil {
panic(err)
}
hash = hex.EncodeToString(hasher.Sum(nil))
return
}