@@ -96,7 +96,7 @@ func (w *Watcher) watchNewLogs() {
9696 }
9797}
9898
99- // updateLogs checks the transparency log list for new logs and adds new 0workers for those to the watcher.
99+ // updateLogs checks the transparency log list for new logs and adds new workers for those to the watcher.
100100func (w * Watcher ) updateLogs () {
101101 // Get a list of urls of all CT logs
102102 logList , err := getAllLogs ()
@@ -105,78 +105,96 @@ func (w *Watcher) updateLogs() {
105105 return
106106 }
107107
108- w .addNewlyAvailableLogs (logList )
109-
110- if * config .AppConfig .General .DropOldLogs {
111- w .dropRemovedLogs (logList )
112- }
113- }
114-
115- // addNewlyAvailableLogs checks the transparency log list for new Log servers and adds workers for those to the watcher.
116- func (w * Watcher ) addNewlyAvailableLogs (logList loglist3.LogList ) {
117108 log .Println ("Checking for new ct logs..." )
118109
110+ // Track all URLs that should be monitored after reconciliation
111+ monitoredUrls := make (map [string ]struct {})
112+ newCTs := 0
113+
119114 w .workersMu .Lock ()
120115 defer w .workersMu .Unlock ()
121- newCTs := 0
122116
123- // Check the ct log list for new, unwatched logs
124- // For each CT log, create a worker and start downloading certs
125117 for _ , operator := range logList .Operators {
126118 // Iterate over each log of the operator
127119 for _ , transparencyLog := range operator .Logs {
128- newURL := normalizeCtlogURL (transparencyLog .URL )
120+ url := transparencyLog .URL
121+ desc := transparencyLog .Description
122+ normUrl := normalizeCtlogURL (url )
129123
130124 if transparencyLog .State .LogStatus () == loglist3 .RetiredLogStatus {
131- log .Printf ("Skipping retired CT log: %s\n " , newURL )
125+ log .Printf ("Skipping retired CT log: %s\n " , normUrl )
132126 continue
133127 }
134128
135- // Check if the log is already being watched
136- alreadyWatched := false
137-
138- for _ , ctWorker := range w .workers {
139- workerURL := normalizeCtlogURL (ctWorker .ctURL )
140- if workerURL == newURL {
141- alreadyWatched = true
142- break
143- }
129+ monitoredUrls [normUrl ] = struct {}{}
130+ if w .addLogIfNew (operator .Name , desc , url ) {
131+ newCTs ++
144132 }
133+ }
134+ }
145135
146- // If the log is already being watched, continue
147- if alreadyWatched {
148- continue
149- }
136+ log .Printf ("New ct logs found: %d\n " , newCTs )
150137
151- w .wg .Add (1 )
152- newCTs ++
153-
154- // Metrics are initialized with 0.
155- // Only if recovery is enabled, it is initialized with the last saved index.
156- lastCTIndex := metrics .GetCTIndex (normalizeCtlogURL (transparencyLog .URL ))
157- ctWorker := worker {
158- name : transparencyLog .Description ,
159- operatorName : operator .Name ,
160- ctURL : transparencyLog .URL ,
161- entryChan : w .certChan ,
162- ctIndex : lastCTIndex ,
163- }
164- w .workers = append (w .workers , & ctWorker )
165- metrics .Init (operator .Name , normalizeCtlogURL (transparencyLog .URL ))
138+ // Also ensure additional logs are kept and started if missing
139+ for _ , additional := range config .AppConfig .General .AdditionalLogs {
140+ norm := normalizeCtlogURL (additional .URL )
141+ monitoredUrls [norm ] = struct {}{}
142+ _ = w .addLogIfNew (additional .Operator , additional .Description , additional .URL )
143+ }
166144
167- // Start a goroutine for each worker
168- go func () {
169- defer w .wg .Done ()
170- ctWorker .startDownloadingCerts (w .context )
171- w .discardWorker (& ctWorker )
172- }()
145+ // Optionally stop workers for logs not in the monitoredUrls set
146+ if * config .AppConfig .General .DropOldLogs {
147+ removed := 0
148+ for _ , ctWorker := range w .workers {
149+ if _ , ok := monitoredUrls [normalizeCtlogURL (ctWorker .ctURL )]; ! ok {
150+ log .Printf ("Stopping worker. CT URL not found in LogList: '%s'\n " , ctWorker .ctURL )
151+ ctWorker .stop ()
152+ removed ++
153+ }
173154 }
155+ log .Printf ("Removed ct logs: %d\n " , removed )
174156 }
175157
176- log .Printf ("New ct logs found: %d\n " , newCTs )
177158 log .Printf ("Currently monitored ct logs: %d\n " , len (w .workers ))
178159}
179160
161+ // addLogIfNew checks if a log is already being watched and adds it if not.
162+ // Returns true if a new log was added, false otherwise.
163+ func (w * Watcher ) addLogIfNew (operatorName , description , url string ) bool {
164+ normURL := normalizeCtlogURL (url )
165+
166+ // Check if the log is already being watched
167+ for _ , ctWorker := range w .workers {
168+ workerURL := normalizeCtlogURL (ctWorker .ctURL )
169+ if workerURL == normURL {
170+ return false
171+ }
172+ }
173+
174+ // Log is not being watched, so add it
175+ w .wg .Add (1 )
176+
177+ lastCTIndex := metrics .GetCTIndex (url )
178+ ctWorker := worker {
179+ name : description ,
180+ operatorName : operatorName ,
181+ ctURL : url ,
182+ entryChan : w .certChan ,
183+ ctIndex : lastCTIndex ,
184+ }
185+ w .workers = append (w .workers , & ctWorker )
186+ metrics .Init (operatorName , url )
187+
188+ // Start a goroutine for each worker
189+ go func () {
190+ defer w .wg .Done ()
191+ ctWorker .startDownloadingCerts (w .context )
192+ w .discardWorker (& ctWorker )
193+ }()
194+
195+ return true
196+ }
197+
180198// discardWorker removes a worker from the watcher's list of workers.
181199// This needs to be done when a worker stops.
182200func (w * Watcher ) discardWorker (worker * worker ) {
@@ -193,67 +211,6 @@ func (w *Watcher) discardWorker(worker *worker) {
193211 }
194212}
195213
196- // dropRemovedLogs checks if any of the currently monitored logs are no longer in the log list or are retired.
197- // If they are not, the CT Logs are probably no longer relevant and the corresponding workers will be stopped.
198- func (w * Watcher ) dropRemovedLogs (logList loglist3.LogList ) {
199- removedCTs := 0
200-
201- // Iterate over all workers and check if they are still in the logList
202- // If they are not, the CT Logs are probably no longer relevant.
203- // We should stop the worker if that didn't already happen.
204- for _ , ctWorker := range w .workers {
205- workerURL := normalizeCtlogURL (ctWorker .ctURL )
206-
207- onLogList := false
208- for _ , operator := range logList .Operators {
209- if ctWorker .operatorName != operator .Name {
210- // This operator is not the one we're looking for
211- continue
212- }
213-
214- // Iterate over each log of the operator
215- for _ , transparencyLog := range operator .Logs {
216- // Remove retired logs from the list
217- if transparencyLog .State .LogStatus () == loglist3 .RetiredLogStatus {
218- // Skip retired logs
219- continue
220- }
221-
222- // Check if the log is already being watched
223- logListURL := normalizeCtlogURL (transparencyLog .URL )
224- if workerURL == logListURL {
225- onLogList = true
226- break
227- }
228- }
229-
230- // Prevent further loop iterations
231- if onLogList {
232- break
233- }
234- }
235-
236- // Make sure to not drop logs that are defined locally in the additional logs list
237- for _ , additionalLogConfig := range config .AppConfig .General .AdditionalLogs {
238- additionalLogListURL := normalizeCtlogURL (additionalLogConfig .URL )
239- if workerURL == additionalLogListURL {
240- onLogList = true
241- break
242- }
243- }
244-
245- // If the log is not in the loglist, stop the worker
246- if ! onLogList {
247- log .Printf ("Stopping worker. CT URL not found in LogList or retired: '%s'\n " , ctWorker .ctURL )
248- removedCTs ++
249- ctWorker .stop ()
250- }
251- }
252-
253- log .Printf ("Removed ct logs: %d\n " , removedCTs )
254- log .Printf ("Currently monitored ct logs: %d\n " , len (w .workers ))
255- }
256-
257214// Stop stops the watcher.
258215func (w * Watcher ) Stop () {
259216 log .Printf ("Stopping watcher\n " )
0 commit comments