@@ -33,25 +33,67 @@ import (
3333 "olympos.io/encoding/edn"
3434)
3535
36+ type TransactionMaker = func () skill.Transaction
37+
3638// UploadSbom transact an image and its data into the data plane
3739func UploadSbom (sb * types.Sbom , workspace string , apikey string ) error {
38- host , name , err := parseReference (sb )
40+ correlationId := uuid .NewString ()
41+ context := skill.RequestContext {
42+ Event : skill.EventIncoming {
43+ ExecutionId : correlationId ,
44+ WorkspaceId : workspace ,
45+ Token : apikey ,
46+ },
47+ }
48+
49+ newTransaction := context .NewTransaction
50+ image , err := transactSbom (sb , newTransaction )
3951 if err != nil {
40- return errors .Wrapf (err , "failed to obtain host and repository " )
52+ return errors .Wrap (err , "failed to transact image " )
4153 }
42- config := (* sb ).Source .Image .Config
43- manifest := (* sb ).Source .Image .Manifest
4454
45- now := time .Now ()
55+ imageName := ""
56+ if image .Repository .Host != "hub.docker.com" {
57+ imageName = image .Repository .Host + "/"
58+ }
59+ imageName += image .Repository .Name
60+
61+ skill .Log .Infof ("Inspect image at https://dso.docker.com/%s/overview/images/%s/digests/%s" , workspace , imageName , image .Digest )
62+
63+ return nil
64+ }
65+
66+ func sendSbom (sb * types.Sbom , entities chan <- string ) error {
4667 correlationId := uuid .NewString ()
4768 context := skill.RequestContext {
4869 Event : skill.EventIncoming {
4970 ExecutionId : correlationId ,
50- WorkspaceId : workspace ,
51- Token : apikey ,
5271 },
5372 }
54- transaction := context .NewTransaction ().Ordered ()
73+
74+ newTransaction := func () skill.Transaction {
75+ return context .NewTransactionWithTransactor (func (entitiesString string ) {
76+ entities <- entitiesString
77+ })
78+ }
79+ _ , err := transactSbom (sb , newTransaction )
80+ if err != nil {
81+ return errors .Wrap (err , "failed to transact image" )
82+ }
83+
84+ return nil
85+ }
86+
87+ func transactSbom (sb * types.Sbom , newTransaction func () skill.Transaction ) (* ImageEntity , error ) {
88+ now := time .Now ()
89+ host , name , err := parseReference (sb )
90+ if err != nil {
91+ return nil , errors .Wrap (err , "failed to obtain host and repository" )
92+ }
93+ config := (* sb ).Source .Image .Config
94+ manifest := (* sb ).Source .Image .Manifest
95+
96+ transaction := newTransaction ().Ordered ()
5597 ports := parsePorts (config )
5698 env , envVars := parseEnvVars (config )
5799 sha := parseSha (config )
@@ -111,7 +153,7 @@ func UploadSbom(sb *types.Sbom, workspace string, apikey string) error {
111153 image .Sha = sha
112154 }
113155
114- if len (* sb .Source .Image .Tags ) > 0 {
156+ if sb . Source . Image . Tags != nil && len (* sb .Source .Image .Tags ) > 0 {
115157 image .Tags = & skill.ManyRef {Add : * sb .Source .Image .Tags }
116158
117159 for _ , t := range * sb .Source .Image .Tags {
@@ -136,13 +178,13 @@ func UploadSbom(sb *types.Sbom, workspace string, apikey string) error {
136178 // transact the image with all its metadata (repo, tags, layers, blobs, ports, env etc)
137179 err = transaction .AddEntities (image , platform ).Transact ()
138180 if err != nil {
139- return errors .Wrapf (err , "failed to transact image" )
181+ return nil , errors .Wrapf (err , "failed to transact image" )
140182 }
141183
142184 // transact all packages in chunks
143185 packageChunks := internal .ChunkSlice (sb .Artifacts , 20 )
144186 for _ , packages := range packageChunks {
145- transaction := context . NewTransaction ().Ordered ()
187+ transaction := newTransaction ().Ordered ()
146188
147189 image = ImageEntity {
148190 Digest : sb .Source .Image .Digest ,
@@ -186,28 +228,19 @@ func UploadSbom(sb *types.Sbom, workspace string, apikey string) error {
186228 image .Dependencies = & skill.ManyRef {Add : transaction .EntityRefs ("package/dependency" )}
187229 err := transaction .AddEntities (image ).Transact ()
188230 if err != nil {
189- return errors .Wrapf (err , "failed to transact packages" )
231+ return nil , errors .Wrapf (err , "failed to transact packages" )
190232 }
191233 }
192234
193235 image = ImageEntity {
194236 Digest : sb .Source .Image .Digest ,
195237 SbomState : Indexed ,
196238 }
197- err = context . NewTransaction ().Ordered ().AddEntities (image ).Transact ()
239+ err = newTransaction ().Ordered ().AddEntities (image ).Transact ()
198240 if err != nil {
199- return errors .Wrapf (err , "failed to transact packages" )
241+ return nil , errors .Wrapf (err , "failed to transact packages" )
200242 }
201-
202- imageName := ""
203- if host != "hub.docker.com" {
204- imageName = host + "/"
205- }
206- imageName += name
207-
208- skill .Log .Infof ("Inspect image at https://dso.docker.com/%s/overview/images/%s/digests/%s" , workspace , imageName , image .Digest )
209-
210- return nil
243+ return & image , nil
211244}
212245
213246func digestChainIds (manifest * v1.Manifest ) []digest.Digest {
0 commit comments