forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_of_trust.go
219 lines (193 loc) · 6.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//go:build multiuser
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"golang.org/x/crypto/ed25519"
"github.com/taskcluster/taskcluster/v47/clients/client-go/tcqueue"
"github.com/taskcluster/taskcluster/v47/internal/scopes"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/artifacts"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/fileutil"
)
const (
// ChainOfTrustKeyNotSecureMessage contains message to log when chain of
// trust key is discovered at runtime not to be secure
ChainOfTrustKeyNotSecureMessage = "Was expecting attempt to read private chain of trust key as task user to fail - however, it did not!"
)
var (
certifiedLogPath = filepath.Join("generic-worker", "certified.log")
certifiedLogName = "public/logs/certified.log"
unsignedCertPath = filepath.Join("generic-worker", "chain-of-trust.json")
unsignedCertName = "public/chain-of-trust.json"
ed25519SignedCertPath = filepath.Join("generic-worker", "chain-of-trust.json.sig")
ed25519SignedCertName = "public/chain-of-trust.json.sig"
)
type ChainOfTrustFeature struct {
Ed25519PrivateKey ed25519.PrivateKey
}
type ArtifactHash struct {
SHA256 string `json:"sha256"`
}
type CoTEnvironment struct {
PublicIPAddress string `json:"publicIpAddress,omitempty"`
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 tcqueue.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
ed25519PrivKey ed25519.PrivateKey
}
func (feature *ChainOfTrustFeature) Name() string {
return "Chain of Trust"
}
func (feature *ChainOfTrustFeature) PersistState() error {
return nil
}
func (feature *ChainOfTrustFeature) Initialise() (err error) {
feature.Ed25519PrivateKey, err = readEd25519PrivateKeyFromFile(config.Ed25519SigningKeyLocation)
if err != nil {
return
}
// platform-specific mechanism to lock down file permissions
// of private signing key
err = fileutil.SecureFiles(config.Ed25519SigningKeyLocation)
return
}
func (feature *ChainOfTrustFeature) IsEnabled(task *TaskRun) bool {
return task.Payload.Features.ChainOfTrust
}
func (feature *ChainOfTrustFeature) NewTaskFeature(task *TaskRun) TaskFeature {
return &ChainOfTrustTaskFeature{
task: task,
ed25519PrivKey: feature.Ed25519PrivateKey,
}
}
func (feature *ChainOfTrustTaskFeature) ReservedArtifacts() []string {
return []string{
unsignedCertName,
ed25519SignedCertName,
certifiedLogName,
}
}
func (feature *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 (feature *ChainOfTrustTaskFeature) Start() *CommandExecutionError {
// Return an error if the task user can read the private key file.
// We shouldn't be able to read the private key, if we can let's raise
// MalformedPayloadError, as it could be a problem with the task definition
// (for example, enabling chainOfTrust on a worker type that has
// runTasksAsCurrentUser enabled).
err := feature.ensureTaskUserCantReadPrivateCotKey()
if err != nil {
return MalformedPayloadError(err)
}
return nil
}
func (feature *ChainOfTrustTaskFeature) Stop(err *ExecutionErrors) {
logFile := filepath.Join(taskContext.TaskDir, logPath)
certifiedLogFile := filepath.Join(taskContext.TaskDir, certifiedLogPath)
unsignedCert := filepath.Join(taskContext.TaskDir, unsignedCertPath)
ed25519SignedCert := filepath.Join(taskContext.TaskDir, ed25519SignedCertPath)
copyErr := copyFileContents(logFile, certifiedLogFile)
if copyErr != nil {
panic(copyErr)
}
err.add(feature.task.uploadLog(certifiedLogName, certifiedLogPath))
artifactHashes := map[string]ArtifactHash{}
for _, artifact := range feature.task.Artifacts {
// make sure SHA256 is calculated
switch a := artifact.(type) {
case *artifacts.S3Artifact:
hash, hashErr := fileutil.CalculateSHA256(a.RawContentFile)
if hashErr != nil {
panic(hashErr)
}
artifactHashes[a.Name] = ArtifactHash{
SHA256: hash,
}
case *artifacts.ObjectArtifact:
hash, hashErr := fileutil.CalculateSHA256(a.RawContentFile)
if hashErr != nil {
panic(hashErr)
}
artifactHashes[a.Name] = ArtifactHash{
SHA256: hash,
}
}
}
cotCert := &ChainOfTrustData{
Version: 1,
Artifacts: artifactHashes,
Task: feature.task.Definition,
TaskID: feature.task.TaskID,
RunID: feature.task.RunID,
WorkerGroup: config.WorkerGroup,
WorkerID: config.WorkerID,
Environment: CoTEnvironment{
PrivateIPAddress: config.PrivateIP.String(),
InstanceID: config.InstanceID,
InstanceType: config.InstanceType,
Region: config.Region,
},
}
if config.PublicIP != nil {
cotCert.Environment.PublicIPAddress = config.PublicIP.String()
}
certBytes, e := json.MarshalIndent(cotCert, "", " ")
if e != nil {
panic(e)
}
// create unsigned chain-of-trust.json
e = os.WriteFile(unsignedCert, certBytes, 0644)
if e != nil {
panic(e)
}
err.add(feature.task.uploadLog(unsignedCertName, unsignedCertPath))
// create detached ed25519 chain-of-trust.json.sig
sig := ed25519.Sign(feature.ed25519PrivKey, certBytes)
e = os.WriteFile(ed25519SignedCert, sig, 0644)
if e != nil {
panic(e)
}
err.add(feature.task.uploadArtifact(
createDataArtifact(
&artifacts.BaseArtifact{
Name: ed25519SignedCertName,
Expires: feature.task.Definition.Expires,
},
ed25519SignedCertPath,
"application/octet-stream",
"gzip",
),
))
}
func (cot *ChainOfTrustTaskFeature) ensureTaskUserCantReadPrivateCotKey() error {
c, err := cot.catCotKeyCommand()
if err != nil {
panic(fmt.Errorf("SERIOUS BUG: Could not create command (not even trying to execute it yet) to cat private chain of trust key %v - %v", config.Ed25519SigningKeyLocation, err))
}
r := c.Execute()
if !r.Failed() {
log.Print(r.String())
return errors.New(ChainOfTrustKeyNotSecureMessage)
}
return nil
}