| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"maps" |
| 6 |
"slices" |
| 7 |
|
| 8 |
slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common" |
| 9 |
slsa02 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2" |
| 10 |
slsa1 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1" |
| 11 |
resourcestypes "github.com/moby/buildkit/executor/resources/types" |
| 12 |
"github.com/moby/buildkit/solver/pb" |
| 13 |
digest "github.com/opencontainers/go-digest" |
| 14 |
ocispecs "github.com/opencontainers/image-spec/specs-go/v1" |
| 15 |
"github.com/pkg/errors" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
BuildKitBuildType1 = "https://github.com/moby/buildkit/blob/master/docs/attestations/slsa-definitions.md" |
| 20 |
BuildKitBuildType02 = "https://mobyproject.org/buildkit@v1" |
| 21 |
|
| 22 |
ProvenanceSLSA1 = ProvenanceSLSA("v1") |
| 23 |
ProvenanceSLSA02 = ProvenanceSLSA("v0.2") |
| 24 |
) |
| 25 |
|
| 26 |
type ProvenanceSLSA string |
| 27 |
|
| 28 |
var provenanceSLSAs = []ProvenanceSLSA{ |
| 29 |
ProvenanceSLSA1, |
| 30 |
ProvenanceSLSA02, |
| 31 |
} |
| 32 |
|
| 33 |
type BuildConfig struct { |
| 34 |
Definition []BuildStep `json:"llbDefinition,omitempty"` |
| 35 |
DigestMapping map[digest.Digest]string `json:"digestMapping,omitempty"` |
| 36 |
} |
| 37 |
|
| 38 |
type BuildStep struct { |
| 39 |
ID string `json:"id,omitempty"` |
| 40 |
Op *pb.Op `json:"op,omitempty"` |
| 41 |
Inputs []string `json:"inputs,omitempty"` |
| 42 |
ResourceUsage *resourcestypes.Samples `json:"resourceUsage,omitempty"` |
| 43 |
} |
| 44 |
|
| 45 |
type Source struct { |
| 46 |
Locations map[string]*pb.Locations `json:"locations,omitempty"` |
| 47 |
Infos []SourceInfo `json:"infos,omitempty"` |
| 48 |
} |
| 49 |
|
| 50 |
type SourceInfo struct { |
| 51 |
Filename string `json:"filename,omitempty"` |
| 52 |
Language string `json:"language,omitempty"` |
| 53 |
Data []byte `json:"data,omitempty"` |
| 54 |
Definition []BuildStep `json:"llbDefinition,omitempty"` |
| 55 |
DigestMapping map[digest.Digest]string `json:"digestMapping,omitempty"` |
| 56 |
} |
| 57 |
|
| 58 |
type ImageSource struct { |
| 59 |
Ref string |
| 60 |
Platform *ocispecs.Platform |
| 61 |
Digest digest.Digest |
| 62 |
Local bool |
| 63 |
} |
| 64 |
|
| 65 |
type GitSource struct { |
| 66 |
URL string |
| 67 |
Commit string |
| 68 |
} |
| 69 |
|
| 70 |
type HTTPSource struct { |
| 71 |
URL string |
| 72 |
Digest digest.Digest |
| 73 |
} |
| 74 |
|
| 75 |
type LocalSource struct { |
| 76 |
Name string `json:"name"` |
| 77 |
} |
| 78 |
|
| 79 |
type Secret struct { |
| 80 |
ID string `json:"id"` |
| 81 |
Optional bool `json:"optional,omitempty"` |
| 82 |
} |
| 83 |
|
| 84 |
type SSH struct { |
| 85 |
ID string `json:"id"` |
| 86 |
Optional bool `json:"optional,omitempty"` |
| 87 |
} |
| 88 |
|
| 89 |
type Sources struct { |
| 90 |
Images []ImageSource |
| 91 |
Git []GitSource |
| 92 |
HTTP []HTTPSource |
| 93 |
Local []LocalSource |
| 94 |
} |
| 95 |
|
| 96 |
func (ps *ProvenanceSLSA) Validate() error { |
| 97 |
if *ps == "" { |
| 98 |
return errors.New("provenance SLSA version cannot be empty") |
| 99 |
} |
| 100 |
if slices.Contains(provenanceSLSAs, *ps) { |
| 101 |
return nil |
| 102 |
} |
| 103 |
return errors.New("invalid provenance SLSA version: " + string(*ps)) |
| 104 |
} |
| 105 |
|
| 106 |
type ProvenancePredicateSLSA02 struct { |
| 107 |
slsa02.ProvenancePredicate |
| 108 |
Invocation ProvenanceInvocationSLSA02 `json:"invocation"` |
| 109 |
BuildConfig *BuildConfig `json:"buildConfig,omitempty"` |
| 110 |
Metadata *ProvenanceMetadataSLSA02 `json:"metadata,omitempty"` |
| 111 |
} |
| 112 |
|
| 113 |
type ProvenanceInvocationSLSA02 struct { |
| 114 |
ConfigSource slsa02.ConfigSource `json:"configSource"` |
| 115 |
Parameters Parameters `json:"parameters"` |
| 116 |
Environment Environment `json:"environment"` |
| 117 |
} |
| 118 |
|
| 119 |
type ProvenanceMetadataSLSA02 struct { |
| 120 |
slsa02.ProvenanceMetadata |
| 121 |
BuildKitMetadata BuildKitMetadata `json:"https://mobyproject.org/buildkit@v1#metadata"` |
| 122 |
Hermetic bool `json:"https://mobyproject.org/buildkit@v1#hermetic,omitempty"` |
| 123 |
} |
| 124 |
|
| 125 |
type ProvenancePredicateSLSA1 struct { |
| 126 |
slsa1.ProvenancePredicate |
| 127 |
BuildDefinition ProvenanceBuildDefinitionSLSA1 `json:"buildDefinition"` |
| 128 |
RunDetails ProvenanceRunDetailsSLSA1 `json:"runDetails"` |
| 129 |
} |
| 130 |
|
| 131 |
type ProvenanceBuildDefinitionSLSA1 struct { |
| 132 |
slsa1.ProvenanceBuildDefinition |
| 133 |
ExternalParameters ProvenanceExternalParametersSLSA1 `json:"externalParameters"` |
| 134 |
InternalParameters ProvenanceInternalParametersSLSA1 `json:"internalParameters"` |
| 135 |
} |
| 136 |
|
| 137 |
type ProvenanceRunDetailsSLSA1 struct { |
| 138 |
slsa1.ProvenanceRunDetails |
| 139 |
Metadata *ProvenanceMetadataSLSA1 `json:"metadata,omitempty"` |
| 140 |
} |
| 141 |
|
| 142 |
type ProvenanceExternalParametersSLSA1 struct { |
| 143 |
ConfigSource ProvenanceConfigSourceSLSA1 `json:"configSource"` |
| 144 |
Request Parameters `json:"request"` |
| 145 |
} |
| 146 |
|
| 147 |
type ProvenanceConfigSourceSLSA1 struct { |
| 148 |
URI string `json:"uri,omitempty"` |
| 149 |
Digest slsa.DigestSet `json:"digest,omitempty"` |
| 150 |
Path string `json:"path,omitempty"` |
| 151 |
} |
| 152 |
|
| 153 |
type ProvenanceInternalParametersSLSA1 struct { |
| 154 |
BuildConfig *BuildConfig `json:"buildConfig,omitempty"` |
| 155 |
BuilderPlatform string `json:"builderPlatform"` |
| 156 |
ProvenanceCustomEnv |
| 157 |
} |
| 158 |
|
| 159 |
type ProvenanceMetadataSLSA1 struct { |
| 160 |
slsa1.BuildMetadata |
| 161 |
BuildKitMetadata BuildKitMetadata `json:"buildkit_metadata"` |
| 162 |
Hermetic bool `json:"buildkit_hermetic,omitempty"` |
| 163 |
Completeness BuildKitComplete `json:"buildkit_completeness"` |
| 164 |
Reproducible bool `json:"buildkit_reproducible,omitempty"` |
| 165 |
} |
| 166 |
|
| 167 |
type Parameters struct { |
| 168 |
Frontend string `json:"frontend,omitempty"` |
| 169 |
Args map[string]string `json:"args,omitempty"` |
| 170 |
Secrets []*Secret `json:"secrets,omitempty"` |
| 171 |
SSH []*SSH `json:"ssh,omitempty"` |
| 172 |
Locals []*LocalSource `json:"locals,omitempty"` |
| 173 |
// TODO: select export attributes |
| 174 |
// TODO: frontend inputs |
| 175 |
} |
| 176 |
|
| 177 |
type Environment struct { |
| 178 |
Platform string `json:"platform"` |
| 179 |
ProvenanceCustomEnv |
| 180 |
} |
| 181 |
|
| 182 |
type BuildKitMetadata struct { |
| 183 |
VCS map[string]string `json:"vcs,omitempty"` |
| 184 |
Source *Source `json:"source,omitempty"` |
| 185 |
Layers map[string][][]ocispecs.Descriptor `json:"layers,omitempty"` |
| 186 |
SysUsage []*resourcestypes.SysSample `json:"sysUsage,omitempty"` |
| 187 |
} |
| 188 |
|
| 189 |
type BuildKitComplete struct { |
| 190 |
Request bool `json:"request"` |
| 191 |
ResolvedDependencies bool `json:"resolvedDependencies"` |
| 192 |
} |
| 193 |
|
| 194 |
type ProvenanceCustomEnv map[string]any |
| 195 |
|
| 196 |
// ConvertToSLSA02 converts to a SLSA v0.2 provenance predicate. |
| 197 |
func (p *ProvenancePredicateSLSA1) ConvertToSLSA02() *ProvenancePredicateSLSA02 { |
| 198 |
var materials []slsa02.ProvenanceMaterial |
| 199 |
for _, m := range p.BuildDefinition.ResolvedDependencies { |
| 200 |
materials = append(materials, slsa02.ProvenanceMaterial{ |
| 201 |
URI: m.URI, |
| 202 |
Digest: m.Digest, |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
var meta *ProvenanceMetadataSLSA02 |
| 207 |
if p.RunDetails.Metadata != nil { |
| 208 |
meta = &ProvenanceMetadataSLSA02{ |
| 209 |
ProvenanceMetadata: slsa02.ProvenanceMetadata{ |
| 210 |
BuildInvocationID: p.RunDetails.Metadata.InvocationID, |
| 211 |
BuildStartedOn: p.RunDetails.Metadata.StartedOn, |
| 212 |
BuildFinishedOn: p.RunDetails.Metadata.FinishedOn, |
| 213 |
Completeness: slsa02.ProvenanceComplete{ |
| 214 |
Parameters: p.RunDetails.Metadata.Completeness.Request, |
| 215 |
Environment: true, |
| 216 |
Materials: p.RunDetails.Metadata.Completeness.ResolvedDependencies, |
| 217 |
}, |
| 218 |
Reproducible: p.RunDetails.Metadata.Reproducible, |
| 219 |
}, |
| 220 |
BuildKitMetadata: p.RunDetails.Metadata.BuildKitMetadata, |
| 221 |
Hermetic: p.RunDetails.Metadata.Hermetic, |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return &ProvenancePredicateSLSA02{ |
| 226 |
ProvenancePredicate: slsa02.ProvenancePredicate{ |
| 227 |
Builder: slsa02.ProvenanceBuilder{ |
| 228 |
ID: p.RunDetails.Builder.ID, |
| 229 |
}, |
| 230 |
BuildType: BuildKitBuildType02, |
| 231 |
Materials: materials, |
| 232 |
}, |
| 233 |
Invocation: ProvenanceInvocationSLSA02{ |
| 234 |
ConfigSource: slsa02.ConfigSource{ |
| 235 |
URI: p.BuildDefinition.ExternalParameters.ConfigSource.URI, |
| 236 |
Digest: p.BuildDefinition.ExternalParameters.ConfigSource.Digest, |
| 237 |
EntryPoint: p.BuildDefinition.ExternalParameters.ConfigSource.Path, |
| 238 |
}, |
| 239 |
Parameters: p.BuildDefinition.ExternalParameters.Request, |
| 240 |
Environment: Environment{ |
| 241 |
Platform: p.BuildDefinition.InternalParameters.BuilderPlatform, |
| 242 |
ProvenanceCustomEnv: p.BuildDefinition.InternalParameters.ProvenanceCustomEnv, |
| 243 |
}, |
| 244 |
}, |
| 245 |
BuildConfig: p.BuildDefinition.InternalParameters.BuildConfig, |
| 246 |
Metadata: meta, |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// ConvertToSLSA1 converts to a SLSA v1 provenance predicate. |
| 251 |
func (p *ProvenancePredicateSLSA02) ConvertToSLSA1() *ProvenancePredicateSLSA1 { |
| 252 |
var resolvedDeps []slsa1.ResourceDescriptor |
| 253 |
for _, m := range p.Materials { |
| 254 |
resolvedDeps = append(resolvedDeps, slsa1.ResourceDescriptor{ |
| 255 |
URI: m.URI, |
| 256 |
Digest: m.Digest, |
| 257 |
}) |
| 258 |
} |
| 259 |
|
| 260 |
buildDef := ProvenanceBuildDefinitionSLSA1{ |
| 261 |
ProvenanceBuildDefinition: slsa1.ProvenanceBuildDefinition{ |
| 262 |
BuildType: BuildKitBuildType1, |
| 263 |
ResolvedDependencies: resolvedDeps, |
| 264 |
}, |
| 265 |
ExternalParameters: ProvenanceExternalParametersSLSA1{ |
| 266 |
ConfigSource: ProvenanceConfigSourceSLSA1{ |
| 267 |
URI: p.Invocation.ConfigSource.URI, |
| 268 |
Digest: p.Invocation.ConfigSource.Digest, |
| 269 |
Path: p.Invocation.ConfigSource.EntryPoint, |
| 270 |
}, |
| 271 |
Request: p.Invocation.Parameters, |
| 272 |
}, |
| 273 |
InternalParameters: ProvenanceInternalParametersSLSA1{ |
| 274 |
BuildConfig: p.BuildConfig, |
| 275 |
BuilderPlatform: p.Invocation.Environment.Platform, |
| 276 |
ProvenanceCustomEnv: p.Invocation.Environment.ProvenanceCustomEnv, |
| 277 |
}, |
| 278 |
} |
| 279 |
|
| 280 |
var meta *ProvenanceMetadataSLSA1 |
| 281 |
if p.Metadata != nil { |
| 282 |
meta = &ProvenanceMetadataSLSA1{ |
| 283 |
BuildMetadata: slsa1.BuildMetadata{ |
| 284 |
InvocationID: p.Metadata.BuildInvocationID, |
| 285 |
StartedOn: p.Metadata.BuildStartedOn, |
| 286 |
FinishedOn: p.Metadata.BuildFinishedOn, |
| 287 |
}, |
| 288 |
BuildKitMetadata: p.Metadata.BuildKitMetadata, |
| 289 |
Hermetic: p.Metadata.Hermetic, |
| 290 |
Completeness: BuildKitComplete{ |
| 291 |
Request: p.Metadata.Completeness.Parameters, |
| 292 |
ResolvedDependencies: p.Metadata.Completeness.Materials, |
| 293 |
}, |
| 294 |
Reproducible: p.Metadata.Reproducible, |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
runDetails := ProvenanceRunDetailsSLSA1{ |
| 299 |
ProvenanceRunDetails: slsa1.ProvenanceRunDetails{ |
| 300 |
Builder: slsa1.Builder{ |
| 301 |
ID: p.Builder.ID, |
| 302 |
// TODO: handle builder components versions |
| 303 |
// Version: map[string]string{ |
| 304 |
// "buildkit": version.Version, |
| 305 |
// }, |
| 306 |
}, |
| 307 |
}, |
| 308 |
Metadata: meta, |
| 309 |
} |
| 310 |
|
| 311 |
return &ProvenancePredicateSLSA1{ |
| 312 |
BuildDefinition: buildDef, |
| 313 |
RunDetails: runDetails, |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
// MarshalJSON flattens ProvenanceCustomEnv into top level. |
| 318 |
func (p ProvenanceInternalParametersSLSA1) MarshalJSON() ([]byte, error) { |
| 319 |
type Alias ProvenanceInternalParametersSLSA1 |
| 320 |
base, err := json.Marshal(Alias(p)) |
| 321 |
if err != nil { |
| 322 |
return nil, err |
| 323 |
} |
| 324 |
var m map[string]any |
| 325 |
if err := json.Unmarshal(base, &m); err != nil { |
| 326 |
return nil, err |
| 327 |
} |
| 328 |
maps.Copy(m, p.ProvenanceCustomEnv) |
| 329 |
delete(m, "ProvenanceCustomEnv") |
| 330 |
return json.Marshal(m) |
| 331 |
} |
| 332 |
|
| 333 |
// UnmarshalJSON fills both struct fields and flattened custom env. |
| 334 |
func (p *ProvenanceInternalParametersSLSA1) UnmarshalJSON(data []byte) error { |
| 335 |
var m map[string]any |
| 336 |
if err := json.Unmarshal(data, &m); err != nil { |
| 337 |
return err |
| 338 |
} |
| 339 |
|
| 340 |
type Alias ProvenanceInternalParametersSLSA1 |
| 341 |
var a Alias |
| 342 |
if err := json.Unmarshal(data, &a); err != nil { |
| 343 |
return err |
| 344 |
} |
| 345 |
|
| 346 |
// Unmarshal known struct again to identify its keys |
| 347 |
structBytes, err := json.Marshal(a) |
| 348 |
if err != nil { |
| 349 |
return err |
| 350 |
} |
| 351 |
var known map[string]any |
| 352 |
if err := json.Unmarshal(structBytes, &known); err != nil { |
| 353 |
return err |
| 354 |
} |
| 355 |
|
| 356 |
for k := range known { |
| 357 |
delete(m, k) |
| 358 |
} |
| 359 |
|
| 360 |
*p = ProvenanceInternalParametersSLSA1(a) |
| 361 |
p.ProvenanceCustomEnv = m |
| 362 |
return nil |
| 363 |
} |
| 364 |
|
| 365 |
func (p Environment) MarshalJSON() ([]byte, error) { |
| 366 |
type Alias Environment |
| 367 |
base, err := json.Marshal(Alias(p)) |
| 368 |
if err != nil { |
| 369 |
return nil, err |
| 370 |
} |
| 371 |
var m map[string]any |
| 372 |
if err := json.Unmarshal(base, &m); err != nil { |
| 373 |
return nil, err |
| 374 |
} |
| 375 |
maps.Copy(m, p.ProvenanceCustomEnv) |
| 376 |
delete(m, "ProvenanceCustomEnv") |
| 377 |
return json.Marshal(m) |
| 378 |
} |
| 379 |
|
| 380 |
func (p *Environment) UnmarshalJSON(data []byte) error { |
| 381 |
var m map[string]any |
| 382 |
if err := json.Unmarshal(data, &m); err != nil { |
| 383 |
return err |
| 384 |
} |
| 385 |
|
| 386 |
type Alias Environment |
| 387 |
var a Alias |
| 388 |
if err := json.Unmarshal(data, &a); err != nil { |
| 389 |
return err |
| 390 |
} |
| 391 |
// Unmarshal known struct again to identify its keys |
| 392 |
structBytes, err := json.Marshal(a) |
| 393 |
if err != nil { |
| 394 |
return err |
| 395 |
} |
| 396 |
var known map[string]any |
| 397 |
if err := json.Unmarshal(structBytes, &known); err != nil { |
| 398 |
return err |
| 399 |
} |
| 400 |
|
| 401 |
for k := range known { |
| 402 |
delete(m, k) |
| 403 |
} |
| 404 |
*p = Environment(a) |
| 405 |
p.ProvenanceCustomEnv = m |
| 406 |
return nil |
| 407 |
} |