clients.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package logic
  2. import (
  3. "errors"
  4. "sort"
  5. "github.com/gravitl/netmaker/logic/acls"
  6. "github.com/gravitl/netmaker/models"
  7. "golang.org/x/exp/slog"
  8. )
  9. // functions defined here, handle client ACLs, should be set on ee
  10. var (
  11. // DenyClientNodeAccess - function to handle adding a node to an ext client's denied node set
  12. DenyClientNodeAccess = func(ec *models.ExtClient, clientOrNodeID string) bool {
  13. return true
  14. }
  15. // IsClientNodeAllowed - function to check if an ext client's denied node set contains a node ID
  16. IsClientNodeAllowed = func(ec *models.ExtClient, clientOrNodeID string) bool {
  17. return true
  18. }
  19. // AllowClientNodeAccess - function to handle removing a node ID from ext client's denied nodes, thus allowing it
  20. AllowClientNodeAccess = func(ec *models.ExtClient, clientOrNodeID string) bool {
  21. return true
  22. }
  23. SetClientDefaultACLs = func(ec *models.ExtClient) error {
  24. // allow all on CE
  25. if !GetServerSettings().OldAClsSupport {
  26. ec.DeniedACLs = make(map[string]struct{})
  27. return nil
  28. }
  29. networkAcls := acls.ACLContainer{}
  30. networkAcls, err := networkAcls.Get(acls.ContainerID(ec.Network))
  31. if err != nil {
  32. slog.Error("failed to get network acls", "error", err)
  33. return err
  34. }
  35. networkAcls[acls.AclID(ec.ClientID)] = make(acls.ACL)
  36. for objId := range networkAcls {
  37. if networkAcls[objId] == nil {
  38. networkAcls[objId] = make(acls.ACL)
  39. }
  40. networkAcls[objId][acls.AclID(ec.ClientID)] = acls.Allowed
  41. networkAcls[acls.AclID(ec.ClientID)][objId] = acls.Allowed
  42. }
  43. delete(networkAcls[acls.AclID(ec.ClientID)], acls.AclID(ec.ClientID))
  44. if _, err = networkAcls.Save(acls.ContainerID(ec.Network)); err != nil {
  45. slog.Error("failed to update network acls", "error", err)
  46. return err
  47. }
  48. return nil
  49. }
  50. SetClientACLs = func(ec *models.ExtClient, newACLs map[string]struct{}) {
  51. }
  52. UpdateProNodeACLs = func(node *models.Node) error {
  53. return nil
  54. }
  55. )
  56. // SortExtClient - Sorts slice of ExtClients by their ClientID alphabetically with numbers first
  57. func SortExtClient(unsortedExtClient []models.ExtClient) {
  58. sort.Slice(unsortedExtClient, func(i, j int) bool {
  59. return unsortedExtClient[i].ClientID < unsortedExtClient[j].ClientID
  60. })
  61. }
  62. // GetExtClientByName - gets an ext client by name
  63. func GetExtClientByName(ID string) (models.ExtClient, error) {
  64. clients, err := GetAllExtClients()
  65. if err != nil {
  66. return models.ExtClient{}, err
  67. }
  68. for i := range clients {
  69. if clients[i].ClientID == ID {
  70. return clients[i], nil
  71. }
  72. }
  73. return models.ExtClient{}, errors.New("client not found")
  74. }