You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.6 KiB

4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
FEATURE: add JWT to HTTP endpoints of Filer and use them in S3 Client - one JWT for reading and one for writing, analogous to how the JWT between Master and Volume Server works - I did not implement IP `whiteList` parameter on the filer Additionally, because http_util.DownloadFile now sets the JWT, the `download` command should now work when `jwt.signing.read` is configured. By looking at the code, I think this case did not work before. ## Docs to be adjusted after a release Page `Amazon-S3-API`: ``` # Authentication with Filer You can use mTLS for the gRPC connection between S3-API-Proxy and the filer, as explained in [Security-Configuration](Security-Configuration) - controlled by the `grpc.*` configuration in `security.toml`. Starting with version XX, it is also possible to authenticate the HTTP operations between the S3-API-Proxy and the Filer (especially uploading new files). This is configured by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. With both configurations (gRPC and JWT), it is possible to have Filer and S3 communicate in fully authenticated fashion; so Filer will reject any unauthenticated communication. ``` Page `Security Overview`: ``` The following items are not covered, yet: - master server http REST services Starting with version XX, the Filer HTTP REST services can be secured with a JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. ... Before version XX: "weed filer -disableHttp", disable http operations, only gRPC operations are allowed. This works with "weed mount" by FUSE. It does **not work** with the [S3 Gateway](Amazon S3 API), as this does HTTP calls to the Filer. Starting with version XX: secured by JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. **This now works with the [S3 Gateway](Amazon S3 API).** ... # Securing Filer HTTP with JWT To enable JWT-based access control for the Filer, 1. generate `security.toml` file by `weed scaffold -config=security` 2. set `filer_jwt.signing.key` to a secret string - and optionally filer_jwt.signing.read.key` as well to a secret string 3. copy the same `security.toml` file to the filers and all S3 proxies. If `filer_jwt.signing.key` is configured: When sending upload/update/delete HTTP operations to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.key`. If `filer_jwt.signing.read.key` is configured: When sending GET or HEAD requests to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.read.key`. The S3 API Gateway reads the above JWT keys and sends authenticated HTTP requests to the filer. ``` Page `Security Configuration`: ``` (update scaffold file) ... [filer_jwt.signing] key = "blahblahblahblah" [filer_jwt.signing.read] key = "blahblahblahblah" ``` Resolves: #158
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
FEATURE: add JWT to HTTP endpoints of Filer and use them in S3 Client - one JWT for reading and one for writing, analogous to how the JWT between Master and Volume Server works - I did not implement IP `whiteList` parameter on the filer Additionally, because http_util.DownloadFile now sets the JWT, the `download` command should now work when `jwt.signing.read` is configured. By looking at the code, I think this case did not work before. ## Docs to be adjusted after a release Page `Amazon-S3-API`: ``` # Authentication with Filer You can use mTLS for the gRPC connection between S3-API-Proxy and the filer, as explained in [Security-Configuration](Security-Configuration) - controlled by the `grpc.*` configuration in `security.toml`. Starting with version XX, it is also possible to authenticate the HTTP operations between the S3-API-Proxy and the Filer (especially uploading new files). This is configured by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. With both configurations (gRPC and JWT), it is possible to have Filer and S3 communicate in fully authenticated fashion; so Filer will reject any unauthenticated communication. ``` Page `Security Overview`: ``` The following items are not covered, yet: - master server http REST services Starting with version XX, the Filer HTTP REST services can be secured with a JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. ... Before version XX: "weed filer -disableHttp", disable http operations, only gRPC operations are allowed. This works with "weed mount" by FUSE. It does **not work** with the [S3 Gateway](Amazon S3 API), as this does HTTP calls to the Filer. Starting with version XX: secured by JWT, by setting `filer_jwt.signing.key` and `filer_jwt.signing.read.key` in `security.toml`. **This now works with the [S3 Gateway](Amazon S3 API).** ... # Securing Filer HTTP with JWT To enable JWT-based access control for the Filer, 1. generate `security.toml` file by `weed scaffold -config=security` 2. set `filer_jwt.signing.key` to a secret string - and optionally filer_jwt.signing.read.key` as well to a secret string 3. copy the same `security.toml` file to the filers and all S3 proxies. If `filer_jwt.signing.key` is configured: When sending upload/update/delete HTTP operations to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.key`. If `filer_jwt.signing.read.key` is configured: When sending GET or HEAD requests to a filer server, the request header `Authorization` should be the JWT string (`Authorization: Bearer [JwtToken]`). The operation is authorized after the filer validates the JWT with `filer_jwt.signing.read.key`. The S3 API Gateway reads the above JWT keys and sends authenticated HTTP requests to the filer. ``` Page `Security Configuration`: ``` (update scaffold file) ... [filer_jwt.signing] key = "blahblahblahblah" [filer_jwt.signing.read] key = "blahblahblahblah" ``` Resolves: #158
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  6. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  7. weed_server "github.com/chrislusf/seaweedfs/weed/server"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
  16. dstBucket, dstObject := xhttp.GetBucketAndObject(r)
  17. // Copy source path.
  18. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  19. if err != nil {
  20. // Save unescaped string as is.
  21. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  22. }
  23. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  24. glog.V(3).Infof("CopyObjectHandler %s %s => %s %s", srcBucket, srcObject, dstBucket, dstObject)
  25. if (srcBucket == dstBucket && srcObject == dstObject || cpSrcPath == "") && isReplace(r) {
  26. fullPath := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, dstBucket, dstObject))
  27. dir, name := fullPath.DirAndName()
  28. entry, err := s3a.getEntry(dir, name)
  29. if err != nil || entry.IsDirectory {
  30. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  31. return
  32. }
  33. entry.Extended = weed_server.SaveAmzMetaData(r, entry.Extended, isReplace(r))
  34. err = s3a.touch(dir, name, entry)
  35. if err != nil {
  36. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  37. return
  38. }
  39. writeSuccessResponseXML(w, r, CopyObjectResult{
  40. ETag: fmt.Sprintf("%x", entry.Attributes.Md5),
  41. LastModified: time.Now().UTC(),
  42. })
  43. return
  44. }
  45. // If source object is empty or bucket is empty, reply back invalid copy source.
  46. if srcObject == "" || srcBucket == "" {
  47. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  48. return
  49. }
  50. srcPath := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, srcBucket, srcObject))
  51. dir, name := srcPath.DirAndName()
  52. if entry, err := s3a.getEntry(dir, name); err != nil || entry.IsDirectory {
  53. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  54. return
  55. }
  56. if srcBucket == dstBucket && srcObject == dstObject {
  57. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopyDest)
  58. return
  59. }
  60. dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
  61. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, dstBucket, urlPathEscape(dstObject), dstBucket)
  62. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  63. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
  64. _, _, resp, err := util.DownloadFile(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false))
  65. if err != nil {
  66. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  67. return
  68. }
  69. defer util.CloseResponse(resp)
  70. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  71. etag, errCode := s3a.putToFiler(r, dstUrl, resp.Body)
  72. if errCode != s3err.ErrNone {
  73. s3err.WriteErrorResponse(w, r, errCode)
  74. return
  75. }
  76. setEtag(w, etag)
  77. response := CopyObjectResult{
  78. ETag: etag,
  79. LastModified: time.Now().UTC(),
  80. }
  81. writeSuccessResponseXML(w, r, response)
  82. }
  83. func pathToBucketAndObject(path string) (bucket, object string) {
  84. path = strings.TrimPrefix(path, "/")
  85. parts := strings.SplitN(path, "/", 2)
  86. if len(parts) == 2 {
  87. return parts[0], "/" + parts[1]
  88. }
  89. return parts[0], "/"
  90. }
  91. type CopyPartResult struct {
  92. LastModified time.Time `xml:"LastModified"`
  93. ETag string `xml:"ETag"`
  94. }
  95. func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  96. // https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjctsUsingRESTMPUapi.html
  97. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
  98. dstBucket, _ := xhttp.GetBucketAndObject(r)
  99. // Copy source path.
  100. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  101. if err != nil {
  102. // Save unescaped string as is.
  103. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  104. }
  105. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  106. // If source object is empty or bucket is empty, reply back invalid copy source.
  107. if srcObject == "" || srcBucket == "" {
  108. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  109. return
  110. }
  111. uploadID := r.URL.Query().Get("uploadId")
  112. partIDString := r.URL.Query().Get("partNumber")
  113. partID, err := strconv.Atoi(partIDString)
  114. if err != nil {
  115. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  116. return
  117. }
  118. glog.V(3).Infof("CopyObjectPartHandler %s %s => %s part %d", srcBucket, srcObject, dstBucket, partID)
  119. // check partID with maximum part ID for multipart objects
  120. if partID > globalMaxPartID {
  121. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  122. return
  123. }
  124. rangeHeader := r.Header.Get("x-amz-copy-source-range")
  125. dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  126. s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(dstBucket), uploadID, partID, dstBucket)
  127. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  128. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
  129. dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false), rangeHeader)
  130. if err != nil {
  131. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  132. return
  133. }
  134. defer dataReader.Close()
  135. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  136. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  137. if errCode != s3err.ErrNone {
  138. s3err.WriteErrorResponse(w, r, errCode)
  139. return
  140. }
  141. setEtag(w, etag)
  142. response := CopyPartResult{
  143. ETag: etag,
  144. LastModified: time.Now().UTC(),
  145. }
  146. writeSuccessResponseXML(w, r, response)
  147. }
  148. func isReplace(r *http.Request) bool {
  149. return r.Header.Get("X-Amz-Metadata-Directive") == "REPLACE"
  150. }