From: Sebastien Douheret Date: Mon, 26 Jun 2017 16:53:06 +0000 (+0200) Subject: Use xds-common go library. X-Git-Tag: 0.1.0~11 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-agent.git;a=commitdiff_plain;h=36e2400d1c0249e81f910e48a967e0202d5a8d46 Use xds-common go library. --- diff --git a/glide.yaml b/glide.yaml index 794de5d..3661d8f 100644 --- a/glide.yaml +++ b/glide.yaml @@ -8,7 +8,11 @@ import: version: ^1.1.4 - package: github.com/gin-contrib/static - package: github.com/syncthing/syncthing - version: ^0.14.27-rc.2 + version: =0.14.28 + subpackages: + - lib/sync + - lib/config + - lib/protocol - package: github.com/codegangsta/cli version: ^1.19.1 - package: github.com/Sirupsen/logrus @@ -17,3 +21,6 @@ import: - package: github.com/zhouhui8915/go-socket.io-client - package: github.com/satori/go.uuid version: ^1.1.0 +- package: github.com/iotbzh/xds-common + subpackages: + - golib/common diff --git a/lib/apiv1/config.go b/lib/apiv1/config.go index 79225f4..47155ed 100644 --- a/lib/apiv1/config.go +++ b/lib/apiv1/config.go @@ -5,8 +5,8 @@ import ( "sync" "github.com/gin-gonic/gin" - "github.com/iotbzh/xds-agent/lib/common" "github.com/iotbzh/xds-agent/lib/xdsconfig" + common "github.com/iotbzh/xds-common/golib" ) var confMut sync.Mutex diff --git a/lib/common/error.go b/lib/common/error.go deleted file mode 100644 index d03c176..0000000 --- a/lib/common/error.go +++ /dev/null @@ -1,13 +0,0 @@ -package common - -import ( - "github.com/gin-gonic/gin" -) - -// APIError returns an uniform json formatted error -func APIError(c *gin.Context, err string) { - c.JSON(500, gin.H{ - "status": "error", - "error": err, - }) -} diff --git a/lib/common/filepath.go b/lib/common/filepath.go deleted file mode 100644 index d9cb3d5..0000000 --- a/lib/common/filepath.go +++ /dev/null @@ -1,65 +0,0 @@ -package common - -import ( - "fmt" - "os" - "os/user" - "path" - "path/filepath" - "regexp" - "runtime" -) - -// Exists returns whether the given file or directory exists or not -func Exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true - } - if os.IsNotExist(err) { - return false - } - return true -} - -// ResolveEnvVar Resolved environment variable regarding the syntax ${MYVAR} -// or $MYVAR following by a slash or a backslash -func ResolveEnvVar(s string) (string, error) { - if s == "" { - return s, nil - } - - // Resolved tilde : ~/ - if len(s) > 2 && s[:2] == "~/" { - if usr, err := user.Current(); err == nil { - s = filepath.Join(usr.HomeDir, s[2:]) - } - } - - // Resolved ${MYVAR} - re := regexp.MustCompile("\\${([^}]+)}") - vars := re.FindAllStringSubmatch(s, -1) - res := s - for _, v := range vars { - val := os.Getenv(v[1]) - if val == "" { - // Specific case to resolved $HOME or ${HOME} on Windows host - if runtime.GOOS == "windows" && v[1] == "HOME" { - if usr, err := user.Current(); err == nil { - val = usr.HomeDir - } - } else { - return res, fmt.Errorf("ERROR: %s env variable not defined", v[1]) - } - } - - rer := regexp.MustCompile("\\${" + v[1] + "}") - res = rer.ReplaceAllString(res, val) - } - - // Resolved $MYVAR following by a slash (or a backslash for Windows) - // TODO - //re := regexp.MustCompile("\\$([^\\/])+/") - - return path.Clean(res), nil -} diff --git a/lib/common/httpclient.go b/lib/common/httpclient.go deleted file mode 100644 index 72132bf..0000000 --- a/lib/common/httpclient.go +++ /dev/null @@ -1,257 +0,0 @@ -package common - -import ( - "bytes" - "crypto/tls" - "encoding/json" - "errors" - "fmt" - "io/ioutil" - "net/http" - "strings" - - "github.com/Sirupsen/logrus" -) - -type HTTPClient struct { - httpClient http.Client - endpoint string - apikey string - username string - password string - id string - csrf string - conf HTTPClientConfig - logger *logrus.Logger -} - -type HTTPClientConfig struct { - URLPrefix string - HeaderAPIKeyName string - Apikey string - HeaderClientKeyName string - CsrfDisable bool -} - -const ( - logError = 1 - logWarning = 2 - logInfo = 3 - logDebug = 4 -) - -// Inspired by syncthing/cmd/cli - -const insecure = false - -// HTTPNewClient creates a new HTTP client to deal with Syncthing -func HTTPNewClient(baseURL string, cfg HTTPClientConfig) (*HTTPClient, error) { - - // Create w new Http client - httpClient := http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: insecure, - }, - }, - } - client := HTTPClient{ - httpClient: httpClient, - endpoint: baseURL, - apikey: cfg.Apikey, - conf: cfg, - /* TODO - add user + pwd support - username: c.GlobalString("username"), - password: c.GlobalString("password"), - */ - } - - if client.apikey == "" { - if err := client.getCidAndCsrf(); err != nil { - return nil, err - } - } - return &client, nil -} - -// SetLogger Define the logger to use -func (c *HTTPClient) SetLogger(log *logrus.Logger) { - c.logger = log -} - -func (c *HTTPClient) log(level int, format string, args ...interface{}) { - if c.logger != nil { - switch level { - case logError: - c.logger.Errorf(format, args...) - break - case logWarning: - c.logger.Warningf(format, args...) - break - case logInfo: - c.logger.Infof(format, args...) - break - default: - c.logger.Debugf(format, args...) - break - } - } -} - -// Send request to retrieve Client id and/or CSRF token -func (c *HTTPClient) getCidAndCsrf() error { - request, err := http.NewRequest("GET", c.endpoint, nil) - if err != nil { - return err - } - if _, err := c.handleRequest(request); err != nil { - return err - } - if c.id == "" { - return errors.New("Failed to get device ID") - } - if !c.conf.CsrfDisable && c.csrf == "" { - return errors.New("Failed to get CSRF token") - } - return nil -} - -// GetClientID returns the id -func (c *HTTPClient) GetClientID() string { - return c.id -} - -// formatURL Build full url by concatenating all parts -func (c *HTTPClient) formatURL(endURL string) string { - url := c.endpoint - if !strings.HasSuffix(url, "/") { - url += "/" - } - url += strings.TrimLeft(c.conf.URLPrefix, "/") - if !strings.HasSuffix(url, "/") { - url += "/" - } - return url + strings.TrimLeft(endURL, "/") -} - -// HTTPGet Send a Get request to client and return an error object -func (c *HTTPClient) HTTPGet(url string, data *[]byte) error { - _, err := c.HTTPGetWithRes(url, data) - return err -} - -// HTTPGetWithRes Send a Get request to client and return both response and error -func (c *HTTPClient) HTTPGetWithRes(url string, data *[]byte) (*http.Response, error) { - request, err := http.NewRequest("GET", c.formatURL(url), nil) - if err != nil { - return nil, err - } - res, err := c.handleRequest(request) - if err != nil { - return res, err - } - if res.StatusCode != 200 { - return res, errors.New(res.Status) - } - - *data = c.responseToBArray(res) - - return res, nil -} - -// HTTPPost Send a POST request to client and return an error object -func (c *HTTPClient) HTTPPost(url string, body string) error { - _, err := c.HTTPPostWithRes(url, body) - return err -} - -// HTTPPostWithRes Send a POST request to client and return both response and error -func (c *HTTPClient) HTTPPostWithRes(url string, body string) (*http.Response, error) { - request, err := http.NewRequest("POST", c.formatURL(url), bytes.NewBufferString(body)) - if err != nil { - return nil, err - } - res, err := c.handleRequest(request) - if err != nil { - return res, err - } - if res.StatusCode != 200 { - return res, errors.New(res.Status) - } - return res, nil -} - -func (c *HTTPClient) responseToBArray(response *http.Response) []byte { - defer response.Body.Close() - bytes, err := ioutil.ReadAll(response.Body) - if err != nil { - // TODO improved error reporting - fmt.Println("ERROR: " + err.Error()) - } - return bytes -} - -func (c *HTTPClient) handleRequest(request *http.Request) (*http.Response, error) { - if c.conf.HeaderAPIKeyName != "" && c.apikey != "" { - request.Header.Set(c.conf.HeaderAPIKeyName, c.apikey) - } - if c.conf.HeaderClientKeyName != "" && c.id != "" { - request.Header.Set(c.conf.HeaderClientKeyName, c.id) - } - if c.username != "" || c.password != "" { - request.SetBasicAuth(c.username, c.password) - } - if c.csrf != "" { - request.Header.Set("X-CSRF-Token-"+c.id[:5], c.csrf) - } - - c.log(logDebug, "HTTP %s %v", request.Method, request.URL) - - response, err := c.httpClient.Do(request) - if err != nil { - return nil, err - } - - // Detect client ID change - cid := response.Header.Get(c.conf.HeaderClientKeyName) - if cid != "" && c.id != cid { - c.id = cid - } - - // Detect CSR token change - for _, item := range response.Cookies() { - if item.Name == "CSRF-Token-"+c.id[:5] { - c.csrf = item.Value - goto csrffound - } - } - // OK CSRF found -csrffound: - - if response.StatusCode == 404 { - return nil, errors.New("Invalid endpoint or API call") - } else if response.StatusCode == 401 { - return nil, errors.New("Invalid username or password") - } else if response.StatusCode == 403 { - if c.apikey == "" { - // Request a new Csrf for next requests - c.getCidAndCsrf() - return nil, errors.New("Invalid CSRF token") - } - return nil, errors.New("Invalid API key") - } else if response.StatusCode != 200 { - data := make(map[string]interface{}) - // Try to decode error field of APIError struct - json.Unmarshal(c.responseToBArray(response), &data) - if err, found := data["error"]; found { - return nil, fmt.Errorf(err.(string)) - } else { - body := strings.TrimSpace(string(c.responseToBArray(response))) - if body != "" { - return nil, fmt.Errorf(body) - } - } - return nil, errors.New("Unknown HTTP status returned: " + response.Status) - } - return response, nil -} diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 8fd50d3..88d0eb0 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -17,8 +17,8 @@ import ( "os/exec" "github.com/Sirupsen/logrus" - "github.com/iotbzh/xds-agent/lib/common" "github.com/iotbzh/xds-agent/lib/xdsconfig" + common "github.com/iotbzh/xds-common/golib" "github.com/syncthing/syncthing/lib/config" ) @@ -185,7 +185,7 @@ func (s *SyncThing) Start() (*exec.Cmd, error) { time.Sleep(500 * time.Millisecond) if common.Exists(stConfigFile) { break - } + } } if tmo <= 0 { return nil, fmt.Errorf("Cannot start Syncthing for config file creation") diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 5ac2ffa..7d762c7 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -7,7 +7,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" - "github.com/iotbzh/xds-agent/lib/common" + common "github.com/iotbzh/xds-common/golib" ) // Config parameters (json format) of /config command diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 3c834fc..add6ef7 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -7,7 +7,7 @@ import ( "path" "path/filepath" - "github.com/iotbzh/xds-agent/lib/common" + common "github.com/iotbzh/xds-common/golib" ) type SyncThingConf struct {