clients.go 2.5 KB

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