diff --git a/api/edge/edge.go b/api/edge/edge.go index 864fbf39a..adafecec5 100644 --- a/api/edge/edge.go +++ b/api/edge/edge.go @@ -14,6 +14,9 @@ type ( // Name of the stack Name string + // Content of the stack file (for compatibility to agent version less than 2.19.0) + StackFileContent string + // Content of stack folder DirEntries []filesystem.DirEntry // Name of the stack entry file diff --git a/api/filesystem/serialize.go b/api/filesystem/serialize.go index e0fd5bc09..f37f5aa99 100644 --- a/api/filesystem/serialize.go +++ b/api/filesystem/serialize.go @@ -2,9 +2,12 @@ package filesystem import ( "encoding/base64" + "fmt" "os" "path/filepath" "strings" + + "golang.org/x/mod/semver" ) type DirEntry struct { @@ -46,6 +49,22 @@ func FilterDirForEntryFile(dirEntries []DirEntry, entryFile string) []DirEntry { return filteredDirEntries } +func FilterDirForCompatibility(dirEntries []DirEntry, entryFilePath, agentVersion string) (string, error) { + + if semver.Compare(fmt.Sprintf("v%s", agentVersion), "v2.19.0") == -1 { + for _, dirEntry := range dirEntries { + if dirEntry.IsFile { + if dirEntry.Name == entryFilePath { + return DecodeFileContent(dirEntry.Content) + } + } + } + return "", fmt.Errorf("Entry file %s not found in dir entries", entryFilePath) + } + + return "", nil +} + // LoadDir reads all files and folders recursively from the given directory // File content is base64-encoded func LoadDir(dir string) ([]DirEntry, error) { @@ -127,14 +146,22 @@ func PersistDir(dir string, dirEntries []DirEntry) error { return nil } +func DecodeFileContent(encodedFileContent string) (string, error) { + decodedBytes, err := base64.StdEncoding.DecodeString(encodedFileContent) + if err != nil { + return "", err + } + return string(decodedBytes), nil +} + func DecodeDirEntries(dirEntries []DirEntry) error { for index, dirEntry := range dirEntries { if dirEntry.IsFile && dirEntry.Content != "" { - decodedBytes, err := base64.StdEncoding.DecodeString(dirEntry.Content) + decodedFile, err := DecodeFileContent(dirEntry.Content) if err != nil { return err } - dirEntries[index].Content = string(decodedBytes) + dirEntries[index].Content = decodedFile } } diff --git a/api/http/handler/endpointedge/endpointedge_stack_inspect.go b/api/http/handler/endpointedge/endpointedge_stack_inspect.go index a7071608c..3ee13d02d 100644 --- a/api/http/handler/endpointedge/endpointedge_stack_inspect.go +++ b/api/http/handler/endpointedge/endpointedge_stack_inspect.go @@ -75,12 +75,18 @@ func (handler *Handler) endpointEdgeStackInspect(w http.ResponseWriter, r *http. return httperror.InternalServerError("Unable to load repository", err) } + fileContent, err := filesystem.FilterDirForCompatibility(dirEntries, fileName, endpoint.Agent.Version) + if err != nil { + return httperror.InternalServerError("File not found", err) + } + dirEntries = filesystem.FilterDirForEntryFile(dirEntries, fileName) return response.JSON(w, edge.StackPayload{ - DirEntries: dirEntries, - EntryFileName: fileName, - Name: edgeStack.Name, - Namespace: namespace, + DirEntries: dirEntries, + EntryFileName: fileName, + StackFileContent: fileContent, + Name: edgeStack.Name, + Namespace: namespace, }) }