remote_access_client.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package pro
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gravitl/netmaker/logic"
  6. "github.com/gravitl/netmaker/models"
  7. "github.com/gravitl/netmaker/mq"
  8. "github.com/gravitl/netmaker/servercfg"
  9. "golang.org/x/exp/slog"
  10. )
  11. const racAutoDisableCheckInterval = 3 * time.Minute
  12. // AddRacHooks - adds hooks for Remote Access Client
  13. func AddRacHooks() {
  14. slog.Debug("adding RAC autodisable hook")
  15. logic.HookManagerCh <- models.HookDetails{
  16. Hook: racAutoDisableHook,
  17. Interval: racAutoDisableCheckInterval,
  18. }
  19. }
  20. // racAutoDisableHook - checks if RAC is enabled and if it is, checks if it should be disabled
  21. func racAutoDisableHook() error {
  22. slog.Debug("running RAC autodisable hook")
  23. users, err := logic.GetUsers()
  24. if err != nil {
  25. slog.Error("error getting users: ", "error", err)
  26. return err
  27. }
  28. clients, err := logic.GetAllExtClients()
  29. if err != nil {
  30. slog.Error("error getting clients: ", "error", err)
  31. return err
  32. }
  33. currentTime := time.Now()
  34. validityDuration := servercfg.GetJwtValidityDuration()
  35. for _, user := range users {
  36. if !currentTime.After(user.LastLoginTime.Add(validityDuration)) {
  37. continue
  38. }
  39. for _, client := range clients {
  40. if (client.OwnerID == user.UserName) && !user.IsAdmin && !user.IsSuperAdmin && client.Enabled {
  41. slog.Info(fmt.Sprintf("disabling ext client %s for user %s due to RAC autodisabling", client.ClientID, client.OwnerID))
  42. if err := disableExtClient(&client); err != nil {
  43. slog.Error("error disabling ext client in RAC autodisable hook", "error", err)
  44. continue // dont return but try for other clients
  45. }
  46. }
  47. }
  48. }
  49. slog.Debug("finished running RAC autodisable hook")
  50. return nil
  51. }
  52. func disableExtClient(client *models.ExtClient) error {
  53. if newClient, err := logic.ToggleExtClientConnectivity(client, false); err != nil {
  54. return err
  55. } else {
  56. // publish peer update to ingress gateway
  57. if ingressNode, err := logic.GetNodeByID(newClient.IngressGatewayID); err == nil {
  58. if err = mq.PublishPeerUpdate(false); err != nil {
  59. slog.Error("error updating ext clients on", "ingress", ingressNode.ID.String(), "err", err.Error())
  60. }
  61. ingressHost, err := logic.GetHost(ingressNode.HostID.String())
  62. if err != nil {
  63. return err
  64. }
  65. nodes, err := logic.GetAllNodes()
  66. if err != nil {
  67. return err
  68. }
  69. go mq.PublishSingleHostPeerUpdate(ingressHost, nodes, nil, []models.ExtClient{*client}, false)
  70. } else {
  71. return err
  72. }
  73. }
  74. return nil
  75. }