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.

324 lines
9.2 KiB

3 years ago
7 years ago
7 years ago
5 months ago
3 years ago
3 years ago
3 years ago
3 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
5 months 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
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
5 months ago
3 years ago
4 years ago
3 years ago
5 years ago
  1. package s3api
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/aws/aws-sdk-go/service/s3"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  8. "github.com/seaweedfs/seaweedfs/weed/s3api/s3acl"
  9. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "strings"
  14. "time"
  15. "github.com/seaweedfs/seaweedfs/weed/filer"
  16. "github.com/seaweedfs/seaweedfs/weed/glog"
  17. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  18. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  19. )
  20. func mimeDetect(r *http.Request, dataReader io.Reader) io.ReadCloser {
  21. mimeBuffer := make([]byte, 512)
  22. size, _ := dataReader.Read(mimeBuffer)
  23. if size > 0 {
  24. r.Header.Set("Content-Type", http.DetectContentType(mimeBuffer[:size]))
  25. return io.NopCloser(io.MultiReader(bytes.NewReader(mimeBuffer[:size]), dataReader))
  26. }
  27. return io.NopCloser(dataReader)
  28. }
  29. func urlEscapeObject(object string) string {
  30. t := urlPathEscape(removeDuplicateSlashes(object))
  31. if strings.HasPrefix(t, "/") {
  32. return t
  33. }
  34. return "/" + t
  35. }
  36. func entryUrlEncode(dir string, entry string, encodingTypeUrl bool) (dirName string, entryName string, prefix string) {
  37. if !encodingTypeUrl {
  38. return dir, entry, entry
  39. }
  40. return urlPathEscape(dir), url.QueryEscape(entry), urlPathEscape(entry)
  41. }
  42. func urlPathEscape(object string) string {
  43. var escapedParts []string
  44. for _, part := range strings.Split(object, "/") {
  45. escapedParts = append(escapedParts, strings.ReplaceAll(url.PathEscape(part), "+", "%2B"))
  46. }
  47. return strings.Join(escapedParts, "/")
  48. }
  49. func removeDuplicateSlashes(object string) string {
  50. result := strings.Builder{}
  51. result.Grow(len(object))
  52. isLastSlash := false
  53. for _, r := range object {
  54. switch r {
  55. case '/':
  56. if !isLastSlash {
  57. result.WriteRune(r)
  58. }
  59. isLastSlash = true
  60. default:
  61. result.WriteRune(r)
  62. isLastSlash = false
  63. }
  64. }
  65. return result.String()
  66. }
  67. func newListEntry(entry *filer_pb.Entry, key string, dir string, name string, bucketPrefix string, fetchOwner bool, isDirectory bool, encodingTypeUrl bool) (listEntry ListEntry) {
  68. storageClass := "STANDARD"
  69. if v, ok := entry.Extended[s3_constants.AmzStorageClass]; ok {
  70. storageClass = string(v)
  71. }
  72. keyFormat := "%s/%s"
  73. if isDirectory {
  74. keyFormat += "/"
  75. }
  76. if key == "" {
  77. key = fmt.Sprintf(keyFormat, dir, name)[len(bucketPrefix):]
  78. }
  79. if encodingTypeUrl {
  80. key = urlPathEscape(key)
  81. }
  82. listEntry = ListEntry{
  83. Key: key,
  84. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  85. ETag: "\"" + filer.ETag(entry) + "\"",
  86. Size: int64(filer.FileSize(entry)),
  87. StorageClass: StorageClass(storageClass),
  88. }
  89. if fetchOwner {
  90. listEntry.Owner = CanonicalUser{
  91. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  92. DisplayName: entry.Attributes.UserName,
  93. }
  94. }
  95. return listEntry
  96. }
  97. func (s3a *S3ApiServer) toFilerUrl(bucket, object string) string {
  98. object = urlPathEscape(removeDuplicateSlashes(object))
  99. destUrl := fmt.Sprintf("http://%s%s/%s%s",
  100. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, object)
  101. return destUrl
  102. }
  103. func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
  104. bucket, object := s3_constants.GetBucketAndObject(r)
  105. glog.V(3).Infof("GetObjectHandler %s %s", bucket, object)
  106. errCode := s3a.checkBucketAccessForReadObject(r, bucket)
  107. if errCode != s3err.ErrNone {
  108. s3err.WriteErrorResponse(w, r, errCode)
  109. return
  110. }
  111. if strings.HasSuffix(r.URL.Path, "/") {
  112. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  113. return
  114. }
  115. destUrl := s3a.toFilerUrl(bucket, object)
  116. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  117. }
  118. // PutObjectAclHandler Put object ACL
  119. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObjecthtml
  120. func (s3a *S3ApiServer) PutObjectAclHandler(w http.ResponseWriter, r *http.Request) {
  121. bucket, object := s3_constants.GetBucketAndObject(r)
  122. objectEntry, ownerId, grants, errCode := s3a.checkAccessForWriteObjectAcl(r, bucket, object)
  123. if errCode != s3err.ErrNone {
  124. s3err.WriteErrorResponse(w, r, errCode)
  125. return
  126. }
  127. errCode = s3acl.AssembleEntryWithAcp(objectEntry, ownerId, grants)
  128. if errCode != s3err.ErrNone {
  129. return
  130. }
  131. err := updateObjectEntry(s3a, bucket, object, objectEntry)
  132. if err != nil {
  133. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  134. return
  135. }
  136. w.WriteHeader(http.StatusOK)
  137. }
  138. // GetObjectAclHandler Put object ACL
  139. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjecthtml
  140. func (s3a *S3ApiServer) GetObjectAclHandler(w http.ResponseWriter, r *http.Request) {
  141. bucket, object := s3_constants.GetBucketAndObject(r)
  142. acp, errCode := s3a.checkAccessForReadObjectAcl(r, bucket, object)
  143. if errCode != s3err.ErrNone {
  144. s3err.WriteErrorResponse(w, r, errCode)
  145. return
  146. }
  147. result := &s3.PutBucketAclInput{
  148. AccessControlPolicy: acp,
  149. }
  150. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, &result)
  151. }
  152. func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
  153. bucket, object := s3_constants.GetBucketAndObject(r)
  154. glog.V(3).Infof("HeadObjectHandler %s %s", bucket, object)
  155. errCode := s3a.checkBucketAccessForReadObject(r, bucket)
  156. if errCode != s3err.ErrNone {
  157. s3err.WriteErrorResponse(w, r, errCode)
  158. return
  159. }
  160. destUrl := s3a.toFilerUrl(bucket, object)
  161. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  162. }
  163. func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, isWrite bool, responseFn func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int)) {
  164. glog.V(3).Infof("s3 proxying %s to %s", r.Method, destUrl)
  165. start := time.Now()
  166. proxyReq, err := http.NewRequest(r.Method, destUrl, r.Body)
  167. if err != nil {
  168. glog.Errorf("NewRequest %s: %v", destUrl, err)
  169. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  170. return
  171. }
  172. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  173. proxyReq.Header.Set("Accept-Encoding", "identity")
  174. for k, v := range r.URL.Query() {
  175. if _, ok := s3_constants.PassThroughHeaders[strings.ToLower(k)]; ok {
  176. proxyReq.Header[k] = v
  177. }
  178. if k == "partNumber" {
  179. proxyReq.Header[s3_constants.SeaweedFSPartNumber] = v
  180. }
  181. }
  182. for header, values := range r.Header {
  183. proxyReq.Header[header] = values
  184. }
  185. // ensure that the Authorization header is overriding any previous
  186. // Authorization header which might be already present in proxyReq
  187. s3a.maybeAddFilerJwtAuthorization(proxyReq, isWrite)
  188. resp, postErr := s3a.client.Do(proxyReq)
  189. if resp.Uncompressed && r.Header.Get("Accept-Encoding") == "" {
  190. r.Header.Set("Accept-Encoding", "gzip")
  191. util_http.CloseResponse(resp)
  192. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  193. return
  194. }
  195. if postErr != nil {
  196. glog.Errorf("post to filer: %v", postErr)
  197. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  198. return
  199. }
  200. defer util_http.CloseResponse(resp)
  201. switch resp.StatusCode {
  202. case http.StatusPreconditionFailed:
  203. s3err.WriteErrorResponse(w, r, s3err.ErrPreconditionFailed)
  204. return
  205. case http.StatusRequestedRangeNotSatisfiable:
  206. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRange)
  207. return
  208. case http.StatusForbidden:
  209. s3err.WriteErrorResponse(w, r, s3err.ErrAccessDenied)
  210. return
  211. default:
  212. }
  213. if r.Method == http.MethodDelete {
  214. if resp.StatusCode == http.StatusNotFound {
  215. // this is normal
  216. responseStatusCode := responseFn(resp, w)
  217. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  218. return
  219. }
  220. }
  221. if resp.StatusCode == http.StatusNotFound {
  222. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchKey)
  223. return
  224. }
  225. TimeToFirstByte(r.Method, start, r)
  226. if resp.Header.Get(s3_constants.SeaweedFSIsDirectoryKey) == "true" {
  227. responseStatusCode := responseFn(resp, w)
  228. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  229. return
  230. }
  231. if resp.StatusCode == http.StatusInternalServerError {
  232. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  233. return
  234. }
  235. // when HEAD a directory, it should be reported as no such key
  236. // https://github.com/seaweedfs/seaweedfs/issues/3457
  237. if resp.ContentLength == -1 && resp.StatusCode != http.StatusNotModified {
  238. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchKey)
  239. return
  240. }
  241. if resp.StatusCode == http.StatusBadRequest {
  242. resp_body, _ := io.ReadAll(resp.Body)
  243. switch string(resp_body) {
  244. case "InvalidPart":
  245. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  246. default:
  247. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  248. }
  249. resp.Body.Close()
  250. return
  251. }
  252. setUserMetadataKeyToLowercase(resp)
  253. responseStatusCode := responseFn(resp, w)
  254. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  255. }
  256. func setUserMetadataKeyToLowercase(resp *http.Response) {
  257. for key, value := range resp.Header {
  258. if strings.HasPrefix(key, s3_constants.AmzUserMetaPrefix) {
  259. resp.Header[strings.ToLower(key)] = value
  260. delete(resp.Header, key)
  261. }
  262. }
  263. }
  264. func passThroughResponse(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int) {
  265. for k, v := range proxyResponse.Header {
  266. w.Header()[k] = v
  267. }
  268. if proxyResponse.Header.Get("Content-Range") != "" && proxyResponse.StatusCode == 200 {
  269. w.WriteHeader(http.StatusPartialContent)
  270. statusCode = http.StatusPartialContent
  271. } else {
  272. statusCode = proxyResponse.StatusCode
  273. }
  274. w.WriteHeader(statusCode)
  275. buf := mem.Allocate(128 * 1024)
  276. defer mem.Free(buf)
  277. if n, err := io.CopyBuffer(w, proxyResponse.Body, buf); err != nil {
  278. glog.V(1).Infof("passthrough response read %d bytes: %v", n, err)
  279. }
  280. return statusCode
  281. }