Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/command.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/files.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type FileManagerService struct {
manifestFilePath string
tempConfigDir string
tempRollbackDir string
configPath string
tempConfigPath string
rollbackManifest bool
filesMutex sync.RWMutex
}
Expand All @@ -127,13 +127,13 @@ func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig
previousManifestFiles: make(map[string]*model.ManifestFile),
rollbackManifest: true,
manifestFilePath: agentConfig.LibDir + "/manifest.json",
configPath: "/etc/nginx/",
manifestLock: manifestLock,
}
}

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

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

func (fms *FileManagerService) ClearCache() {
slog.Debug("Clearing cache and temp files after config apply")
slog.Debug("Clearing cache and temp files")
clear(fms.fileActions)
clear(fms.previousManifestFiles)

configErr := os.RemoveAll(fms.tempConfigDir)
if configErr != nil {
slog.Error("Error removing temp config directory", "path", fms.tempConfigDir, "err", configErr)
}

rollbackErr := os.RemoveAll(fms.tempRollbackDir)
if rollbackErr != nil {
slog.Error("Error removing temp rollback directory", "path", fms.tempRollbackDir, "err", rollbackErr)
configErr := os.RemoveAll(fms.tempConfigPath)
if configErr != nil && !os.IsNotExist(configErr) {
slog.Error("Error removing temp directory", "path", fms.tempConfigDir, "err", configErr)
}
}

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

func (fms *FileManagerService) createTempConfigDirectory(pattern string) (string, error) {
tempDir, tempDirError := os.MkdirTemp(fms.configPath, pattern)
if _, err := os.Stat(fms.tempConfigPath); os.IsNotExist(err) {
mkdirErr := os.MkdirAll(fms.tempConfigPath, dirPerm)
if mkdirErr != nil {
return "", mkdirErr
}
}
tempDir, tempDirError := os.MkdirTemp(fms.tempConfigPath, pattern)
if tempDirError != nil {
return "", fmt.Errorf("failed creating temp config directory: %w", tempDirError)
}
Expand Down
33 changes: 19 additions & 14 deletions internal/file/file_manager_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {
agentConfig.AllowedDirectories = []string{tempDir}

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
fileManagerService.configPath = filepath.Dir(filePath)
fileManagerService.tempConfigPath = filepath.Dir(filePath)
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath

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

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

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.configPath = filepath.Dir(tempFile.Name())
fileManagerService.tempConfigPath = filepath.Dir(tempFile.Name())
fileManagerService.manifestFilePath = manifestFilePath
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
require.NoError(t, err)
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath
fileManagerService.configPath = filepath.Dir(tempFile.Name())
fileManagerService.tempConfigPath = filepath.Dir(tempFile.Name())
err := fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk, false)
require.NoError(t, err)

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

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.configPath = filepath.Dir(filePath)
fileManagerService.tempConfigPath = filepath.Dir(filePath)
fileManagerService.manifestFilePath = manifestFilePath

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

fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
fileManagerService.configPath = filepath.Dir(filePath)
fileManagerService.tempConfigPath = filepath.Dir(filePath)
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath

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

fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig(), &sync.RWMutex{})
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig, &sync.RWMutex{})
fileManagerService.tempConfigDir = configDir
fileManagerService.tempRollbackDir = rollbackDir
fileManagerService.tempConfigPath = tempPath

filesCache := map[string]*model.FileCache{
"file/path/test.conf": {
Expand Down Expand Up @@ -654,7 +659,7 @@ func TestFileManagerService_Rollback(t *testing.T) {
fileManagerService.fileActions = filesCache
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.tempRollbackDir = rollbackDir
fileManagerService.configPath = filepath.Dir(updateFile.Name())
fileManagerService.tempConfigPath = filepath.Dir(updateFile.Name())
fileManagerService.manifestFilePath = manifestFilePath

err := fileManagerService.Rollback(ctx, instanceID)
Expand Down Expand Up @@ -841,7 +846,7 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
fileManagerService.agentConfig.AllowedDirectories = test.allowedDirs
fileManagerService.agentConfig.LibDir = manifestDirPath
fileManagerService.manifestFilePath = manifestFilePath
fileManagerService.configPath = filepath.Dir(updateTestFile.Name())
fileManagerService.tempConfigPath = filepath.Dir(updateTestFile.Name())

require.NoError(tt, err)

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

fileManagerService := FileManagerService{
agentConfig: agentConfig,
configPath: configPath,
agentConfig: agentConfig,
tempConfigPath: configPath,
}

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

// Test for unknown directory path
fileManagerService.configPath = "/unknown/"
fileManagerService.tempConfigPath = "/unknown/"

dir, err = fileManagerService.createTempConfigDirectory("config")
assert.Empty(t, dir)
Expand Down
Loading