security.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. netRoles := userG.NetworkRoles[models.NetworkID(netID)]
  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. }
  90. return errors.New("access denied")
  91. }
  92. func checkNetworkAccessPermissions(netRoleID models.UserRoleID, username, reqScope, targetRsrc, targetRsrcID, netID string) error {
  93. networkPermissionScope, err := logic.GetRole(netRoleID)
  94. if err != nil {
  95. return err
  96. }
  97. if networkPermissionScope.FullAccess {
  98. return nil
  99. }
  100. rsrcPermissionScope, ok := networkPermissionScope.NetworkLevelAccess[models.RsrcType(targetRsrc)]
  101. if targetRsrc == models.HostRsrc.String() && !ok {
  102. rsrcPermissionScope, ok = networkPermissionScope.NetworkLevelAccess[models.RemoteAccessGwRsrc]
  103. }
  104. if !ok {
  105. return errors.New("access denied")
  106. }
  107. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {
  108. // handle extclient apis here
  109. if models.RsrcType(targetRsrc) == models.ExtClientsRsrc && allRsrcsTypePermissionScope.SelfOnly && targetRsrcID != "" {
  110. extclient, err := logic.GetExtClient(targetRsrcID, netID)
  111. if err != nil {
  112. return err
  113. }
  114. if !logic.IsUserAllowedAccessToExtClient(username, extclient) {
  115. return errors.New("access denied")
  116. }
  117. }
  118. err = checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, reqScope)
  119. if err == nil {
  120. return nil
  121. }
  122. }
  123. if targetRsrc == models.HostRsrc.String() {
  124. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", models.RemoteAccessGwRsrc))]; ok {
  125. err = checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, reqScope)
  126. if err == nil {
  127. return nil
  128. }
  129. }
  130. }
  131. if targetRsrcID == "" {
  132. return errors.New("target rsrc id is empty")
  133. }
  134. if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {
  135. err = checkPermissionScopeWithReqMethod(scope, reqScope)
  136. if err == nil {
  137. return nil
  138. }
  139. }
  140. return errors.New("access denied")
  141. }
  142. func GlobalPermissionsCheck(username string, r *http.Request) error {
  143. user, err := logic.GetUser(username)
  144. if err != nil {
  145. return err
  146. }
  147. userRole, err := logic.GetRole(user.PlatformRoleID)
  148. if err != nil {
  149. return errors.New("access denied")
  150. }
  151. if userRole.FullAccess {
  152. return nil
  153. }
  154. targetRsrc := r.Header.Get("TARGET_RSRC")
  155. targetRsrcID := r.Header.Get("TARGET_RSRC_ID")
  156. if targetRsrc == "" {
  157. return errors.New("target rsrc is missing")
  158. }
  159. if r.Method == "" {
  160. r.Method = http.MethodGet
  161. }
  162. if targetRsrc == models.MetricRsrc.String() {
  163. return nil
  164. }
  165. if (targetRsrc == models.HostRsrc.String() || targetRsrc == models.NetworkRsrc.String()) && r.Method == http.MethodGet && targetRsrcID == "" {
  166. return nil
  167. }
  168. if targetRsrc == models.UserRsrc.String() && username == targetRsrcID && (r.Method != http.MethodDelete) {
  169. return nil
  170. }
  171. rsrcPermissionScope, ok := userRole.GlobalLevelAccess[models.RsrcType(targetRsrc)]
  172. if !ok {
  173. return fmt.Errorf("access denied to %s", targetRsrc)
  174. }
  175. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {
  176. return checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, r.Method)
  177. }
  178. if targetRsrcID == "" {
  179. return errors.New("target rsrc id is missing")
  180. }
  181. if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {
  182. return checkPermissionScopeWithReqMethod(scope, r.Method)
  183. }
  184. return errors.New("access denied")
  185. }
  186. func checkPermissionScopeWithReqMethod(scope models.RsrcPermissionScope, reqmethod string) error {
  187. if reqmethod == http.MethodGet && scope.Read {
  188. return nil
  189. }
  190. if (reqmethod == http.MethodPatch || reqmethod == http.MethodPut) && scope.Update {
  191. return nil
  192. }
  193. if reqmethod == http.MethodDelete && scope.Delete {
  194. return nil
  195. }
  196. if reqmethod == http.MethodPost && scope.Create {
  197. return nil
  198. }
  199. return errors.New("operation not permitted")
  200. }
  201. func GetAccountsHost() string {
  202. switch servercfg.GetEnvironment() {
  203. case "dev":
  204. return accountsHostDevelopment
  205. case "staging":
  206. return accountsHostStaging
  207. default:
  208. return accountsHostProduction
  209. }
  210. }
  211. func GetAccountsUIHost() string {
  212. switch servercfg.GetEnvironment() {
  213. case "dev":
  214. return accountsUIHostDevelopment
  215. case "staging":
  216. return accountsUIHostStaging
  217. default:
  218. return accountsUIHostProduction
  219. }
  220. }