dynsec_helper.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package mq
  2. import (
  3. "errors"
  4. "time"
  5. mqtt "github.com/eclipse/paho.mqtt.golang"
  6. "github.com/gravitl/netmaker/servercfg"
  7. )
  8. const (
  9. // constant for admin role
  10. adminRole = "admin"
  11. // constant for generic role
  12. genericRole = "generic"
  13. // const for dynamic security file
  14. dynamicSecurityFile = "dynamic-security.json"
  15. )
  16. var (
  17. // default configuration of dynamic security
  18. dynConfigInI = dynJSON{
  19. Clients: []client{
  20. {
  21. Username: mqAdminUserName,
  22. TextName: "netmaker admin user",
  23. Password: "",
  24. Salt: "",
  25. Iterations: 0,
  26. Roles: []clientRole{
  27. {
  28. Rolename: adminRole,
  29. },
  30. },
  31. },
  32. {
  33. Username: mqNetmakerServerUserName,
  34. TextName: "netmaker server user",
  35. Password: "",
  36. Salt: "",
  37. Iterations: 0,
  38. Roles: []clientRole{
  39. {
  40. Rolename: genericRole,
  41. },
  42. },
  43. },
  44. exporterMQClient,
  45. },
  46. Roles: []role{
  47. {
  48. Rolename: adminRole,
  49. Acls: fetchAdminAcls(),
  50. },
  51. {
  52. Rolename: genericRole,
  53. Acls: fetchGenericAcls(),
  54. },
  55. },
  56. DefaultAcl: defaultAccessAcl{
  57. PublishClientSend: false,
  58. PublishClientReceive: true,
  59. Subscribe: false,
  60. Unsubscribe: true,
  61. },
  62. }
  63. exporterMQClient = client{
  64. Username: mqExporterUserName,
  65. TextName: "netmaker metrics exporter",
  66. Password: "",
  67. Salt: "",
  68. Iterations: 101,
  69. Roles: []clientRole{
  70. {
  71. Rolename: genericRole,
  72. },
  73. },
  74. }
  75. )
  76. // GetAdminClient - fetches admin client of the MQ
  77. func GetAdminClient() (mqtt.Client, error) {
  78. opts := mqtt.NewClientOptions()
  79. setMqOptions(mqAdminUserName, servercfg.GetMqAdminPassword(), opts)
  80. mqclient := mqtt.NewClient(opts)
  81. var connecterr error
  82. if token := mqclient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  83. if token.Error() == nil {
  84. connecterr = errors.New("connect timeout")
  85. } else {
  86. connecterr = token.Error()
  87. }
  88. }
  89. return mqclient, connecterr
  90. }
  91. // genericAcls - fetches generice role related acls
  92. func fetchGenericAcls() []Acl {
  93. return []Acl{
  94. {
  95. AclType: "publishClientSend",
  96. Topic: "#",
  97. Priority: -1,
  98. Allow: true,
  99. },
  100. {
  101. AclType: "publishClientReceive",
  102. Topic: "#",
  103. Priority: -1,
  104. Allow: true,
  105. },
  106. {
  107. AclType: "subscribePattern",
  108. Topic: "#",
  109. Priority: -1,
  110. Allow: true,
  111. },
  112. {
  113. AclType: "unsubscribePattern",
  114. Topic: "#",
  115. Priority: -1,
  116. Allow: true,
  117. },
  118. }
  119. }
  120. // fetchAdminAcls - fetches admin role related acls
  121. func fetchAdminAcls() []Acl {
  122. return []Acl{
  123. {
  124. AclType: "publishClientSend",
  125. Topic: "$CONTROL/dynamic-security/#",
  126. Priority: -1,
  127. Allow: true,
  128. },
  129. {
  130. AclType: "publishClientReceive",
  131. Topic: "$CONTROL/dynamic-security/#",
  132. Priority: -1,
  133. Allow: true,
  134. },
  135. {
  136. AclType: "subscribePattern",
  137. Topic: "$CONTROL/dynamic-security/#",
  138. Priority: -1,
  139. Allow: true,
  140. },
  141. {
  142. AclType: "publishClientReceive",
  143. Topic: "$SYS/#",
  144. Priority: -1,
  145. Allow: true,
  146. },
  147. {
  148. AclType: "subscribePattern",
  149. Topic: "$SYS/#",
  150. Priority: -1,
  151. Allow: true,
  152. },
  153. {
  154. AclType: "publishClientReceive",
  155. Topic: "#",
  156. Priority: -1,
  157. Allow: true,
  158. },
  159. {
  160. AclType: "subscribePattern",
  161. Topic: "#",
  162. Priority: -1,
  163. Allow: true,
  164. },
  165. {
  166. AclType: "unsubscribePattern",
  167. Topic: "#",
  168. Priority: -1,
  169. Allow: true,
  170. },
  171. {
  172. AclType: "publishClientSend",
  173. Topic: "#",
  174. Priority: -1,
  175. Allow: true,
  176. },
  177. }
  178. }