Skip to content

Commit b1ee4a9

Browse files
committed
refactor: consolidate CT log reconciliation into single updateLogs method
1 parent 7352a2e commit b1ee4a9

File tree

1 file changed

+69
-111
lines changed

1 file changed

+69
-111
lines changed

internal/certificatetransparency/ct-watcher.go

Lines changed: 69 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
100100
func (w *Watcher) updateLogs() {
101101
// Get a list of urls of all CT logs
102102
logList, err := getAllLogs()
@@ -105,78 +105,97 @@ 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+
normURL := normalizeCtlogURL(additional.URL)
141+
monitoredURLs[normURL] = 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+
normURL := normalizeCtlogURL(ctWorker.ctURL)
150+
if _, ok := monitoredURLs[normURL]; !ok {
151+
log.Printf("Stopping worker. CT URL not found in LogList or retired: '%s'\n", ctWorker.ctURL)
152+
ctWorker.stop()
153+
removed++
154+
}
173155
}
156+
log.Printf("Removed ct logs: %d\n", removed)
174157
}
175158

176-
log.Printf("New ct logs found: %d\n", newCTs)
177159
log.Printf("Currently monitored ct logs: %d\n", len(w.workers))
178160
}
179161

162+
// addLogIfNew checks if a log is already being watched and adds it if not.
163+
// Returns true if a new log was added, false otherwise.
164+
func (w *Watcher) addLogIfNew(operatorName, description, url string) bool {
165+
normURL := normalizeCtlogURL(url)
166+
167+
// Check if the log is already being watched
168+
for _, ctWorker := range w.workers {
169+
workerURL := normalizeCtlogURL(ctWorker.ctURL)
170+
if workerURL == normURL {
171+
return false
172+
}
173+
}
174+
175+
// Log is not being watched, so add it
176+
w.wg.Add(1)
177+
178+
lastCTIndex := metrics.GetCTIndex(normURL)
179+
ctWorker := worker{
180+
name: description,
181+
operatorName: operatorName,
182+
ctURL: url,
183+
entryChan: w.certChan,
184+
ctIndex: lastCTIndex,
185+
}
186+
w.workers = append(w.workers, &ctWorker)
187+
metrics.Init(operatorName, normURL)
188+
189+
// Start a goroutine for each worker
190+
go func() {
191+
defer w.wg.Done()
192+
ctWorker.startDownloadingCerts(w.context)
193+
w.discardWorker(&ctWorker)
194+
}()
195+
196+
return true
197+
}
198+
180199
// discardWorker removes a worker from the watcher's list of workers.
181200
// This needs to be done when a worker stops.
182201
func (w *Watcher) discardWorker(worker *worker) {
@@ -193,67 +212,6 @@ func (w *Watcher) discardWorker(worker *worker) {
193212
}
194213
}
195214

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-
257215
// Stop stops the watcher.
258216
func (w *Watcher) Stop() {
259217
log.Printf("Stopping watcher\n")

0 commit comments

Comments
 (0)