summaryrefslogtreecommitdiffstats
path: root/lib/agent
diff options
context:
space:
mode:
Diffstat (limited to 'lib/agent')
-rw-r--r--lib/agent/project-pathmap.go58
-rw-r--r--lib/agent/xdsserver.go12
2 files changed, 62 insertions, 8 deletions
diff --git a/lib/agent/project-pathmap.go b/lib/agent/project-pathmap.go
index 6c49d6a..aacbd1f 100644
--- a/lib/agent/project-pathmap.go
+++ b/lib/agent/project-pathmap.go
@@ -1,5 +1,14 @@
package agent
+import (
+ "fmt"
+ "io/ioutil"
+ "os"
+ "strings"
+
+ common "github.com/iotbzh/xds-common/golib"
+)
+
// IPROJECT interface implementation for native/path mapping projects
// PathMap .
@@ -22,12 +31,55 @@ func NewProjectPathMap(ctx *Context, svr *XdsServer) *PathMap {
// Add a new project
func (p *PathMap) Add(cfg ProjectConfig) (*ProjectConfig, error) {
var err error
+ var file *os.File
+ errMsg := "ClientPath sanity check error (%d): %v"
- // SEB TODO: check local/server directory access
+ // Sanity check to verify that we have RW permission and path-mapping is correct
+ dir := cfg.ClientPath
+ if !common.Exists(dir) {
+ // try to create if not existing
+ if err := os.MkdirAll(dir, 0755); err != nil {
+ return nil, fmt.Errorf("Cannot create ClientPath directory: %s", dir)
+ }
+ }
+ if !common.Exists(dir) {
+ return nil, fmt.Errorf("ClientPath directory is not accessible: %s", dir)
+ }
+ if file, err = ioutil.TempFile(dir, ".xds_pathmap_check"); err != nil {
+ return nil, fmt.Errorf(errMsg, 1, err)
+ }
+ // Write a specific message that will be check by server during folder add
+ msg := "Pathmap checked message written by xds-agent ID: " + p.Config.AgentUID + "\n"
+ if n, err := file.WriteString(msg); n != len(msg) || err != nil {
+ return nil, fmt.Errorf(errMsg, 2, err)
+ }
+ defer func() {
+ if file != nil {
+ os.Remove(file.Name())
+ file.Close()
+ }
+ }()
- err = p.server.FolderAdd(p.server.ProjectToFolder(cfg), p.folder)
+ // Convert to Xds folder
+ fld := p.server.ProjectToFolder(cfg)
+ fld.DataPathMap.CheckFile = file.Name()
+ fld.DataPathMap.CheckContent = msg
+
+ // Send request to create folder on XDS server side
+ err = p.server.FolderAdd(fld, p.folder)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("Folders mapping verification failure.\n%v", err)
+ }
+
+ // 2nd part of sanity checker
+ // check specific message added by XDS Server during folder add processing
+ content, err := ioutil.ReadFile(file.Name())
+ if err != nil {
+ return nil, fmt.Errorf(errMsg, 3, err)
+ }
+ if !strings.Contains(string(content),
+ "Pathmap checked message written by xds-server ID") {
+ return nil, fmt.Errorf(errMsg, 4, "file content differ")
}
return p.GetProject(), nil
diff --git a/lib/agent/xdsserver.go b/lib/agent/xdsserver.go
index c900c9e..b76908c 100644
--- a/lib/agent/xdsserver.go
+++ b/lib/agent/xdsserver.go
@@ -84,7 +84,9 @@ type XdsFolderConfig struct {
// XdsPathMapConfig Path mapping specific data
type XdsPathMapConfig struct {
- ServerPath string `json:"serverPath"`
+ ServerPath string `json:"serverPath"`
+ CheckFile string `json:"checkFile"`
+ CheckContent string `json:"checkContent"`
}
// XdsCloudSyncConfig CloudSync (AKA Syncthing) specific data
@@ -205,13 +207,13 @@ func (xs *XdsServer) GetVersion(res interface{}) error {
}
// GetFolders Send GET request to get current folder configuration
-func (xs *XdsServer) GetFolders(prjs *[]XdsFolderConfig) error {
- return xs._HTTPGet("/folders", prjs)
+func (xs *XdsServer) GetFolders(folders *[]XdsFolderConfig) error {
+ return xs._HTTPGet("/folders", folders)
}
// FolderAdd Send POST request to add a folder
-func (xs *XdsServer) FolderAdd(prj *XdsFolderConfig, res interface{}) error {
- response, err := xs._HTTPPost("/folder", prj)
+func (xs *XdsServer) FolderAdd(fld *XdsFolderConfig, res interface{}) error {
+ response, err := xs._HTTPPost("/folder", fld)
if err != nil {
return err
}