2
0

security.go 7.3 KB

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