httppull_source.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package http
  15. import (
  16. "crypto/md5"
  17. "encoding/hex"
  18. "encoding/json"
  19. "fmt"
  20. "github.com/lf-edge/ekuiper/internal/io/mock"
  21. "github.com/lf-edge/ekuiper/internal/pkg/cert"
  22. "github.com/lf-edge/ekuiper/pkg/infra"
  23. "io"
  24. "net/http"
  25. "strings"
  26. "time"
  27. "github.com/lf-edge/ekuiper/internal/conf"
  28. "github.com/lf-edge/ekuiper/internal/pkg/httpx"
  29. "github.com/lf-edge/ekuiper/pkg/api"
  30. "github.com/lf-edge/ekuiper/pkg/cast"
  31. )
  32. const DefaultInterval = 10000
  33. const DefaultTimeout = 5000
  34. type HTTPPullConf struct {
  35. Url string `json:"url"`
  36. Method string `json:"method"`
  37. Body string `json:"body"`
  38. BodyType string `json:"bodyType"`
  39. Headers map[string]string `json:"headers"`
  40. InsecureSkipVerify bool `json:"insecureSkipVerify"`
  41. CertificationPath string `json:"certificationPath"`
  42. PrivateKeyPath string `json:"privateKeyPath"`
  43. RootCaPath string `json:"rootCaPath"`
  44. Timeout int `json:"timeout"`
  45. // Could be code or body
  46. ResponseType string `json:"responseType"`
  47. OAuth map[string]map[string]interface{} `json:"oauth"`
  48. // Pull specific properties
  49. Interval int `json:"interval"`
  50. Incremental bool `json:"incremental"`
  51. }
  52. type AccessTokenConf struct {
  53. Url string `json:"url"`
  54. Body string `json:"body"`
  55. Expire string `json:"expire"`
  56. ExpireInSecond int
  57. }
  58. type RefreshTokenConf struct {
  59. Url string `json:"url"`
  60. Headers map[string]string `json:"headers"`
  61. Body string `json:"body"`
  62. }
  63. type HTTPPullSource struct {
  64. config *HTTPPullConf
  65. accessConf *AccessTokenConf
  66. refreshConf *RefreshTokenConf
  67. tokens map[string]interface{}
  68. client *http.Client
  69. }
  70. type bodyResp struct {
  71. Code int `json:"code"`
  72. }
  73. var bodyTypeMap = map[string]string{"none": "", "text": "text/plain", "json": "application/json", "html": "text/html", "xml": "application/xml", "javascript": "application/javascript", "form": ""}
  74. func (hps *HTTPPullSource) Configure(device string, props map[string]interface{}) error {
  75. conf.Log.Infof("Initialized Httppull source with configurations %#v.", props)
  76. c := &HTTPPullConf{
  77. Url: "http://localhost",
  78. Method: http.MethodGet,
  79. Interval: DefaultInterval,
  80. Timeout: DefaultTimeout,
  81. BodyType: "json",
  82. InsecureSkipVerify: true,
  83. ResponseType: "code",
  84. Headers: map[string]string{},
  85. }
  86. if err := cast.MapToStruct(props, c); err != nil {
  87. return fmt.Errorf("fail to parse the properties: %v", err)
  88. }
  89. if c.Url == "" {
  90. return fmt.Errorf("url is required")
  91. }
  92. c.Url = c.Url + device
  93. switch strings.ToUpper(c.Method) {
  94. case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete:
  95. c.Method = strings.ToUpper(c.Method)
  96. default:
  97. return fmt.Errorf("Not supported HTTP method %s.", c.Method)
  98. }
  99. if c.Interval <= 0 {
  100. return fmt.Errorf("interval must be greater than 0")
  101. }
  102. if c.Timeout < 0 {
  103. return fmt.Errorf("timeout must be greater than or equal to 0")
  104. }
  105. if _, ok2 := bodyTypeMap[strings.ToLower(c.BodyType)]; ok2 {
  106. c.BodyType = strings.ToLower(c.BodyType)
  107. } else {
  108. return fmt.Errorf("Not valid body type value %v.", c.BodyType)
  109. }
  110. switch c.ResponseType {
  111. case "code", "body":
  112. // correct
  113. default:
  114. return fmt.Errorf("Not valid response type value %v.", c.ResponseType)
  115. }
  116. err := httpx.IsHttpUrl(c.Url)
  117. if err != nil {
  118. return err
  119. }
  120. tlsOpts := cert.TlsConfigurationOptions{
  121. SkipCertVerify: c.InsecureSkipVerify,
  122. CertFile: c.CertificationPath,
  123. KeyFile: c.PrivateKeyPath,
  124. CaFile: c.RootCaPath,
  125. }
  126. tlscfg, err := cert.GenerateTLSForClient(tlsOpts)
  127. if err != nil {
  128. return err
  129. }
  130. // validate oAuth
  131. if c.OAuth != nil {
  132. // validate access token
  133. if ap, ok := c.OAuth["access"]; ok {
  134. accessConf := &AccessTokenConf{}
  135. if err := cast.MapToStruct(ap, accessConf); err != nil {
  136. return fmt.Errorf("fail to parse the access properties of oAuth: %v", err)
  137. }
  138. if accessConf.Url == "" {
  139. return fmt.Errorf("access token url is required")
  140. }
  141. // expire time will update every time when access token is refreshed if expired is set
  142. hps.accessConf = accessConf
  143. } else {
  144. return fmt.Errorf("if setting oAuth, `access` property is required")
  145. }
  146. // validate refresh token, it is optional
  147. if rp, ok := c.OAuth["refresh"]; ok {
  148. refreshConf := &RefreshTokenConf{}
  149. if err := cast.MapToStruct(rp, refreshConf); err != nil {
  150. return fmt.Errorf("fail to parse the refresh token properties: %v", err)
  151. }
  152. if refreshConf.Url == "" {
  153. return fmt.Errorf("refresh token url is required")
  154. }
  155. hps.refreshConf = refreshConf
  156. }
  157. }
  158. tr := &http.Transport{
  159. TLSClientConfig: tlscfg,
  160. }
  161. hps.client = &http.Client{
  162. Transport: tr,
  163. Timeout: time.Duration(c.Timeout) * time.Millisecond,
  164. }
  165. hps.config = c
  166. // try to get access token
  167. if hps.accessConf != nil {
  168. conf.Log.Infof("Try to get access token from %s", hps.accessConf.Url)
  169. ctx := mock.NewMockContext("none", "httppull_init")
  170. hps.tokens = make(map[string]interface{})
  171. err := hps.auth(ctx)
  172. if err != nil {
  173. return fmt.Errorf("fail to authorize by oAuth: %v", err)
  174. }
  175. }
  176. return nil
  177. }
  178. func (hps *HTTPPullSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  179. ctx.GetLogger().Infof("Opening HTTP pull source with conf %+v", hps.config)
  180. // trigger refresh token timer
  181. if hps.accessConf != nil && hps.accessConf.ExpireInSecond > 0 {
  182. go infra.SafeRun(func() error {
  183. ctx.GetLogger().Infof("Starting refresh token for %d seconds", hps.accessConf.ExpireInSecond/2)
  184. ticker := time.NewTicker(time.Duration(hps.accessConf.ExpireInSecond/2) * time.Second)
  185. defer ticker.Stop()
  186. for {
  187. select {
  188. case <-ticker.C:
  189. ctx.GetLogger().Debugf("Refreshing token")
  190. hps.refresh(ctx)
  191. case <-ctx.Done():
  192. ctx.GetLogger().Infof("Closing refresh token timer")
  193. return nil
  194. }
  195. }
  196. })
  197. }
  198. hps.initTimerPull(ctx, consumer, errCh)
  199. }
  200. func (hps *HTTPPullSource) Close(ctx api.StreamContext) error {
  201. logger := ctx.GetLogger()
  202. logger.Infof("Closing HTTP pull source")
  203. return nil
  204. }
  205. // initialize the oAuth access token
  206. func (hps *HTTPPullSource) auth(ctx api.StreamContext) error {
  207. if resp, e := httpx.Send(conf.Log, hps.client, "json", http.MethodPost, hps.accessConf.Url, nil, true, []byte(hps.accessConf.Body)); e == nil {
  208. conf.Log.Infof("try to get access token got response %v", resp)
  209. hps.tokens, e = parseResponse(ctx, resp, hps.config.ResponseType, false, nil)
  210. if e != nil {
  211. return fmt.Errorf("Cannot parse access token response to json: %v", e)
  212. }
  213. ctx.GetLogger().Infof("Got access token %v", hps.tokens)
  214. expireIn, err := ctx.ParseTemplate(hps.accessConf.Expire, hps.tokens)
  215. if err != nil {
  216. return fmt.Errorf("fail to parse the expire time for access token: %v", err)
  217. }
  218. hps.accessConf.ExpireInSecond, err = cast.ToInt(expireIn, cast.CONVERT_ALL)
  219. if err != nil {
  220. return fmt.Errorf("fail to covert the expire time %s for access token: %v", expireIn, err)
  221. }
  222. if hps.refreshConf != nil {
  223. err := hps.refresh(ctx)
  224. if err != nil {
  225. return err
  226. }
  227. }
  228. } else {
  229. return fmt.Errorf("fail to get access token: %v", e)
  230. }
  231. return nil
  232. }
  233. func (hps *HTTPPullSource) refresh(ctx api.StreamContext) error {
  234. if hps.refreshConf != nil {
  235. headers := make(map[string]string, len(hps.refreshConf.Headers))
  236. var err error
  237. for k, v := range hps.refreshConf.Headers {
  238. headers[k], err = ctx.ParseTemplate(v, hps.tokens)
  239. if err != nil {
  240. return fmt.Errorf("fail to parse the header for refresh token request %s: %v", k, err)
  241. }
  242. }
  243. rr, ee := httpx.Send(conf.Log, hps.client, "json", http.MethodPost, hps.refreshConf.Url, headers, true, []byte(hps.accessConf.Body))
  244. if ee != nil {
  245. return fmt.Errorf("fail to get refresh token: %v", ee)
  246. }
  247. hps.tokens, err = parseResponse(ctx, rr, hps.config.ResponseType, false, nil)
  248. if err != nil {
  249. return fmt.Errorf("Cannot parse refresh token response to json: %v", err)
  250. }
  251. return nil
  252. } else if hps.accessConf != nil {
  253. return hps.auth(ctx)
  254. } else {
  255. return fmt.Errorf("no oAuth config")
  256. }
  257. }
  258. func (hps *HTTPPullSource) initTimerPull(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  259. logger := ctx.GetLogger()
  260. logger.Infof("Starting HTTP pull source with interval %d", hps.config.Interval)
  261. ticker := time.NewTicker(time.Millisecond * time.Duration(hps.config.Interval))
  262. defer ticker.Stop()
  263. var omd5 = ""
  264. headers := make(map[string]string, len(hps.config.Headers))
  265. var err error
  266. for {
  267. select {
  268. case <-ticker.C:
  269. for k, v := range hps.config.Headers {
  270. headers[k], err = ctx.ParseTemplate(v, hps.tokens)
  271. if err != nil {
  272. logger.Errorf("fail to parse the header for refresh token request %s: %v", k, err)
  273. break
  274. }
  275. }
  276. if err != nil {
  277. continue
  278. }
  279. ctx.GetLogger().Debugf("rest sink sending request url: %s, headers: %v, body %s", hps.config.Url, headers, hps.config.Body)
  280. if resp, e := httpx.Send(logger, hps.client, hps.config.BodyType, hps.config.Method, hps.config.Url, headers, true, []byte(hps.config.Body)); e != nil {
  281. logger.Warnf("Found error %s when trying to reach %v ", e, hps)
  282. } else {
  283. logger.Debugf("rest sink got response %v", resp)
  284. result, e := parseResponse(ctx, resp, hps.config.ResponseType, hps.config.Incremental, &omd5)
  285. if e != nil {
  286. logger.Errorf("Parse response error %v", e)
  287. continue
  288. }
  289. if result == nil {
  290. logger.Debugf("no data to send for incremental")
  291. continue
  292. }
  293. meta := make(map[string]interface{})
  294. select {
  295. case consumer <- api.NewDefaultSourceTuple(result, meta):
  296. logger.Debugf("send data to device node")
  297. case <-ctx.Done():
  298. return
  299. }
  300. }
  301. case <-ctx.Done():
  302. return
  303. }
  304. }
  305. }
  306. func getMD5Hash(text []byte) string {
  307. hash := md5.Sum(text)
  308. return hex.EncodeToString(hash[:])
  309. }
  310. func parseResponse(ctx api.StreamContext, resp *http.Response, responseType string, isIncremental bool, omd5 *string) (map[string]interface{}, error) {
  311. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  312. return nil, fmt.Errorf("http return code error: %d", resp.StatusCode)
  313. }
  314. c, err := io.ReadAll(resp.Body)
  315. if err != nil {
  316. return nil, err
  317. }
  318. defer resp.Body.Close()
  319. if isIncremental {
  320. nmd5 := getMD5Hash(c)
  321. if *omd5 == nmd5 {
  322. ctx.GetLogger().Debugf("Content has not changed since last fetch, so skip processing.")
  323. return nil, nil
  324. } else {
  325. *omd5 = nmd5
  326. }
  327. }
  328. switch responseType {
  329. case "code":
  330. return decode(ctx, c)
  331. case "body":
  332. payload, err := decode(ctx, c)
  333. if err != nil {
  334. return nil, err
  335. }
  336. ro := &bodyResp{}
  337. err = cast.MapToStruct(payload, ro)
  338. if err != nil {
  339. return nil, fmt.Errorf("invalid body response: %v", err)
  340. }
  341. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  342. return nil, fmt.Errorf("http status code is not 200: %v", payload)
  343. }
  344. return payload, nil
  345. default:
  346. return nil, fmt.Errorf("unsupported response type: %s", responseType)
  347. }
  348. }
  349. // TODO remove this function after all the sources are migrated to use the new API
  350. func decode(ctx api.StreamContext, data []byte) (map[string]interface{}, error) {
  351. r, err := ctx.Decode(data)
  352. if err == nil {
  353. return r, nil
  354. }
  355. r = make(map[string]interface{})
  356. err = json.Unmarshal(data, &r)
  357. return r, nil
  358. }