@@ -13,6 +13,10 @@ import (
1313 "sync/atomic"
1414 "time"
1515
16+ "github.com/nginx/agent/v3/internal/model"
17+
18+ "github.com/nginx/agent/v3/internal/grpc"
19+
1620 "github.com/fsnotify/fsnotify"
1721 "github.com/nginx/agent/v3/internal/config"
1822 "github.com/nginx/agent/v3/internal/logger"
@@ -28,56 +32,75 @@ var emptyEvent = fsnotify.Event{
2832}
2933
3034type CredentialUpdateMessage struct {
31- CorrelationID slog.Attr
35+ CorrelationID slog.Attr
36+ GrpcConnection * grpc.GrpcConnection
37+ ServerType model.ServerType
3238}
3339
3440type CredentialWatcherService struct {
3541 agentConfig * config.Config
3642 watcher * fsnotify.Watcher
3743 filesBeingWatched * sync.Map
3844 filesChanged * atomic.Bool
45+ serverType model.ServerType
46+ watcherMutex sync.Mutex
3947}
4048
41- func NewCredentialWatcherService (agentConfig * config.Config ) * CredentialWatcherService {
49+ func NewCredentialWatcherService (agentConfig * config.Config , serverType model. ServerType ) * CredentialWatcherService {
4250 filesChanged := & atomic.Bool {}
4351 filesChanged .Store (false )
4452
4553 return & CredentialWatcherService {
4654 agentConfig : agentConfig ,
4755 filesBeingWatched : & sync.Map {},
4856 filesChanged : filesChanged ,
57+ serverType : serverType ,
58+ watcherMutex : sync.Mutex {},
4959 }
5060}
5161
5262func (cws * CredentialWatcherService ) Watch (ctx context.Context , ch chan <- CredentialUpdateMessage ) {
53- slog .DebugContext (ctx , "Starting credential watcher monitoring" )
63+ newCtx := context .WithValue (
64+ ctx ,
65+ logger .ServerTypeContextKey ,
66+ slog .Any (logger .ServerTypeKey , cws .serverType .String ()),
67+ )
68+ slog .DebugContext (newCtx , "Starting credential watcher monitoring" )
5469
5570 ticker := time .NewTicker (monitoringInterval )
5671 watcher , err := fsnotify .NewWatcher ()
5772 if err != nil {
58- slog .ErrorContext (ctx , "Failed to create credential watcher" , "error" , err )
73+ slog .ErrorContext (newCtx , "Failed to create credential watcher" , "error" , err )
5974 return
6075 }
6176
6277 cws .watcher = watcher
6378
64- cws .watchFiles (ctx , credentialPaths (cws .agentConfig ))
79+ cws .watcherMutex .Lock ()
80+ commandServer := cws .agentConfig .Command
81+
82+ if cws .serverType == model .Auxiliary {
83+ commandServer = cws .agentConfig .AuxiliaryCommand
84+ }
85+
86+ cws .watchFiles (newCtx , credentialPaths (commandServer ))
87+ cws .watcherMutex .Unlock ()
6588
6689 for {
6790 select {
68- case <- ctx .Done ():
91+ case <- newCtx .Done ():
6992 closeError := cws .watcher .Close ()
7093 if closeError != nil {
71- slog .ErrorContext (ctx , "Unable to close credential watcher" , "error" , closeError )
94+ slog .ErrorContext (newCtx , "Unable to close credential watcher" , "error" , closeError )
7295 }
7396
7497 return
7598 case event := <- cws .watcher .Events :
76- cws .handleEvent (ctx , event )
99+ cws .handleEvent (newCtx , event )
77100 case <- ticker .C :
78- cws .checkForUpdates (ctx , ch )
101+ cws .checkForUpdates (newCtx , ch )
79102 case watcherError := <- cws .watcher .Errors :
80- slog .ErrorContext (ctx , "Unexpected error in credential watcher" , "error" , watcherError )
103+ slog .ErrorContext (newCtx , "Unexpected error in credential watcher" , "error" , watcherError )
81104 }
82105 }
83106}
@@ -146,31 +169,50 @@ func (cws *CredentialWatcherService) checkForUpdates(ctx context.Context, ch cha
146169 slog .Any (logger .CorrelationIDKey , logger .GenerateCorrelationID ()),
147170 )
148171
172+ cws .watcherMutex .Lock ()
173+ defer cws .watcherMutex .Unlock ()
174+
175+ commandServer := cws .agentConfig .Command
176+ if cws .serverType == model .Auxiliary {
177+ commandServer = cws .agentConfig .AuxiliaryCommand
178+ }
179+
180+ conn , err := grpc .NewGrpcConnection (newCtx , cws .agentConfig , commandServer )
181+ if err != nil {
182+ slog .ErrorContext (newCtx , "Unable to create new grpc connection" , "error" , err )
183+ cws .filesChanged .Store (false )
184+
185+ return
186+ }
149187 slog .DebugContext (ctx , "Credential watcher has detected changes" )
150- ch <- CredentialUpdateMessage {CorrelationID : logger .CorrelationIDAttr (newCtx )}
188+ ch <- CredentialUpdateMessage {
189+ CorrelationID : logger .CorrelationIDAttr (newCtx ),
190+ ServerType : cws .serverType ,
191+ GrpcConnection : conn ,
192+ }
151193 cws .filesChanged .Store (false )
152194 }
153195}
154196
155- func credentialPaths (agentConfig * config.Config ) []string {
197+ func credentialPaths (agentConfig * config.Command ) []string {
156198 var paths []string
157199
158- if agentConfig .Command . Auth != nil {
159- if agentConfig .Command . Auth .TokenPath != "" {
160- paths = append (paths , agentConfig .Command . Auth .TokenPath )
200+ if agentConfig .Auth != nil {
201+ if agentConfig .Auth .TokenPath != "" {
202+ paths = append (paths , agentConfig .Auth .TokenPath )
161203 }
162204 }
163205
164206 // agent's tls certs
165- if agentConfig .Command . TLS != nil {
166- if agentConfig .Command . TLS .Ca != "" {
167- paths = append (paths , agentConfig .Command . TLS .Ca )
207+ if agentConfig .TLS != nil {
208+ if agentConfig .TLS .Ca != "" {
209+ paths = append (paths , agentConfig .TLS .Ca )
168210 }
169- if agentConfig .Command . TLS .Cert != "" {
170- paths = append (paths , agentConfig .Command . TLS .Cert )
211+ if agentConfig .TLS .Cert != "" {
212+ paths = append (paths , agentConfig .TLS .Cert )
171213 }
172- if agentConfig .Command . TLS .Key != "" {
173- paths = append (paths , agentConfig .Command . TLS .Key )
214+ if agentConfig .TLS .Key != "" {
215+ paths = append (paths , agentConfig .TLS .Key )
174216 }
175217 }
176218
0 commit comments