emqx_on_prem.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package mq
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "sync"
  10. "github.com/gravitl/netmaker/servercfg"
  11. )
  12. type EmqxOnPrem struct {
  13. URL string
  14. UserName string
  15. Password string
  16. }
  17. const already_exists = "ALREADY_EXISTS"
  18. type (
  19. emqxUser struct {
  20. UserID string `json:"user_id"`
  21. Password string `json:"password"`
  22. Admin bool `json:"is_superuser"`
  23. }
  24. emqxLogin struct {
  25. Username string `json:"username"`
  26. Password string `json:"password"`
  27. }
  28. emqxLoginResponse struct {
  29. License struct {
  30. Edition string `json:"edition"`
  31. } `json:"license"`
  32. Token string `json:"token"`
  33. Version string `json:"version"`
  34. }
  35. aclRule struct {
  36. Topic string `json:"topic"`
  37. Permission string `json:"permission"`
  38. Action string `json:"action"`
  39. }
  40. aclObject struct {
  41. Rules []aclRule `json:"rules"`
  42. Username string `json:"username,omitempty"`
  43. }
  44. )
  45. func getEmqxAuthToken() (string, error) {
  46. payload, err := json.Marshal(&emqxLogin{
  47. Username: servercfg.GetMqUserName(),
  48. Password: servercfg.GetMqPassword(),
  49. })
  50. if err != nil {
  51. return "", err
  52. }
  53. resp, err := http.Post(servercfg.GetEmqxRestEndpoint()+"/api/v5/login", "application/json", bytes.NewReader(payload))
  54. if err != nil {
  55. return "", err
  56. }
  57. msg, err := io.ReadAll(resp.Body)
  58. if err != nil {
  59. return "", err
  60. }
  61. if resp.StatusCode != http.StatusOK {
  62. return "", fmt.Errorf("error during EMQX login %v", string(msg))
  63. }
  64. var loginResp emqxLoginResponse
  65. if err := json.Unmarshal(msg, &loginResp); err != nil {
  66. return "", err
  67. }
  68. return loginResp.Token, nil
  69. }
  70. func (e *EmqxOnPrem) GetType() servercfg.Emqxdeploy { return servercfg.EmqxOnPremDeploy }
  71. // CreateEmqxUser - creates an EMQX user
  72. func (e *EmqxOnPrem) CreateEmqxUser(username, password string) error {
  73. token, err := getEmqxAuthToken()
  74. if err != nil {
  75. return err
  76. }
  77. payload, err := json.Marshal(&emqxUser{
  78. UserID: username,
  79. Password: password,
  80. })
  81. if err != nil {
  82. return err
  83. }
  84. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication/password_based:built_in_database/users", bytes.NewReader(payload))
  85. if err != nil {
  86. return err
  87. }
  88. req.Header.Add("content-type", "application/json")
  89. req.Header.Add("authorization", "Bearer "+token)
  90. resp, err := (&http.Client{}).Do(req)
  91. if err != nil {
  92. return err
  93. }
  94. defer resp.Body.Close()
  95. if resp.StatusCode >= 300 {
  96. msg, err := io.ReadAll(resp.Body)
  97. if err != nil {
  98. return err
  99. }
  100. if !strings.Contains(string(msg), already_exists) {
  101. return fmt.Errorf("error creating EMQX user %v", string(msg))
  102. }
  103. }
  104. return nil
  105. }
  106. func (e *EmqxOnPrem) CreateEmqxUserforServer() error {
  107. token, err := getEmqxAuthToken()
  108. if err != nil {
  109. return err
  110. }
  111. payload, err := json.Marshal(&emqxUser{
  112. UserID: servercfg.GetMqUserName(),
  113. Password: servercfg.GetMqPassword(),
  114. Admin: true,
  115. })
  116. if err != nil {
  117. return err
  118. }
  119. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication/password_based:built_in_database/users", bytes.NewReader(payload))
  120. if err != nil {
  121. return err
  122. }
  123. req.Header.Add("content-type", "application/json")
  124. req.Header.Add("authorization", "Bearer "+token)
  125. resp, err := (&http.Client{}).Do(req)
  126. if err != nil {
  127. return err
  128. }
  129. defer resp.Body.Close()
  130. if resp.StatusCode >= 300 {
  131. msg, err := io.ReadAll(resp.Body)
  132. if err != nil {
  133. return err
  134. }
  135. if !strings.Contains(string(msg), already_exists) {
  136. return fmt.Errorf("error creating EMQX user %v", string(msg))
  137. }
  138. }
  139. return nil
  140. }
  141. // DeleteEmqxUser - deletes an EMQX user
  142. func (e *EmqxOnPrem) DeleteEmqxUser(username string) error {
  143. token, err := getEmqxAuthToken()
  144. if err != nil {
  145. return err
  146. }
  147. req, err := http.NewRequest(http.MethodDelete, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication/password_based:built_in_database/users/"+username, nil)
  148. if err != nil {
  149. return err
  150. }
  151. req.Header.Add("authorization", "Bearer "+token)
  152. resp, err := (&http.Client{}).Do(req)
  153. if err != nil {
  154. return err
  155. }
  156. defer resp.Body.Close()
  157. if resp.StatusCode >= 300 {
  158. msg, err := io.ReadAll(resp.Body)
  159. if err != nil {
  160. return err
  161. }
  162. return fmt.Errorf("error deleting EMQX user %v", string(msg))
  163. }
  164. return nil
  165. }
  166. // CreateEmqxDefaultAuthenticator - creates a default authenticator based on password and using EMQX's built in database as storage
  167. func (e *EmqxOnPrem) CreateEmqxDefaultAuthenticator() error {
  168. token, err := getEmqxAuthToken()
  169. if err != nil {
  170. return err
  171. }
  172. payload, err := json.Marshal(&struct {
  173. Mechanism string `json:"mechanism"`
  174. Backend string `json:"backend"`
  175. UserIDType string `json:"user_id_type"`
  176. }{Mechanism: "password_based", Backend: "built_in_database", UserIDType: "username"})
  177. if err != nil {
  178. return err
  179. }
  180. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication", bytes.NewReader(payload))
  181. if err != nil {
  182. return err
  183. }
  184. req.Header.Add("content-type", "application/json")
  185. req.Header.Add("authorization", "Bearer "+token)
  186. resp, err := (&http.Client{}).Do(req)
  187. if err != nil {
  188. return err
  189. }
  190. defer resp.Body.Close()
  191. if resp.StatusCode != http.StatusOK {
  192. msg, err := io.ReadAll(resp.Body)
  193. if err != nil {
  194. return err
  195. }
  196. return fmt.Errorf("error creating default EMQX authenticator %v", string(msg))
  197. }
  198. return nil
  199. }
  200. // CreateEmqxDefaultAuthorizer - creates a default ACL authorization mechanism based on the built in database
  201. func (e *EmqxOnPrem) CreateEmqxDefaultAuthorizer() error {
  202. token, err := getEmqxAuthToken()
  203. if err != nil {
  204. return err
  205. }
  206. payload, err := json.Marshal(&struct {
  207. Enable bool `json:"enable"`
  208. Type string `json:"type"`
  209. }{Enable: true, Type: "built_in_database"})
  210. if err != nil {
  211. return err
  212. }
  213. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources", bytes.NewReader(payload))
  214. if err != nil {
  215. return err
  216. }
  217. req.Header.Add("content-type", "application/json")
  218. req.Header.Add("authorization", "Bearer "+token)
  219. resp, err := (&http.Client{}).Do(req)
  220. if err != nil {
  221. return err
  222. }
  223. defer resp.Body.Close()
  224. if resp.StatusCode != http.StatusNoContent {
  225. msg, err := io.ReadAll(resp.Body)
  226. if err != nil {
  227. return err
  228. }
  229. return fmt.Errorf("error creating default EMQX ACL authorization mechanism %v", string(msg))
  230. }
  231. return nil
  232. }
  233. // GetUserACL - returns ACL rules by username
  234. func (e *EmqxOnPrem) GetUserACL(username string) (*aclObject, error) {
  235. token, err := getEmqxAuthToken()
  236. if err != nil {
  237. return nil, err
  238. }
  239. req, err := http.NewRequest(http.MethodGet, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources/built_in_database/username/"+username, nil)
  240. if err != nil {
  241. return nil, err
  242. }
  243. req.Header.Add("content-type", "application/json")
  244. req.Header.Add("authorization", "Bearer "+token)
  245. resp, err := (&http.Client{}).Do(req)
  246. if err != nil {
  247. return nil, err
  248. }
  249. defer resp.Body.Close()
  250. response, err := io.ReadAll(resp.Body)
  251. if err != nil {
  252. return nil, err
  253. }
  254. if resp.StatusCode != http.StatusOK {
  255. return nil, fmt.Errorf("error fetching ACL rules %v", string(response))
  256. }
  257. body := new(aclObject)
  258. if err := json.Unmarshal(response, body); err != nil {
  259. return nil, err
  260. }
  261. return body, nil
  262. }
  263. // CreateDefaultDenyRule - creates a rule to deny access to all topics for all users by default
  264. // to allow user access to topics use the `mq.CreateUserAccessRule` function
  265. func (e *EmqxOnPrem) CreateDefaultDenyRule() error {
  266. token, err := getEmqxAuthToken()
  267. if err != nil {
  268. return err
  269. }
  270. payload, err := json.Marshal(&aclObject{Rules: []aclRule{{Topic: "#", Permission: "deny", Action: "all"}}})
  271. if err != nil {
  272. return err
  273. }
  274. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources/built_in_database/all", bytes.NewReader(payload))
  275. if err != nil {
  276. return err
  277. }
  278. req.Header.Add("content-type", "application/json")
  279. req.Header.Add("authorization", "Bearer "+token)
  280. resp, err := (&http.Client{}).Do(req)
  281. if err != nil {
  282. return err
  283. }
  284. defer resp.Body.Close()
  285. if resp.StatusCode != http.StatusNoContent {
  286. msg, err := io.ReadAll(resp.Body)
  287. if err != nil {
  288. return err
  289. }
  290. return fmt.Errorf("error creating default ACL rules %v", string(msg))
  291. }
  292. return nil
  293. }
  294. // CreateHostACL - create host ACL rules
  295. func (e *EmqxOnPrem) CreateHostACL(hostID, serverName string) error {
  296. token, err := getEmqxAuthToken()
  297. if err != nil {
  298. return err
  299. }
  300. payload, err := json.Marshal(&aclObject{
  301. Username: hostID,
  302. Rules: []aclRule{
  303. {
  304. Topic: fmt.Sprintf("peers/host/%s/%s", hostID, serverName),
  305. Permission: "allow",
  306. Action: "all",
  307. },
  308. {
  309. Topic: fmt.Sprintf("host/update/%s/%s", hostID, serverName),
  310. Permission: "allow",
  311. Action: "all",
  312. },
  313. {
  314. Topic: fmt.Sprintf("host/serverupdate/%s/%s", serverName, hostID),
  315. Permission: "allow",
  316. Action: "all",
  317. },
  318. },
  319. })
  320. if err != nil {
  321. return err
  322. }
  323. req, err := http.NewRequest(http.MethodPut, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources/built_in_database/username/"+hostID, bytes.NewReader(payload))
  324. if err != nil {
  325. return err
  326. }
  327. req.Header.Add("content-type", "application/json")
  328. req.Header.Add("authorization", "Bearer "+token)
  329. resp, err := (&http.Client{}).Do(req)
  330. if err != nil {
  331. return err
  332. }
  333. defer resp.Body.Close()
  334. if resp.StatusCode != http.StatusNoContent {
  335. msg, err := io.ReadAll(resp.Body)
  336. if err != nil {
  337. return err
  338. }
  339. return fmt.Errorf("error adding ACL Rules for user %s Error: %v", hostID, string(msg))
  340. }
  341. return nil
  342. }
  343. // a lock required for preventing simultaneous updates to the same ACL object leading to overwriting each other
  344. // might occur when multiple nodes belonging to the same host are created at the same time
  345. var nodeAclMux sync.Mutex
  346. // AppendNodeUpdateACL - adds ACL rule for subscribing to node updates for a node ID
  347. func (e *EmqxOnPrem) AppendNodeUpdateACL(hostID, nodeNetwork, nodeID, serverName string) error {
  348. nodeAclMux.Lock()
  349. defer nodeAclMux.Unlock()
  350. token, err := getEmqxAuthToken()
  351. if err != nil {
  352. return err
  353. }
  354. aclObject, err := emqx.GetUserACL(hostID)
  355. if err != nil {
  356. return err
  357. }
  358. aclObject.Rules = append(aclObject.Rules, []aclRule{
  359. {
  360. Topic: fmt.Sprintf("node/update/%s/%s", nodeNetwork, nodeID),
  361. Permission: "allow",
  362. Action: "subscribe",
  363. },
  364. {
  365. Topic: fmt.Sprintf("ping/%s/%s", serverName, nodeID),
  366. Permission: "allow",
  367. Action: "all",
  368. },
  369. {
  370. Topic: fmt.Sprintf("update/%s/%s", serverName, nodeID),
  371. Permission: "allow",
  372. Action: "all",
  373. },
  374. {
  375. Topic: fmt.Sprintf("signal/%s/%s", serverName, nodeID),
  376. Permission: "allow",
  377. Action: "all",
  378. },
  379. {
  380. Topic: fmt.Sprintf("metrics/%s/%s", serverName, nodeID),
  381. Permission: "allow",
  382. Action: "all",
  383. },
  384. }...)
  385. payload, err := json.Marshal(aclObject)
  386. if err != nil {
  387. return err
  388. }
  389. req, err := http.NewRequest(http.MethodPut, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources/built_in_database/username/"+hostID, bytes.NewReader(payload))
  390. if err != nil {
  391. return err
  392. }
  393. req.Header.Add("content-type", "application/json")
  394. req.Header.Add("authorization", "Bearer "+token)
  395. resp, err := (&http.Client{}).Do(req)
  396. if err != nil {
  397. return err
  398. }
  399. defer resp.Body.Close()
  400. if resp.StatusCode != http.StatusNoContent {
  401. msg, err := io.ReadAll(resp.Body)
  402. if err != nil {
  403. return err
  404. }
  405. return fmt.Errorf("error adding ACL Rules for user %s Error: %v", hostID, string(msg))
  406. }
  407. return nil
  408. }