@@ -3,7 +3,6 @@ package attest
3
3
import (
4
4
"bufio"
5
5
"context"
6
- "encoding/json"
7
6
"errors"
8
7
"fmt"
9
8
"log"
@@ -13,7 +12,9 @@ import (
13
12
14
13
spb "github.com/in-toto/attestation/go/v1"
15
14
"google.golang.org/protobuf/encoding/protojson"
15
+ "google.golang.org/protobuf/proto"
16
16
"google.golang.org/protobuf/types/known/structpb"
17
+ "google.golang.org/protobuf/types/known/timestamppb"
17
18
18
19
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/ghcontrol"
19
20
"github.com/slsa-framework/slsa-source-poc/sourcetool/pkg/provenance"
@@ -46,7 +47,7 @@ func GetSourceProvPred(statement *spb.Statement) (*provenance.SourceProvenancePr
46
47
47
48
var predStruct provenance.SourceProvenancePred
48
49
// Using regular json.Unmarshal because this is just a regular struct.
49
- err = json .Unmarshal (predJson , & predStruct )
50
+ err = protojson .Unmarshal (predJson , & predStruct )
50
51
if err != nil {
51
52
return nil , fmt .Errorf ("unmarshaling predicate: %w" , err )
52
53
}
@@ -73,7 +74,7 @@ func GetTagProvPred(statement *spb.Statement) (*provenance.TagProvenancePred, er
73
74
74
75
var predStruct provenance.TagProvenancePred
75
76
// Using regular json.Unmarshal because this is just a regular struct.
76
- err = json .Unmarshal (predJson , & predStruct )
77
+ err = protojson .Unmarshal (predJson , & predStruct )
77
78
if err != nil {
78
79
return nil , fmt .Errorf ("unmarshaling predicate: %w" , err )
79
80
}
@@ -84,10 +85,16 @@ func GetTagProvPred(statement *spb.Statement) (*provenance.TagProvenancePred, er
84
85
}
85
86
86
87
func addPredToStatement (provPred any , predicateType , commit string ) (* spb.Statement , error ) {
87
- // Using regular json.Marshal because this is just a regular struct and not from a proto.
88
- predJson , err := json .Marshal (provPred )
88
+ msg , ok := provPred .(proto.Message )
89
+ if ! ok {
90
+ return nil , fmt .Errorf ("unable to serialize predicate as proto message" )
91
+ }
92
+ predJson , err := protojson.MarshalOptions {
93
+ Multiline : true ,
94
+ Indent : " " ,
95
+ }.Marshal (msg )
89
96
if err != nil {
90
- return nil , err
97
+ return nil , fmt . Errorf ( "marshaling predicate proto: %w" , err )
91
98
}
92
99
93
100
sub := []* spb.ResourceDescriptor {{
@@ -125,11 +132,11 @@ func (pa ProvenanceAttestor) createCurrentProvenance(ctx context.Context, commit
125
132
curProvPred .Actor = controlStatus .ActorLogin
126
133
curProvPred .ActivityType = controlStatus .ActivityType
127
134
curProvPred .Branch = ref
128
- curProvPred .CreatedOn = curTime
135
+ curProvPred .CreatedOn = timestamppb . New ( curTime )
129
136
curProvPred .Controls = controlStatus .Controls
130
137
131
138
// At the very least provenance is available starting now. :)
132
- curProvPred .Controls . AddControl (& slsa .Control {Name : slsa .ProvenanceAvailable , Since : curTime })
139
+ curProvPred .AddControl (& provenance .Control {Name : slsa .ProvenanceAvailable . String () , Since : timestamppb . New ( curTime ) })
133
140
134
141
return addPredToStatement (& curProvPred , provenance .SourceProvPredicateType , commit )
135
142
}
@@ -170,7 +177,7 @@ func (pa ProvenanceAttestor) getProvFromReader(reader *BundleReader, commit, ref
170
177
if err != nil {
171
178
return nil , nil , err
172
179
}
173
- if pa .gh_connection .GetRepoUri () == provPred .RepoUri && (ref == ghcontrol .AnyReference || provPred .Branch == ref ) {
180
+ if pa .gh_connection .GetRepoUri () == provPred .GetRepoUri () && (ref == ghcontrol .AnyReference || provPred .GetBranch () == ref ) {
174
181
// Should be good!
175
182
return stmt , provPred , nil
176
183
} else {
@@ -223,13 +230,13 @@ func (pa ProvenanceAttestor) CreateSourceProvenance(ctx context.Context, prevAtt
223
230
224
231
// There was prior provenance, so update the Since field for each property
225
232
// to the oldest encountered.
226
- for i , curControl := range curProvPred .Controls {
227
- prevControl := prevProvPred .Controls . GetControl (curControl .Name )
233
+ for i , curControl := range curProvPred .GetControls () {
234
+ prevControl := prevProvPred .GetControl (curControl .GetName () )
228
235
// No prior version of this control
229
236
if prevControl == nil {
230
237
continue
231
238
}
232
- curControl .Since = slsa .EarlierTime (curControl .Since , prevControl .Since )
239
+ curControl .Since = timestamppb . New ( slsa .EarlierTime (curControl .GetSince (). AsTime () , prevControl .GetSince (). AsTime ()) )
233
240
// Update the value.
234
241
curProvPred .Controls [i ] = curControl
235
242
}
@@ -259,8 +266,6 @@ func (pa ProvenanceAttestor) CreateTagProvenance(ctx context.Context, commit, re
259
266
return nil , nil
260
267
}
261
268
262
- curTime := time .Now ()
263
-
264
269
vsaRefs , err := GetSourceRefsForCommit (vsaStatement , commit )
265
270
if err != nil {
266
271
return nil , fmt .Errorf ("error getting source refs from vsa %w" , err )
@@ -270,12 +275,12 @@ func (pa ProvenanceAttestor) CreateTagProvenance(ctx context.Context, commit, re
270
275
RepoUri : pa .gh_connection .GetRepoUri (),
271
276
Actor : actor ,
272
277
Tag : ref ,
273
- CreatedOn : curTime ,
278
+ CreatedOn : timestamppb . Now () ,
274
279
Controls : controlStatus .Controls ,
275
- VsaSummaries : []provenance.VsaSummary {
280
+ VsaSummaries : []* provenance.VsaSummary {
276
281
{
277
282
SourceRefs : vsaRefs ,
278
- VerifiedLevels : slsa . StringsToControlNames ( vsaPred .GetVerifiedLevels () ),
283
+ VerifiedLevels : vsaPred .GetVerifiedLevels (),
279
284
},
280
285
},
281
286
}
0 commit comments