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,142 @@
|
||||
package panel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ScopedExecution carries the controller's resource snapshot. It is never
|
||||
// persisted by the node. Native panel state remains authoritative for paths.
|
||||
type ScopedExecution struct {
|
||||
Request json.RawMessage `json:"request"`
|
||||
Registry json.RawMessage `json:"registry"`
|
||||
}
|
||||
type ScopedResult struct {
|
||||
Response json.RawMessage `json:"response"`
|
||||
Registry json.RawMessage `json:"registry"`
|
||||
}
|
||||
|
||||
func ExecuteScoped(ctx context.Context, direct Interface, tenantRoot string, in ScopedExecution) (ScopedResult, error) {
|
||||
var request brokerRequest
|
||||
decoder := json.NewDecoder(bytes.NewReader(in.Request))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&request) != nil || decoder.Decode(&struct{}{}) != io.EOF {
|
||||
return ScopedResult{}, errors.New("invalid scoped operation")
|
||||
}
|
||||
var registry brokerRegistry
|
||||
decoder = json.NewDecoder(bytes.NewReader(in.Registry))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(®istry) != nil || decoder.Decode(&struct{}{}) != io.EOF || registry.Version != 1 || registry.Sites == nil || registry.FTP == nil || registry.Databases == nil {
|
||||
return ScopedResult{}, errors.New("invalid controller resource snapshot")
|
||||
}
|
||||
if !filepath.IsAbs(tenantRoot) || path.Clean(tenantRoot) != tenantRoot || tenantRoot == "/" {
|
||||
return ScopedResult{}, errors.New("invalid node resource root")
|
||||
}
|
||||
server := &BrokerServer{stateless: true, tenantRoot: tenantRoot, direct: direct, registry: registry, runtimeJobs: map[string]bool{}}
|
||||
for _, site := range registry.Sites {
|
||||
if site == nil || !brokerHostPattern.MatchString(site.ResourceName) || path.Clean(site.Path) != path.Join(tenantRoot, site.ResourceName) || site.RunUser != site.ResourceName || !validRunPath(site.RunPath) {
|
||||
return ScopedResult{}, errors.New("snapshot resource is outside the node namespace")
|
||||
}
|
||||
}
|
||||
for _, name := range registry.FTP {
|
||||
if !brokerHostPattern.MatchString(name) {
|
||||
return ScopedResult{}, errors.New("invalid scoped FTP identity")
|
||||
}
|
||||
}
|
||||
for _, name := range registry.Databases {
|
||||
if !brokerHostPattern.MatchString(name) {
|
||||
return ScopedResult{}, errors.New("invalid scoped database identity")
|
||||
}
|
||||
}
|
||||
if native, ok := direct.(interface {
|
||||
VerifyScopedResources(context.Context, string, json.RawMessage, json.RawMessage) error
|
||||
}); ok {
|
||||
if err := native.VerifyScopedResources(ctx, tenantRoot, in.Request, in.Registry); err != nil {
|
||||
return ScopedResult{}, err
|
||||
}
|
||||
} else if _, ok := direct.(*Client); ok {
|
||||
return ScopedResult{}, errors.New("native resource verification is unavailable")
|
||||
}
|
||||
result, err := server.execute(ctx, request)
|
||||
if err != nil {
|
||||
return ScopedResult{}, err
|
||||
}
|
||||
response, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return ScopedResult{}, err
|
||||
}
|
||||
snapshot, err := json.Marshal(server.registry)
|
||||
if err != nil {
|
||||
return ScopedResult{}, err
|
||||
}
|
||||
return ScopedResult{Response: response, Registry: snapshot}, nil
|
||||
}
|
||||
|
||||
// Verify only the resource touched by the operation, using real panel data.
|
||||
// A forged snapshot must not turn an unrelated panel site into a managed site.
|
||||
func (c *Client) VerifyScopedResources(ctx context.Context, root string, rawRequest, rawRegistry json.RawMessage) error {
|
||||
var request brokerRequest
|
||||
var registry brokerRegistry
|
||||
if json.Unmarshal(rawRequest, &request) != nil || json.Unmarshal(rawRegistry, ®istry) != nil {
|
||||
return errors.New("invalid resource proof")
|
||||
}
|
||||
if request.Operation=="delete_ftp"||request.Operation=="set_ftp_state" {
|
||||
name,ok:=registry.FTP[request.ID];if !ok||name!=request.Username{return errOutsideManaged}
|
||||
id,err:=c.lookupResourceID(ctx,"ftps",name);if err!=nil||id!=request.ID{return errors.New("native FTP identity does not match controller scope")};return nil
|
||||
}
|
||||
if request.Operation=="delete_database"||request.Operation=="database_size" {
|
||||
name,ok:=registry.Databases[request.ID];if !ok||name!=request.Name{return errOutsideManaged}
|
||||
id,err:=c.lookupResourceID(ctx,"databases",name);if err!=nil||id!=request.ID{return errors.New("native database identity does not match controller scope")};return nil
|
||||
}
|
||||
var target *managedSite
|
||||
var id int64
|
||||
if request.SiteID != 0 {
|
||||
id = request.SiteID
|
||||
} else if registry.Sites[request.ID] != nil {
|
||||
id = request.ID
|
||||
}
|
||||
if id != 0 {
|
||||
target = registry.Sites[id]
|
||||
}
|
||||
if target == nil && request.Runtime != nil {
|
||||
for key, site := range registry.Sites {
|
||||
if site.Path == request.Runtime.Path && site.Domain == request.Runtime.Domain {
|
||||
target = site
|
||||
id = key
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if target == nil || strings.HasPrefix(request.Operation, "create_") || request.Operation == "adopt_site" || request.Operation == "rollback_adoption" {
|
||||
return nil
|
||||
}
|
||||
// FTP/database IDs may equal a site ID. They are checked by their own name
|
||||
// registry inside the broker; do not accidentally compare unrelated tables.
|
||||
if strings.Contains(request.Operation, "ftp") || strings.Contains(request.Operation, "database") {
|
||||
return nil
|
||||
}
|
||||
actual, err := c.ListUnmanagedSites(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, site := range actual {
|
||||
if site.ID == id {
|
||||
if path.Clean(site.Path) != path.Clean(target.Path) || !strings.HasPrefix(path.Clean(site.Path)+"/", root+"/") || !strings.EqualFold(site.Domain, target.Domain) {
|
||||
return errors.New("native site does not match controller resource scope")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// A repeated delete may already have removed the native record. The broker
|
||||
// still validates the exact registered domain and never deletes by path.
|
||||
if request.Operation == "delete_site" {
|
||||
return nil
|
||||
}
|
||||
return errors.New("managed native site is missing")
|
||||
}
|
||||
Reference in new issue
Block a user