emqx_on_prem.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package mq
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/gravitl/netmaker/servercfg"
  10. )
  11. type EmqxOnPrem struct {
  12. URL string
  13. UserName string
  14. Password string
  15. }
  16. const already_exists = "ALREADY_EXISTS"
  17. type (
  18. emqxUser struct {
  19. UserID string `json:"user_id"`
  20. Password string `json:"password"`
  21. Admin bool `json:"is_superuser"`
  22. }
  23. emqxLogin struct {
  24. Username string `json:"username"`
  25. Password string `json:"password"`
  26. }
  27. emqxLoginResponse struct {
  28. License struct {
  29. Edition string `json:"edition"`
  30. } `json:"license"`
  31. Token string `json:"token"`
  32. Version string `json:"version"`
  33. }
  34. aclRule struct {
  35. Topic string `json:"topic"`
  36. Permission string `json:"permission"`
  37. Action string `json:"action"`
  38. }
  39. aclObject struct {
  40. Rules []aclRule `json:"rules"`
  41. Username string `json:"username,omitempty"`
  42. }
  43. )
  44. func getEmqxAuthToken() (string, error) {
  45. payload, err := json.Marshal(&emqxLogin{
  46. Username: servercfg.GetMqUserName(),
  47. Password: servercfg.GetMqPassword(),
  48. })
  49. if err != nil {
  50. return "", err
  51. }
  52. resp, err := http.Post(servercfg.GetEmqxRestEndpoint()+"/api/v5/login", "application/json", bytes.NewReader(payload))
  53. if err != nil {
  54. return "", err
  55. }
  56. msg, err := io.ReadAll(resp.Body)
  57. if err != nil {
  58. return "", err
  59. }
  60. if resp.StatusCode != http.StatusOK {
  61. return "", fmt.Errorf("error during EMQX login %v", string(msg))
  62. }
  63. var loginResp emqxLoginResponse
  64. if err := json.Unmarshal(msg, &loginResp); err != nil {
  65. return "", err
  66. }
  67. return loginResp.Token, nil
  68. }
  69. func (e *EmqxOnPrem) GetType() servercfg.Emqxdeploy { return servercfg.EmqxOnPremDeploy }
  70. // CreateEmqxUser - creates an EMQX user
  71. func (e *EmqxOnPrem) CreateEmqxUser(username, password string) error {
  72. token, err := getEmqxAuthToken()
  73. if err != nil {
  74. return err
  75. }
  76. payload, err := json.Marshal(&emqxUser{
  77. UserID: username,
  78. Password: password,
  79. })
  80. if err != nil {
  81. return err
  82. }
  83. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication/password_based:built_in_database/users", bytes.NewReader(payload))
  84. if err != nil {
  85. return err
  86. }
  87. req.Header.Add("content-type", "application/json")
  88. req.Header.Add("authorization", "Bearer "+token)
  89. resp, err := (&http.Client{}).Do(req)
  90. if err != nil {
  91. return err
  92. }
  93. defer resp.Body.Close()
  94. if resp.StatusCode >= 300 {
  95. msg, err := io.ReadAll(resp.Body)
  96. if err != nil {
  97. return err
  98. }
  99. if !strings.Contains(string(msg), already_exists) {
  100. return fmt.Errorf("error creating EMQX user %v", string(msg))
  101. }
  102. }
  103. return nil
  104. }
  105. func (e *EmqxOnPrem) CreateEmqxUserforServer() error {
  106. token, err := getEmqxAuthToken()
  107. if err != nil {
  108. return err
  109. }
  110. payload, err := json.Marshal(&emqxUser{
  111. UserID: servercfg.GetMqUserName(),
  112. Password: servercfg.GetMqPassword(),
  113. Admin: true,
  114. })
  115. if err != nil {
  116. return err
  117. }
  118. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication/password_based:built_in_database/users", bytes.NewReader(payload))
  119. if err != nil {
  120. return err
  121. }
  122. req.Header.Add("content-type", "application/json")
  123. req.Header.Add("authorization", "Bearer "+token)
  124. resp, err := (&http.Client{}).Do(req)
  125. if err != nil {
  126. return err
  127. }
  128. defer resp.Body.Close()
  129. if resp.StatusCode >= 300 {
  130. msg, err := io.ReadAll(resp.Body)
  131. if err != nil {
  132. return err
  133. }
  134. if !strings.Contains(string(msg), already_exists) {
  135. return fmt.Errorf("error creating EMQX user %v", string(msg))
  136. }
  137. }
  138. return nil
  139. }
  140. // DeleteEmqxUser - deletes an EMQX user
  141. func (e *EmqxOnPrem) DeleteEmqxUser(username string) error {
  142. token, err := getEmqxAuthToken()
  143. if err != nil {
  144. return err
  145. }
  146. req, err := http.NewRequest(http.MethodDelete, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication/password_based:built_in_database/users/"+username, nil)
  147. if err != nil {
  148. return err
  149. }
  150. req.Header.Add("authorization", "Bearer "+token)
  151. resp, err := (&http.Client{}).Do(req)
  152. if err != nil {
  153. return err
  154. }
  155. defer resp.Body.Close()
  156. if resp.StatusCode >= 300 {
  157. msg, err := io.ReadAll(resp.Body)
  158. if err != nil {
  159. return err
  160. }
  161. return fmt.Errorf("error deleting EMQX user %v", string(msg))
  162. }
  163. return nil
  164. }
  165. // CreateEmqxDefaultAuthenticator - creates a default authenticator based on password and using EMQX's built in database as storage
  166. func (e *EmqxOnPrem) CreateEmqxDefaultAuthenticator() error {
  167. token, err := getEmqxAuthToken()
  168. if err != nil {
  169. return err
  170. }
  171. payload, err := json.Marshal(&struct {
  172. Mechanism string `json:"mechanism"`
  173. Backend string `json:"backend"`
  174. UserIDType string `json:"user_id_type"`
  175. }{Mechanism: "password_based", Backend: "built_in_database", UserIDType: "username"})
  176. if err != nil {
  177. return err
  178. }
  179. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authentication", bytes.NewReader(payload))
  180. if err != nil {
  181. return err
  182. }
  183. req.Header.Add("content-type", "application/json")
  184. req.Header.Add("authorization", "Bearer "+token)
  185. resp, err := (&http.Client{}).Do(req)
  186. if err != nil {
  187. return err
  188. }
  189. defer resp.Body.Close()
  190. if resp.StatusCode != http.StatusOK {
  191. msg, err := io.ReadAll(resp.Body)
  192. if err != nil {
  193. return err
  194. }
  195. return fmt.Errorf("error creating default EMQX authenticator %v", string(msg))
  196. }
  197. return nil
  198. }
  199. // CreateEmqxDefaultAuthorizer - creates a default ACL authorization mechanism based on the built in database
  200. func (e *EmqxOnPrem) CreateEmqxDefaultAuthorizer() error {
  201. token, err := getEmqxAuthToken()
  202. if err != nil {
  203. return err
  204. }
  205. payload, err := json.Marshal(&struct {
  206. Enable bool `json:"enable"`
  207. Type string `json:"type"`
  208. }{Enable: true, Type: "built_in_database"})
  209. if err != nil {
  210. return err
  211. }
  212. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources", bytes.NewReader(payload))
  213. if err != nil {
  214. return err
  215. }
  216. req.Header.Add("content-type", "application/json")
  217. req.Header.Add("authorization", "Bearer "+token)
  218. resp, err := (&http.Client{}).Do(req)
  219. if err != nil {
  220. return err
  221. }
  222. defer resp.Body.Close()
  223. if resp.StatusCode != http.StatusNoContent {
  224. msg, err := io.ReadAll(resp.Body)
  225. if err != nil {
  226. return err
  227. }
  228. return fmt.Errorf("error creating default EMQX ACL authorization mechanism %v", string(msg))
  229. }
  230. return nil
  231. }
  232. // CreateDefaultAllowRule - creates a rule to deny access to all topics for all users by default
  233. // to allow user access to topics use the `mq.CreateUserAccessRule` function
  234. func (e *EmqxOnPrem) CreateDefaultAllowRule() error {
  235. token, err := getEmqxAuthToken()
  236. if err != nil {
  237. return err
  238. }
  239. payload, err := json.Marshal(&aclObject{Rules: []aclRule{{Topic: "#", Permission: "allow", Action: "all"}}})
  240. if err != nil {
  241. return err
  242. }
  243. req, err := http.NewRequest(http.MethodPost, servercfg.GetEmqxRestEndpoint()+"/api/v5/authorization/sources/built_in_database/all", bytes.NewReader(payload))
  244. if err != nil {
  245. return err
  246. }
  247. req.Header.Add("content-type", "application/json")
  248. req.Header.Add("authorization", "Bearer "+token)
  249. resp, err := (&http.Client{}).Do(req)
  250. if err != nil {
  251. return err
  252. }
  253. defer resp.Body.Close()
  254. if resp.StatusCode != http.StatusNoContent {
  255. msg, err := io.ReadAll(resp.Body)
  256. if err != nil {
  257. return err
  258. }
  259. return fmt.Errorf("error creating default ACL rules %v", string(msg))
  260. }
  261. return nil
  262. }