security.go 6.5 KB

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