emqx.go 10 KB

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