Added watchlist reload via SIGHUP

Signed-off-by: The-Inceptions <83852285+The-Inceptions@users.noreply.github.com>
This commit is contained in:
The-Inceptions 2024-06-12 16:37:42 +00:00
parent cd4d796a7c
commit 5653af7491
3 changed files with 52 additions and 15 deletions

View File

@ -124,6 +124,27 @@ func readWatchListFile(filename string) (monitor.WatchList, error) {
return monitor.ReadWatchList(file) return monitor.ReadWatchList(file)
} }
func setupSignalListener(ctx context.Context, filename string, watchList *monitor.WatchList) {
sighup := make(chan os.Signal, 1)
signal.Notify(sighup, syscall.SIGHUP)
go func() {
for {
select {
case <-sighup:
newWatchList, err := readWatchListFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading watchlist file: %v", err.Error())
continue
}
*watchList = newWatchList
case <-ctx.Done():
return
}
}
}()
}
func readEmailFile(filename string) ([]string, error) { func readEmailFile(filename string) ([]string, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
@ -226,6 +247,9 @@ func main() {
os.Exit(2) os.Exit(2)
} }
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if flags.watchlist == "-" { if flags.watchlist == "-" {
watchlist, err := monitor.ReadWatchList(os.Stdin) watchlist, err := monitor.ReadWatchList(os.Stdin)
if err != nil { if err != nil {
@ -240,11 +264,9 @@ func main() {
os.Exit(1) os.Exit(1)
} }
config.WatchList = watchlist config.WatchList = watchlist
setupSignalListener(ctx, flags.watchlist, &config.WatchList)
} }
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := monitor.Run(ctx, config); err != nil && !errors.Is(err, context.Canceled) { if err := monitor.Run(ctx, config); err != nil && !errors.Is(err, context.Canceled) {
fmt.Fprintf(os.Stderr, "%s: %s\n", programName, err) fmt.Fprintf(os.Stderr, "%s: %s\n", programName, err)
os.Exit(1) os.Exit(1)

View File

@ -30,10 +30,11 @@ func EmptyCollapsedTree() *CollapsedTree {
} }
func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) { func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) {
if len(nodes) != calculateNumNodes(size) { tree := new(CollapsedTree)
return nil, fmt.Errorf("nodes has wrong length (should be %d, not %d)", calculateNumNodes(size), len(nodes)) if err := tree.Init(nodes, size); err != nil {
return nil, err
} }
return &CollapsedTree{nodes: nodes, size: size}, nil return tree, nil
} }
func CloneCollapsedTree(source *CollapsedTree) *CollapsedTree { func CloneCollapsedTree(source *CollapsedTree) *CollapsedTree {
@ -53,10 +54,12 @@ func (tree *CollapsedTree) Add(hash Hash) {
} }
func (tree *CollapsedTree) Append(other *CollapsedTree) error { func (tree *CollapsedTree) Append(other *CollapsedTree) error {
if tree.size > 0 {
maxSize := uint64(1) << bits.TrailingZeros64(tree.size) maxSize := uint64(1) << bits.TrailingZeros64(tree.size)
if other.size > maxSize { if other.size > maxSize {
return fmt.Errorf("tree of size %d is too large to append to a tree of size %d (maximum size is %d)", other.size, tree.size, maxSize) return fmt.Errorf("tree of size %d is too large to append to a tree of size %d (maximum size is %d)", other.size, tree.size, maxSize)
} }
}
tree.nodes = append(tree.nodes, other.nodes...) tree.nodes = append(tree.nodes, other.nodes...)
tree.size += other.size tree.size += other.size
@ -86,6 +89,10 @@ func (tree *CollapsedTree) CalculateRoot() Hash {
return hash return hash
} }
func (tree *CollapsedTree) Nodes() []Hash {
return tree.nodes
}
func (tree *CollapsedTree) Size() uint64 { func (tree *CollapsedTree) Size() uint64 {
return tree.size return tree.size
} }
@ -105,10 +112,17 @@ func (tree *CollapsedTree) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &rawTree); err != nil { if err := json.Unmarshal(b, &rawTree); err != nil {
return fmt.Errorf("error unmarshalling Collapsed Merkle Tree: %w", err) return fmt.Errorf("error unmarshalling Collapsed Merkle Tree: %w", err)
} }
if len(rawTree.Nodes) != calculateNumNodes(rawTree.Size) { if err := tree.Init(rawTree.Nodes, rawTree.Size); err != nil {
return fmt.Errorf("error unmarshalling Collapsed Merkle Tree: nodes has wrong length (should be %d, not %d)", calculateNumNodes(rawTree.Size), len(rawTree.Nodes)) return fmt.Errorf("error unmarshalling Collapsed Merkle Tree: %w", err)
} }
tree.size = rawTree.Size return nil
tree.nodes = rawTree.Nodes }
func (tree *CollapsedTree) Init(nodes []Hash, size uint64) error {
if len(nodes) != calculateNumNodes(size) {
return fmt.Errorf("nodes has wrong length (should be %d, not %d)", calculateNumNodes(size), len(nodes))
}
tree.size = size
tree.nodes = nodes
return nil return nil
} }

View File

@ -61,7 +61,8 @@ type StateProvider interface {
// feailure is not associated with a log. // feailure is not associated with a log.
NotifyHealthCheckFailure(context.Context, *loglist.Log, HealthCheckFailure) error NotifyHealthCheckFailure(context.Context, *loglist.Log, HealthCheckFailure) error
// Called when an error occurs. The log is nil if the error is // Called when a non-fatal error occurs. The log is nil if the error is
// not associated with a log. Note that most errors are transient. // not associated with a log. Note that most errors are transient, and
// certspotter will retry the failed operation later.
NotifyError(context.Context, *loglist.Log, error) error NotifyError(context.Context, *loglist.Log, error) error
} }