@@ -16,23 +16,13 @@ import (
1616)
1717
1818type bucketSourceRecord struct {
19- bucket clients.CloudStorage
20- objectPath string
21- keyPath string
22- hasUpdateTime bool
23- lastUpdated time.Time
24- format RecordFormat
25- sourceRepository string
26- strict bool
27- isDeleted bool
19+ bucket clients.CloudStorage
20+ objectPath string
2821}
2922
3023var _ SourceRecord = bucketSourceRecord {}
3124
3225func (b bucketSourceRecord ) Open (ctx context.Context ) (io.ReadCloser , error ) {
33- if b .isDeleted {
34- return nil , errors .New ("cannot open a deleted record" )
35- }
3626 data , err := b .bucket .ReadObject (ctx , b .objectPath )
3727 if err != nil {
3828 return nil , err
@@ -41,43 +31,11 @@ func (b bucketSourceRecord) Open(ctx context.Context) (io.ReadCloser, error) {
4131 return io .NopCloser (bytes .NewReader (data )), nil
4232}
4333
44- func (b bucketSourceRecord ) KeyPath () string {
45- return b .keyPath
46- }
47-
48- func (b bucketSourceRecord ) Format () RecordFormat {
49- return b .format
50- }
51-
52- func (b bucketSourceRecord ) LastUpdated () (time.Time , bool ) {
53- return b .lastUpdated , b .hasUpdateTime
54- }
55-
56- func (b bucketSourceRecord ) SourceRepository () string {
57- return b .sourceRepository
58- }
59-
60- func (b bucketSourceRecord ) SourcePath () string {
61- return b .objectPath
62- }
63-
64- func (b bucketSourceRecord ) ShouldSendModifiedTime () bool {
65- return b .hasUpdateTime
66- }
67-
68- func (b bucketSourceRecord ) IsDeleted () bool {
69- return b .isDeleted
70- }
71-
72- func (b bucketSourceRecord ) Strictness () bool {
73- return b .strict
74- }
75-
76- func handleImportBucket (ctx context.Context , ch chan <- SourceRecord , config Config , sourceRepo * models.SourceRepository ) error {
34+ func handleImportBucket (ctx context.Context , ch chan <- WorkItem , config Config , sourceRepo * models.SourceRepository ) error {
7735 if sourceRepo .Type != models .SourceRepositoryTypeBucket || sourceRepo .Bucket == nil {
7836 return errors .New ("invalid SourceRepository for bucket import" )
7937 }
80- logger .Info ( "Importing bucket source repository" ,
38+ logger .InfoContext ( ctx , "Importing bucket source repository" ,
8139 slog .String ("source" , sourceRepo .Name ), slog .String ("bucket" , sourceRepo .Bucket .Name ))
8240
8341 compiledIgnorePatterns := compileIgnorePatterns (sourceRepo )
@@ -100,7 +58,7 @@ func handleImportBucket(ctx context.Context, ch chan<- SourceRecord, config Conf
10058 return err
10159 }
10260 if hasUpdateTime {
103- if obj .Attrs .CustomTime .Before (lastUpdated ) {
61+ if obj .Attrs .Updated .Before (lastUpdated ) {
10462 continue
10563 }
10664 }
@@ -111,37 +69,42 @@ func handleImportBucket(ctx context.Context, ch chan<- SourceRecord, config Conf
11169 if shouldIgnore (base , sourceRepo .IDPrefixes , compiledIgnorePatterns ) {
11270 continue
11371 }
114- ch <- bucketSourceRecord {
115- bucket : bucket ,
116- objectPath : obj .Name ,
117- lastUpdated : lastUpdated ,
118- hasUpdateTime : hasUpdateTime ,
119- format : format ,
120- keyPath : sourceRepo .KeyPath ,
121- sourceRepository : sourceRepo .Name ,
122- strict : sourceRepo .Strictness ,
72+ ch <- WorkItem {
73+ Context : ctx ,
74+ SourceRecord : bucketSourceRecord {
75+ bucket : bucket ,
76+ objectPath : obj .Name ,
77+ },
78+ SourceRepository : sourceRepo .Name ,
79+ SourcePath : obj .Name ,
80+ LastUpdated : lastUpdated ,
81+ HasLastUpdated : hasUpdateTime ,
82+ Format : format ,
83+ KeyPath : sourceRepo .KeyPath ,
84+ Strict : sourceRepo .Strictness ,
85+ ShouldSendModifiedTime : hasUpdateTime ,
12386 }
12487 }
12588
12689 sourceRepo .Bucket .LastUpdated = & timeOfRun
12790 sourceRepo .Bucket .IgnoreLastImportTime = false
12891 if err := config .SourceRepoStore .Update (ctx , sourceRepo .Name , sourceRepo ); err != nil {
129- logger .Error ( "Failed to update source repository" , slog .Any ("error" , err ), slog .String ("source" , sourceRepo .Name ))
92+ logger .ErrorContext ( ctx , "Failed to update source repository" , slog .Any ("error" , err ), slog .String ("source" , sourceRepo .Name ))
13093 return err
13194 }
132- logger .Info ( "Finished importing bucket source repository" ,
95+ logger .InfoContext ( ctx , "Finished importing bucket source repository" ,
13396 slog .String ("source" , sourceRepo .Name ),
13497 slog .String ("bucket" , sourceRepo .Bucket .Name ))
13598
13699 return nil
137100}
138101
139- func handleDeleteBucket (ctx context.Context , ch chan <- SourceRecord , config Config , sourceRepo * models.SourceRepository ) error {
102+ func handleDeleteBucket (ctx context.Context , ch chan <- WorkItem , config Config , sourceRepo * models.SourceRepository ) error {
140103 if sourceRepo .Type != models .SourceRepositoryTypeBucket || sourceRepo .Bucket == nil {
141104 return errors .New ("invalid SourceRepository for bucket deletion" )
142105 }
143106
144- logger .Info ( "Processing bucket deletions" ,
107+ logger .InfoContext ( ctx , "Processing bucket deletions" ,
145108 slog .String ("source" , sourceRepo .Name ), slog .String ("bucket" , sourceRepo .Bucket .Name ))
146109
147110 // Get all objects in the bucket
@@ -167,7 +130,7 @@ func handleDeleteBucket(ctx context.Context, ch chan<- SourceRecord, config Conf
167130 }
168131
169132 if len (vulnsInDatastore ) == 0 {
170- logger .Info ( "No vulnerabilities found in Datastore for source" , slog .String ("source" , sourceRepo .Name ))
133+ logger .InfoContext ( ctx , "No vulnerabilities found in Datastore for source" , slog .String ("source" , sourceRepo .Name ))
171134 return nil
172135 }
173136
@@ -180,7 +143,7 @@ func handleDeleteBucket(ctx context.Context, ch chan<- SourceRecord, config Conf
180143 }
181144
182145 if len (toDelete ) == 0 {
183- logger .Info ( "No vulnerabilities to delete" , slog .String ("source" , sourceRepo .Name ))
146+ logger .InfoContext ( ctx , "No vulnerabilities to delete" , slog .String ("source" , sourceRepo .Name ))
184147 return nil
185148 }
186149
@@ -191,7 +154,7 @@ func handleDeleteBucket(ctx context.Context, ch chan<- SourceRecord, config Conf
191154 }
192155 percentage := (float64 (len (toDelete )) / float64 (len (vulnsInDatastore ))) * 100.0
193156 if percentage >= threshold {
194- logger .Error ( "Cowardly refusing to delete missing records (threshold exceeded)" ,
157+ logger .ErrorContext ( ctx , "Cowardly refusing to delete missing records (threshold exceeded)" ,
195158 slog .String ("source" , sourceRepo .Name ),
196159 slog .Int ("to_delete" , len (toDelete )),
197160 slog .Int ("total" , len (vulnsInDatastore )),
@@ -203,10 +166,15 @@ func handleDeleteBucket(ctx context.Context, ch chan<- SourceRecord, config Conf
203166
204167 // Trigger deletions
205168 for _ , entry := range toDelete {
206- ch <- bucketSourceRecord {
207- sourceRepository : entry .Source ,
208- objectPath : entry .Path ,
209- isDeleted : true ,
169+ ch <- WorkItem {
170+ Context : ctx ,
171+ SourceRecord : bucketSourceRecord {
172+ bucket : bucket ,
173+ objectPath : entry .Path ,
174+ },
175+ SourceRepository : entry .Source ,
176+ SourcePath : entry .Path ,
177+ IsDeleted : true ,
210178 }
211179 }
212180
0 commit comments