security.go 6.9 KB

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