security.go 4.8 KB

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