security.go 7.0 KB

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