diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-10-16 18:17:44 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-10-16 18:17:44 +0200 |
commit | 77ab312b87324090ec6cb619d11f792b53fdddbd (patch) | |
tree | 194cb312f831eabf9a5f1cd93fa7f5c13274fa50 /lib | |
parent | 4695555e178bcabe54c5bf82117c9c4cef5440b5 (diff) |
Add sanity check when adding a new pathmap project/folder
Diffstat (limited to 'lib')
-rw-r--r-- | lib/agent/project-pathmap.go | 58 | ||||
-rw-r--r-- | lib/agent/xdsserver.go | 12 | ||||
-rw-r--r-- | lib/xdsconfig/config.go | 3 |
3 files changed, 64 insertions, 9 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 } diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 9279a67..9e44070 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -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) |