Add sanity check when adding a new pathmap project/folder
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 16 Oct 2017 16:17:44 +0000 (18:17 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 16 Oct 2017 16:17:44 +0000 (18:17 +0200)
lib/agent/project-pathmap.go
lib/agent/xdsserver.go
lib/xdsconfig/config.go
webapp/src/app/projects/projectAddModal.component.ts
webapp/src/app/services/alert.service.ts

index 6c49d6a..aacbd1f 100644 (file)
@@ -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
index c900c9e..b76908c 100644 (file)
@@ -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
        }
index 9279a67..9e44070 100644 (file)
@@ -78,6 +78,8 @@ func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
                Log: log,
        }
 
+       c.Log.Infoln("Agent UUID:     ", uuid)
+
        // config file settings overwrite default config
        err = readGlobalConfig(&c, c.Options.ConfigFile)
        if err != nil {
@@ -113,7 +115,6 @@ func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
                }
        }
 
-       c.Log.Infoln("Agent UUID:     ", uuid)
        c.Log.Infoln("Logs file:      ", c.Options.LogFile)
        c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
 
index 1584b5b..1352060 100644 (file)
@@ -139,7 +139,7 @@ export class ProjectAddModalComponent {
 
             },
             err => {
-                this.alert.error("Configuration ERROR: " + err, 60);
+                this.alert.error(err, 60);
                 this.hide();
             });
     }
index c3cae7a..2978e84 100644 (file)
@@ -45,10 +45,11 @@ export class AlertService {
     }
 
     public add(al: IAlert) {
+        let msg = String(al.msg).replace("\n", "<br>");
         this._alerts.push({
             show: true,
             type: al.type,
-            msg: this.sanitizer.sanitize(SecurityContext.HTML, al.msg),
+            msg: this.sanitizer.sanitize(SecurityContext.HTML, msg),
             dismissible: al.dismissible || true,
             dismissTimeout: (al.dismissTimeout * 1000) || 0,
             id: this.uid,