Skip to content

Commit

Permalink
fix(newsigv4): persists the watch event beyond the first instance
Browse files Browse the repository at this point in the history
Inotify works on watching specific inodes, the current
implementation proved brittle if the process mutating
the watched file does so by created a new file and then
renaming. This action causes the file under the same
path to change contents and exist with a new inode.
Instead we watch the parent directory and filter incoming
events by filename.
  • Loading branch information
tanuck committed Jan 9, 2025
1 parent 8fe9e68 commit 6882e63
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions extensions/newsigv4/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"

"github.com/aws/aws-sdk-go-v2/aws"
sigv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
Expand Down Expand Up @@ -75,7 +76,6 @@ func (sa *sigv4Auth) Start(_ context.Context, host component.Host) error {
if err := sa.startWatcher(); err != nil {
componentstatus.ReportStatus(host, componentstatus.NewFatalErrorEvent(err))
}
sa.logger.Info("Started credentials file watcher")
}

return nil
Expand All @@ -96,6 +96,7 @@ func (sa *sigv4Auth) Shutdown(_ context.Context) error {

func (sa *sigv4Auth) startWatcher() error {
location := sa.cfg.SharedCredentialsWatcher.FileLocation
parentDir := filepath.Dir(location)

// invalidator is a local copy of the internal interface for cache invalidators
// from the AWS Go SDK.
Expand All @@ -110,14 +111,16 @@ func (sa *sigv4Auth) startWatcher() error {
}

go func() {
sa.logger.Info("Launching shared credentials file watcher")
for {
select {
case event, ok := <-sa.watcher.Events:
sa.logger.Debug("Received watcher event", zap.Any("event", event))
if !ok {
return
}

if event.Has(fsnotify.Create | fsnotify.Write | fsnotify.Rename) {
if event.Has(fsnotify.Create|fsnotify.Write|fsnotify.Rename|fsnotify.Remove) && event.Name == location {
sa.logger.Info("Detected changes within shared credentials file")
cache.Invalidate()
}
Expand All @@ -131,7 +134,7 @@ func (sa *sigv4Auth) startWatcher() error {
}
}()

if err := sa.watcher.Add(location); err != nil {
if err := sa.watcher.Add(parentDir); err != nil {
return err
}

Expand Down

0 comments on commit 6882e63

Please sign in to comment.