security.go 5.1 KB

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