auth.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package cwapi
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/crowdsecurity/crowdsec/pkg/cwversion"
  10. "github.com/crowdsecurity/crowdsec/pkg/types"
  11. log "github.com/sirupsen/logrus"
  12. "gopkg.in/yaml.v2"
  13. "github.com/dghubble/sling"
  14. "gopkg.in/tomb.v2"
  15. )
  16. type ApiCtx struct {
  17. /*config*/
  18. ApiVersion string `yaml:"version"`
  19. PullPath string `yaml:"pull_path"`
  20. PushPath string `yaml:"push_path"`
  21. SigninPath string `yaml:"signin_path"`
  22. RegisterPath string `yaml:"register_path"`
  23. ResetPwdPath string `yaml:"reset_pwd_path"`
  24. EnrollPath string `yaml:"enroll_path"`
  25. BaseURL string `yaml:"url"`
  26. CfgUser string `yaml:"machine_id"`
  27. CfgPassword string `yaml:"password"`
  28. Creds ApiCreds `yaml:"-"`
  29. Muted bool `yaml:"muted"`
  30. DebugDump bool `yaml:"debug_dump"`
  31. /*runtime*/
  32. tokenExpired bool `yaml:"-"`
  33. toPush []types.Event `yaml:"-"`
  34. Http *sling.Sling `yaml:"-"`
  35. PusherTomb tomb.Tomb
  36. }
  37. type ApiCreds struct {
  38. User string `json:"machine_id" yaml:"machine_id"`
  39. Password string `json:"password" yaml:"password"`
  40. Profile string `json:"profile,omitempty" yaml:"profile,omitempty"`
  41. }
  42. type ApiResp struct {
  43. StatusCode int `json:"statusCode"`
  44. Error string `json:"error"`
  45. Message string `json:"message"`
  46. }
  47. type PullResp struct {
  48. StatusCode int `json:"statusCode"`
  49. Body []map[string]string `json:"message"`
  50. }
  51. func (ctx *ApiCtx) WriteConfig(cfg string) error {
  52. ret, err := yaml.Marshal(ctx)
  53. if err != nil {
  54. return fmt.Errorf("failed to marshal config : %s", err)
  55. }
  56. if err := ioutil.WriteFile(cfg, ret, 0600); err != nil {
  57. return fmt.Errorf("failed to write api file %s : %s", cfg, ret)
  58. }
  59. return nil
  60. }
  61. func (ctx *ApiCtx) LoadConfig(cfg string) error {
  62. rcfg, err := ioutil.ReadFile(cfg)
  63. if err != nil {
  64. return fmt.Errorf("api load configuration: unable to read configuration file '%s' : %s", cfg, err)
  65. }
  66. if err := yaml.UnmarshalStrict(rcfg, &ctx); err != nil {
  67. return fmt.Errorf("api load configuration: unable to unmarshall configuration file '%s' : %s", cfg, err)
  68. }
  69. if ctx.ApiVersion != cwversion.Constraint_api {
  70. return fmt.Errorf("api load configuration: cscli version only supports %s api, not %s", cwversion.Constraint_api, ctx.ApiVersion)
  71. }
  72. ctx.Creds.User = ctx.CfgUser
  73. ctx.Creds.Password = ctx.CfgPassword
  74. /*
  75. For sling, if a path starts with '/', it's an absolute path, and it will get rid of the 'prefix',
  76. leading to bad urls
  77. */
  78. if strings.HasPrefix(ctx.PullPath, "/") ||
  79. strings.HasPrefix(ctx.PushPath, "/") ||
  80. strings.HasPrefix(ctx.SigninPath, "/") ||
  81. strings.HasPrefix(ctx.RegisterPath, "/") ||
  82. strings.HasPrefix(ctx.ResetPwdPath, "/") ||
  83. strings.HasPrefix(ctx.EnrollPath, "/") {
  84. log.Warningf("!API paths must not be prefixed by /")
  85. }
  86. ctx.Http = sling.New().Base(ctx.BaseURL+"/"+ctx.ApiVersion+"/").Set("User-Agent", fmt.Sprintf("CrowdWatch/%s", cwversion.VersionStr()))
  87. log.Printf("api load configuration: configuration loaded successfully (base:%s)", ctx.BaseURL+"/"+ctx.ApiVersion+"/")
  88. return nil
  89. }
  90. func (ctx *ApiCtx) Init(cfg string, profile string) error {
  91. var err error
  92. err = ctx.LoadConfig(cfg)
  93. if err != nil {
  94. return err
  95. }
  96. ctx.Creds.Profile = profile
  97. ctx.toPush = make([]types.Event, 0)
  98. err = ctx.Signin()
  99. if err != nil {
  100. return err
  101. }
  102. //start the background go-routine
  103. ctx.PusherTomb.Go(func() error {
  104. err := ctx.pushLoop()
  105. if err != nil {
  106. log.Errorf("api push error : %s", err)
  107. return err
  108. }
  109. return nil
  110. })
  111. return nil
  112. }
  113. func (ctx *ApiCtx) Shutdown() error {
  114. ctx.PusherTomb.Kill(nil)
  115. log.Infof("Waiting for API routine to finish")
  116. if err := ctx.PusherTomb.Wait(); err != nil {
  117. return fmt.Errorf("API routine returned error : %s", err)
  118. }
  119. return nil
  120. }
  121. func (ctx *ApiCtx) Signin() error {
  122. if ctx.Creds.User == "" || ctx.Creds.Password == "" {
  123. return fmt.Errorf("api signin: missing credentials in api.yaml")
  124. }
  125. req, err := ctx.Http.New().Post(ctx.SigninPath).BodyJSON(ctx.Creds).Request()
  126. if err != nil {
  127. return fmt.Errorf("api signin: HTTP request creation failed: %s", err)
  128. }
  129. log.Debugf("api signin: URL: '%s'", req.URL)
  130. httpClient := http.Client{Timeout: 20 * time.Second}
  131. resp, err := httpClient.Do(req)
  132. if err != nil {
  133. return fmt.Errorf("api signin: API call failed : %s", err)
  134. }
  135. defer resp.Body.Close()
  136. body, err := ioutil.ReadAll(resp.Body)
  137. if err != nil {
  138. return fmt.Errorf("api signin: unable to read API response body: '%s'", err)
  139. }
  140. if resp.StatusCode != 200 {
  141. return fmt.Errorf("api signin: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
  142. }
  143. jsonResp := ApiResp{}
  144. err = json.Unmarshal(body, &jsonResp)
  145. if err != nil {
  146. return fmt.Errorf("api signin: unable to unmarshall api response '%s': %s", string(body), err.Error())
  147. }
  148. ctx.Http = ctx.Http.Set("Authorization", jsonResp.Message)
  149. log.Printf("api signin: signed in successfuly")
  150. return nil
  151. }
  152. func (ctx *ApiCtx) RegisterMachine(machineID string, password string) error {
  153. ctx.Creds.User = machineID
  154. ctx.Creds.Password = password
  155. req, err := ctx.Http.New().Post(ctx.RegisterPath).BodyJSON(ctx.Creds).Request()
  156. if err != nil {
  157. return fmt.Errorf("api register machine: HTTP request creation failed: %s", err)
  158. }
  159. log.Debugf("api register: URL: '%s'", req.URL)
  160. httpClient := http.Client{Timeout: 20 * time.Second}
  161. resp, err := httpClient.Do(req)
  162. if err != nil {
  163. return fmt.Errorf("api register machine: API call failed : %s", err)
  164. }
  165. defer resp.Body.Close()
  166. body, err := ioutil.ReadAll(resp.Body)
  167. if err != nil {
  168. return fmt.Errorf("api register machine: unable to read API response body: %s", err.Error())
  169. }
  170. if resp.StatusCode != 200 {
  171. return fmt.Errorf("api register machine: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
  172. }
  173. jsonResp := ApiResp{}
  174. err = json.Unmarshal(body, &jsonResp)
  175. if err != nil {
  176. return fmt.Errorf("api register machine: unable to unmarshall api response '%s': %s", string(body), err.Error())
  177. }
  178. return nil
  179. }
  180. func (ctx *ApiCtx) ResetPassword(machineID string, password string) error {
  181. ctx.Creds.User = machineID
  182. ctx.Creds.Password = password
  183. data := map[string]string{"machine_id": ctx.Creds.User, "password": ctx.Creds.Password}
  184. req, err := ctx.Http.New().Post(ctx.ResetPwdPath).BodyJSON(data).Request()
  185. if err != nil {
  186. return fmt.Errorf("api reset password: HTTP request creation failed: %s", err)
  187. }
  188. log.Debugf("api reset: URL: '%s'", req.URL)
  189. httpClient := http.Client{Timeout: 20 * time.Second}
  190. resp, err := httpClient.Do(req)
  191. if err != nil {
  192. return fmt.Errorf("api reset password: API call failed : %s", err)
  193. }
  194. defer resp.Body.Close()
  195. body, err := ioutil.ReadAll(resp.Body)
  196. if err != nil {
  197. return fmt.Errorf("api reset password: unable to read API response body: %s", err.Error())
  198. }
  199. if resp.StatusCode != 200 {
  200. return fmt.Errorf("api reset password: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
  201. }
  202. jsonResp := ApiResp{}
  203. err = json.Unmarshal(body, &jsonResp)
  204. if err != nil {
  205. return fmt.Errorf("api reset password: unable to unmarshall api response '%s': %s", string(body), err.Error())
  206. }
  207. if jsonResp.StatusCode != 200 {
  208. return fmt.Errorf("api reset password: return bad HTTP code (%d): %s", jsonResp.StatusCode, string(body))
  209. }
  210. return nil
  211. }