security.go 5.3 KB

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