-
Notifications
You must be signed in to change notification settings - Fork 101
Update mock management to support external file server #1243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()}) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slog.Info("Serving individual external files from", "directory", cs.externalFileServer) | |
slog.Info("Serving individual external files from", "external_file_server", cs.externalFileServer) |
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.
CONTRIBUTING
documentmake install-tools
and have attached any dependency changes to this pull requestREADME.md
)