Skip to content

Commit bdfee34

Browse files
authored
add temp hidden agent dir (#1318)
1 parent 5642dbe commit bdfee34

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

api/grpc/mpi/v1/command.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/common.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/files.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/file/file_manager_service.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type FileManagerService struct {
110110
manifestFilePath string
111111
tempConfigDir string
112112
tempRollbackDir string
113-
configPath string
113+
tempConfigPath string
114114
rollbackManifest bool
115115
filesMutex sync.RWMutex
116116
}
@@ -127,13 +127,13 @@ func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig
127127
previousManifestFiles: make(map[string]*model.ManifestFile),
128128
rollbackManifest: true,
129129
manifestFilePath: agentConfig.LibDir + "/manifest.json",
130-
configPath: "/etc/nginx/",
131130
manifestLock: manifestLock,
132131
}
133132
}
134133

135134
func (fms *FileManagerService) SetConfigPath(configPath string) {
136-
fms.configPath = filepath.Dir(configPath)
135+
fms.tempConfigPath = fmt.Sprintf("%s/.agent-%s", filepath.Dir(configPath), fms.agentConfig.UUID)
136+
fms.ClearCache()
137137
}
138138

139139
func (fms *FileManagerService) ResetClient(ctx context.Context, fileServiceClient mpi.FileServiceClient) {
@@ -219,18 +219,13 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
219219
}
220220

221221
func (fms *FileManagerService) ClearCache() {
222-
slog.Debug("Clearing cache and temp files after config apply")
222+
slog.Debug("Clearing cache and temp files")
223223
clear(fms.fileActions)
224224
clear(fms.previousManifestFiles)
225225

226-
configErr := os.RemoveAll(fms.tempConfigDir)
227-
if configErr != nil {
228-
slog.Error("Error removing temp config directory", "path", fms.tempConfigDir, "err", configErr)
229-
}
230-
231-
rollbackErr := os.RemoveAll(fms.tempRollbackDir)
232-
if rollbackErr != nil {
233-
slog.Error("Error removing temp rollback directory", "path", fms.tempRollbackDir, "err", rollbackErr)
226+
configErr := os.RemoveAll(fms.tempConfigPath)
227+
if configErr != nil && !os.IsNotExist(configErr) {
228+
slog.Error("Error removing temp directory", "path", fms.tempConfigDir, "err", configErr)
234229
}
235230
}
236231

@@ -759,7 +754,13 @@ func (fms *FileManagerService) convertToFile(manifestFile *model.ManifestFile) *
759754
}
760755

761756
func (fms *FileManagerService) createTempConfigDirectory(pattern string) (string, error) {
762-
tempDir, tempDirError := os.MkdirTemp(fms.configPath, pattern)
757+
if _, err := os.Stat(fms.tempConfigPath); os.IsNotExist(err) {
758+
mkdirErr := os.MkdirAll(fms.tempConfigPath, dirPerm)
759+
if mkdirErr != nil {
760+
return "", mkdirErr
761+
}
762+
}
763+
tempDir, tempDirError := os.MkdirTemp(fms.tempConfigPath, pattern)
763764
if tempDirError != nil {
764765
return "", fmt.Errorf("failed creating temp config directory: %w", tempDirError)
765766
}

internal/file/file_manager_service_test.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {
5959
agentConfig.AllowedDirectories = []string{tempDir}
6060

6161
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
62-
fileManagerService.configPath = filepath.Dir(filePath)
62+
fileManagerService.tempConfigPath = filepath.Dir(filePath)
6363
fileManagerService.agentConfig.LibDir = manifestDirPath
6464
fileManagerService.manifestFilePath = manifestFilePath
6565

@@ -109,7 +109,7 @@ func TestFileManagerService_ConfigApply_Add_LargeFile(t *testing.T) {
109109
agentConfig.AllowedDirectories = []string{tempDir}
110110
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
111111
fileManagerService.agentConfig.LibDir = manifestDirPath
112-
fileManagerService.configPath = filepath.Dir(filePath)
112+
fileManagerService.tempConfigPath = filepath.Dir(filePath)
113113
fileManagerService.manifestFilePath = manifestFilePath
114114

115115
request := protos.CreateConfigApplyRequest(overview)
@@ -170,7 +170,7 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
170170

171171
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
172172
fileManagerService.agentConfig.LibDir = manifestDirPath
173-
fileManagerService.configPath = filepath.Dir(tempFile.Name())
173+
fileManagerService.tempConfigPath = filepath.Dir(tempFile.Name())
174174
fileManagerService.manifestFilePath = manifestFilePath
175175
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
176176
require.NoError(t, err)
@@ -226,7 +226,7 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
226226
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
227227
fileManagerService.agentConfig.LibDir = manifestDirPath
228228
fileManagerService.manifestFilePath = manifestFilePath
229-
fileManagerService.configPath = filepath.Dir(tempFile.Name())
229+
fileManagerService.tempConfigPath = filepath.Dir(tempFile.Name())
230230
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
231231
require.NoError(t, err)
232232

@@ -290,7 +290,7 @@ func TestFileManagerService_ConfigApply_Failed(t *testing.T) {
290290

291291
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
292292
fileManagerService.agentConfig.LibDir = manifestDirPath
293-
fileManagerService.configPath = filepath.Dir(filePath)
293+
fileManagerService.tempConfigPath = filepath.Dir(filePath)
294294
fileManagerService.manifestFilePath = manifestFilePath
295295

296296
request := protos.CreateConfigApplyRequest(overview)
@@ -332,7 +332,7 @@ func TestFileManagerService_ConfigApply_FileWithExecutePermissions(t *testing.T)
332332
agentConfig.AllowedDirectories = []string{tempDir}
333333

334334
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
335-
fileManagerService.configPath = filepath.Dir(filePath)
335+
fileManagerService.tempConfigPath = filepath.Dir(filePath)
336336
fileManagerService.agentConfig.LibDir = manifestDirPath
337337
fileManagerService.manifestFilePath = manifestFilePath
338338

@@ -512,15 +512,20 @@ func TestFileManagerService_removeExecuteFilePermissions(t *testing.T) {
512512
//nolint:usetesting // need to use MkDirTemp instead of t.tempDir for rollback as t.tempDir does not accept a pattern
513513
func TestFileManagerService_ClearCache(t *testing.T) {
514514
tempDir := t.TempDir()
515-
rollbackDir, err := os.MkdirTemp(tempDir, "rollback")
515+
agentConfig := types.AgentConfig()
516+
tempPath := fmt.Sprintf("%s.agent_%s", tempDir, agentConfig.UUID)
517+
err := os.Mkdir(tempPath, dirPerm)
518+
require.NoError(t, err)
519+
rollbackDir, err := os.MkdirTemp(tempPath, "rollback")
516520
require.NoError(t, err)
517-
configDir, err := os.MkdirTemp(tempDir, "config")
521+
configDir, err := os.MkdirTemp(tempPath, "config")
518522
require.NoError(t, err)
519523

520524
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
521-
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig(), &sync.RWMutex{})
525+
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
522526
fileManagerService.tempConfigDir = configDir
523527
fileManagerService.tempRollbackDir = rollbackDir
528+
fileManagerService.tempConfigPath = tempPath
524529

525530
filesCache := map[string]*model.FileCache{
526531
"file/path/test.conf": {
@@ -654,7 +659,7 @@ func TestFileManagerService_Rollback(t *testing.T) {
654659
fileManagerService.fileActions = filesCache
655660
fileManagerService.agentConfig.LibDir = manifestDirPath
656661
fileManagerService.tempRollbackDir = rollbackDir
657-
fileManagerService.configPath = filepath.Dir(updateFile.Name())
662+
fileManagerService.tempConfigPath = filepath.Dir(updateFile.Name())
658663
fileManagerService.manifestFilePath = manifestFilePath
659664

660665
err := fileManagerService.Rollback(ctx, instanceID)
@@ -841,7 +846,7 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
841846
fileManagerService.agentConfig.AllowedDirectories = test.allowedDirs
842847
fileManagerService.agentConfig.LibDir = manifestDirPath
843848
fileManagerService.manifestFilePath = manifestFilePath
844-
fileManagerService.configPath = filepath.Dir(updateTestFile.Name())
849+
fileManagerService.tempConfigPath = filepath.Dir(updateTestFile.Name())
845850

846851
require.NoError(tt, err)
847852

@@ -1238,16 +1243,16 @@ func TestFileManagerService_createTempConfigDirectory(t *testing.T) {
12381243
configPath := tempDir
12391244

12401245
fileManagerService := FileManagerService{
1241-
agentConfig: agentConfig,
1242-
configPath: configPath,
1246+
agentConfig: agentConfig,
1247+
tempConfigPath: configPath,
12431248
}
12441249

12451250
dir, err := fileManagerService.createTempConfigDirectory("config")
12461251
assert.NotEmpty(t, dir)
12471252
require.NoError(t, err)
12481253

12491254
// Test for unknown directory path
1250-
fileManagerService.configPath = "/unknown/"
1255+
fileManagerService.tempConfigPath = "/unknown/"
12511256

12521257
dir, err = fileManagerService.createTempConfigDirectory("config")
12531258
assert.Empty(t, dir)

0 commit comments

Comments
 (0)