security.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package logic
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/gorilla/mux"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/gravitl/netmaker/servercfg"
  10. )
  11. const (
  12. MasterUser = "masteradministrator"
  13. Forbidden_Msg = "forbidden"
  14. Forbidden_Err = models.Error(Forbidden_Msg)
  15. Unauthorized_Msg = "unauthorized"
  16. Unauthorized_Err = models.Error(Unauthorized_Msg)
  17. )
  18. func networkPermissionsCheck(username string, r *http.Request) error {
  19. user, err := GetUser(username)
  20. if err != nil {
  21. return err
  22. }
  23. if user.PermissionTemplate.DashBoardAcls.FullAccess {
  24. return nil
  25. }
  26. // get info from header to determine the target rsrc
  27. targetRsrc := r.Header.Get("TARGET_RSRC")
  28. targetRsrcID := r.Header.Get("TARGET_RSRC_ID")
  29. netID := r.Header.Get("NET_ID")
  30. if targetRsrc == "" {
  31. return errors.New("target rsrc is missing")
  32. }
  33. if netID == "" {
  34. return errors.New("network id is missing")
  35. }
  36. if r.Method == "" {
  37. r.Method = http.MethodGet
  38. }
  39. // check if user has scope for target resource
  40. // TODO - differentitate between global scope and network scope apis
  41. networkPermissionScope, ok := user.PermissionTemplate.DashBoardAcls.NetworkLevelAccess[models.NetworkID(netID)]
  42. if !ok {
  43. return errors.New("access denied")
  44. }
  45. if networkPermissionScope.FullAccess {
  46. return nil
  47. }
  48. rsrcPermissionScope, ok := networkPermissionScope.NetworkRsrcPermissionsScope[models.RsrcType(targetRsrc)]
  49. if !ok {
  50. return fmt.Errorf("access denied to %s rsrc", targetRsrc)
  51. }
  52. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {
  53. return checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, r.Method)
  54. }
  55. if targetRsrcID == "" {
  56. return errors.New("target rsrc is missing")
  57. }
  58. if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {
  59. return checkPermissionScopeWithReqMethod(scope, r.Method)
  60. }
  61. return errors.New("access denied")
  62. }
  63. func globalPermissionsCheck(username string, r *http.Request) error {
  64. user, err := GetUser(username)
  65. if err != nil {
  66. return err
  67. }
  68. if user.PermissionTemplate.DashBoardAcls.FullAccess {
  69. return nil
  70. }
  71. targetRsrc := r.Header.Get("TARGET_RSRC")
  72. targetRsrcID := r.Header.Get("TARGET_RSRC_ID")
  73. if targetRsrc == "" {
  74. return errors.New("target rsrc is missing")
  75. }
  76. if r.Method == "" {
  77. r.Method = http.MethodGet
  78. }
  79. if user.PermissionTemplate.DashBoardAcls.FullAccess {
  80. return nil
  81. }
  82. rsrcPermissionScope, ok := user.PermissionTemplate.DashBoardAcls.GlobalLevelAccess[models.RsrcType(targetRsrc)]
  83. if !ok {
  84. return fmt.Errorf("access denied to %s rsrc", targetRsrc)
  85. }
  86. if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {
  87. return checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, r.Method)
  88. }
  89. if targetRsrcID == "" {
  90. return errors.New("target rsrc id is missing")
  91. }
  92. if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {
  93. return checkPermissionScopeWithReqMethod(scope, r.Method)
  94. }
  95. return errors.New("access denied")
  96. }
  97. func checkPermissionScopeWithReqMethod(scope models.RsrcPermissionScope, reqmethod string) error {
  98. if reqmethod == http.MethodGet && scope.Read {
  99. return nil
  100. }
  101. if (reqmethod == http.MethodPatch || reqmethod == http.MethodPut) && scope.Update {
  102. return nil
  103. }
  104. if reqmethod == http.MethodDelete && scope.Delete {
  105. return nil
  106. }
  107. if reqmethod == http.MethodPost && scope.Create {
  108. return nil
  109. }
  110. return errors.New("operation not permitted")
  111. }
  112. // SecurityCheck - Check if user has appropriate permissions
  113. func SecurityCheck(reqAdmin bool, next http.Handler) http.HandlerFunc {
  114. return func(w http.ResponseWriter, r *http.Request) {
  115. r.Header.Set("ismaster", "no")
  116. bearerToken := r.Header.Get("Authorization")
  117. isGlobalAccesss := r.Header.Get("IS_GLOBAL_ACCESS") == "yes"
  118. username, err := UserPermissions(reqAdmin, bearerToken)
  119. if err != nil {
  120. ReturnErrorResponse(w, r, FormatError(err, err.Error()))
  121. return
  122. }
  123. // detect masteradmin
  124. if username == MasterUser {
  125. r.Header.Set("ismaster", "yes")
  126. } else {
  127. if isGlobalAccesss {
  128. globalPermissionsCheck(username, r)
  129. } else {
  130. networkPermissionsCheck(username, r)
  131. }
  132. }
  133. r.Header.Set("user", username)
  134. next.ServeHTTP(w, r)
  135. }
  136. }
  137. // UserPermissions - checks token stuff
  138. func UserPermissions(reqAdmin bool, token string) (string, error) {
  139. var tokenSplit = strings.Split(token, " ")
  140. var authToken = ""
  141. if len(tokenSplit) < 2 {
  142. return "", Unauthorized_Err
  143. } else {
  144. authToken = tokenSplit[1]
  145. }
  146. //all endpoints here require master so not as complicated
  147. if authenticateMaster(authToken) {
  148. // TODO log in as an actual admin user
  149. return MasterUser, nil
  150. }
  151. username, issuperadmin, isadmin, err := VerifyUserToken(authToken)
  152. if err != nil {
  153. return username, Unauthorized_Err
  154. }
  155. if reqAdmin && !(issuperadmin || isadmin) {
  156. return username, Forbidden_Err
  157. }
  158. return username, nil
  159. }
  160. // Consider a more secure way of setting master key
  161. func authenticateMaster(tokenString string) bool {
  162. return tokenString == servercfg.GetMasterKey() && servercfg.GetMasterKey() != ""
  163. }
  164. func ContinueIfUserMatch(next http.Handler) http.HandlerFunc {
  165. return func(w http.ResponseWriter, r *http.Request) {
  166. var errorResponse = models.ErrorResponse{
  167. Code: http.StatusForbidden, Message: Forbidden_Msg,
  168. }
  169. var params = mux.Vars(r)
  170. var requestedUser = params["username"]
  171. if requestedUser != r.Header.Get("user") {
  172. ReturnErrorResponse(w, r, errorResponse)
  173. return
  174. }
  175. next.ServeHTTP(w, r)
  176. }
  177. }