feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,148 @@
|
||||
package isolation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (a Agent) importLegacyProject(w http.ResponseWriter, r *http.Request) {
|
||||
var input LegacyImportSpec
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&input) != nil || decoder.Decode(&struct{}{}) != io.EOF || !safeName.MatchString(input.Project.HostName) || input.Project.TenantID == 0 || !validProjectRunPath(input.Project.RunPath) {
|
||||
http.Error(w, "invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
target, err := a.projectPath(input.Project)
|
||||
if err != nil || requirePhysicalDirectory(target) != nil {
|
||||
http.Error(w, "unsafe target directory", http.StatusPreconditionFailed)
|
||||
return
|
||||
}
|
||||
legacyRoot := strings.TrimSpace(a.LegacyRoot)
|
||||
if legacyRoot == "" {
|
||||
legacyRoot = "/www/wwwroot"
|
||||
}
|
||||
legacyRoot = filepath.Clean(legacyRoot)
|
||||
source := filepath.Clean(strings.TrimSpace(input.SourcePath))
|
||||
managedRoot := filepath.Clean(a.Root)
|
||||
if !filepath.IsAbs(source) || source == legacyRoot || !strings.HasPrefix(source+string(filepath.Separator), legacyRoot+string(filepath.Separator)) || strings.HasPrefix(source+string(filepath.Separator), managedRoot+string(filepath.Separator)) || requirePhysicalDirectory(source) != nil {
|
||||
http.Error(w, "legacy source is outside import boundary", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
empty, err := importTargetEmpty(target, input.Project.RunPath)
|
||||
if err != nil || !empty {
|
||||
http.Error(w, "target directory is not empty", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
if err = copyLegacyTree(r.Context(), source, target); err != nil {
|
||||
_ = removeImportedContents(target)
|
||||
http.Error(w, "cannot import legacy project", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func importTargetEmpty(root, runPath string) (bool, error) {
|
||||
// Prepare creates the selected run-directory chain before importing. Only
|
||||
// those empty directories may pre-exist; never overwrite files or siblings.
|
||||
runRoot := filepath.Clean(filepath.Join(root, strings.TrimPrefix(runPath, "/")))
|
||||
empty := true
|
||||
err := filepath.WalkDir(root, func(current string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if current == root {
|
||||
return nil
|
||||
}
|
||||
if !entry.IsDir() || (current != runRoot && !strings.HasPrefix(runRoot+string(filepath.Separator), current+string(filepath.Separator))) {
|
||||
empty = false
|
||||
return fs.SkipAll
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return empty, err
|
||||
}
|
||||
|
||||
func copyLegacyTree(ctx context.Context, source, target string) error {
|
||||
sourceRoot, err := os.OpenRoot(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sourceRoot.Close()
|
||||
return filepath.WalkDir(source, func(current string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
relative, err := filepath.Rel(source, current)
|
||||
if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
return errors.New("legacy import escaped source")
|
||||
}
|
||||
if relative == "." {
|
||||
return nil
|
||||
}
|
||||
destination := filepath.Join(target, relative)
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !(info.IsDir() || info.Mode().IsRegular()) {
|
||||
return errors.New("legacy project contains unsupported links or special files")
|
||||
}
|
||||
if info.IsDir() {
|
||||
if err := os.Mkdir(destination, info.Mode().Perm()|0700); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
return requirePhysicalDirectory(destination)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return copyLegacyFile(sourceRoot, relative, destination, info.Mode().Perm())
|
||||
})
|
||||
}
|
||||
|
||||
func copyLegacyFile(sourceRoot *os.Root, relative, target string, mode os.FileMode) error {
|
||||
input, err := sourceRoot.Open(relative)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer input.Close()
|
||||
info, err := input.Stat()
|
||||
if err != nil || !info.Mode().IsRegular() {
|
||||
return errors.New("legacy source is not a regular file")
|
||||
}
|
||||
output, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode|0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, copyErr := io.Copy(output, input)
|
||||
closeErr := output.Close()
|
||||
if copyErr != nil {
|
||||
return copyErr
|
||||
}
|
||||
return closeErr
|
||||
}
|
||||
|
||||
func removeImportedContents(root string) error {
|
||||
entries, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var result error
|
||||
for _, entry := range entries {
|
||||
result = errors.Join(result, os.RemoveAll(filepath.Join(root, entry.Name())))
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in new issue
Block a user