diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-10-16 18:02:07 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-06 15:57:04 +0100 |
commit | b1708565ee2b4d725bb98c6b015f16af3d2a097e (patch) | |
tree | c0c77d3eb5fb035dc1b18e7a8169fc06afaf0343 /lib/folder/folder-pathmap.go | |
parent | 17ab6663d5522bf4db3e6cce70352f8ba4379e99 (diff) |
Add sanity check on pathmap folder creation.
Diffstat (limited to 'lib/folder/folder-pathmap.go')
-rw-r--r-- | lib/folder/folder-pathmap.go | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/lib/folder/folder-pathmap.go b/lib/folder/folder-pathmap.go index 1020026..bc714dd 100644 --- a/lib/folder/folder-pathmap.go +++ b/lib/folder/folder-pathmap.go @@ -24,6 +24,9 @@ type PathMap struct { func NewFolderPathMap(gc *xdsconfig.Config) *PathMap { f := PathMap{ globalConfig: gc, + config: FolderConfig{ + Status: StatusDisable, + }, } return &f } @@ -55,22 +58,43 @@ func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) { if !common.Exists(dir) { return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir) } - file, err := ioutil.TempFile(dir, "xds_pathmap_check") - if err != nil { - return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error()) - } - defer os.Remove(file.Name()) - - msg := "sanity check PathMap Add folder" - n, err := file.Write([]byte(msg)) - if err != nil || n != len(msg) { - return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error()) - } f.config = cfg f.config.RootPath = dir f.config.DataPathMap.ServerPath = dir f.config.IsInSync = true + + // Verify file created by XDS agent when needed + if cfg.DataPathMap.CheckFile != "" { + errMsg := "ServerPath sanity check error (%d): %v" + ckFile := f.ConvPathCli2Svr(cfg.DataPathMap.CheckFile) + if !common.Exists(ckFile) { + return nil, fmt.Errorf(errMsg, 1, "file not present") + } + if cfg.DataPathMap.CheckContent != "" { + fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600) + if err != nil { + return nil, fmt.Errorf(errMsg, 2, err) + } + defer fd.Close() + + // Check specific message written by agent + content, err := ioutil.ReadAll(fd) + if err != nil { + return nil, fmt.Errorf(errMsg, 3, err) + } + if string(content) != cfg.DataPathMap.CheckContent { + return nil, fmt.Errorf(errMsg, 4, "file content differ") + } + + // Write a specific message that will be check back on agent side + msg := "Pathmap checked message written by xds-server ID: " + f.globalConfig.ServerUID + "\n" + if n, err := fd.WriteString(msg); n != len(msg) || err != nil { + return nil, fmt.Errorf(errMsg, 5, err) + } + } + } + f.config.Status = StatusEnable return &f.config, nil |