security.go 7.2 KB

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