security.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package logic
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "github.com/gravitl/netmaker/logger"
  7. "github.com/gravitl/netmaker/logic"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/gravitl/netmaker/servercfg"
  10. )
  11. // constants for accounts api hosts
  12. const (
  13. // accountsHostDevelopment is the accounts api host for development environment
  14. accountsHostDevelopment = "https://api.dev.accounts.netmaker.io"
  15. // accountsHostStaging is the accounts api host for staging environment
  16. accountsHostStaging = "https://api.staging.accounts.netmaker.io"
  17. // accountsHostProduction is the accounts api host for production environment
  18. accountsHostProduction = "https://api.accounts.netmaker.io"
  19. )
  20. func NetworkPermissionsCheck(username string, r *http.Request) error {
  21. // at this point global checks should be completed
  22. user, err := logic.GetUser(username)
  23. if err != nil {
  24. return err
  25. }
  26. logger.Log(0, "NET MIDDL----> 1")
  27. userRole, err := logic.GetRole(user.PlatformRoleID)
  28. if err != nil {
  29. return errors.New("access denied")
  30. }
  31. if userRole.FullAccess {
  32. return nil
  33. }
  34. logger.Log(0, "NET MIDDL----> 2")
  35. // get info from header to determine the target rsrc
  36. targetRsrc := r.Header.Get("TARGET_RSRC")
  37. targetRsrcID := r.Header.Get("TARGET_RSRC_ID")
  38. netID := r.Header.Get("NET_ID")
  39. if targetRsrc == "" {
  40. return errors.New("target rsrc is missing")
  41. }
  42. if netID == "" {
  43. return errors.New("network id is missing")
  44. }
  45. if r.Method == "" {
  46. r.Method = http.MethodGet
  47. }
  48. if targetRsrc == models.MetricRsrc.String() {
  49. return nil
  50. }
  51. // check if user has scope for target resource
  52. // TODO - differentitate between global scope and network scope apis
  53. // check for global network role
  54. if netRoles, ok := user.NetworkRoles[models.AllNetworks]; ok {
  55. for netRoleID := range netRoles {
  56. err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)
  57. if err == nil {
  58. return nil
  59. }
  60. }
  61. }
  62. netRoles := user.NetworkRoles[models.NetworkID(netID)]
  63. for netRoleID := range netRoles {
  64. err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)
  65. if err == nil {
  66. return nil
  67. }
  68. }
  69. for groupID := range user.UserGroups {
  70. userG, err := GetUserGroup(groupID)
  71. if err == nil {
  72. netRoles := userG.NetworkRoles[models.NetworkID(netID)]
  73. for netRoleID := range netRoles {
  74. err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)
  75. if err == nil {
  76. return nil
  77. }
  78. }
  79. }
  80. }
  81. return errors.New("access denied")
  82. }
  83. func checkNetworkAccessPermissions(netRoleID models.UserRoleID, username, reqScope, targetRsrc, targetRsrcID, netID string) error {
  84. networkPermissionScope, err := logic.GetRole(netRoleID)
  85. if err != nil {
  86. return err
  87. }
  88. logger.Log(0, "NET MIDDL----> 3", string(netRoleID))
  89. if networkPermissionScope.FullAccess {
  90. return nil
  91. }
  92. rsrcPermissionScope, ok := networkPermissionScope.NetworkLevelAccess[models.RsrcType(targetRsrc)]
  93. if targetRsrc == models.HostRsrc.String() && !ok {
  94. rsrcPermissionScope, ok = networkPermissionScope.NetworkLevelAccess[models.RemoteAccessGwRsrc]
  95. }
  96. if !ok {
  97. return errors.New("access denied")
  98. }
  99. logger.Log(0, "NET MIDDL----> 4", string(netRoleID))
  100. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {
  101. // handle extclient apis here
  102. if models.RsrcType(targetRsrc) == models.ExtClientsRsrc && allRsrcsTypePermissionScope.SelfOnly && targetRsrcID != "" {
  103. extclient, err := logic.GetExtClient(targetRsrcID, netID)
  104. if err != nil {
  105. return err
  106. }
  107. if !logic.IsUserAllowedAccessToExtClient(username, extclient) {
  108. return errors.New("access denied")
  109. }
  110. }
  111. err = checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, reqScope)
  112. if err == nil {
  113. return nil
  114. }
  115. }
  116. if targetRsrc == models.HostRsrc.String() {
  117. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", models.RemoteAccessGwRsrc))]; ok {
  118. err = checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, reqScope)
  119. if err == nil {
  120. return nil
  121. }
  122. }
  123. }
  124. logger.Log(0, "NET MIDDL----> 5", string(netRoleID))
  125. if targetRsrcID == "" {
  126. return errors.New("target rsrc id is empty")
  127. }
  128. if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {
  129. err = checkPermissionScopeWithReqMethod(scope, reqScope)
  130. if err == nil {
  131. return nil
  132. }
  133. }
  134. logger.Log(0, "NET MIDDL----> 6", string(netRoleID))
  135. return errors.New("access denied")
  136. }
  137. func GlobalPermissionsCheck(username string, r *http.Request) error {
  138. user, err := logic.GetUser(username)
  139. if err != nil {
  140. return err
  141. }
  142. userRole, err := logic.GetRole(user.PlatformRoleID)
  143. if err != nil {
  144. return errors.New("access denied")
  145. }
  146. if userRole.FullAccess {
  147. return nil
  148. }
  149. targetRsrc := r.Header.Get("TARGET_RSRC")
  150. targetRsrcID := r.Header.Get("TARGET_RSRC_ID")
  151. if targetRsrc == "" {
  152. return errors.New("target rsrc is missing")
  153. }
  154. if r.Method == "" {
  155. r.Method = http.MethodGet
  156. }
  157. if targetRsrc == models.MetricRsrc.String() {
  158. return nil
  159. }
  160. if (targetRsrc == models.HostRsrc.String() || targetRsrc == models.NetworkRsrc.String()) && r.Method == http.MethodGet && targetRsrcID == "" {
  161. return nil
  162. }
  163. if targetRsrc == models.UserRsrc.String() && username == targetRsrcID && (r.Method != http.MethodDelete) {
  164. return nil
  165. }
  166. rsrcPermissionScope, ok := userRole.GlobalLevelAccess[models.RsrcType(targetRsrc)]
  167. if !ok {
  168. return fmt.Errorf("access denied to %s", targetRsrc)
  169. }
  170. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {
  171. return checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, r.Method)
  172. }
  173. if targetRsrcID == "" {
  174. return errors.New("target rsrc id is missing")
  175. }
  176. if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {
  177. return checkPermissionScopeWithReqMethod(scope, r.Method)
  178. }
  179. return errors.New("access denied")
  180. }
  181. func checkPermissionScopeWithReqMethod(scope models.RsrcPermissionScope, reqmethod string) error {
  182. if reqmethod == http.MethodGet && scope.Read {
  183. return nil
  184. }
  185. if (reqmethod == http.MethodPatch || reqmethod == http.MethodPut) && scope.Update {
  186. return nil
  187. }
  188. if reqmethod == http.MethodDelete && scope.Delete {
  189. return nil
  190. }
  191. if reqmethod == http.MethodPost && scope.Create {
  192. return nil
  193. }
  194. return errors.New("operation not permitted")
  195. }
  196. func GetAccountsHost() string {
  197. switch servercfg.GetEnvironment() {
  198. case "dev":
  199. return accountsHostDevelopment
  200. case "staging":
  201. return accountsHostStaging
  202. default:
  203. return accountsHostProduction
  204. }
  205. }