clients.go 2.0 KB

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