dynsec.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package mq
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. mqtt "github.com/eclipse/paho.mqtt.golang"
  7. "github.com/gravitl/netmaker/logger"
  8. )
  9. const DynamicSecSubTopic = "$CONTROL/dynamic-security/#"
  10. const DynamicSecPubTopic = "$CONTROL/dynamic-security/v1"
  11. type DynSecActionType string
  12. var (
  13. CreateClient DynSecActionType = "CREATE_CLIENT"
  14. DisableClient DynSecActionType = "DISABLE_CLIENT"
  15. EnableClient DynSecActionType = "ENABLE_CLIENT"
  16. DeleteClient DynSecActionType = "DELETE_CLIENT"
  17. CreateAdminClient DynSecActionType = "CREATE_ADMIN_CLIENT"
  18. ModifyClient DynSecActionType = "MODIFY_CLIENT"
  19. DISABLE_EXISTING_ADMINS DynSecActionType = "DISABLE_EXISTING_ADMINS"
  20. )
  21. var (
  22. CreateClientCmd = "createClient"
  23. DisableClientCmd = "disableClient"
  24. DeleteClientCmd = "deleteClient"
  25. ModifyClientCmd = "modifyClient"
  26. )
  27. var (
  28. mqDynSecAdmin string = "Netmaker-Admin"
  29. adminPassword string = "Netmaker-Admin"
  30. )
  31. type MqDynSecGroup struct {
  32. Groupname string `json:"groupname"`
  33. Priority int `json:"priority"`
  34. }
  35. type MqDynSecRole struct {
  36. Rolename string `json:"rolename"`
  37. Priority int `json:"priority"`
  38. }
  39. type Acl struct {
  40. AclType string `json:"acl_type"`
  41. Topic string `json:"topic"`
  42. Priority int `json:"priority"`
  43. Allow bool `json:"allow"`
  44. }
  45. type MqDynSecCmd struct {
  46. Command string `json:"command"`
  47. Username string `json:"username"`
  48. Password string `json:"password"`
  49. RoleName string `json:"rolename,omitempty"`
  50. Acls []Acl `json:"acls,omitempty"`
  51. Clientid string `json:"clientid"`
  52. Textname string `json:"textname"`
  53. Textdescription string `json:"textdescription"`
  54. Groups []MqDynSecGroup `json:"groups"`
  55. Roles []MqDynSecRole `json:"roles"`
  56. }
  57. type DynSecAction struct {
  58. ActionType DynSecActionType
  59. Payload MqDynsecPayload
  60. }
  61. type MqDynsecPayload struct {
  62. Commands []MqDynSecCmd `json:"commands"`
  63. }
  64. var DynSecChan = make(chan DynSecAction, 100)
  65. func DynamicSecManager(ctx context.Context) {
  66. defer close(DynSecChan)
  67. for {
  68. select {
  69. case <-ctx.Done():
  70. return
  71. case dynSecAction := <-DynSecChan:
  72. d, err := json.Marshal(dynSecAction.Payload)
  73. if err != nil {
  74. continue
  75. }
  76. if token := mqclient.Publish(DynamicSecPubTopic, 2, false, d); token.Error() != nil {
  77. logger.Log(0, fmt.Sprintf("failed to perform action [%s]: %v",
  78. dynSecAction.ActionType, token.Error()))
  79. }
  80. }
  81. }
  82. }
  83. func watchDynSecTopic(client mqtt.Client, msg mqtt.Message) {
  84. logger.Log(1, fmt.Sprintf("----->WatchDynSecTopic Message: %+v", string(msg.Payload())))
  85. }