dynsec.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package mq
  2. import (
  3. "crypto/sha512"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "os"
  9. "time"
  10. mqtt "github.com/eclipse/paho.mqtt.golang"
  11. "github.com/gravitl/netmaker/functions"
  12. "github.com/gravitl/netmaker/logger"
  13. "github.com/gravitl/netmaker/logic"
  14. "github.com/gravitl/netmaker/netclient/ncutils"
  15. "github.com/gravitl/netmaker/servercfg"
  16. "golang.org/x/crypto/pbkdf2"
  17. )
  18. var (
  19. dynamicSecurityFile = "dynamic-security.json"
  20. dynConfig = dynJSON{
  21. Clients: []client{
  22. {
  23. Username: "Netmaker-Admin",
  24. TextName: "netmaker admin user",
  25. Password: "",
  26. Salt: "",
  27. Iterations: 0,
  28. Roles: []clientRole{
  29. {
  30. Rolename: "admin",
  31. },
  32. },
  33. },
  34. {
  35. Username: "Netmaker-Server",
  36. TextName: "netmaker server user",
  37. Password: "",
  38. Salt: "",
  39. Iterations: 0,
  40. Roles: []clientRole{
  41. {
  42. Rolename: "server",
  43. },
  44. },
  45. },
  46. {
  47. Username: "netmaker-exporter",
  48. TextName: "netmaker metrics exporter",
  49. Password: "yl7HZglF4CvCxgjPLLIYc73LRtjEwp2/SAEQXeW5Ta1Dl4RoLN5/gjqiv8xmue+F9LfRk8KICkNbhSYuEfJ7ww==",
  50. Salt: "veLl9eN02i+hKkyT",
  51. Iterations: 101,
  52. Roles: []clientRole{
  53. {
  54. Rolename: "exporter",
  55. },
  56. },
  57. },
  58. },
  59. Roles: []role{
  60. {
  61. Rolename: "admin",
  62. Acls: []Acl{
  63. {
  64. AclType: "publishClientSend",
  65. Topic: "$CONTROL/dynamic-security/#",
  66. Priority: -1,
  67. Allow: true,
  68. },
  69. {
  70. AclType: "publishClientReceive",
  71. Topic: "$CONTROL/dynamic-security/#",
  72. Priority: -1,
  73. Allow: true,
  74. },
  75. {
  76. AclType: "subscribePattern",
  77. Topic: "$CONTROL/dynamic-security/#",
  78. Priority: -1,
  79. Allow: true,
  80. },
  81. {
  82. AclType: "publishClientReceive",
  83. Topic: "$SYS/#",
  84. Priority: -1,
  85. Allow: true,
  86. },
  87. {
  88. AclType: "subscribePattern",
  89. Topic: "$SYS/#",
  90. Priority: -1,
  91. Allow: true,
  92. },
  93. {
  94. AclType: "publishClientReceive",
  95. Topic: "#",
  96. Priority: -1,
  97. Allow: true,
  98. },
  99. {
  100. AclType: "subscribePattern",
  101. Topic: "#",
  102. Priority: -1,
  103. Allow: true,
  104. },
  105. {
  106. AclType: "unsubscribePattern",
  107. Topic: "#",
  108. Priority: -1,
  109. Allow: true,
  110. },
  111. {
  112. AclType: "publishClientSend",
  113. Topic: "#",
  114. Priority: -1,
  115. Allow: true,
  116. },
  117. },
  118. },
  119. {
  120. Rolename: "server",
  121. Acls: []Acl{
  122. {
  123. AclType: "publishClientSend",
  124. Topic: "peers/#",
  125. Priority: -1,
  126. Allow: true,
  127. },
  128. {
  129. AclType: "publishClientSend",
  130. Topic: "update/#",
  131. Priority: -1,
  132. Allow: true,
  133. },
  134. {
  135. AclType: "publishClientSend",
  136. Topic: "metrics_exporter",
  137. Priority: -1,
  138. Allow: true,
  139. },
  140. {
  141. AclType: "publishClientReceive",
  142. Topic: "ping/#",
  143. Priority: -1,
  144. Allow: true,
  145. },
  146. {
  147. AclType: "publishClientReceive",
  148. Topic: "update/#",
  149. Priority: -1,
  150. Allow: true,
  151. },
  152. {
  153. AclType: "publishClientReceive",
  154. Topic: "signal/#",
  155. Priority: -1,
  156. Allow: true,
  157. },
  158. {
  159. AclType: "publishClientReceive",
  160. Topic: "metrics/#",
  161. Priority: -1,
  162. Allow: true,
  163. },
  164. },
  165. },
  166. {
  167. Rolename: "exporter",
  168. Acls: []Acl{
  169. {
  170. AclType: "publishClientReceive",
  171. Topic: "metrics_exporter",
  172. Allow: true,
  173. },
  174. },
  175. },
  176. },
  177. DefaultAcl: defaultAccessAcl{
  178. PublishClientSend: false,
  179. PublishClientReceive: true,
  180. Subscribe: false,
  181. Unsubscribe: true,
  182. },
  183. }
  184. )
  185. const DynamicSecSubTopic = "$CONTROL/dynamic-security/#"
  186. const DynamicSecPubTopic = "$CONTROL/dynamic-security/v1"
  187. var mqAdminClient mqtt.Client
  188. var (
  189. CreateClientCmd = "createClient"
  190. DisableClientCmd = "disableClient"
  191. DeleteClientCmd = "deleteClient"
  192. ModifyClientCmd = "modifyClient"
  193. )
  194. var (
  195. CreateRoleCmd = "createRole"
  196. DeleteRoleCmd = "deleteRole"
  197. )
  198. type dynJSON struct {
  199. Clients []client `json:"clients"`
  200. Roles []role `json:"roles"`
  201. DefaultAcl defaultAccessAcl `json:"defaultACLAccess"`
  202. }
  203. var (
  204. mqAdminUserName string = "Netmaker-Admin"
  205. mqNetmakerServerUserName string = "Netmaker-Server"
  206. )
  207. type clientRole struct {
  208. Rolename string `json:"rolename"`
  209. }
  210. type client struct {
  211. Username string `json:"username"`
  212. TextName string `json:"textName"`
  213. Password string `json:"password"`
  214. Salt string `json:"salt"`
  215. Iterations int `json:"iterations"`
  216. Roles []clientRole `json:"roles"`
  217. }
  218. type role struct {
  219. Rolename string `json:"rolename"`
  220. Acls []Acl `json:"acls"`
  221. }
  222. type defaultAccessAcl struct {
  223. PublishClientSend bool `json:"publishClientSend"`
  224. PublishClientReceive bool `json:"publishClientReceive"`
  225. Subscribe bool `json:"subscribe"`
  226. Unsubscribe bool `json:"unsubscribe"`
  227. }
  228. type dynCnf struct {
  229. Clients []client `json:"clients"`
  230. Roles []role `json:"roles"`
  231. DefaultACLAccess defaultAccessAcl `json:"defaultACLAccess"`
  232. }
  233. type MqDynSecGroup struct {
  234. Groupname string `json:"groupname"`
  235. Priority int `json:"priority"`
  236. }
  237. type MqDynSecRole struct {
  238. Rolename string `json:"rolename"`
  239. Priority int `json:"priority"`
  240. }
  241. type Acl struct {
  242. AclType string `json:"acltype"`
  243. Topic string `json:"topic"`
  244. Priority int `json:"priority,omitempty"`
  245. Allow bool `json:"allow"`
  246. }
  247. type MqDynSecCmd struct {
  248. Command string `json:"command"`
  249. Username string `json:"username"`
  250. Password string `json:"password"`
  251. RoleName string `json:"rolename,omitempty"`
  252. Acls []Acl `json:"acls,omitempty"`
  253. Clientid string `json:"clientid"`
  254. Textname string `json:"textname"`
  255. Textdescription string `json:"textdescription"`
  256. Groups []MqDynSecGroup `json:"groups"`
  257. Roles []MqDynSecRole `json:"roles"`
  258. }
  259. type DynSecAction struct {
  260. Payload MqDynsecPayload
  261. }
  262. type MqDynsecPayload struct {
  263. Commands []MqDynSecCmd `json:"commands"`
  264. }
  265. func encodePasswordToPBKDF2(password string, salt string, iterations int, keyLength int) string {
  266. binaryEncoded := pbkdf2.Key([]byte(password), []byte(salt), iterations, keyLength, sha512.New)
  267. return base64.StdEncoding.EncodeToString(binaryEncoded)
  268. }
  269. func Configure() error {
  270. password := servercfg.GetMqAdminPassword()
  271. if password == "" {
  272. return errors.New("MQ admin password not provided")
  273. }
  274. for i, cI := range dynConfig.Clients {
  275. if cI.Username == mqAdminUserName || cI.Username == mqNetmakerServerUserName {
  276. salt := logic.RandomString(12)
  277. hashed := encodePasswordToPBKDF2(password, salt, 101, 64)
  278. cI.Password = hashed
  279. cI.Iterations = 101
  280. cI.Salt = base64.StdEncoding.EncodeToString([]byte(salt))
  281. dynConfig.Clients[i] = cI
  282. }
  283. }
  284. data, err := json.MarshalIndent(dynConfig, "", " ")
  285. if err != nil {
  286. return err
  287. }
  288. path := functions.GetNetmakerPath() + ncutils.GetSeparator() + dynamicSecurityFile
  289. return os.WriteFile(path, data, 0755)
  290. }
  291. func PublishEventToDynSecTopic(event DynSecAction) error {
  292. d, err := json.Marshal(event.Payload)
  293. if err != nil {
  294. return err
  295. }
  296. var connecterr error
  297. if token := mqAdminClient.Publish(DynamicSecPubTopic, 2, false, d); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  298. if token.Error() == nil {
  299. connecterr = errors.New("connect timeout")
  300. } else {
  301. connecterr = token.Error()
  302. }
  303. }
  304. return connecterr
  305. }
  306. func watchDynSecTopic(client mqtt.Client, msg mqtt.Message) {
  307. logger.Log(1, fmt.Sprintf("----->WatchDynSecTopic Message: %+v", string(msg.Payload())))
  308. }