Skip to content

Commit 6ba1969

Browse files
ChaosInTheCRDjkjell
authored andcommitted
improving run attestors
Signed-off-by: chaosinthecrd <[email protected]>
1 parent dd59a2b commit 6ba1969

File tree

2 files changed

+18
-72
lines changed

2 files changed

+18
-72
lines changed

attestation/context.go

+16-72
Original file line numberDiff line numberDiff line change
@@ -117,75 +117,33 @@ func NewContext(attestors []Attestor, opts ...AttestationContextOption) (*Attest
117117
}
118118

119119
func (ctx *AttestationContext) RunAttestors() error {
120-
preAttestors := []Attestor{}
121-
materialAttestors := []Attestor{}
122-
exeucteAttestors := []Attestor{}
123-
productAttestors := []Attestor{}
124-
postAttestors := []Attestor{}
125-
120+
attestors := make(map[RunType][]Attestor)
126121
for _, attestor := range ctx.attestors {
127-
switch attestor.RunType() {
128-
case PreMaterialRunType:
129-
preAttestors = append(preAttestors, attestor)
130-
131-
case MaterialRunType:
132-
materialAttestors = append(materialAttestors, attestor)
133-
134-
case ExecuteRunType:
135-
exeucteAttestors = append(exeucteAttestors, attestor)
136-
137-
case ProductRunType:
138-
productAttestors = append(productAttestors, attestor)
139-
140-
case PostProductRunType:
141-
postAttestors = append(postAttestors, attestor)
142-
143-
default:
122+
if attestor.RunType() == "" {
144123
return ErrInvalidOption{
145-
Option: "attestor.RunType",
124+
Option: "RunType",
146125
Reason: fmt.Sprintf("unknown run type %v", attestor.RunType()),
147126
}
148127
}
149128
}
150129

151-
for _, attestor := range preAttestors {
152-
if err := ctx.runAttestor(attestor); err != nil {
153-
return err
154-
}
155-
}
156-
157-
for _, attestor := range materialAttestors {
158-
if err := ctx.runAttestor(attestor); err != nil {
159-
return err
160-
}
161-
}
162-
163-
for _, attestor := range exeucteAttestors {
164-
if err := ctx.runAttestor(attestor); err != nil {
165-
return err
166-
}
167-
}
168-
169-
for _, attestor := range productAttestors {
170-
if err := ctx.runAttestor(attestor); err != nil {
171-
return err
172-
}
173-
}
174-
175-
for _, attestor := range postAttestors {
176-
if err := ctx.runAttestor(attestor); err != nil {
177-
return err
130+
for _, atts := range attestors {
131+
for _, att := range atts {
132+
log.Infof("Starting %v attestor...", att.Name())
133+
if err := ctx.runAttestor(att); err != nil {
134+
log.Errorf("Error running %v attestor: %w", att.Name(), err)
135+
return err
136+
}
178137
}
179138
}
180139

181140
return nil
182141
}
183142

184143
func (ctx *AttestationContext) runAttestor(attestor Attestor) error {
185-
log.Infof("Starting %v attestor...", attestor.Name())
186144
startTime := time.Now()
145+
// NOTE: Not sure if this is the right place to check for an error running the attestor - might be better to let the caller handle it
187146
if err := attestor.Attest(ctx); err != nil {
188-
log.Errorf("Error running %v attestor: %w", attestor.Name(), err)
189147
ctx.completedAttestors = append(ctx.completedAttestors, CompletedAttestor{
190148
Attestor: attestor,
191149
StartTime: startTime,
@@ -205,48 +163,34 @@ func (ctx *AttestationContext) runAttestor(attestor Attestor) error {
205163
ctx.addMaterials(materialer)
206164
}
207165

208-
if producter, ok := attestor.(Producer); ok {
209-
ctx.addProducts(producter)
166+
if producer, ok := attestor.(Producer); ok {
167+
ctx.addProducts(producer)
210168
}
211169

212170
return nil
213171
}
214172

215173
func (ctx *AttestationContext) CompletedAttestors() []CompletedAttestor {
216-
attestors := make([]CompletedAttestor, len(ctx.completedAttestors))
217-
copy(attestors, ctx.completedAttestors)
218-
return attestors
174+
return ctx.completedAttestors
219175
}
220176

221177
func (ctx *AttestationContext) WorkingDir() string {
222178
return ctx.workingDir
223179
}
224180

225181
func (ctx *AttestationContext) Hashes() []crypto.Hash {
226-
hashes := make([]crypto.Hash, len(ctx.hashes))
227-
copy(hashes, ctx.hashes)
228-
return hashes
182+
return ctx.hashes
229183
}
230184

231185
func (ctx *AttestationContext) Context() context.Context {
232186
return ctx.ctx
233187
}
234188

235189
func (ctx *AttestationContext) Materials() map[string]cryptoutil.DigestSet {
236-
matCopy := make(map[string]cryptoutil.DigestSet)
237-
for k, v := range ctx.materials {
238-
matCopy[k] = v
239-
}
240-
241-
return matCopy
190+
return ctx.materials
242191
}
243192

244193
func (ctx *AttestationContext) Products() map[string]Product {
245-
prodCopy := make(map[string]Product)
246-
for k, v := range ctx.products {
247-
prodCopy[k] = v
248-
}
249-
250194
return ctx.products
251195
}
252196

attestation/factory.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type Subjecter interface {
4141
Subjects() map[string]cryptoutil.DigestSet
4242
}
4343

44+
// NOTE: not sure on the name of this interface, however I can't think of an alternative for now
45+
4446
// Materialer allows attestors to communicate about materials that were observed
4547
// while the attestor executed. For example the material attestor records the hashes
4648
// of all files before a command is run.

0 commit comments

Comments
 (0)