Skip to content

Commit 3a2b5b4

Browse files
committed
refactor: consolidate CT log reconciliation into single updateLogs method
1 parent 2903e6a commit 3a2b5b4

1 file changed

Lines changed: 67 additions & 106 deletions

File tree

internal/certificatetransparency/ct-watcher.go

Lines changed: 67 additions & 106 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,74 +105,96 @@ func (w *Watcher) updateLogs() {
105105
return
106106
}
107107

108-
w.addNewlyAvailableLogs(logList)
109-
if *config.AppConfig.General.DropOldLogs {
110-
w.dropRemovedLogs(logList)
111-
}
112-
}
113-
114-
// addNewlyAvailableLogs checks the transparency log list for new Log servers and adds workers for those to the watcher.
115-
func (w *Watcher) addNewlyAvailableLogs(logList loglist3.LogList) {
116108
log.Println("Checking for new ct logs...")
117109

110+
// Track all URLs that should be monitored after reconciliation
111+
monitoredUrls := make(map[string]struct{})
112+
newCTs := 0
113+
118114
w.workersMu.Lock()
119115
defer w.workersMu.Unlock()
120-
newCTs := 0
121116

122-
// Check the ct log list for new, unwatched logs
123-
// For each CT log, create a worker and start downloading certs
124117
for _, operator := range logList.Operators {
125118
// Iterate over each log of the operator
126119
for _, transparencyLog := range operator.Logs {
127-
newURL := normalizeCtlogURL(transparencyLog.URL)
120+
url := transparencyLog.URL
121+
desc := transparencyLog.Description
122+
normUrl := normalizeCtlogURL(url)
128123

129124
if transparencyLog.State.LogStatus() == loglist3.RetiredLogStatus {
130-
log.Printf("Skipping retired CT log: %s\n", newURL)
125+
log.Printf("Skipping retired CT log: %s\n", normUrl)
131126
continue
132127
}
133128

134-
// Check if the log is already being watched
135-
alreadyWatched := false
136-
for _, ctWorker := range w.workers {
137-
workerURL := normalizeCtlogURL(ctWorker.ctURL)
138-
if workerURL == newURL {
139-
alreadyWatched = true
140-
break
141-
}
129+
monitoredUrls[normUrl] = struct{}{}
130+
if w.addLogIfNew(operator.Name, desc, url) {
131+
newCTs++
142132
}
133+
}
134+
}
143135

144-
// If the log is already being watched, continue
145-
if alreadyWatched {
146-
continue
147-
}
136+
log.Printf("New ct logs found: %d\n", newCTs)
148137

149-
w.wg.Add(1)
150-
newCTs++
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+
}
151144

152-
lastCTIndex := metrics.GetCTIndex(transparencyLog.URL)
153-
ctWorker := worker{
154-
name: transparencyLog.Description,
155-
operatorName: operator.Name,
156-
ctURL: transparencyLog.URL,
157-
entryChan: w.certChan,
158-
ctIndex: lastCTIndex,
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++
159153
}
160-
w.workers = append(w.workers, &ctWorker)
161-
metrics.Init(operator.Name, transparencyLog.URL)
162-
163-
// Start a goroutine for each worker
164-
go func() {
165-
defer w.wg.Done()
166-
ctWorker.startDownloadingCerts(w.context)
167-
w.discardWorker(&ctWorker)
168-
}()
169154
}
155+
log.Printf("Removed ct logs: %d\n", removed)
170156
}
171157

172-
log.Printf("New ct logs found: %d\n", newCTs)
173158
log.Printf("Currently monitored ct logs: %d\n", len(w.workers))
174159
}
175160

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+
176198
// discardWorker removes a worker from the watcher's list of workers.
177199
// This needs to be done when a worker stops.
178200
func (w *Watcher) discardWorker(worker *worker) {
@@ -189,67 +211,6 @@ func (w *Watcher) discardWorker(worker *worker) {
189211
}
190212
}
191213

192-
// dropRemovedLogs checks if any of the currently monitored logs are no longer in the log list or are retired.
193-
// If they are not, the CT Logs are probably no longer relevant and the corresponding workers will be stopped.
194-
func (w *Watcher) dropRemovedLogs(logList loglist3.LogList) {
195-
removedCTs := 0
196-
197-
// Iterate over all workers and check if they are still in the logList
198-
// If they are not, the CT Logs are probably no longer relevant.
199-
// We should stop the worker if that didn't already happen.
200-
for _, ctWorker := range w.workers {
201-
workerURL := normalizeCtlogURL(ctWorker.ctURL)
202-
203-
onLogList := false
204-
for _, operator := range logList.Operators {
205-
if ctWorker.operatorName != operator.Name {
206-
// This operator is not the one we're looking for
207-
continue
208-
}
209-
210-
// Iterate over each log of the operator
211-
for _, transparencyLog := range operator.Logs {
212-
// Remove retired logs from the list
213-
if transparencyLog.State.LogStatus() == loglist3.RetiredLogStatus {
214-
// Skip retired logs
215-
continue
216-
}
217-
218-
// Check if the log is already being watched
219-
logListURL := normalizeCtlogURL(transparencyLog.URL)
220-
if workerURL == logListURL {
221-
onLogList = true
222-
break
223-
}
224-
}
225-
226-
// Prevent further loop iterations
227-
if onLogList {
228-
break
229-
}
230-
}
231-
232-
// Make sure to not drop logs that are defined locally in the additional logs list
233-
for _, additionalLogConfig := range config.AppConfig.General.AdditionalLogs {
234-
additionalLogListURL := normalizeCtlogURL(additionalLogConfig.URL)
235-
if workerURL == additionalLogListURL {
236-
onLogList = true
237-
break
238-
}
239-
}
240-
241-
// If the log is not in the loglist, stop the worker
242-
if !onLogList {
243-
log.Printf("Stopping worker. CT URL not found in LogList or retired: '%s'\n", ctWorker.ctURL)
244-
removedCTs++
245-
ctWorker.stop()
246-
}
247-
}
248-
249-
log.Printf("Removed ct logs: %d\n", removedCTs)
250-
log.Printf("Currently monitored ct logs: %d\n", len(w.workers))
251-
}
252-
253214
// Stop stops the watcher.
254215
func (w *Watcher) Stop() {
255216
log.Printf("Stopping watcher\n")

0 commit comments

Comments
 (0)