clients.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. networkAcls := acls.ACLContainer{}
  26. networkAcls, err := networkAcls.Get(acls.ContainerID(ec.Network))
  27. if err != nil {
  28. slog.Error("failed to get network acls", "error", err)
  29. return err
  30. }
  31. networkAcls[acls.AclID(ec.ClientID)] = acls.ACL{}
  32. for objId := range networkAcls {
  33. networkAcls[objId][acls.AclID(ec.ClientID)] = acls.Allowed
  34. networkAcls[acls.AclID(ec.ClientID)][objId] = acls.Allowed
  35. }
  36. delete(networkAcls[acls.AclID(ec.ClientID)], acls.AclID(ec.ClientID))
  37. if _, err = networkAcls.Save(acls.ContainerID(ec.Network)); err != nil {
  38. slog.Error("failed to update network acls", "error", err)
  39. return err
  40. }
  41. return nil
  42. }
  43. SetClientACLs = func(ec *models.ExtClient, newACLs map[string]struct{}) {
  44. }
  45. UpdateProNodeACLs = func(node *models.Node) error {
  46. return nil
  47. }
  48. )
  49. // SortExtClient - Sorts slice of ExtClients by their ClientID alphabetically with numbers first
  50. func SortExtClient(unsortedExtClient []models.ExtClient) {
  51. sort.Slice(unsortedExtClient, func(i, j int) bool {
  52. return unsortedExtClient[i].ClientID < unsortedExtClient[j].ClientID
  53. })
  54. }
  55. // GetExtClientByName - gets an ext client by name
  56. func GetExtClientByName(ID string) (models.ExtClient, error) {
  57. clients, err := GetAllExtClients()
  58. if err != nil {
  59. return models.ExtClient{}, err
  60. }
  61. for i := range clients {
  62. if clients[i].ClientID == ID {
  63. return clients[i], nil
  64. }
  65. }
  66. return models.ExtClient{}, errors.New("client not found")
  67. }