security.go 7.2 KB

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