Skip to content

Conversation

Akshay2191
Copy link
Contributor

Proposed changes

Updated Mock Management Plane to support testing for a feature where the Agent should be able to fetch a file from a URL.
Have added a new endpoint that can be used as an external file server.
Updated the config apply endpoint, which can now handle a POST API request containing JSON  to specify external file locations.

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING document
  • I have run make install-tools and have attached any dependency changes to this pull request
  • If applicable, I have added tests that prove my fix is effective or that my feature works
  • If applicable, I have checked that any relevant tests pass after adding my changes
  • If applicable, I have updated any relevant documentation (README.md)
  • If applicable, I have tested my cross-platform changes on Ubuntu 22, Redhat 8, SUSE 15 and FreeBSD 13

@Akshay2191 Akshay2191 requested a review from a team as a code owner August 29, 2025 14:27
@github-actions github-actions bot added chore Pull requests for routine tasks documentation Improvements or additions to documentation labels Aug 29, 2025
Akshay2191 and others added 2 commits September 1, 2025 11:14
Adding validation to the user defined file path for external file server to stop possible  traversal attacks.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
defaultExternalFileServer, externalFileServerErr := generateDefaultExternalFileSevrevDirectory()
externalFileServer = &defaultExternalFileServer
if externalFileServerErr != nil {
slog.ErrorContext(ctx, "Failed to create default config directory", "error", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.ErrorContext(ctx, "Failed to create default config directory", "error", err)
slog.ErrorContext(ctx, "Failed to create external file server directory", "error", err)


err := os.MkdirAll(externalFileServer, directoryPermissions)
if err != nil {
slog.Error("Failed to create external file server directory", "error", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log error message here his error returned is already being logged above

@@ -469,3 +536,33 @@ func createFile(fullPath, filePath string) (*mpi.File, error) {
func isValidFile(info os.FileInfo, fileFullPath string) bool {
return !info.IsDir() && !strings.HasSuffix(fileFullPath, ".DS_Store")
}

func processConfigApplyRequestBody(c *gin.Context, initialFiles []*mpi.File) ([]*mpi.File, bool, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialFiles won't have any external files in the list so I think you need to append to the list instead of updating an entry in the list

@@ -359,7 +378,17 @@ func (cs *CommandService) addConfigApplyEndpoint() {
return
}

cs.instanceFiles[instanceID] = configFiles
updatedConfigFiles, externalFilesWereUpdated, err := processConfigApplyRequestBody(c, configFiles)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
updatedConfigFiles, externalFilesWereUpdated, err := processConfigApplyRequestBody(c, configFiles)
updatedConfigFiles, externalFilesUpdated, err := processConfigApplyRequestBody(c, configFiles)

cs.instanceFiles[instanceID] = configFiles
updatedConfigFiles, externalFilesWereUpdated, err := processConfigApplyRequestBody(c, configFiles)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity why gin.H{"error": err.Error()} instead of just err ?

c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid file name"})
return
}
if !strings.HasPrefix(absFile, absBase) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the checking if the file is valid be put in a separate function ?

return
}
filePath := filepath.Join(cs.externalFileServer, filename)
absBase, err := filepath.Abs(cs.externalFileServer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this check be done in NewCommandService

c.File(absFile)
})

slog.Info("Serving individual external files from", "directory", cs.externalFileServer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.Info("Serving individual external files from", "directory", cs.externalFileServer)
slog.Info("Serving individual external files from", "external_file_server", cs.externalFileServer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Pull requests for routine tasks documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants