clients.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package logic
  2. import (
  3. "sort"
  4. "github.com/gravitl/netmaker/models"
  5. )
  6. // functions defined here, handle client ACLs, should be set on ee
  7. var (
  8. // DenyClientNodeAccess - function to handle adding a node to an ext client's denied node set
  9. DenyClientNodeAccess = func(ec *models.ExtClient, clientOrNodeID string) bool {
  10. return true
  11. }
  12. // IsClientNodeAllowed - function to check if an ext client's denied node set contains a node ID
  13. IsClientNodeAllowed = func(ec *models.ExtClient, clientOrNodeID string) bool {
  14. return true
  15. }
  16. // AllowClientNodeAccess - function to handle removing a node ID from ext client's denied nodes, thus allowing it
  17. AllowClientNodeAccess = func(ec *models.ExtClient, clientOrNodeID string) bool {
  18. return true
  19. }
  20. )
  21. // SetClientDefaultACLs - set's a client's default ACLs based on network and nodes in network
  22. func SetClientDefaultACLs(ec *models.ExtClient) error {
  23. if !isEE {
  24. return nil
  25. }
  26. networkNodes, err := GetNetworkNodes(ec.Network)
  27. if err != nil {
  28. return err
  29. }
  30. network, err := GetNetwork(ec.Network)
  31. if err != nil {
  32. return err
  33. }
  34. for i := range networkNodes {
  35. currNode := networkNodes[i]
  36. if network.DefaultACL == "no" || currNode.DefaultACL == "no" {
  37. DenyClientNodeAccess(ec, currNode.ID.String())
  38. } else {
  39. AllowClientNodeAccess(ec, currNode.ID.String())
  40. }
  41. }
  42. return nil
  43. }
  44. // SetClientACLs - overwrites an ext client's ACL
  45. func SetClientACLs(ec *models.ExtClient, newACLs map[string]struct{}) {
  46. if ec == nil || newACLs == nil || !isEE {
  47. return
  48. }
  49. ec.DeniedACLs = newACLs
  50. }
  51. // IsClientNodeAllowedByID - checks if a given ext client ID + nodeID are allowed
  52. func IsClientNodeAllowedByID(clientID, networkName, clientOrNodeID string) bool {
  53. client, err := GetExtClient(clientID, networkName)
  54. if err != nil {
  55. return false
  56. }
  57. return IsClientNodeAllowed(&client, clientOrNodeID)
  58. }
  59. // SortExtClient - Sorts slice of ExtClients by their ClientID alphabetically with numbers first
  60. func SortExtClient(unsortedExtClient []models.ExtClient) {
  61. sort.Slice(unsortedExtClient, func(i, j int) bool {
  62. return unsortedExtClient[i].ClientID < unsortedExtClient[j].ClientID
  63. })
  64. }